Skip to content

Commit 2a7851b

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: add nf_ipv6_ops hook to fix xt_addrtype with IPv6
Quoting https://bugzilla.netfilter.org/show_bug.cgi?id=812: [ ip6tables -m addrtype ] When I tried to use in the nat/PREROUTING it messes up the routing cache even if the rule didn't matched at all. [..] If I remove the --limit-iface-in from the non-working scenario, so just use the -m addrtype --dst-type LOCAL it works! This happens when LOCAL type matching is requested with --limit-iface-in, and the default ipv6 route is via the interface the packet we test arrived on. Because xt_addrtype uses ip6_route_output, the ipv6 routing implementation creates an unwanted cached entry, and the packet won't make it to the real/expected destination. Silently ignoring --limit-iface-in makes the routing work but it breaks rule matching (--dst-type LOCAL with limit-iface-in is supposed to only match if the dst address is configured on the incoming interface; without --limit-iface-in it will match if the address is reachable via lo). The test should call ipv6_chk_addr() instead. However, this would add a link-time dependency on ipv6. There are two possible solutions: 1) Revert the commit that moved ipt_addrtype to xt_addrtype, and put ipv6 specific code into ip6t_addrtype. 2) add new "nf_ipv6_ops" struct to register pointers to ipv6 functions. While the former might seem preferable, Pablo pointed out that there are more xt modules with link-time dependeny issues regarding ipv6, so lets go for 2). Signed-off-by: Florian Westphal <fw@strlen.de> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 497574c commit 2a7851b

6 files changed

Lines changed: 43 additions & 13 deletions

File tree

‎include/linux/netfilter_ipv6.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ extern __sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
1717

1818
extern int ipv6_netfilter_init(void);
1919
extern void ipv6_netfilter_fini(void);
20+
21+
/*
22+
* Hook functions for ipv6 to allow xt_* modules to be built-in even
23+
* if IPv6 is a module.
24+
*/
25+
struct nf_ipv6_ops {
26+
int (*chk_addr)(struct net *net, const struct in6_addr *addr,
27+
const struct net_device *dev, int strict);
28+
};
29+
30+
extern const struct nf_ipv6_ops __rcu *nf_ipv6_ops;
31+
static inline const struct nf_ipv6_ops *nf_get_ipv6_ops(void)
32+
{
33+
return rcu_dereference(nf_ipv6_ops);
34+
}
35+
2036
#else /* CONFIG_NETFILTER */
2137
static inline int ipv6_netfilter_init(void) { return 0; }
2238
static inline void ipv6_netfilter_fini(void) { return; }

‎include/net/addrconf.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern int addrconf_set_dstaddr(struct net *net,
6565

6666
extern int ipv6_chk_addr(struct net *net,
6767
const struct in6_addr *addr,
68-
struct net_device *dev,
68+
const struct net_device *dev,
6969
int strict);
7070

7171
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)

‎net/ipv6/addrconf.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ static int ipv6_count_addresses(struct inet6_dev *idev)
14871487
}
14881488

