Skip to contentSkip to conversation

netfilter reverse path filter matches

RFC PATCH Superseded Florian Westphal 3 participants 12 messages Aug 26, 2011 – Aug 30, 2011

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover netfilter reverse path filter matches

Here are the ipv4/ipv6 reverse path filter matches that were discussed briefly at the workshop.

These are not ready for merging at this time, but I think sending current status is helpful in moving things forward.

There are 3 matches:
xt_rpfilter, ipt_rpfilter, ip6t_rpfilter.

Obviously, it will be either xt_rpfilter or the latter two.
At the moment, xt_rpfilter has too many issues: it does not behave the same as current ip4-builtin rp_filter, because output route resolution is used for validation.
Depending on the routes this may result in different route lookup results than what a real reply packet would see.

It also breaks with ipv4 multipath setups when there are several matching routes.
As ipv4 and ipv6 need different treatment anyway, using different modules does not seem to be a problem. (This also avoids extra module dependency issues)

The ipv4-only match (ipt_rpfilter) tries to do exactly what the current fib_validate_source does. Known remaining Problems are:

- additional fib lookup to get oif (will be used in reverse lookup as iif)
- using match in PREROUTING may do the wrong thing when
policy routing is used via mangle PREROUTING (we may perform fib lookup with wrong nfmark)
- using it in FORWARD is problematic because stack could send out icmp errors (ttl exceeded, PMTU, ...) before the packet would have been rpf-tested.

We seemed to have consesus on the following:
- no special treatment of ipsec-protected packets (users can use policy match)
- packets from IFF_LOOPBACK should always match
- result caching can be implemented later on, if needed.

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' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git (i'll send the relevant patches as followup to this email).

Please let me know if I forgot any remaing issues, or if you spot any additional issues that have not been raised so far.

Thanks,
Florian

author maintainer
#1 lore raw

RFC PATCH 1/3 netfilter: add reverse path filter match

More flexible replacement for the reverse path filter built into the ipv4 network stack; also supports ipv6.

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

---
 include/linux/netfilter/xt_rpfilter.h |   21 ++++
 net/netfilter/Kconfig                 |   10 ++
 net/netfilter/Makefile                |    1 +
 net/netfilter/xt_rpfilter.c           |  175 +++++++++++++++++++++++++++++++++
 4 files changed, 207 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/netfilter/xt_rpfilter.h
 create mode 100644 net/netfilter/xt_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/netfilter/Kconfig +10 -0
index 32bff6d..2e746e5 100644
@@ -881,6 +881,16 @@ config NETFILTER_XT_MATCH_MULTIPORT
 
 	  To compile it as a module, choose M here.  If unsure, say N.
 
+config NETFILTER_XT_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 xt_rpfilter.
+
 config NETFILTER_XT_MATCH_OSF
 	tristate '"osf" Passive OS fingerprint match'
 	depends on NETFILTER_ADVANCED && NETFILTER_NETLINK
net/netfilter/Makefile +1 -0
index 1a02853..c69d9fb 100644
@@ -90,6 +90,7 @@ obj-$(CONFIG_NETFILTER_XT_MATCH_LENGTH) += xt_length.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_LIMIT) += xt_limit.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_MAC) += xt_mac.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_MULTIPORT) += xt_multiport.o
+obj-$(CONFIG_NETFILTER_XT_MATCH_RPFILTER) += xt_rpfilter.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OSF) += xt_osf.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_OWNER) += xt_owner.o
 obj-$(CONFIG_NETFILTER_XT_MATCH_PHYSDEV) += xt_physdev.o
net/netfilter/xt_rpfilter.c new file +175 -0

175 lines not on this page · raw message

Signature · 1 line
1.7.3.4
author maintainer
#2 lore raw

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

In reply to Florian Westphal

This tries to mimic behaviour of fib_validate_source.

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)
- no result caching so far

Other issues:
- can't use 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
- using it in PREROUTING may do the wrong thing (e.g. when using policy routing via mangle PREROUTING)

---
 net/ipv4/netfilter/Kconfig        |   10 ++
 net/ipv4/netfilter/Makefile       |    1 +
 net/ipv4/netfilter/ipt_rpfilter.c |  172 +++++++++++++++++++++++++++++++++++++
 3 files changed, 183 insertions(+), 0 deletions(-)
 create mode 100644 net/ipv4/netfilter/ipt_rpfilter.c
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 +172 -0

172 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#3 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 4 lines · Florian Westphal

This tries to mimic behaviour of fib_validate_source.

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)

