Merge branch 'smack-for-4.17' of git://github.com/cschaufler/next-smack into next...
[sfrench/cifs-2.6.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         int route_err, addrtype;
64         struct rt6_info *rt;
65         struct flowi6 fl6 = {
66                 .flowi6_iif = LOOPBACK_IFINDEX,
67                 .flowi6_proto = pkt->tprot,
68         };
69         u32 ret = 0;
70
71         v6ops = nf_get_ipv6_ops();
72         if (!v6ops)
73                 return RTN_UNREACHABLE;
74
75         if (priv->flags & NFTA_FIB_F_IIF)
76                 dev = nft_in(pkt);
77         else if (priv->flags & NFTA_FIB_F_OIF)
78                 dev = nft_out(pkt);
79
80         nft_fib6_flowi_init(&fl6, priv, pkt, dev, iph);
81
82         if (dev && v6ops->chk_addr(nft_net(pkt), &fl6.daddr, dev, true))
83                 ret = RTN_LOCAL;
84
85         route_err = v6ops->route(nft_net(pkt), (struct dst_entry **)&rt,
86                                  flowi6_to_flowi(&fl6), false);
87         if (route_err)
88                 goto err;
89
90         if (rt->rt6i_flags & RTF_REJECT) {
91                 route_err = rt->dst.error;
92                 dst_release(&rt->dst);
93                 goto err;
94         }
95
96         if (ipv6_anycast_destination((struct dst_entry *)rt, &fl6.daddr))
97                 ret = RTN_ANYCAST;
98         else if (!dev && rt->rt6i_flags & RTF_LOCAL)
99                 ret = RTN_LOCAL;
100
101         dst_release(&rt->dst);
102
103         if (ret)
104                 return ret;
105
106         addrtype = ipv6_addr_type(&fl6.daddr);
107
108         if (addrtype & IPV6_ADDR_MULTICAST)
109                 return RTN_MULTICAST;
110         if (addrtype & IPV6_ADDR_UNICAST)
111                 return RTN_UNICAST;
112
113         return RTN_UNSPEC;
114  err:
115         switch (route_err) {
116         case -EINVAL:
117                 return RTN_BLACKHOLE;
118         case -EACCES:
119                 return RTN_PROHIBIT;
120         case -EAGAIN:
121                 return RTN_THROW;
122         default:
123                 break;
124         }
125
126         return RTN_UNREACHABLE;
127 }
128
129 void nft_fib6_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
130                         const struct nft_pktinfo *pkt)
131 {
132         const struct nft_fib *priv = nft_expr_priv(expr);
133         int noff = skb_network_offset(pkt->skb);
134         u32 *dest = &regs->data[priv->dreg];
135         struct ipv6hdr *iph, _iph;
136
137         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
138         if (!iph) {
139                 regs->verdict.code = NFT_BREAK;
140                 return;
141         }
142
143         *dest = __nft_fib6_eval_type(priv, pkt, iph);
144 }
145 EXPORT_SYMBOL_GPL(nft_fib6_eval_type);
146
147 void nft_fib6_eval(const struct nft_expr *expr, struct nft_regs *regs,
148                    const struct nft_pktinfo *pkt)
149 {
150         const struct nft_fib *priv = nft_expr_priv(expr);
151         int noff = skb_network_offset(pkt->skb);
152         const struct net_device *oif = NULL;
153         u32 *dest = &regs->data[priv->dreg];
154         struct ipv6hdr *iph, _iph;
155         struct flowi6 fl6 = {
156                 .flowi6_iif = LOOPBACK_IFINDEX,
157                 .flowi6_proto = pkt->tprot,
158         };
159         struct rt6_info *rt;
160         int lookup_flags;
161
162         if (priv->flags & NFTA_FIB_F_IIF)
163                 oif = nft_in(pkt);
164         else if (priv->flags & NFTA_FIB_F_OIF)
165                 oif = nft_out(pkt);
166
167         iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
168         if (!iph) {
169                 regs->verdict.code = NFT_BREAK;
170                 return;
171         }
172
173         lookup_flags = nft_fib6_flowi_init(&fl6, priv, pkt, oif, iph);
174
175         if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
176             nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
177                 nft_fib_store_result(dest, priv, pkt,
178                                      nft_in(pkt)->ifindex);
179                 return;
180         }
181
182         *dest = 0;
183         rt = (void *)ip6_route_lookup(nft_net(pkt), &fl6, lookup_flags);
184         if (rt->dst.error)
185                 goto put_rt_err;
186
187         /* Should not see RTF_LOCAL here */
188         if (rt->rt6i_flags & (RTF_REJECT | RTF_ANYCAST | RTF_LOCAL))
189                 goto put_rt_err;
190
191         if (oif && oif != rt->rt6i_idev->dev)
192                 goto put_rt_err;
193
194         switch (priv->result) {
195         case NFT_FIB_RESULT_OIF:
196                 *dest = rt->rt6i_idev->dev->ifindex;
197                 break;
198         case NFT_FIB_RESULT_OIFNAME:
199                 strncpy((char *)dest, rt->rt6i_idev->dev->name, IFNAMSIZ);
200                 break;
201         default:
202                 WARN_ON_ONCE(1);
203                 break;
204         }
205
206  put_rt_err:
207         ip6_rt_put(rt);
208 }
209 EXPORT_SYMBOL_GPL(nft_fib6_eval);
210
211 static struct nft_expr_type nft_fib6_type;
212
213 static const struct nft_expr_ops nft_fib6_type_ops = {
214         .type           = &nft_fib6_type,
215         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
216         .eval           = nft_fib6_eval_type,
217         .init           = nft_fib_init,
218         .dump           = nft_fib_dump,
219         .validate       = nft_fib_validate,
220 };
221
222 static const struct nft_expr_ops nft_fib6_ops = {
223         .type           = &nft_fib6_type,
224         .size           = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
225         .eval           = nft_fib6_eval,
226         .init           = nft_fib_init,
227         .dump           = nft_fib_dump,
228         .validate       = nft_fib_validate,
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");