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