As discussed during the workshop, we could just perform input routing in the module to get the oif for free. That would require to take care of statistics as currently done in ip_rcv_finish() though.

Quoted 8 lines

- no result caching so far

Other issues:
- can't use 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
- using it in PREROUTING may do the wrong thing (e.g. when using policy routing via mangle PREROUTING)

Not if you're setting up policy routing keys (marks) before doing rp_filter, right? Would require using it in the mangle table of course.

author maintainer
#4 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 6 lines · Patrick McHardy
Quoted 2 lines

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)

As discussed during the workshop, we could just perform input routing in the module to get the oif for free. That would require to take care of statistics as currently done in ip_rcv_finish() though.

Right.
Any idea on how to solve the 'struct sk_buff *' (ip_route_input) vs. 'const struct sk_buff *' (matches) problem?

We'd have to modify all the match signatures...

Quoted 10 lines
Quoted 6 lines

Other issues:
- can't use 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
- using it in PREROUTING may do the wrong thing (e.g. when using policy routing via mangle PREROUTING)

Not if you're setting up policy routing keys (marks) before doing rp_filter, right? Would require using it in the mangle table of course.

Yes that should work as well.
It might be unexpected for some people, though (but this is just a documentation issue).

#5 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 8 lines · Florian Westphal
Quoted 6 lines · Patrick McHardy
Quoted 2 lines

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)

As discussed during the workshop, we could just perform input routing in the module to get the oif for free. That would require to take care of statistics as currently done in ip_rcv_finish() though.

Quoted 5 lines

Right.
Any idea on how to solve the 'struct sk_buff *' (ip_route_input) vs. 'const struct sk_buff *' (matches) problem?

We'd have to modify all the match signatures...

Some modules already remove the const by casting it away, not pretty, but works. Since the kernel doesn't assume strict aliasing this also shouldn't cause any problems in the future. Alternatively we can change the function signatures of course, although that would be a bit unfortunate just for this special case.

Quoted 7 lines
Quoted 7 lines
Quoted 6 lines

Other issues:
- can't use 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
- using it in PREROUTING may do the wrong thing (e.g. when using policy routing via mangle PREROUTING)

Quoted 7 lines
Quoted 3 lines

Not if you're setting up policy routing keys (marks) before doing rp_filter, right? Would require using it in the mangle table of course.

Yes that should work as well.
It might be unexpected for some people, though (but this is just a documentation issue).

I agree.

author maintainer
#6 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Florian Westphal
Quoted 6 lines · Patrick McHardy
Quoted 2 lines

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)

As discussed during the workshop, we could just perform input routing in the module to get the oif for free. That would require to take care of statistics as currently done in ip_rcv_finish() though.

Right.
Any idea on how to solve the 'struct sk_buff *' (ip_route_input) vs. 'const struct sk_buff *' (matches) problem?

Quoted 7 lines
Quoted 1 line

We'd have to modify all the match signatures...

Some modules already remove the const by casting it away, not pretty, but works. Since the kernel doesn't assume strict aliasing this also shouldn't cause any problems in the future. Alternatively we can change the function signatures of course, although that would be a bit unfortunate just for this special case.

Agreed, modifying the match function signature for this case would be sad.

I'll wait a couple of days and will send an updated version that uses the ugly cast + ip_route_input.

Thanks for your input!

#7 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 22 lines · Florian Westphal
Quoted 13 lines · Patrick McHardy
Quoted 12 lines · Florian Westphal
Quoted 6 lines · Patrick McHardy
Quoted 2 lines

main drawback:
- additional fib lookup to get oif (used as flow key in reverse lookup)

As discussed during the workshop, we could just perform input routing in the module to get the oif for free. That would require to take care of statistics as currently done in ip_rcv_finish() though.

Right.
Any idea on how to solve the 'struct sk_buff *' (ip_route_input) vs. 'const struct sk_buff *' (matches) problem?

Quoted 7 lines
Quoted 1 line

We'd have to modify all the match signatures...

Some modules already remove the const by casting it away, not pretty, but works. Since the kernel doesn't assume strict aliasing this also shouldn't cause any problems in the future. Alternatively we can change the function signatures of course, although that would be a bit unfortunate just for this special case.

Quoted 5 lines

Agreed, modifying the match function signature for this case would be sad.

I'll wait a couple of days and will send an updated version that uses the ugly cast + ip_route_input.

Regarding the statistic update, I'd suggest to move those to a new function and call it from your module to avoid duplication and possibly getting out of sync in the future.

#8 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

In reply to Florian Westphal
Quoted 7 lines · Florian Westphal
Quoted 6 lines

