Skip to contentSkip to conversation

netfilter: nf_reject_ipv6: fix locally generated packet to local address

PATCH Superseded Pablo Neira Ayuso 2 participants 4 messages Oct 22 – Oct 23

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH v4 1/1 netfilter: nf_reject_ipv6: fix locally generated packet to local address

Fix reject expression in inet/ingress for locally generated IPv6 packets that are sent to a local address on ethX arrives through lo, really perform the similar handling as ip6_rcv_core().

Reported by sashiko.

Fixes 73a73d442f99 ("netfilter: nf_reject: initialize IPCB at inet ingress")

---
v4: do not move route lookup before, this seems to interfer with the special loopback case.

 net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
net/ipv6/netfilter/nf_reject_ipv6.c +12 -1
index 5e08d8529cc8..8551e40faf04 100644
@@ -445,8 +445,19 @@ void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
 		skb_in->dev = net->loopback_dev;
 
 	if (hooknum == NF_INET_INGRESS) {
+		const struct inet6_dev *idev;
+
 		memset(IP6CB(skb_in), 0, sizeof(*IP6CB(skb_in)));
-		IP6CB(skb_in)->iif = skb_in->skb_iif;
+
+		if (skb_valid_dst(skb_in)) {
+			idev = ip6_dst_idev(skb_dst(skb_in));
+			if (idev)
+				IP6CB(skb_in)->iif = idev->dev->ifindex;
+			else
+				IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		} else {
+			IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		}
 	}
 
 	if (!skb_valid_dst(skb_in) && nf_reject6_fill_skb_dst(skb_in) < 0)
Signature · 1 line
2.47.3
#1 lore raw
Quoted 27 lines · Pablo Neira Ayuso

Fix reject expression in inet/ingress for locally generated IPv6 packets that are sent to a local address on ethX arrives through lo, really perform the similar handling as ip6_rcv_core().

Reported by sashiko.

Fixes 73a73d442f99 ("netfilter: nf_reject: initialize IPCB at inet ingress")

---
v4: do not move route lookup before, this seems to interfer with the special loopback case.

  net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
