Merge branches 'acpi-scan', 'acpi-resource', 'acpi-apei', 'acpi-extlog' and 'acpi...
[sfrench/cifs-2.6.git] / net / ipv6 / netfilter / nft_fib_ipv6.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/kernel.h>
4 #include <linux/init.h>
5 #include <linux/module.h>
6 #include <linux/netlink.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nf_tables.h>
9 #include <linux/netfilter_ipv6.h>
10 #include <net/netfilter/nf_tables_core.h>
11 #include <net/netfilter/nf_tables.h>
12 #include <net/netfilter/nft_fib.h>
13
14 #include <net/ip6_fib.h>
15 #include <net/ip6_route.h>
16
17 static int get_ifindex(const struct net_device *dev)
18 {
19         return dev ? dev->ifindex : 0;
20 }
21
22 static int nft_fib6_flowi_init(struct flowi6 *fl6, const struct nft_fib *priv,
23                                const struct nft_pktinfo *pkt,
24                                const struct net_device *dev,
25                                struct ipv6hdr *iph)
26 {
27         int lookup_flags = 0;
28
29         if (priv->flags & NFTA_FIB_F_DADDR) {
30                 fl6->daddr = iph->daddr;
31                 fl6->saddr = iph->saddr;
32         } else {
33                 if (nft_hook(pkt) == NF_INET_FORWARD &&
34                     priv->flags & NFTA_FIB_F_IIF)
35                         fl6->flowi6_iif = nft_out(pkt)->ifindex;
36
37                 fl6->daddr = iph->saddr;
38                 fl6->saddr = iph->daddr;
39         }
40
41         if (ipv6_addr_type(&fl6->daddr) & IPV6_ADDR_LINKLOCAL) {
42                 lookup_flags |= RT6_LOOKUP_F_IFACE;
43                 fl6->flowi6_oif = get_ifindex(dev ? dev : pkt->skb->dev);
44         } else if (priv->flags & NFTA_FIB_F_IIF) {
45                 fl6->flowi6_l3mdev = l3mdev_master_ifindex_rcu(dev);
46         }
47
48         if (ipv6_addr_type(&fl6->saddr) & IPV6_ADDR_UNICAST)
49                 lookup_flags |= RT6_LOOKUP_F_HAS_SADDR;
50
51         if (priv->flags & NFTA_FIB_F_MARK)
52                 fl6->flowi6_mark = pkt->skb->mark;
53
54         fl6->flowlabel = (*(__be32 *)iph) & IPV6_FLOWINFO_MASK;
55
56         return lookup_flags;
57 }
58
59 static u32 __nft_fib6_eval_type(const struct nft_fib *priv,
60                                 const struct nft_pktinfo *pkt,
61                                 struct ipv6hdr *iph)
62 {
63         const struct net_device *dev = NULL;
64         int route_err, addrtype;
65         struct rt6_info *rt;
66         struct flowi6 fl6 = {
67                 .flowi6_iif = LOOPBACK_IFINDEX,
68                 .flowi6_proto = pkt->tprot,
69         };
70         u32 ret = 0;
71
72         if (priv->flags & NFTA_FIB_F_IIF)
73                 dev = nft_in(pkt);
74         else if (priv->flags & NFTA_FIB_F_OIF)
75                 dev = nft_out(pkt);
76
77         nft_fib6_flowi_init(&fl6, priv, pkt, dev, iph);
78
79         if (dev && nf_ipv6_chk_addr(nft_net(pkt), &fl6.daddr, dev, true))
80                 ret = RTN_LOCAL;
81
82         route_err = nf_ip6_route(nft_net(pkt), (struct dst_entry **)&rt,
83                                  flowi6_to_flowi(&fl6), false);
84         if (route_err)
85                 goto err;
86
87         if (rt->rt6i_flags & RTF_REJECT) {
88                 route_err = rt->dst.error;
89                 dst_release(&rt->dst);
90                 goto err;
91         }
92
93         if (ipv6_anycast_destination((struct dst_entry *)rt, &fl6.daddr))
94                 ret = RTN_ANYCAST;
95         else if (!dev && rt->rt6i_flags & RTF_LOCAL)
96                 ret = RTN_LOCAL;
97
98         dst_release(&rt->dst);
99
100         if (ret)
101                 return ret;
102
103         addrtype = ipv6_addr_type(&fl6.daddr);
104
105         if (addrtype & IPV6_ADDR_MULTICAST)
106                 return RTN_MULTICAST;
107         if (addrtype & IPV6_ADDR_UNICAST)
108                 return RTN_UNICAST;
109
110         return RTN_UNSPEC;
111  err:
112         switch (route_err) {
113         case -EINVAL:
114                 return RTN_BLACKHOLE;
115         case -EACCES:
116                 return RTN_PROHIBIT;
117         case -EAGAIN:
118                 return RTN_THROW;
119         default:
120                 break;
121         }
122
123         return RTN_UNREACHABLE;
124 }
125
126 void nft_fib6_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
127                         const struct nft_pktinfo *pkt)
128 {
129         const struct nft_fib *priv = nft_expr_priv(expr);
130         int noff = skb_network_offset(pkt->skb);
131         u32 *dest = &regs->data[priv->dreg];
132         struct ipv6hdr *iph, _iph;
133
134         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
135         if (!iph) {
136                 regs->verdict.code = NFT_BREAK;
137                 return;
138         }
139
140         *dest = __nft_fib6_eval_type(priv, pkt, iph);
141 }
142 EXPORT_SYMBOL_GPL(nft_fib6_eval_type);
143
144 static bool nft_fib_v6_skip_icmpv6(const struct sk_buff *skb, u8 next, const struct ipv6hdr *iph)
145 {
146         if (likely(next != IPPROTO_ICMPV6))
147                 return false;
148
149         if (ipv6_addr_type(&iph->saddr) != IPV6_ADDR_ANY)
150                 return false;
151
152         return ipv6_addr_type(&iph->daddr) & IPV6_ADDR_LINKLOCAL;
153 }
154
155 void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
156                    const struct nft_pktinfo *pkt)
157 {
158         const struct nft_fib *priv = nft_expr_priv(expr);
159         int noff = skb_network_offset(pkt->skb);
160         const struct net_device *oif = NULL;
161         u32 *dest = &regs->data[priv->dreg];
162         struct ipv6hdr *iph, _iph;
163         struct flowi6 fl6 = {
164                 .flowi6_iif = LOOPBACK_IFINDEX,
165                 .flowi6_proto = pkt->tprot,
166         };
167         struct rt6_info *rt;
168         int lookup_flags;
169
170         if (priv->flags & NFTA_FIB_F_IIF)
171                 oif = nft_in(pkt);
172         else if (priv->flags & NFTA_FIB_F_OIF)
173                 oif = nft_out(pkt);
174
175         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
176         if (!iph) {
177                 regs->verdict.code = NFT_BREAK;
178                 return;
179         }
180
181         lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif, iph);
182
183         if (nft_hook(pkt) == NF_INET_PRE_ROUTING ||
184             nft_hook(pkt) == NF_INET_INGRESS) {
185                 if (nft_fib_is_loopback(pkt->skb, nft_in(pkt)) ||
186                     nft_fib_v6_skip_icmpv6(pkt->skb, pkt->tprot, iph)) {
187                         nft_fib_store_result(dest, priv, nft_in(pkt));
188                         return;
189                 }
190         }
191
192         *dest = 0;
193         rt = (void *)ip6_route_lookup(nft_net(pkt), &fl6, pkt->skb,
194                                       lookup_flags);
195         if (rt->dst.error)
196                 goto put_rt_err;
197
198         /* Should not see RTF_LOCAL here */
199         if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
200                 goto put_rt_err;
201
202         if (oif && oif != rt->rt6i_idev->dev &&
203             l3mdev_master_ifindex_rcu(rt->rt6i_idev->dev) != oif->ifindex)
204                 goto put_rt_err;
205
206         nft_fib_store_result(dest, priv, rt->rt6i_idev->dev);
207  put_rt_err:
208         ip6_rt_put(rt);
209 }
210 EXPORT_SYMBOL_GPL(nft_fib6_eval);
211
212 static struct nft_expr_type nft_fib6_type;
213
214 static const struct nft_expr_ops nft_fib6_type_ops = {
215         .type           = &nft_fib6_type,
216         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
217         .eval           = nft_fib6_eval_type,
218         .init           = nft_fib_init,
219         .dump           = nft_fib_dump,
220         .validate       = nft_fib_validate,
221         .reduce         = nft_fib_reduce,
222 };
223
224 static const struct nft_expr_ops nft_fib6_ops = {
225         .type           = &nft_fib6_type,
226         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
227         .eval           = nft_fib6_eval,
228         .init           = nft_fib_init,
229         .dump           = nft_fib_dump,
230         .validate       = nft_fib_validate,
231         .reduce         = nft_fib_reduce,
232 };
233
234 static const struct nft_expr_ops *
235 nft_fib6_select_ops(const struct nft_ctx *ctx,
236                     const struct nlattr * const tb[])
237 {
238         enum nft_fib_result result;
239
240         if (!tb[NFTA_FIB_RESULT])
241                 return ERR_PTR(-EINVAL);
242
243         result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
244
245         switch (result) {
246         case NFT_FIB_RESULT_OIF:
247                 return &nft_fib6_ops;
248         case NFT_FIB_RESULT_OIFNAME:
249                 return &nft_fib6_ops;
250         case NFT_FIB_RESULT_ADDRTYPE:
251                 return &nft_fib6_type_ops;
252         default:
253                 return ERR_PTR(-EOPNOTSUPP);
254         }
255 }
256
257 static struct nft_expr_type nft_fib6_type __read_mostly = {
258         .name           = "fib",
259         .select_ops     = nft_fib6_select_ops,
260         .policy         = nft_fib_policy,
261         .maxattr        = NFTA_FIB_MAX,
262         .family         = NFPROTO_IPV6,
263         .owner          = THIS_MODULE,
264 };
265
266 static int __init nft_fib6_module_init(void)
267 {
268         return nft_register_expr(&nft_fib6_type);
269 }
270
271 static void __exit nft_fib6_module_exit(void)
272 {
273         nft_unregister_expr(&nft_fib6_type);
274 }
275 module_init(nft_fib6_module_init);
276 module_exit(nft_fib6_module_exit);
277
278 MODULE_LICENSE("GPL");
279 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
280 MODULE_ALIAS_NFT_AF_EXPR(10, "fib");
281 MODULE_DESCRIPTION("nftables fib / ipv6 route lookup support");