Skip to contentSkip to conversation

netfilter reverse path filter matches

RFC PATCH In review Florian Westphal 3 participants 9 messages Sep 12, 2011 – Sep 28, 2011

Subsystems (4)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

v2 cover netfilter reverse path filter matches

Version 2 of the ipv4/v6 reverse path filter matches discussed during nfws 2011.

The ipv4 match (ipt_rpfilter) tries to do exactly what the current fib_validate_source does. The main problem with this is that we need to do an additional fib lookup to get the oif in the match. [ delaying until FORWARD is invoked is not possible because by
that point the stack might have already sent icmp errors ].

Patrick McHardy suggested to simply attach the result as the dst, so ipv4 input path doesn't have to do it again.

This works, but does have a few side effects wrt. route-by-mark and TPROXY, see patch changelog for details.

The ipv6 version does a pure 'reverse' lookup instead. This makes things a lot easier (e.g. when multiple route entries exist), but has the caveat that a real reply packet might be handled differently due to policy routing rules.

Userspace part is stored in my iptables repository on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/iptables.git (branch 'rpfilter').

Kernel patches are located in the 'xt_rpfilter_5' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git
(patches will be sent as followup to this email).

[ in case you are wondering: the earlier xt_rpfilter version was
removed -- causes too many module dependency issues and most of the code cannot be shared anyway ].

Thanks,
Florian

author maintainer
#1 lore raw

RFC PATCH v2 1/5 net: ipv4: export fib_table_lookup

modules using fib_lookup() fail to link when
CONFIG_IP_MULTIPLE_TABLES is not set.

The reason is that in this case fib_lookup is a static inline helper that calls fib_table_lookup.

---
 net/ipv4/fib_trie.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
net/ipv4/fib_trie.c +1 -0
index 58c25ea..e13eb24 100644
@@ -1606,6 +1606,7 @@ found:
 	rcu_read_unlock();
 	return ret;
 }
