Skip to contentSkip to conversation

netfilter: nft_fib: store loopback interface to dreg when rt is local

PATCH In review Liping Zhang 3 participants 7 messages Nov 24, 2016 – Nov 30, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: nft_fib: store loopback interface to dreg when rt is local

From: Liping Zhang <[email protected]>

In general, we haven't do routing lookup in PREROUTING hook, so it's very likely that fib4/6_is_local will not be met. Then the *dest will be set to 0 because we do nothing when the fib result is RTN_LOCAL.

So if the user want to drop all packets which cannot be routed, and input the following nft rule:

  # nft add rule filter prerouting fib daddr oif eq 0 drop

Then all the packets which destinate to local will be dropped incorrectly.

Fixes f6d0cbcf09c5 ("netfilter: nf_tables: add fib expression")
---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 ++-
 net/ipv6/netfilter/nft_fib_ipv6.c | 8 ++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
net/ipv4/netfilter/nft_fib_ipv4.c +2 -1
index 2581363..2107775 100644
@@ -130,7 +130,8 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	switch (res.type) {
 	case RTN_UNICAST:
 		break;
-	case RTN_LOCAL:	/* should not appear here, see fib4_is_local() above */
+	case RTN_LOCAL:
+		nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);
 		return;
 	default:
 		break;
net/ipv6/netfilter/nft_fib_ipv6.c +6 -2
index c947aad..5e2de1b 100644
@@ -175,8 +175,12 @@ void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	if (rt->dst.error)
 		goto put_rt_err;
 
-	/* Should not see RTF_LOCAL here */
-	if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
+	if (rt->rt6i_flags & RTF_LOCAL) {
+		nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);
+		goto put_rt_err;
+	}
+
+	if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST))
 		goto put_rt_err;
 
 	if (oif && oif != rt->rt6i_idev->dev) {
Signature · 1 line
2.5.5
maintainer
#1 lore raw
Quoted 2 lines · Liping Zhang

In general, we haven't do routing lookup in PREROUTING hook, so it's very likely that fib4/6_is_local will not be met.

loopback packets retain skb->dst (and thats what this test is about).

Quoted 2 lines

Then the *dest will
be set to 0 because we do nothing when the fib result is RTN_LOCAL.

Yes.

Quoted 6 lines

So if the user want to drop all packets which cannot be routed, and input the following nft rule:

  # nft add rule filter prerouting fib daddr oif eq 0 drop

Then all the packets which destinate to local will be dropped incorrectly.

but in "saddr oif eq 0 drop" case they really should have no oif, the address should not be considered routeable.

Pablo, please don't apply this; I would like to look at this next week.

Msybe this needs a check if we're testing daddr or saddr.

#2 lore raw

Hi Florian,

Quoted 5 lines · Florian Westphal
Quoted 2 lines · Liping Zhang

In general, we haven't do routing lookup in PREROUTING hook, so it's very likely that fib4/6_is_local will not be met.

loopback packets retain skb->dst (and thats what this test is about).

Yes, so I use the words "very likely" :)
[...]

Quoted 2 lines

but in "saddr oif eq 0 drop" case they really should have no oif, the address should not be considered routeable.

Yes, I read the ipt_rpfilter.c's source codes, and I find that there's a test flag XT_RPFILTER_ACCEPT_LOCAL, so I guess your initial intention is (just my guess, maybe I'm wrong):
0 - no route
1 - local route
others - routing oif

Quoted 4 lines

Pablo, please don't apply this; I would like to look at this next week.

Msybe this needs a check if we're testing daddr or saddr.

maintainer
#3 lore raw
Quoted 4 lines · Liping Zhang
Quoted 3 lines · Florian Westphal
Quoted 2 lines · Liping Zhang

In general, we haven't do routing lookup in PREROUTING hook, so it's very likely that fib4/6_is_local will not be met.

[..]

Quoted 11 lines

Yes, so I use the words "very likely" :)
[...]

Quoted 2 lines

but in "saddr oif eq 0 drop" case they really should have no oif, the address should not be considered routeable.

