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