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