Some modules already remove the const by casting it away, not pretty, but works. Since the kernel doesn't assume strict aliasing this also shouldn't cause any problems in the future. Alternatively we can change the function signatures of course, although that would be a bit unfortunate just for this special case.

Quoted 5 lines

Agreed, modifying the match function signature for this case would be sad.

I'll wait a couple of days and will send an updated version that uses the ugly cast + ip_route_input.

What could be done is:

struct xt_match {
	union {
		int (*match)(const struct sk_buff *, ...);
		int (*match_nonc)(struct sk_buff *, ...);
	};
};

falls into the same "ugh, what beauty" category :-)

#9 lore raw

Re: [PATCH RFC 2/3] netfilter: add ipv4 reverse path filter match

Quoted 17 lines · Jan Engelhardt
Quoted 7 lines · Florian Westphal
Quoted 6 lines

Some modules already remove the const by casting it away, not pretty, but works. Since the kernel doesn't assume strict aliasing this also shouldn't cause any problems in the future. Alternatively we can change the function signatures of course, although that would be a bit unfortunate just for this special case.

Quoted 5 lines

Agreed, modifying the match function signature for this case would be sad.

I'll wait a couple of days and will send an updated version that uses the ugly cast + ip_route_input.

What could be done is:

Quoted 8 lines
struct xt_match {
	union {
		int (*match)(const struct sk_buff *, ...);
		int (*match_nonc)(struct sk_buff *, ...);
	};
};

falls into the same "ugh, what beauty" category :-)

Yes, I've considered that as well. But yeah, its not much better than simply casting the const away.

author maintainer
#10 lore raw

RFC PATCH 3/3 ip6t_rpf: initial version

In reply to Florian Westphal

compile tested only.
Differs significantly from ipv4 version, as the oif will not be used as flow key (we use the iif).

Also, multicast probably needs special handling, too.

---
 net/ipv6/netfilter/Kconfig         |   10 +++
 net/ipv6/netfilter/Makefile        |    1 +
 net/ipv6/netfilter/ip6t_rpfilter.c |  116 ++++++++++++++++++++++++++++++++++++
 3 files changed, 127 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 +116 -0

116 lines not on this page · raw message

Signature · 1 line
1.7.3.4
#11 lore raw

Re: [PATCH RFC 3/3] ip6t_rpf: initial version

Quoted 86 lines · Florian Westphal

compile tested only.
Differs significantly from ipv4 version, as the oif will not be used as flow key (we use the iif).

Also, multicast probably needs special handling, too.
---

net/ipv6/netfilter/Kconfig         |   10 +++
net/ipv6/netfilter/Makefile        |    1 +
net/ipv6/netfilter/ip6t_rpfilter.c |  116 ++++++++++++++++++++++++++++++++++++
3 files changed, 127 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 +0 -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 +35 -0
new file mode 100644
index 0000000..bc5075d
@@ -0,0 +1,116 @@
+/*
+ *  Copyright (c) 2011 Florian Westphal <[email protected]>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ */
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/module.h>
+#include <linux/skbuff.h>
+#include <linux/netdevice.h>
+#include <linux/route.h>
+#include <net/dsfield.h>
+#include <net/ip6_fib.h>
+#include <net/ip6_route.h>
+
+#include <linux/netfilter/xt_rpfilter.h>
+#include <linux/netfilter/x_tables.h>
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Florian Westphal <[email protected]>");
+MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
+
+/* don't try to find route from mcast/bcast/zeronet */
+static bool rpfilter_addr_unicast(const struct in6_addr *addr)
+{
+	int addr_type = ipv6_addr_type(addr);
+	return addr_type & IPV6_ADDR_UNICAST;
+}
+
+static bool rpfilter_lookup_reverse6(const struct sk_buff *skb,
+				     const struct net_device *dev, u8 flags)
+{
+	struct rt6_info *rt;
+	struct dst_entry *dst;

net/ipv6/netfilter/ip6t_rpfilter.c line 45

Quoted 10 lines
net/ipv6/netfilter/ip6t_rpfilter.c new file +10 -0
+	bool ret = false;
+	struct ipv6hdr *iph = ipv6_hdr(skb);
+	struct flowi6 fl6 = {
+		.flowi6_oif = dev->ifindex,
+		.daddr = iph->daddr,
+		.flowi6_proto = iph->nexthdr,
+	};
+
+	fl6.flowlabel = ipv6_get_dsfield(iph);
+	fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;

This looks weird. In xt_rpfilter, you used

+ .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,

and now you are copying the traffic class into the flowlabel instead.