14891489
int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
1490-
struct net_device *dev, int strict)
1490+
const struct net_device *dev, int strict)
14911491
{
14921492
struct inet6_ifaddr *ifp;
14931493
unsigned int hash = inet6_addr_hash(addr);

‎net/ipv6/netfilter.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <linux/netfilter.h>
1111
#include <linux/netfilter_ipv6.h>
1212
#include <linux/export.h>
13+
#include <net/addrconf.h>
1314
#include <net/dst.h>
1415
#include <net/ipv6.h>
1516
#include <net/ip6_route.h>
@@ -186,6 +187,10 @@ static __sum16 nf_ip6_checksum_partial(struct sk_buff *skb, unsigned int hook,
186187
return csum;
187188
};
188189

190+
static const struct nf_ipv6_ops ipv6ops = {
191+
.chk_addr = ipv6_chk_addr,
192+
};
193+
189194
static const struct nf_afinfo nf_ip6_afinfo = {
190195
.family = AF_INET6,
191196
.checksum = nf_ip6_checksum,
@@ -198,6 +203,7 @@ static const struct nf_afinfo nf_ip6_afinfo = {
198203

199204
int __init ipv6_netfilter_init(void)
200205
{
206+
RCU_INIT_POINTER(nf_ipv6_ops, &ipv6ops);
201207
return nf_register_afinfo(&nf_ip6_afinfo);
202208
}
203209

@@ -206,5 +212,6 @@ int __init ipv6_netfilter_init(void)
206212
*/
207213
void ipv6_netfilter_fini(void)
208214
{
215+
RCU_INIT_POINTER(nf_ipv6_ops, NULL);
209216
nf_unregister_afinfo(&nf_ip6_afinfo);
210217
}

‎net/netfilter/core.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static DEFINE_MUTEX(afinfo_mutex);
3030

3131
const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO] __read_mostly;
3232
EXPORT_SYMBOL(nf_afinfo);
33+
const struct nf_ipv6_ops __rcu *nf_ipv6_ops __read_mostly;
34+
EXPORT_SYMBOL_GPL(nf_ipv6_ops);
3335

3436
int nf_register_afinfo(const struct nf_afinfo *afinfo)
3537
{

‎net/netfilter/xt_addrtype.c‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <net/ip6_fib.h>
2323
#endif
2424

25+
#include <linux/netfilter_ipv6.h>
2526
#include <linux/netfilter/xt_addrtype.h>
2627
#include <linux/netfilter/x_tables.h>
2728

@@ -33,12 +34,12 @@ MODULE_ALIAS("ip6t_addrtype");
3334

3435
#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
3536
static u32 match_lookup_rt6(struct net *net, const struct net_device *dev,
36-
const struct in6_addr *addr)
37+
const struct in6_addr *addr, u16 mask)
3738
{
3839
const struct nf_afinfo *afinfo;
3940
struct flowi6 flow;
4041
struct rt6_info *rt;
41-
u32 ret;
42+
u32 ret = 0;
4243
int route_err;
4344

4445
memset(&flow, 0, sizeof(flow));
@@ -49,28 +50,32 @@ static u32 match_lookup_rt6(struct net *net, const struct net_device *dev,
4950
rcu_read_lock();
5051

5152
afinfo = nf_get_afinfo(NFPROTO_IPV6);
52-
if (afinfo != NULL)
53+
if (afinfo != NULL) {
54+
const struct nf_ipv6_ops *v6ops;
55+
56+
if (dev && (mask & XT_ADDRTYPE_LOCAL)) {
57+
v6ops = nf_get_ipv6_ops();
58+
if (v6ops && v6ops->chk_addr(net, addr, dev, true))
59+
ret = XT_ADDRTYPE_LOCAL;
60+
}
5361
route_err = afinfo->route(net, (struct dst_entry **)&rt,
54-
flowi6_to_flowi(&flow), !!dev);
55-
else
62+
flowi6_to_flowi(&flow), false);
63+
} else {
5664
route_err = 1;
57-
65+
}
5866
rcu_read_unlock();
5967

6068
if (route_err)
6169
return XT_ADDRTYPE_UNREACHABLE;
6270

6371
if (rt->rt6i_flags & RTF_REJECT)
6472
ret = XT_ADDRTYPE_UNREACHABLE;
65-
else
66-
ret = 0;
6773

68-
if (rt->rt6i_flags & RTF_LOCAL)
74+
if (dev == NULL && rt->rt6i_flags & RTF_LOCAL)
6975
ret |= XT_ADDRTYPE_LOCAL;
7076
if (rt->rt6i_flags & RTF_ANYCAST)
7177
ret |= XT_ADDRTYPE_ANYCAST;
7278

73-
7479
dst_release(&rt->dst);
7580
return ret;
7681
}
@@ -90,7 +95,7 @@ static bool match_type6(struct net *net, const struct net_device *dev,
9095

9196
if ((XT_ADDRTYPE_LOCAL | XT_ADDRTYPE_ANYCAST |
9297
XT_ADDRTYPE_UNREACHABLE) & mask)
93-
return !!(mask & match_lookup_rt6(net, dev, addr));
98+
return !!(mask & match_lookup_rt6(net, dev, addr, mask));
9499
return true;
95100
}
96101

0 commit comments

Comments
 (0)