Yes, I read the ipt_rpfilter.c's source codes, and I find that there's a test flag XT_RPFILTER_ACCEPT_LOCAL, so I guess your initial intention is (just my guess, maybe I'm wrong):
0 - no route
1 - local route
others - routing oif

Yes, thats right.

"1" should only appear if lookup-up address is configured on this machine. For saddr, I don't think its good idea, because it will pass

oif ne 0 accept

For ACCEPT_LOCAL i think its easier to combine this with the addrtype check of just add explicit accept rules that make it bypass nft_fib rule.

What do you think?

I agree that for your prerouting daddr example 0 makes no sense and 1 would indeed be a better option.

#4 lore raw
Quoted 1 line · Florian Westphal

Liping Zhang <[email protected]> wrote:

[...]

Quoted 4 lines

"1" should only appear if lookup-up address is configured on this machine. For saddr, I don't think its good idea, because it will pass

oif ne 0 accept

Yes, my patch will break this.

Quoted 4 lines

For ACCEPT_LOCAL i think its easier to combine this with the addrtype check of just add explicit accept rules that make it bypass nft_fib rule.

Yes, combine this with addrtype will be easier. My first thought was that we can also use "fib saddr oif eq 1" to simulate the ACCECPT_LOCAL, but I'm wrong, it will become more complicated.

Quoted 6 lines

What do you think?

I agree that for your prerouting daddr example 0 makes no sense and 1 would indeed be a better option.

maintainer
#5 lore raw
In reply to Liping Zhang
Quoted 25 lines · Liping Zhang

From: Liping Zhang <[email protected]>

In general, we haven't do routing lookup in PREROUTING hook, so it's very likely that fib4/6_is_local will not be met. Then the *dest will be set to 0 because we do nothing when the fib result is RTN_LOCAL.

So if the user want to drop all packets which cannot be routed, and input the following nft rule:

  # nft add rule filter prerouting fib daddr oif eq 0 drop

Then all the packets which destinate to local will be dropped incorrectly.

Fixes f6d0cbcf09c5 ("netfilter: nf_tables: add fib expression")
---
 net/ipv4/netfilter/nft_fib_ipv4.c | 3 ++-
 net/ipv6/netfilter/nft_fib_ipv6.c | 8 ++++++--
 2 files changed, 8 insertions(+), 3 deletions(-)
net/ipv4/netfilter/nft_fib_ipv4.c +0 -0
index 2581363..2107775 100644

net/ipv4/netfilter/nft_fib_ipv4.c void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 134

Quoted 6 lines
net/ipv4/netfilter/nft_fib_ipv4.c +2 -1
@@ -130,7 +130,8 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
 	switch (res.type) {
 	case RTN_UNICAST:
 		break;
-	case RTN_LOCAL:	/* should not appear here, see fib4_is_local() above */
+	case RTN_LOCAL:
+		nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);

Liping, what about doing:

	case RTN_LOCAL:
		if (priv->flags & NFTA_FIB_F_DADDR)
			nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);

AFAICS this will make above rule work while the saddr test will still appear to not have a route at all.

What do you think?

#6 lore raw

Hi Florian,

2016-11-28 20:25 GMT+08:00 Florian Westphal <[email protected]>:
[...]

Quoted 19 lines

net/ipv4/netfilter/nft_fib_ipv4.c void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs, · line 134

Quoted 11 lines
net/ipv4/netfilter/nft_fib_ipv4.c +2 -1
index 2581363..2107775 100644
@@ -130,7 +130,8 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
      switch (res.type) {
      case RTN_UNICAST:
              break;
-     case RTN_LOCAL: /* should not appear here, see fib4_is_local() above */
+     case RTN_LOCAL:
+             nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);

Liping, what about doing:

        case RTN_LOCAL:
                if (priv->flags & NFTA_FIB_F_DADDR)
                        nft_fib_store_result(dest, priv->result, pkt, LOOPBACK_IFINDEX);
Quoted 4 lines

AFAICS this will make above rule work while the saddr test will still appear to not have a route at all.

What do you think?

Yes, this will work both for *rpfilter* and my user case. It seems a little ugly but I cannot find a better solution now ...