GNU Linux-libre 4.14.266-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         }
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         const struct nf_ipv6_ops *v6ops;
63         const struct nf_afinfo *afinfo;
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         afinfo = nf_get_afinfo(NFPROTO_IPV6);
73         if (!afinfo)
74                 return RTN_UNREACHABLE;
75
76         if (priv->flags & NFTA_FIB_F_IIF)
77                 dev = nft_in(pkt);
78         else if (priv->flags & NFTA_FIB_F_OIF)
79                 dev = nft_out(pkt);
80
81         nft_fib6_flowi_init(&fl6, priv, pkt, dev, iph);
82
83         v6ops = nf_get_ipv6_ops();
84         if (dev && v6ops && v6ops->chk_addr(nft_net(pkt), &fl6.daddr, dev, true))
85                 ret = RTN_LOCAL;
86
87         route_err = afinfo->route(nft_net(pkt), (struct dst_entry **)&rt,
88                                   flowi6_to_flowi(&fl6), false);
89         if (route_err)
90                 goto err;
91
92         if (rt->rt6i_flags & RTF_REJECT) {
93                 route_err = rt->dst.error;
94                 dst_release(&rt->dst);
95                 goto err;
96         }
97
98         if (ipv6_anycast_destination((struct dst_entry *)rt, &fl6.daddr))
99                 ret = RTN_ANYCAST;
100         else if (!dev && rt->rt6i_flags & RTF_LOCAL)
101                 ret = RTN_LOCAL;
102
103         dst_release(&rt->dst);
104
105         if (ret)
106                 return ret;
107
108         addrtype = ipv6_addr_type(&fl6.daddr);
109
110         if (addrtype & IPV6_ADDR_MULTICAST)
111                 return RTN_MULTICAST;
112         if (addrtype & IPV6_ADDR_UNICAST)
113                 return RTN_UNICAST;
114
115         return RTN_UNSPEC;
116  err:
117         switch (route_err) {
118         case -EINVAL:
119                 return RTN_BLACKHOLE;
120         case -EACCES:
121                 return RTN_PROHIBIT;
122         case -EAGAIN:
123                 return RTN_THROW;
124         default:
125                 break;
126         }
127
128         return RTN_UNREACHABLE;
129 }
130
131 void nft_fib6_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
132                         const struct nft_pktinfo *pkt)
133 {
134         const struct nft_fib *priv = nft_expr_priv(expr);
135         int noff = skb_network_offset(pkt->skb);
136         u32 *dest = &regs->data[priv->dreg];
137         struct ipv6hdr *iph, _iph;
138
139         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
140         if (!iph) {
141                 regs->verdict.code = NFT_BREAK;
142                 return;
143         }
144
145         *dest = __nft_fib6_eval_type(priv, pkt, iph);
146 }
147 EXPORT_SYMBOL_GPL(nft_fib6_eval_type);
148
149 void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
150                    const struct nft_pktinfo *pkt)
151 {
152         const struct nft_fib *priv = nft_expr_priv(expr);
153         int noff = skb_network_offset(pkt->skb);
154         const struct net_device *oif = NULL;
155         u32 *dest = &regs->data[priv->dreg];
156         struct ipv6hdr *iph, _iph;
157         struct flowi6 fl6 = {
158                 .flowi6_iif = LOOPBACK_IFINDEX,
159                 .flowi6_proto = pkt->tprot,
160         };
161         struct rt6_info *rt;
162         int lookup_flags;
163
164         if (priv->flags & NFTA_FIB_F_IIF)
165                 oif = nft_in(pkt);
166         else if (priv->flags & NFTA_FIB_F_OIF)
167                 oif = nft_out(pkt);
168
169         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
170         if (!iph) {
171                 regs->verdict.code = NFT_BREAK;
172                 return;
173         }
174
175         lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif, iph);
176
177         if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
178             nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
179                 nft_fib_store_result(dest, priv, pkt,
180                                      nft_in(pkt)->ifindex);
181                 return;
182         }
183
184         *dest = 0;
185         rt = (void *)ip6_route_lookup(nft_net(pkt), &fl6, lookup_flags);
186         if (rt->dst.error)
187                 goto put_rt_err;
188
189         /* Should not see RTF_LOCAL here */
190         if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
191                 goto put_rt_err;
192
193         if (oif && oif != rt->rt6i_idev->dev)
194                 goto put_rt_err;
195
196         switch (priv->result) {
197         case NFT_FIB_RESULT_OIF:
198                 *dest = rt->rt6i_idev->dev->ifindex;
199                 break;
200         case NFT_FIB_RESULT_OIFNAME:
201                 strncpy((char *)dest, rt->rt6i_idev->dev->name, IFNAMSIZ);
202                 break;
203         default:
204                 WARN_ON_ONCE(1);
205                 break;
206         }
207
208  put_rt_err:
209         ip6_rt_put(rt);
210 }
211 EXPORT_SYMBOL_GPL(nft_fib6_eval);
212
213 static struct nft_expr_type nft_fib6_type;
214
215 static const struct nft_expr_ops nft_fib6_type_ops = {
216         .type           = &nft_fib6_type,
217         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
218         .eval           = nft_fib6_eval_type,
219         .init           = nft_fib_init,
220         .dump           = nft_fib_dump,
221         .validate       = nft_fib_validate,
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 };
232
233 static const struct nft_expr_ops *
234 nft_fib6_select_ops(const struct nft_ctx *ctx,
235                     const struct nlattr * const tb[])
236 {
237         enum nft_fib_result result;
238
239         if (!tb[NFTA_FIB_RESULT])
240                 return ERR_PTR(-EINVAL);
241
242         result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
243
244         switch (result) {
245         case NFT_FIB_RESULT_OIF:
246                 return &nft_fib6_ops;
247         case NFT_FIB_RESULT_OIFNAME:
248                 return &nft_fib6_ops;
249         case NFT_FIB_RESULT_ADDRTYPE:
250                 return &nft_fib6_type_ops;
251         default:
252                 return ERR_PTR(-EOPNOTSUPP);
253         }
254 }
255
256 static struct nft_expr_type nft_fib6_type __read_mostly = {
257         .name           = "fib",
258         .select_ops     = nft_fib6_select_ops,
259         .policy         = nft_fib_policy,
260         .maxattr        = NFTA_FIB_MAX,
261         .family         = NFPROTO_IPV6,
262         .owner          = THIS_MODULE,
263 };
264
265 static int __init nft_fib6_module_init(void)
266 {
267         return nft_register_expr(&nft_fib6_type);
268 }
269
270 static void __exit nft_fib6_module_exit(void)
271 {
272         nft_unregister_expr(&nft_fib6_type);
273 }
274 module_init(nft_fib6_module_init);
275 module_exit(nft_fib6_module_exit);
276
277 MODULE_LICENSE("GPL");
278 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
279 MODULE_ALIAS_NFT_AF_EXPR(10, "fib");