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