]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/netfilter/ip6t_rpfilter.c
3119e720a6c884caef3a09bafdeded03d888d357
[mirror_ubuntu-bionic-kernel.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_lookup_reverse6(struct net *net, const struct sk_buff *skb,
30 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 = {
36 .flowi6_iif = LOOPBACK_IFINDEX,
37 .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
52 rt = (void *) ip6_route_lookup(net, &fl6, lookup_flags);
53 if (rt->dst.error)
54 goto out;
55
56 if (rt->rt6i_flags & (RTF_REJECT|RTF_ANYCAST))
57 goto out;
58
59 if (rt->rt6i_flags & RTF_LOCAL) {
60 ret = flags & XT_RPFILTER_ACCEPT_LOCAL;
61 goto out;
62 }
63
64 if (rt->rt6i_idev->dev == dev || (flags & XT_RPFILTER_LOOSE))
65 ret = true;
66 out:
67 ip6_rt_put(rt);
68 return ret;
69 }
70
71 static bool
72 rpfilter_is_loopback(const struct sk_buff *skb, const struct net_device *in)
73 {
74 return skb->pkt_type == PACKET_LOOPBACK || in->flags & IFF_LOOPBACK;
75 }
76
77 static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par)
78 {
79 const struct xt_rpfilter_info *info = par->matchinfo;
80 int saddrtype;
81 struct ipv6hdr *iph;
82 bool invert = info->flags & XT_RPFILTER_INVERT;
83
84 if (rpfilter_is_loopback(skb, xt_in(par)))
85 return true ^ invert;
86
87 iph = ipv6_hdr(skb);
88 saddrtype = ipv6_addr_type(&iph->saddr);
89 if (unlikely(saddrtype == IPV6_ADDR_ANY))
90 return true ^ invert; /* not routable: forward path will drop it */
91
92 return rpfilter_lookup_reverse6(xt_net(par), skb, xt_in(par),
93 info->flags) ^ invert;
94 }
95
96 static int rpfilter_check(const struct xt_mtchk_param *par)
97 {
98 const struct xt_rpfilter_info *info = par->matchinfo;
99 unsigned int options = ~XT_RPFILTER_OPTION_MASK;
100
101 if (info->flags & options) {
102 pr_info("unknown options encountered");
103 return -EINVAL;
104 }
105
106 if (strcmp(par->table, "mangle") != 0 &&
107 strcmp(par->table, "raw") != 0) {
108 pr_info("match only valid in the \'raw\' "
109 "or \'mangle\' tables, not \'%s\'.\n", par->table);
110 return -EINVAL;
111 }
112
113 return 0;
114 }
115
116 static struct xt_match rpfilter_mt_reg __read_mostly = {
117 .name = "rpfilter",
118 .family = NFPROTO_IPV6,
119 .checkentry = rpfilter_check,
120 .match = rpfilter_mt,
121 .matchsize = sizeof(struct xt_rpfilter_info),
122 .hooks = (1 << NF_INET_PRE_ROUTING),
123 .me = THIS_MODULE
124 };
125
126 static int __init rpfilter_mt_init(void)
127 {
128 return xt_register_match(&rpfilter_mt_reg);
129 }
130
131 static void __exit rpfilter_mt_exit(void)
132 {
133 xt_unregister_match(&rpfilter_mt_reg);
134 }
135
136 module_init(rpfilter_mt_init);
137 module_exit(rpfilter_mt_exit);