GNU Linux-libre 4.19.264-gnu1
[releases.git] / net / ipv6 / netfilter / ip6t_rpfilter.c
1 /*
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
19 MODULE_LICENSE("GPL");
20 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
21 MODULE_DESCRIPTION("Xtables: IPv6 reverse path filter match");
22
23 static 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
29 static bool rpfilter_addr_linklocal(const struct in6_addr *addr)
30 {
31         int addr_type = ipv6_addr_type(addr);
32         return addr_type & IPV6_ADDR_LINKLOCAL;
33 }
34
35 static bool rpfilter_lookup_reverse6(struct net *net, const struct sk_buff *skb,
36                                      const struct net_device *dev, u8 flags)
37 {
38         struct rt6_info *rt;
39         struct ipv6hdr *iph = ipv6_hdr(skb);
40         bool ret = false;
41         struct flowi6 fl6 = {
42                 .flowi6_iif = LOOPBACK_IFINDEX,
43                 .flowlabel = (* (__be32 *) iph) & IPV6_FLOWINFO_MASK,
44                 .flowi6_proto = iph->nexthdr,
45                 .daddr = iph->saddr,
46         };
47         int lookup_flags;
48
49         if (rpfilter_addr_unicast(&iph->daddr)) {
50                 memcpy(&fl6.saddr, &iph->daddr, sizeof(struct in6_addr));
51                 lookup_flags = RT6_LOOKUP_F_HAS_SADDR;
52         } else {
53                 lookup_flags = 0;
54         }
55
56         fl6.flowi6_mark = flags & XT_RPFILTER_VALID_MARK ? skb->mark : 0;
57
58         if (rpfilter_addr_linklocal(&iph->saddr)) {
59                 lookup_flags |= RT6_LOOKUP_F_IFACE;
60                 fl6.flowi6_oif = dev->ifindex;
61         /* Set flowi6_oif for vrf devices to lookup route in l3mdev domain. */
62         } else if (netif_is_l3_master(dev) || netif_is_l3_slave(dev) ||
63                   (flags & XT_RPFILTER_LOOSE) == 0)
64                 fl6.flowi6_oif = dev->ifindex;
65
66         rt = (void *)ip6_route_lookup(net, &fl6, skb, lookup_flags);
67         if (rt->dst.error)
68                 goto out;
69
70         if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
71                 goto out;
72
73         if (rt->rt6i_flags & RTF_LOCAL) {
74                 ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
75                 goto out;
76         }
77
78         if (rt->rt6i_idev->dev == dev ||
79             l3mdev_master_ifindex_rcu(rt->rt6i_idev->dev) == dev->ifindex ||
80             (flags & XT_RPFILTER_LOOSE))
81                 ret = true;
82  out:
83         ip6_rt_put(rt);
84         return ret;
85 }
86
87 static bool
88 rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
89 {
90         return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
91 }
92
93 static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
94 {
95         const struct xt_rpfilter_info *info = par->matchinfo;
96         int saddrtype;
97         struct ipv6hdr *iph;
98         bool invert = info->flags & XT_RPFILTER_INVERT;
99
100         if (rpfilter_is_loopback(skb, xt_in(par)))
101                 return true ^ invert;
102
103         iph = ipv6_hdr(skb);
104         saddrtype = ipv6_addr_type(&iph->saddr);
105         if (unlikely(saddrtype == IPV6_ADDR_ANY))
106                 return true ^ invert; /* not routable: forward path will drop it */
107
108         return rpfilter_lookup_reverse6(xt_net(par), skb, xt_in(par),
109                                         info->flags) ^ invert;
110 }
111
112 static int rpfilter_check(const struct xt_mtchk_param *par)
113 {
114         const struct xt_rpfilter_info *info = par->matchinfo;
115         unsigned int options = ~XT_RPFILTER_OPTION_MASK;
116
117         if (info->flags & options) {
118                 pr_info_ratelimited("unknown options\n");
119                 return -EINVAL;
120         }
121
122         if (strcmp(par->table, "mangle") != 0 &&
123             strcmp(par->table, "raw") != 0) {
124                 pr_info_ratelimited("only valid in \'raw\' or \'mangle\' table, not \'%s\'\n",
125                                     par->table);
126                 return -EINVAL;
127         }
128
129         return 0;
130 }
131
132 static struct xt_match rpfilter_mt_reg __read_mostly = {
133         .name           = "rpfilter",
134         .family         = NFPROTO_IPV6,
135         .checkentry     = rpfilter_check,
136         .match          = rpfilter_mt,
137         .matchsize      = sizeof(struct xt_rpfilter_info),
138         .hooks          = (1 << NF_INET_PRE_ROUTING),
139         .me             = THIS_MODULE
140 };
141
142 static int __init rpfilter_mt_init(void)
143 {
144         return xt_register_match(&rpfilter_mt_reg);
145 }
146
147 static void __exit rpfilter_mt_exit(void)
148 {
149         xt_unregister_match(&rpfilter_mt_reg);
150 }
151
152 module_init(rpfilter_mt_init);
153 module_exit(rpfilter_mt_exit);