net/ipv6/netfilter/nf_reject_ipv6.c +3 -1
index 5e08d8529cc8..8551e40faf04 100644
@@ -445,8 +445,19 @@ void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
  		skb_in->dev = net->loopback_dev;
 
  	if (hooknum == NF_INET_INGRESS) {
+		const struct inet6_dev *idev;
+
  		memset(IP6CB(skb_in), 0, sizeof(*IP6CB(skb_in)));
-		IP6CB(skb_in)->iif = skb_in->skb_iif;
+

net/ipv6/netfilter/nf_reject_ipv6.c void nf_send_unreach6(struct net *net, struct sk_buff *skb_in, · line 461

Quoted 10 lines
net/ipv6/netfilter/nf_reject_ipv6.c +9 -0
+		if (skb_valid_dst(skb_in)) {
+			idev = ip6_dst_idev(skb_dst(skb_in));
+			if (idev)
+				IP6CB(skb_in)->iif = idev->dev->ifindex;
+			else
+				IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		} else {
+			IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		}
  	}

Hi Pablo, I did a bit of testing and patch LGTM.

Could we maybe simplify the code a bit by using a helper? Something like:

static int nf_reject6_ingress_iff(const struct sk_buff *skb)

{
	const struct inet6_dev *idev;

	if (skb_valid_dst(skb)) {
		idev = ip6_dst_idev(skb_dst(skb));
		if (idev)
			return idev->dev->ifindex;
	}

	return skb->dev->ifindex;
}

Then at nf_send_unreach6() we can do:

IP6CB(skb_in)->iif = nf_reject6_ingress_iif(skb_in);

If I am not wrong, compiler should just inline the helper and there should not be a performance hit. In addition, the code is cleaner from my point of view.

What do you think?

Quoted 2 lines
net/ipv6/netfilter/nf_reject_ipv6.c +0 -0
  	if (!skb_valid_dst(skb_in) && nf_reject6_fill_skb_dst(skb_in) < 0)
author maintainer
#2 lore raw
Quoted 32 lines · Fernando Fernandez Mancera
Quoted 31 lines · Pablo Neira Ayuso

Fix reject expression in inet/ingress for locally generated IPv6 packets that are sent to a local address on ethX arrives through lo, really perform the similar handling as ip6_rcv_core().

Reported by sashiko.

Fixes 73a73d442f99 ("netfilter: nf_reject: initialize IPCB at inet ingress")

---
v4: do not move route lookup before, this seems to interfer with the special loopback case.

  net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++++++++++-
  1 file changed, 12 insertions(+), 1 deletion(-)
net/ipv6/netfilter/nf_reject_ipv6.c +8 -1
index 5e08d8529cc8..8551e40faf04 100644
@@ -445,8 +445,19 @@ void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
  		skb_in->dev = net->loopback_dev;
  	if (hooknum == NF_INET_INGRESS) {
+		const struct inet6_dev *idev;
+
  		memset(IP6CB(skb_in), 0, sizeof(*IP6CB(skb_in)));
-		IP6CB(skb_in)->iif = skb_in->skb_iif;
+
+		if (skb_valid_dst(skb_in)) {
+			idev = ip6_dst_idev(skb_dst(skb_in));
+			if (idev)
+				IP6CB(skb_in)->iif = idev->dev->ifindex;
+			else
Quoted 8 lines

net/ipv6/netfilter/nf_reject_ipv6.c void nf_send_unreach6(struct net *net, struct sk_buff *skb_in, · line 461

Quoted 5 lines
net/ipv6/netfilter/nf_reject_ipv6.c +4 -0
+				IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		} else {
+			IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		}
  	}

Hi Pablo, I did a bit of testing and patch LGTM.

May I have your Tested-by: tag?

Quoted 17 lines

Could we maybe simplify the code a bit by using a helper? Something like:

static int nf_reject6_ingress_iff(const struct sk_buff *skb)

{
	const struct inet6_dev *idev;

	if (skb_valid_dst(skb)) {
		idev = ip6_dst_idev(skb_dst(skb));
		if (idev)
			return idev->dev->ifindex;
	}

	return skb->dev->ifindex;
}

Then at nf_send_unreach6() we can do:

Quoted 7 lines

IP6CB(skb_in)->iif = nf_reject6_ingress_iif(skb_in);

If I am not wrong, compiler should just inline the helper and there should not be a performance hit. In addition, the code is cleaner from my point of view.

What do you think?

That sounds fine, I will post a v5.

#3 lore raw
Quoted 42 lines · Pablo Neira Ayuso
Quoted 32 lines · Fernando Fernandez Mancera
Quoted 31 lines · Pablo Neira Ayuso

Fix reject expression in inet/ingress for locally generated IPv6 packets that are sent to a local address on ethX arrives through lo, really perform the similar handling as ip6_rcv_core().

Reported by sashiko.

Fixes 73a73d442f99 ("netfilter: nf_reject: initialize IPCB at inet ingress")

---
v4: do not move route lookup before, this seems to interfer with the special loopback case.

   net/ipv6/netfilter/nf_reject_ipv6.c | 13 ++++++++++++-
   1 file changed, 12 insertions(+), 1 deletion(-)
net/ipv6/netfilter/nf_reject_ipv6.c +8 -1
index 5e08d8529cc8..8551e40faf04 100644
@@ -445,8 +445,19 @@ void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
   		skb_in->dev = net->loopback_dev;
   	if (hooknum == NF_INET_INGRESS) {
+		const struct inet6_dev *idev;
+
   		memset(IP6CB(skb_in), 0, sizeof(*IP6CB(skb_in)));
-		IP6CB(skb_in)->iif = skb_in->skb_iif;
+
+		if (skb_valid_dst(skb_in)) {
+			idev = ip6_dst_idev(skb_dst(skb_in));
+			if (idev)
+				IP6CB(skb_in)->iif = idev->dev->ifindex;
+			else
Quoted 8 lines

net/ipv6/netfilter/nf_reject_ipv6.c void nf_send_unreach6(struct net *net, struct sk_buff *skb_in, · line 461

Quoted 5 lines
net/ipv6/netfilter/nf_reject_ipv6.c +4 -0
+				IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		} else {
+			IP6CB(skb_in)->iif = skb_in->dev->ifindex;
+		}
   	}

Hi Pablo, I did a bit of testing and patch LGTM.

Quoted 1 line

May I have your Tested-by: tag?

Sure thing!

Quoted 27 lines
Quoted 18 lines

Could we maybe simplify the code a bit by using a helper? Something like:

static int nf_reject6_ingress_iff(const struct sk_buff *skb)

{
	const struct inet6_dev *idev;

	if (skb_valid_dst(skb)) {
		idev = ip6_dst_idev(skb_dst(skb));
		if (idev)
			return idev->dev->ifindex;
	}

	return skb->dev->ifindex;
}

Then at nf_send_unreach6() we can do:

Quoted 7 lines

IP6CB(skb_in)->iif = nf_reject6_ingress_iif(skb_in);

If I am not wrong, compiler should just inline the helper and there should not be a performance hit. In addition, the code is cleaner from my point of view.

What do you think?

That sounds fine, I will post a v5.