blob: 1ee1b25df09679c32e0bd21526024cea9f7d26d1 [file] [log] [blame]
Florian Westphale26f9a42011-08-19 13:52:40 +02001/*
2 * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/netdevice.h>
12#include <linux/route.h>
13#include <net/ip6_fib.h>
14#include <net/ip6_route.h>
15
16#include <linux/netfilter/xt_rpfilter.h>
17#include <linux/netfilter/x_tables.h>
18
19MODULE_LICENSE("GPL");
20MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
21MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
22
23static bool rpfilter_addr_unicast(const struct in6_addr *addr)
24{
25 int addr_type = ipv6_addr_type(addr);
26 return addr_type & IPV6_ADDR_UNICAST;
27}
28
Eric W. Biederman686c9b52015-09-18 14:32:59 -050029static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
Florian Westphale26f9a42011-08-19 13:52:40 +020030 const struct net_device *dev, u8 flags)
31{
32 struct rt6_info *rt;
33 struct ipv6hdr *iph = ipv6_hdr(skb);
34 bool ret = false;
35 struct flowi6 fl6 = {
Julian Anastasove374c612014-04-28 10:51:56 +030036 .flowi6_iif = LOOPBACK_IFINDEX,
Florian Westphale26f9a42011-08-19 13:52:40 +020037 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
38 .flowi6_proto = iph->nexthdr,
39 .daddr = iph->saddr,
40 };
41 int lookup_flags;
42
43 if (rpfilter_addr_unicast(&iph->daddr)) {
44 memcpy(&fl6.saddr, &iph->daddr, sizeof(struct in6_addr));
45 lookup_flags = RT6_LOOKUP_F_HAS_SADDR;
46 } else {
47 lookup_flags = 0;
48 }
49
50 fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
51 if ((flags & XT_RPFILTER_LOOSE) == 0) {
52 fl6.flowi6_oif = dev->ifindex;
53 lookup_flags |= RT6_LOOKUP_F_IFACE;
54 }
55
Eric W. Biederman686c9b52015-09-18 14:32:59 -050056 rt = (void *) ip6_route_lookup(net, &fl6, lookup_flags);
Florian Westphale26f9a42011-08-19 13:52:40 +020057 if (rt->dst.error)
58 goto out;
59
60 if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
61 goto out;
62
63 if (rt->rt6i_flags & RTF_LOCAL) {
64 ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
65 goto out;
66 }
67
68 if (rt->rt6i_idev->dev == dev || (flags & XT_RPFILTER_LOOSE))
69 ret = true;
70 out:
Amerigo Wang94e187c2012-10-29 00:13:19 +000071 ip6_rt_put(rt);
Florian Westphale26f9a42011-08-19 13:52:40 +020072 return ret;
73}
74
Florian Westphalf83a7ea2013-04-17 22:45:24 +000075static bool rpfilter_is_local(const struct sk_buff *skb)
76{
77 const struct rt6_info *rt = (const void *) skb_dst(skb);
78 return rt && (rt->rt6i_flags & RTF_LOCAL);
79}
80
Florian Westphale26f9a42011-08-19 13:52:40 +020081static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
82{
83 const struct xt_rpfilter_info *info = par->matchinfo;
84 int saddrtype;
85 struct ipv6hdr *iph;
86 bool invert = info->flags & XT_RPFILTER_INVERT;
87
Florian Westphalf83a7ea2013-04-17 22:45:24 +000088 if (rpfilter_is_local(skb))
Florian Westphale26f9a42011-08-19 13:52:40 +020089 return true ^ invert;
90
91 iph = ipv6_hdr(skb);
92 saddrtype = ipv6_addr_type(&iph->saddr);
93 if (unlikely(saddrtype == IPV6_ADDR_ANY))
94 return true ^ invert; /* not routable: forward path will drop it */
95
Eric W. Biederman686c9b52015-09-18 14:32:59 -050096 return rpfilter_lookup_reverse6(par->net, skb, par->in, info->flags) ^ invert;
Florian Westphale26f9a42011-08-19 13:52:40 +020097}
98
99static int rpfilter_check(const struct xt_mtchk_param *par)
100{
101 const struct xt_rpfilter_info *info = par->matchinfo;
102 unsigned int options = ~XT_RPFILTER_OPTION_MASK;
103
104 if (info->flags & options) {
105 pr_info("unknown options encountered");
106 return -EINVAL;
107 }
108
109 if (strcmp(par->table, "mangle") != 0 &&
110 strcmp(par->table, "raw") != 0) {
111 pr_info("match only valid in the \'raw\' "
112 "or \'mangle\' tables, not \'%s\'.\n", par->table);
113 return -EINVAL;
114 }
115
116 return 0;
117}
118
119static struct xt_match rpfilter_mt_reg __read_mostly = {
120 .name = "rpfilter",
121 .family = NFPROTO_IPV6,
122 .checkentry = rpfilter_check,
123 .match = rpfilter_mt,
124 .matchsize = sizeof(struct xt_rpfilter_info),
125 .hooks = (1 << NF_INET_PRE_ROUTING),
126 .me = THIS_MODULE
127};
128
129static int __init rpfilter_mt_init(void)
130{
131 return xt_register_match(&rpfilter_mt_reg);
132}
133
134static void __exit rpfilter_mt_exit(void)
135{
136 xt_unregister_match(&rpfilter_mt_reg);
137}
138
139module_init(rpfilter_mt_init);
140module_exit(rpfilter_mt_exit);