Merge tag 'arc-5.2-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-2.6-microblaze.git] / net / ipv6 / netfilter / ip6t_rpfilter.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2011 Florian Westphal <fw@strlen.de>
4  */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/skbuff.h>
8 #include <linux/netdevice.h>
9 #include <linux/route.h>
10 #include <net/ip6_fib.h>
11 #include <net/ip6_route.h>
12
13 #include <linux/netfilter/xt_rpfilter.h>
14 #include <linux/netfilter/x_tables.h>
15
16 MODULE_LICENSE("GPL");
17 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
18 MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
19
20 static bool rpfilter_addr_unicast(const struct in6_addr *addr)
21 {
22         int addr_type = ipv6_addr_type(addr);
23         return addr_type & IPV6_ADDR_UNICAST;
24 }
25
26 static bool rpfilter_addr_linklocal(const struct in6_addr *addr)
27 {
28         int addr_type = ipv6_addr_type(addr);
29         return addr_type & IPV6_ADDR_LINKLOCAL;
30 }
31
32 static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
33                                      const struct net_device *dev, u8 flags)
34 {
35         struct rt6_info *rt;
36         struct ipv6hdr *iph = ipv6_hdr(skb);
37         bool ret = false;
38         struct flowi6 fl6 = {
39                 .flowi6_iif = LOOPBACK_IFINDEX,
40                 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
41                 .flowi6_proto = iph->nexthdr,
42                 .daddr = iph->saddr,
43         };
44         int lookup_flags;
45
46         if (rpfilter_addr_unicast(&iph->daddr)) {
47                 memcpy(&fl6.saddr, &iph->daddr, sizeof(struct in6_addr));
48                 lookup_flags = RT6_LOOKUP_F_HAS_SADDR;
49         } else {
50                 lookup_flags = 0;
51         }
52
53         fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
54
55         if (rpfilter_addr_linklocal(&iph->saddr)) {
56                 lookup_flags |= RT6_LOOKUP_F_IFACE;
57                 fl6.flowi6_oif = dev->ifindex;
58         } else if ((flags & XT_RPFILTER_LOOSE) == 0)
59                 fl6.flowi6_oif = dev->ifindex;
60
61         rt = (void *)ip6_route_lookup(net, &fl6, skb, lookup_flags);
62         if (rt->dst.error)
63                 goto out;
64
65         if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
66                 goto out;
67
68         if (rt->rt6i_flags & RTF_LOCAL) {
69                 ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
70                 goto out;
71         }
72
73         if (rt->rt6i_idev->dev == dev || (flags & XT_RPFILTER_LOOSE))
74                 ret = true;
75  out:
76         ip6_rt_put(rt);
77         return ret;
78 }
79
80 static bool
81 rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
82 {
83         return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
84 }
85
86 static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
87 {
88         const struct xt_rpfilter_info *info = par->matchinfo;
89         int saddrtype;
90         struct ipv6hdr *iph;
91         bool invert = info->flags & XT_RPFILTER_INVERT;
92
93         if (rpfilter_is_loopback(skb, xt_in(par)))
94                 return true ^ invert;
95
96         iph = ipv6_hdr(skb);
97         saddrtype = ipv6_addr_type(&iph->saddr);
98         if (unlikely(saddrtype == IPV6_ADDR_ANY))
99                 return true ^ invert; /* not routable: forward path will drop it */
100
101         return rpfilter_lookup_reverse6(xt_net(par), skb, xt_in(par),
102                                         info->flags) ^ invert;
103 }
104
105 static int rpfilter_check(const struct xt_mtchk_param *par)
106 {
107         const struct xt_rpfilter_info *info = par->matchinfo;
108         unsigned int options = ~XT_RPFILTER_OPTION_MASK;
109
110         if (info->flags & options) {
111                 pr_info_ratelimited("unknown options\n");
112                 return -EINVAL;
113         }
114
115         if (strcmp(par->table, "mangle") != 0 &&
116             strcmp(par->table, "raw") != 0) {
117                 pr_info_ratelimited("only valid in \'raw\' or \'mangle\' table, not \'%s\'\n",
118                                     par->table);
119                 return -EINVAL;
120         }
121
122         return 0;
123 }
124
125 static struct xt_match rpfilter_mt_reg __read_mostly = {
126         .name           = "rpfilter",
127         .family         = NFPROTO_IPV6,
128         .checkentry     = rpfilter_check,
129         .match          = rpfilter_mt,
130         .matchsize      = sizeof(struct xt_rpfilter_info),
131         .hooks          = (1 << NF_INET_PRE_ROUTING),
132         .me             = THIS_MODULE
133 };
134
135 static int __init rpfilter_mt_init(void)
136 {
137         return xt_register_match(&rpfilter_mt_reg);
138 }
139
140 static void __exit rpfilter_mt_exit(void)
141 {
142         xt_unregister_match(&rpfilter_mt_reg);
143 }
144
145 module_init(rpfilter_mt_init);
146 module_exit(rpfilter_mt_exit);