+EXPORT_SYMBOL_GPL(fib_table_lookup);
 
 /*
  * Remove the leaf and return parent.
Signature · 1 line
1.7.3.4
author maintainer
#2 lore raw

RFC PATCH v2 2/5 net: ipv4: move ip_rcv route error counter handling into helper function

In reply to Florian Westphal

This will avoid code duplication and messing with mib counters in the planned netfilter reverse path filter match.

---
 include/net/ip.h    |    1 +
 net/ipv4/ip_input.c |   21 ++++++++++++---------
 2 files changed, 13 insertions(+), 9 deletions(-)
include/net/ip.h +1 -0
index e9ea7c7..0001633 100644
@@ -95,6 +95,7 @@ extern int		ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
 					      struct ip_options_rcu *opt);
 extern int		ip_rcv(struct sk_buff *skb, struct net_device *dev,
 			       struct packet_type *pt, struct net_device *orig_dev);
+extern void		ip_rcv_inc_route_err_stats(struct net *net, int err);
 extern int		ip_local_deliver(struct sk_buff *skb);
 extern int		ip_mr_input(struct sk_buff *skb);
 extern int		ip_output(struct sk_buff *skb);
net/ipv4/ip_input.c +12 -9
index c8f48ef..43679f4 100644
@@ -314,6 +314,17 @@ drop:
 	return -1;
 }
 
+void ip_rcv_inc_route_err_stats(struct net *net, int err)
+{
+	if (err == -EHOSTUNREACH)
+		IP_INC_STATS_BH(net, IPSTATS_MIB_INADDRERRORS);
+	else if (err == -ENETUNREACH)
+		IP_INC_STATS_BH(net, IPSTATS_MIB_INNOROUTES);
+	else if (err == -EXDEV)
+		NET_INC_STATS_BH(net, LINUX_MIB_IPRPFILTER);
+}
+EXPORT_SYMBOL_GPL(ip_rcv_inc_route_err_stats);
+
 static int ip_rcv_finish(struct sk_buff *skb)
 {
 	const struct iphdr *iph = ip_hdr(skb);
@@ -327,15 +338,7 @@ static int ip_rcv_finish(struct sk_buff *skb)
 		int err = ip_route_input_noref(skb, iph->daddr, iph->saddr,
 					       iph->tos, skb->dev);
 		if (unlikely(err)) {
-			if (err == -EHOSTUNREACH)
-				IP_INC_STATS_BH(dev_net(skb->dev),
-						IPSTATS_MIB_INADDRERRORS);
-			else if (err == -ENETUNREACH)
-				IP_INC_STATS_BH(dev_net(skb->dev),
-						IPSTATS_MIB_INNOROUTES);
-			else if (err == -EXDEV)
-				NET_INC_STATS_BH(dev_net(skb->dev),
-						 LINUX_MIB_IPRPFILTER);
+			ip_rcv_inc_route_err_stats(dev_net(skb->dev), err);
 			goto drop;
 		}
 	}
Signature · 1 line
1.7.3.4
author maintainer
#3 lore raw

RFC PATCH v2 3/5 netfilter: add ipv4 reverse path filter match

In reply to Florian Westphal

Tries to mimic behaviour of fib_validate_source.

As fib_validate_source uses the oif as iif when performing reverse lookup, we would need one additional fib lookup to get oif.

We can't wait until FORWARD chain because by the time FORWARD is invoked ipv4 forward path may have already sent icmp messages is response to to-be-discarded-via-rpfilter packets.

To avoid the additional lookup in PREROUTING, Patrick McHardy suggested to attach the path information directly in the match.

This has a few caveats. Most importantly, when using marks in PREROUTING to re-route traffic based on the nfmark, -m rpfilter has to be used after the nfmark has been set; otherwise the nfmark will have no effect (because the route is already attached to the skb).

Another problem is interaction with -j TPROXY, as this target sets an nfmark and uses ACCEPT instead of CONTINUE, i.e. -m rpfilter cannot be used for the initial to-be-intercepted packets.

Also, no result caching so far.

Unlike the current builtin ipv4 rpfilter, packets subject to ipsec transformation are not automatically excluded; if you want this combine with policy match.

Packets arriving on loopback interfaces always match.

---
 include/linux/netfilter/xt_rpfilter.h |   21 ++++
 net/ipv4/netfilter/Kconfig            |   10 ++
 net/ipv4/netfilter/Makefile           |    1 +
 net/ipv4/netfilter/ipt_rpfilter.c     |  165 +++++++++++++++++++++++++++++++++
 4 files changed, 197 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/netfilter/xt_rpfilter.h
 create mode 100644 net/ipv4/netfilter/ipt_rpfilter.c
include/linux/netfilter/xt_rpfilter.h new file +21 -0
new file mode 100644
index 0000000..8d2d3f8
@@ -0,0 +1,21 @@
+#ifndef _XT_RPATH_H
+#define _XT_RPATH_H
+
+#include <linux/types.h>
+
+enum {
+	XT_RPFILTER_LOOSE = 1 << 0,
+	XT_RPFILTER_VALID_MARK = 1 << 1,
+	XT_RPFILTER_ACCEPT_LOCAL = 1 << 2,
+#ifdef __KERNEL__
+	XT_RPFILTER_OPTION_MASK = XT_RPFILTER_LOOSE |
+				  XT_RPFILTER_VALID_MARK |
+				  XT_RPFILTER_ACCEPT_LOCAL,
+#endif
+};
+
+struct xt_rpfilter_info {
+	__u8 flags;
+};
+
+#endif
net/ipv4/netfilter/Kconfig +10 -0
index 1dfc18a..ef8c1ab 100644
@@ -82,6 +82,16 @@ config IP_NF_MATCH_ECN
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP_NF_MATCH_RPFILTER
+	tristate '"rpfilter" reverse path filter match support'
+	depends on NETFILTER_ADVANCED
+	---help---
+	  This option allows you to match packets whose replies would
+	  go out via the interface the packet came in.
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+	  The module will be called ipt_rpfilter.
+
 config IP_NF_MATCH_TTL
 	tristate '"ttl" match support'
 	depends on NETFILTER_ADVANCED
net/ipv4/netfilter/Makefile +1 -0
index dca2082..123dd88 100644
@@ -50,6 +50,7 @@ obj-$(CONFIG_IP_NF_SECURITY) += iptable_security.o
 # matches
 obj-$(CONFIG_IP_NF_MATCH_AH) += ipt_ah.o
 obj-$(CONFIG_IP_NF_MATCH_ECN) += ipt_ecn.o
+obj-$(CONFIG_IP_NF_MATCH_RPFILTER) += ipt_rpfilter.o
 
 # targets
 obj-$(CONFIG_IP_NF_TARGET_CLUSTERIP) += ipt_CLUSTERIP.o
net/ipv4/netfilter/ipt_rpfilter.c new file +165 -0

165 lines not on this page · raw message

Signature · 1 line
1.7.3.4
author maintainer
#4 lore raw

RFC PATCH v2 4/5 ipv6: add ip6_route_lookup

In reply to Florian Westphal

like rt6_lookup, but allows caller to pass in flowi6 structure. Will be used by the upcoming ipv6 netfilter reverse path filter match.

---
 include/net/ip6_route.h |    2 ++
 net/ipv6/route.c        |    7 +++++++
 2 files changed, 9 insertions(+), 0 deletions(-)
include/net/ip6_route.h +2 -0
index 5e91b72..9c9399c 100644
@@ -70,6 +70,8 @@ extern void			ip6_route_input(struct sk_buff *skb);
 extern struct dst_entry *	ip6_route_output(struct net *net,
 						 const struct sock *sk,
 						 struct flowi6 *fl6);
+extern struct dst_entry *	ip6_route_lookup(struct net *net,
+						 struct flowi6 *fl6, int flags);
 
 extern int			ip6_route_init(void);
 extern void			ip6_route_cleanup(void);
net/ipv6/route.c +7 -0
index 216ff31..183722c 100644
@@ -629,6 +629,13 @@ out:
 
 }
 
+struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6,
+				    int flags)
+{
+	return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup);
+}
+EXPORT_SYMBOL_GPL(ip6_route_lookup);
+
 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
 			    const struct in6_addr *saddr, int oif, int strict)
 {
Signature · 1 line
1.7.3.4
author maintainer
#5 lore raw

RFC PATCH v2 5/5 netfilter: add ipv6 reverse path filter match

In reply to Florian Westphal

Differs from the ipv4 version:
The oif is not used as input interface, instead this only tests if the saddr of the packet is reachable via the interface the packet arrived on.

This is not merged with the ipv4 match into xt_rpfilter.c to avoid introduce module dependency issues.

---
 net/ipv6/netfilter/Kconfig         |   10 +++
 net/ipv6/netfilter/Makefile        |    1 +
 net/ipv6/netfilter/ip6t_rpfilter.c |  124 ++++++++++++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 0 deletions(-)
 create mode 100644 net/ipv6/netfilter/ip6t_rpfilter.c
net/ipv6/netfilter/Kconfig +10 -0
index 4484648..49ec98b 100644
@@ -125,6 +125,16 @@ config IP6_NF_MATCH_MH
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config IP6_NF_MATCH_RPFILTER
+	tristate '"rpfilter" reverse path filter match support'
+	depends on NETFILTER_ADVANCED
+	---help---
+	  This option allows you to match packets whose replies would
+	  go out via the interface the packet came in.
+
+	  To compile it as a module, choose M here.  If unsure, say N.
+	  The module will be called ip6t_rpfilter.
+
 config IP6_NF_MATCH_RT
 	tristate '"rt" Routing header match support'
 	depends on NETFILTER_ADVANCED
net/ipv6/netfilter/Makefile +1 -0
index abfee91..2eaed96 100644
@@ -27,6 +27,7 @@ obj-$(CONFIG_IP6_NF_MATCH_FRAG) += ip6t_frag.o
 obj-$(CONFIG_IP6_NF_MATCH_IPV6HEADER) += ip6t_ipv6header.o
 obj-$(CONFIG_IP6_NF_MATCH_MH) += ip6t_mh.o
 obj-$(CONFIG_IP6_NF_MATCH_OPTS) += ip6t_hbh.o
+obj-$(CONFIG_IP6_NF_MATCH_RPFILTER) += ip6t_rpfilter.o
 obj-$(CONFIG_IP6_NF_MATCH_RT) += ip6t_rt.o
 
 # targets
net/ipv6/netfilter/ip6t_rpfilter.c new file +124 -0

124 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#6 lore raw
In reply to Florian Westphal
Quoted 23 lines · Florian Westphal

Version 2 of the ipv4/v6 reverse path filter matches discussed during nfws 2011.

The ipv4 match (ipt_rpfilter) tries to do exactly what the current fib_validate_source does. The main problem with this is that we need to do an additional fib lookup to get the oif in the match. [ delaying until FORWARD is invoked is not possible because by
that point the stack might have already sent icmp errors ].

Patrick McHardy suggested to simply attach the result as the dst, so ipv4 input path doesn't have to do it again.

This works, but does have a few side effects wrt. route-by-mark and TPROXY, see patch changelog for details.

The ipv6 version does a pure 'reverse' lookup instead. This makes things a lot easier (e.g. when multiple route entries exist), but has the caveat that a real reply packet might be handled differently due to policy routing rules.

Userspace part is stored in my iptables repository on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/iptables.git (branch 'rpfilter').

Quoted 7 lines

Kernel patches are located in the 'xt_rpfilter_5' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git
(patches will be sent as followup to this email).

[ in case you are wondering: the earlier xt_rpfilter version was
removed -- causes too many module dependency issues and most of the code cannot be shared anyway ].

This involves other net changes, I'd like to get an ack from David before applying this. Or let me know if it's better to follow the netdev path.

author maintainer
#7 lore raw
Quoted 11 lines · Pablo Neira Ayuso
Quoted 7 lines

Kernel patches are located in the 'xt_rpfilter_5' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git
(patches will be sent as followup to this email).

[ in case you are wondering: the earlier xt_rpfilter version was
removed -- causes too many module dependency issues and most of the code cannot be shared anyway ].

This involves other net changes, I'd like to get an ack from David before applying this. Or let me know if it's better to follow the netdev path.

Pablo, please do not apply these patches, I have newer versions available.

David, it would be nice if you could indicate how these patches should be merged (ie. via net-next or netfilter-next).

[ in case there are objections, I'd like to hear about those too :-) ]

Thanks,
Florian

maintainer
#8 lore raw

From: Florian Westphal <[email protected]>
Date: Wed, 28 Sep 2011 23:23:58 +0200

Quoted 2 lines

David, it would be nice if you could indicate how these patches should be merged (ie. via net-next or netfilter-next).

I'm fine with them going into netfilter-next, I'll add my ACKs to the next version you post.