GNU Linux-libre 4.4.288-gnu1
[releases.git] / net / ipv6 / fib6_rules.c
1 /*
2  * net/ipv6/fib6_rules.c        IPv6 Routing Policy Rules
3  *
4  * Copyright (C)2003-2006 Helsinki University of Technology
5  * Copyright (C)2003-2006 USAGI/WIDE Project
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License as
9  *      published by the Free Software Foundation, version 2.
10  *
11  * Authors
12  *      Thomas Graf             <tgraf@suug.ch>
13  *      Ville Nuorvala          <vnuorval@tcs.hut.fi>
14  */
15
16 #include <linux/netdevice.h>
17 #include <linux/export.h>
18
19 #include <net/fib_rules.h>
20 #include <net/ipv6.h>
21 #include <net/addrconf.h>
22 #include <net/ip6_route.h>
23 #include <net/netlink.h>
24
25 struct fib6_rule {
26         struct fib_rule         common;
27         struct rt6key           src;
28         struct rt6key           dst;
29         u8                      tclass;
30 };
31
32 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
33                                    int flags, pol_lookup_t lookup)
34 {
35         struct fib_lookup_arg arg = {
36                 .lookup_ptr = lookup,
37                 .flags = FIB_LOOKUP_NOREF,
38         };
39
40         fib_rules_lookup(net->ipv6.fib6_rules_ops,
41                          flowi6_to_flowi(fl6), flags, &arg);
42
43         if (arg.result)
44                 return arg.result;
45
46         dst_hold(&net->ipv6.ip6_null_entry->dst);
47         return &net->ipv6.ip6_null_entry->dst;
48 }
49
50 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
51                             int flags, struct fib_lookup_arg *arg)
52 {
53         struct flowi6 *flp6 = &flp->u.ip6;
54         struct rt6_info *rt = NULL;
55         struct fib6_table *table;
56         struct net *net = rule->fr_net;
57         pol_lookup_t lookup = arg->lookup_ptr;
58         int err = 0;
59
60         switch (rule->action) {
61         case FR_ACT_TO_TBL:
62                 break;
63         case FR_ACT_UNREACHABLE:
64                 err = -ENETUNREACH;
65                 rt = net->ipv6.ip6_null_entry;
66                 goto discard_pkt;
67         default:
68         case FR_ACT_BLACKHOLE:
69                 err = -EINVAL;
70                 rt = net->ipv6.ip6_blk_hole_entry;
71                 goto discard_pkt;
72         case FR_ACT_PROHIBIT:
73                 err = -EACCES;
74                 rt = net->ipv6.ip6_prohibit_entry;
75                 goto discard_pkt;
76         }
77
78         table = fib6_get_table(net, rule->table);
79         if (!table) {
80                 err = -EAGAIN;
81                 goto out;
82         }
83
84         rt = lookup(net, table, flp6, flags);
85         if (rt != net->ipv6.ip6_null_entry) {
86                 struct fib6_rule *r = (struct fib6_rule *)rule;
87
88                 /*
89                  * If we need to find a source address for this traffic,
90                  * we check the result if it meets requirement of the rule.
91                  */
92                 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
93                     r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
94                         struct in6_addr saddr;
95
96                         if (ipv6_dev_get_saddr(net,
97                                                ip6_dst_idev(&rt->dst)->dev,
98                                                &flp6->daddr,
99                                                rt6_flags2srcprefs(flags),
100                                                &saddr))
101                                 goto again;
102                         if (!ipv6_prefix_equal(&saddr, &r->src.addr,
103                                                r->src.plen))
104                                 goto again;
105                         flp6->saddr = saddr;
106                 }
107                 err = rt->dst.error;
108                 if (err != -EAGAIN)
109                         goto out;
110         }
111 again:
112         ip6_rt_put(rt);
113         err = -EAGAIN;
114         rt = NULL;
115         goto out;
116
117 discard_pkt:
118         dst_hold(&rt->dst);
119 out:
120         arg->result = rt;
121         return err;
122 }
123
124 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
125 {
126         struct rt6_info *rt = (struct rt6_info *) arg->result;
127         struct net_device *dev = NULL;
128
129         if (rt->rt6i_idev)
130                 dev = rt->rt6i_idev->dev;
131
132         /* do not accept result if the route does
133          * not meet the required prefix length
134          */
135         if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
136                 goto suppress_route;
137
138         /* do not accept result if the route uses a device
139          * belonging to a forbidden interface group
140          */
141         if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
142                 goto suppress_route;
143
144         return false;
145
146 suppress_route:
147         ip6_rt_put(rt);
148         return true;
149 }
150
151 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
152 {
153         struct fib6_rule *r = (struct fib6_rule *) rule;
154         struct flowi6 *fl6 = &fl->u.ip6;
155
156         if (r->dst.plen &&
157             !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
158                 return 0;
159
160         /*
161          * If FIB_RULE_FIND_SADDR is set and we do not have a
162          * source address for the traffic, we defer check for
163          * source address.
164          */
165         if (r->src.plen) {
166                 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
167                         if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
168                                                r->src.plen))
169                                 return 0;
170                 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
171                         return 0;
172         }
173
174         if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
175                 return 0;
176
177         return 1;
178 }
179
180 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
181         FRA_GENERIC_POLICY,
182 };
183
184 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
185                                struct fib_rule_hdr *frh,
186                                struct nlattr **tb)
187 {
188         int err = -EINVAL;
189         struct net *net = sock_net(skb->sk);
190         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
191
192         if (rule->action == FR_ACT_TO_TBL) {
193                 if (rule->table == RT6_TABLE_UNSPEC)
194                         goto errout;
195
196                 if (fib6_new_table(net, rule->table) == NULL) {
197                         err = -ENOBUFS;
198                         goto errout;
199                 }
200         }
201
202         if (frh->src_len)
203                 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
204
205         if (frh->dst_len)
206                 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
207
208         rule6->src.plen = frh->src_len;
209         rule6->dst.plen = frh->dst_len;
210         rule6->tclass = frh->tos;
211
212         err = 0;
213 errout:
214         return err;
215 }
216
217 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
218                              struct nlattr **tb)
219 {
220         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
221
222         if (frh->src_len && (rule6->src.plen != frh->src_len))
223                 return 0;
224
225         if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
226                 return 0;
227
228         if (frh->tos && (rule6->tclass != frh->tos))
229                 return 0;
230
231         if (frh->src_len &&
232             nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
233                 return 0;
234
235         if (frh->dst_len &&
236             nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
237                 return 0;
238
239         return 1;
240 }
241
242 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
243                           struct fib_rule_hdr *frh)
244 {
245         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
246
247         frh->dst_len = rule6->dst.plen;
248         frh->src_len = rule6->src.plen;
249         frh->tos = rule6->tclass;
250
251         if ((rule6->dst.plen &&
252              nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
253             (rule6->src.plen &&
254              nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
255                 goto nla_put_failure;
256         return 0;
257
258 nla_put_failure:
259         return -ENOBUFS;
260 }
261
262 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
263 {
264         return nla_total_size(16) /* dst */
265                + nla_total_size(16); /* src */
266 }
267
268 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
269         .family                 = AF_INET6,
270         .rule_size              = sizeof(struct fib6_rule),
271         .addr_size              = sizeof(struct in6_addr),
272         .action                 = fib6_rule_action,
273         .match                  = fib6_rule_match,
274         .suppress               = fib6_rule_suppress,
275         .configure              = fib6_rule_configure,
276         .compare                = fib6_rule_compare,
277         .fill                   = fib6_rule_fill,
278         .nlmsg_payload          = fib6_rule_nlmsg_payload,
279         .nlgroup                = RTNLGRP_IPV6_RULE,
280         .policy                 = fib6_rule_policy,
281         .owner                  = THIS_MODULE,
282         .fro_net                = &init_net,
283 };
284
285 static int __net_init fib6_rules_net_init(struct net *net)
286 {
287         struct fib_rules_ops *ops;
288         int err = -ENOMEM;
289
290         ops = fib_rules_register(&fib6_rules_ops_template, net);
291         if (IS_ERR(ops))
292                 return PTR_ERR(ops);
293
294         err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
295         if (err)
296                 goto out_fib6_rules_ops;
297
298         err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
299         if (err)
300                 goto out_fib6_rules_ops;
301
302         net->ipv6.fib6_rules_ops = ops;
303 out:
304         return err;
305
306 out_fib6_rules_ops:
307         fib_rules_unregister(ops);
308         goto out;
309 }
310
311 static void __net_exit fib6_rules_net_exit(struct net *net)
312 {
313         rtnl_lock();
314         fib_rules_unregister(net->ipv6.fib6_rules_ops);
315         rtnl_unlock();
316 }
317
318 static struct pernet_operations fib6_rules_net_ops = {
319         .init = fib6_rules_net_init,
320         .exit = fib6_rules_net_exit,
321 };
322
323 int __init fib6_rules_init(void)
324 {
325         return register_pernet_subsys(&fib6_rules_net_ops);
326 }
327
328
329 void fib6_rules_cleanup(void)
330 {
331         unregister_pernet_subsys(&fib6_rules_net_ops);
332 }