netfilter reverse path filter matches
Subsystems (4)
Florian Westphal
maintainercover netfilter reverse path filter matches
Version 3 of the ipv4/v6 reverse path filter matches discussed during nfws 2011.
Major change since v2 is that the ipv4 version no longer tries to determine the (likely) input interface for the reverse test.
To obtain that information, an additional lookup would be required (first obtain oif, then use that oif as iif in the reversed lookup -- this is what the existing rpf in fib_validate_source() does).
While its certainly possible to mimic fib_validate_source more closely, doing so has certain side effects:
- either pay additonal rt lookup cost
- or, attach the input route early on ourselves, so ipv4 stack does not need to do it (which can then cause strange effects when using policy routing, as marking in pre-routing has no effect when done after -m rpfilter ...)
2nd change is a new "--invert" flag to permit one-line dropping ("-m rpfilter --invert -j DROP") of packets that failed the reverse path lookup test.
Userspace part is stored in my iptables repository on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/iptables.git (branch 'xt_rpfilter_9').
Kernel patches are located in the 'xt_rpfilter_9' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git.
Patches will be sent as followup to this email.
Summary of changes:
The following changes since commit e56c57d0d3fdbbdf583d3af96bfb803b8dfa713e:
net: rename sk_clone to sk_clone_lock (2011-11-08 17:07:07 -0500)
are available in the git repository at:git://git.breakpoint.cc/fw/nf-next.git xt_rpfilter_9
Florian Westphal (4):
net: ipv4: export fib_lookup and fib_table_lookup
netfilter: add ipv4 reverse path filter match
ipv6: add ip6_route_lookup
netfilter: add ipv6 reverse path filter match
include/linux/netfilter/xt_rpfilter.h | 23 ++++++ include/net/ip6_route.h | 2 + net/ipv4/fib_rules.c | 1 + net/ipv4/fib_trie.c | 1 + net/ipv4/netfilter/Kconfig | 10 +++ net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/ipt_rpfilter.c | 141 +++++++++++++++++++++++++++++++++ net/ipv6/netfilter/Kconfig | 10 +++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_rpfilter.c | 133 +++++++++++++++++++++++++++++++ net/ipv6/route.c | 7 ++ 11 files changed, 330 insertions(+), 0 deletions(-)
create mode 100644 include/linux/netfilter/xt_rpfilter.h create mode 100644 net/ipv4/netfilter/ipt_rpfilter.c create mode 100644 net/ipv6/netfilter/ip6t_rpfilter.c
Florian Westphal
maintainerPATCH 1/4 net: ipv4: export fib_lookup and fib_table_lookup
The reverse path filter module will use fib_lookup.
If CONFIG_IP_MULTIPLE_TABLES is not set, fib_lookup is only a static inline helper that calls fib_table_lookup, so export that too.
--- net/ipv4/fib_rules.c | 1 + net/ipv4/fib_trie.c | 1 + 2 files changed, 2 insertions(+), 0 deletions(-)
net/ipv4/fib_rules.c +1 -0
@@ -67,6 +67,7 @@ int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)return err; } +EXPORT_SYMBOL_GPL (fib_lookup);static int fib4_rule_action (struct fib_rule *rule,struct flowi *flp,int flags,struct fib_lookup_arg *arg)
net/ipv4/fib_trie.c +1 -0
@@ -1607,6 +1607,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
Florian Westphal
maintainerPATCH 2/4 netfilter: add ipv4 reverse path filter match
In reply to Florian WestphalThis tries to do the same thing as fib_validate_source(), but differs in several aspects.
The most important difference is that the reverse path filter built into fib_validate_source uses the oif as iif when performing the reverse lookup. We do not do this, as the oif is not yet known by the time the PREROUTING hook is invoked.
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 such an additional lookup in PREROUTING, Patrick McHardy suggested to attach the path information directly in the match.
This works, but it also has a few caveats. Most importantly, when using marks in PREROUTING to re-route traffic based on the nfmark, -m rpfilter would have to be used after the nfmark has been set; otherwise the nfmark would have no effect (because the route is already attached).
Another problem would be interaction with -j TPROXY, as this target sets an nfmark and uses ACCEPT instead of continue, i.e. such a version of -m rpfilter cannot be used for the initial to-be-intercepted packets.
In case it turns out that the oif is required, we can add Patricks suggestion with a new match option (e.g. --rpf-use-oif) to keep ruleset compatibility.
Another difference to current builtin ipv4 rpfilter is that packets subject to ipsec transformation are not automatically excluded. If you want this, simply combine -m rpfilter with the policy match.
Packets arriving on loopback interfaces always match.
--- include/linux/netfilter/xt_rpfilter.h | 23 ++++++ net/ipv4/netfilter/Kconfig | 10 +++ net/ipv4/netfilter/Makefile | 1 + net/ipv4/netfilter/ipt_rpfilter.c | 141 +++++++++++++++++++++++++++++++++ 4 files changed, 175 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 +23 -0
@@ -0,0 +1,23 @@ +#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 , +XT_RPFILTER_INVERT =1 <<3 , +#ifdef __KERNEL__ +XT_RPFILTER_OPTION_MASK =XT_RPFILTER_LOOSE | +XT_RPFILTER_VALID_MARK | +XT_RPFILTER_ACCEPT_LOCAL | +XT_RPFILTER_INVERT , +#endif +}; + +struct xt_rpfilter_info { +__u8 flags; +}; + +#endif
net/ipv4/netfilter/Kconfig +10 -0
@@ -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
@@ -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 +141 -0
141 lines not on this page · raw message
Signature · 1 line
1.7.3.4
Florian Westphal
maintainerPATCH 3/4 ipv6: add ip6_route_lookup
In reply to Florian Westphallike 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
@@ -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
@@ -656,6 +656,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
Florian Westphal
maintainerPATCH 4/4 netfilter: add ipv6 reverse path filter match
In reply to Florian WestphalThis is not merged with the ipv4 match into xt_rpfilter.c to avoid ipv6 module dependency issues.
--- net/ipv6/netfilter/Kconfig | 10 +++ net/ipv6/netfilter/Makefile | 1 + net/ipv6/netfilter/ip6t_rpfilter.c | 133 ++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 0 deletions(-) create mode 100644 net/ipv6/netfilter/ip6t_rpfilter.c
net/ipv6/netfilter/Kconfig +10 -0
@@ -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
@@ -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 +133 -0
133 lines not on this page · raw message
Signature · 1 line
1.7.3.4
From: Florian Westphal <[email protected]>
Date: Wed, 9 Nov 2011 23:19:44 +0100
Quoted 2 lines
Version 3 of the ipv4/v6 reverse path filter matches discussed during nfws 2011.
I fully support these changes, please feel free to merge them in via the netfilter tree and to add my ack:
Pablo Neira Ayuso
maintainerQuoted 10 lines · David Miller
From: Florian Westphal <[email protected]>
Date: Wed, 9 Nov 2011 23:19:44 +0100Quoted 2 lines
Version 3 of the ipv4/v6 reverse path filter matches discussed during nfws 2011.
I fully support these changes, please feel free to merge them in via the netfilter tree and to add my ack:
Acked-by David S. Miller <[email protected]>
Thanks for taking the time to review this, I need some time to test it here but I think we'll get it into net-next soon.
Hi Florian,
Quoted 2 lines · Florian Westphal
Userspace part is stored in my iptables repository on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/iptables.git (branch 'xt_rpfilter_9').
I have taken this into iptables, now it is available in the rpfilter branch.
One inquiry, I need to know copyright / license details for this extensions/libxt_rpfilter.c
I remember that Harald insisted that having one copyright notice per .c file is a good practise (even if this doesn't seem to happen in other iptables extensions files).
No need to resend the patch, I can ammend the patch with the copyright / licensing header.
See extensions/libxt_cluster.c for reference.
Quoted 2 lines
Kernel patches are located in the '
xt_rpfilter_9' branch on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/nf-next.git.
Applied to my nf-next tree.
http://1984.lsi.us.es/git/net-next/
thanks Florian.
Florian Westphal
maintainerQuoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Florian Westphal
Userspace part is stored in my iptables repository on
http://git.breakpoint.cc/cgi-bin/gitweb.cgi?p=fw/iptables.git (branch 'xt_rpfilter_9').I have taken this into iptables, now it is available in the rpfilter branch.
Thanks Pablo.
Quoted 3 lines
One inquiry, I need to know copyright / license details for this
extensions/libxt_rpfilter.c
[..]
Quoted 4 lines
No need to resend the patch, I can ammend the patch with the copyright / licensing header.
See
extensions/libxt_cluster.cfor reference.
In this case, please amend it to include the same GPL-2 boilerplate as libxt_cluster.c has.
Thanks,
Florian