]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/fib6_rules.c
x86/speculation/mds: Conditionally clear CPU buffers on idle entry
[mirror_ubuntu-bionic-kernel.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/notifier.h>
18 #include <linux/export.h>
19
20 #include <net/fib_rules.h>
21 #include <net/ipv6.h>
22 #include <net/addrconf.h>
23 #include <net/ip6_route.h>
24 #include <net/netlink.h>
25
26 struct fib6_rule {
27 struct fib_rule common;
28 struct rt6key src;
29 struct rt6key dst;
30 u8 tclass;
31 };
32
33 static bool fib6_rule_matchall(const struct fib_rule *rule)
34 {
35 struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
36
37 if (r->dst.plen || r->src.plen || r->tclass)
38 return false;
39 return fib_rule_matchall(rule);
40 }
41
42 bool fib6_rule_default(const struct fib_rule *rule)
43 {
44 if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
45 rule->l3mdev)
46 return false;
47 if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
48 return false;
49 return true;
50 }
51 EXPORT_SYMBOL_GPL(fib6_rule_default);
52
53 int fib6_rules_dump(struct net *net, struct notifier_block *nb)
54 {
55 return fib_rules_dump(net, nb, AF_INET6);
56 }
57
58 unsigned int fib6_rules_seq_read(struct net *net)
59 {
60 return fib_rules_seq_read(net, AF_INET6);
61 }
62
63 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
64 int flags, pol_lookup_t lookup)
65 {
66 if (net->ipv6.fib6_has_custom_rules) {
67 struct fib_lookup_arg arg = {
68 .lookup_ptr = lookup,
69 .flags = FIB_LOOKUP_NOREF,
70 };
71
72 /* update flow if oif or iif point to device enslaved to l3mdev */
73 l3mdev_update_flow(net, flowi6_to_flowi(fl6));
74
75 fib_rules_lookup(net->ipv6.fib6_rules_ops,
76 flowi6_to_flowi(fl6), flags, &arg);
77
78 if (arg.result)
79 return arg.result;
80 } else {
81 struct rt6_info *rt;
82
83 rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, flags);
84 if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
85 return &rt->dst;
86 ip6_rt_put(rt);
87 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
88 if (rt->dst.error != -EAGAIN)
89 return &rt->dst;
90 ip6_rt_put(rt);
91 }
92
93 dst_hold(&net->ipv6.ip6_null_entry->dst);
94 return &net->ipv6.ip6_null_entry->dst;
95 }
96
97 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
98 int flags, struct fib_lookup_arg *arg)
99 {
100 struct flowi6 *flp6 = &flp->u.ip6;
101 struct rt6_info *rt = NULL;
102 struct fib6_table *table;
103 struct net *net = rule->fr_net;
104 pol_lookup_t lookup = arg->lookup_ptr;
105 int err = 0;
106 u32 tb_id;
107
108 switch (rule->action) {
109 case FR_ACT_TO_TBL:
110 break;
111 case FR_ACT_UNREACHABLE:
112 err = -ENETUNREACH;
113 rt = net->ipv6.ip6_null_entry;
114 goto discard_pkt;
115 default:
116 case FR_ACT_BLACKHOLE:
117 err = -EINVAL;
118 rt = net->ipv6.ip6_blk_hole_entry;
119 goto discard_pkt;
120 case FR_ACT_PROHIBIT:
121 err = -EACCES;
122 rt = net->ipv6.ip6_prohibit_entry;
123 goto discard_pkt;
124 }
125
126 tb_id = fib_rule_get_table(rule, arg);
127 table = fib6_get_table(net, tb_id);
128 if (!table) {
129 err = -EAGAIN;
130 goto out;
131 }
132
133 rt = lookup(net, table, flp6, flags);
134 if (rt != net->ipv6.ip6_null_entry) {
135 struct fib6_rule *r = (struct fib6_rule *)rule;
136
137 /*
138 * If we need to find a source address for this traffic,
139 * we check the result if it meets requirement of the rule.
140 */
141 if ((rule->flags & FIB_RULE_FIND_SADDR) &&
142 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
143 struct in6_addr saddr;
144
145 if (ipv6_dev_get_saddr(net,
146 ip6_dst_idev(&rt->dst)->dev,
147 &flp6->daddr,
148 rt6_flags2srcprefs(flags),
149 &saddr))
150 goto again;
151 if (!ipv6_prefix_equal(&saddr, &r->src.addr,
152 r->src.plen))
153 goto again;
154 flp6->saddr = saddr;
155 }
156 err = rt->dst.error;
157 if (err != -EAGAIN)
158 goto out;
159 }
160 again:
161 ip6_rt_put(rt);
162 err = -EAGAIN;
163 rt = NULL;
164 goto out;
165
166 discard_pkt:
167 dst_hold(&rt->dst);
168 out:
169 arg->result = rt;
170 return err;
171 }
172
173 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
174 {
175 struct rt6_info *rt = (struct rt6_info *) arg->result;
176 struct net_device *dev = NULL;
177
178 if (rt->rt6i_idev)
179 dev = rt->rt6i_idev->dev;
180
181 /* do not accept result if the route does
182 * not meet the required prefix length
183 */
184 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
185 goto suppress_route;
186
187 /* do not accept result if the route uses a device
188 * belonging to a forbidden interface group
189 */
190 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
191 goto suppress_route;
192
193 return false;
194
195 suppress_route:
196 ip6_rt_put(rt);
197 return true;
198 }
199
200 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
201 {
202 struct fib6_rule *r = (struct fib6_rule *) rule;
203 struct flowi6 *fl6 = &fl->u.ip6;
204
205 if (r->dst.plen &&
206 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
207 return 0;
208
209 /*
210 * If FIB_RULE_FIND_SADDR is set and we do not have a
211 * source address for the traffic, we defer check for
212 * source address.
213 */
214 if (r->src.plen) {
215 if (flags & RT6_LOOKUP_F_HAS_SADDR) {
216 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
217 r->src.plen))
218 return 0;
219 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
220 return 0;
221 }
222
223 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
224 return 0;
225
226 return 1;
227 }
228
229 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
230 FRA_GENERIC_POLICY,
231 };
232
233 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
234 struct fib_rule_hdr *frh,
235 struct nlattr **tb)
236 {
237 int err = -EINVAL;
238 struct net *net = sock_net(skb->sk);
239 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
240
241 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
242 if (rule->table == RT6_TABLE_UNSPEC)
243 goto errout;
244
245 if (fib6_new_table(net, rule->table) == NULL) {
246 err = -ENOBUFS;
247 goto errout;
248 }
249 }
250
251 if (frh->src_len)
252 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
253
254 if (frh->dst_len)
255 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
256
257 rule6->src.plen = frh->src_len;
258 rule6->dst.plen = frh->dst_len;
259 rule6->tclass = frh->tos;
260
261 net->ipv6.fib6_has_custom_rules = true;
262 err = 0;
263 errout:
264 return err;
265 }
266
267 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
268 struct nlattr **tb)
269 {
270 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
271
272 if (frh->src_len && (rule6->src.plen != frh->src_len))
273 return 0;
274
275 if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
276 return 0;
277
278 if (frh->tos && (rule6->tclass != frh->tos))
279 return 0;
280
281 if (frh->src_len &&
282 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
283 return 0;
284
285 if (frh->dst_len &&
286 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
287 return 0;
288
289 return 1;
290 }
291
292 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
293 struct fib_rule_hdr *frh)
294 {
295 struct fib6_rule *rule6 = (struct fib6_rule *) rule;
296
297 frh->dst_len = rule6->dst.plen;
298 frh->src_len = rule6->src.plen;
299 frh->tos = rule6->tclass;
300
301 if ((rule6->dst.plen &&
302 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
303 (rule6->src.plen &&
304 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
305 goto nla_put_failure;
306 return 0;
307
308 nla_put_failure:
309 return -ENOBUFS;
310 }
311
312 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
313 {
314 return nla_total_size(16) /* dst */
315 + nla_total_size(16); /* src */
316 }
317
318 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
319 .family = AF_INET6,
320 .rule_size = sizeof(struct fib6_rule),
321 .addr_size = sizeof(struct in6_addr),
322 .action = fib6_rule_action,
323 .match = fib6_rule_match,
324 .suppress = fib6_rule_suppress,
325 .configure = fib6_rule_configure,
326 .compare = fib6_rule_compare,
327 .fill = fib6_rule_fill,
328 .nlmsg_payload = fib6_rule_nlmsg_payload,
329 .nlgroup = RTNLGRP_IPV6_RULE,
330 .policy = fib6_rule_policy,
331 .owner = THIS_MODULE,
332 .fro_net = &init_net,
333 };
334
335 static int __net_init fib6_rules_net_init(struct net *net)
336 {
337 struct fib_rules_ops *ops;
338 int err = -ENOMEM;
339
340 ops = fib_rules_register(&fib6_rules_ops_template, net);
341 if (IS_ERR(ops))
342 return PTR_ERR(ops);
343
344 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
345 if (err)
346 goto out_fib6_rules_ops;
347
348 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
349 if (err)
350 goto out_fib6_rules_ops;
351
352 net->ipv6.fib6_rules_ops = ops;
353 out:
354 return err;
355
356 out_fib6_rules_ops:
357 fib_rules_unregister(ops);
358 goto out;
359 }
360
361 static void __net_exit fib6_rules_net_exit(struct net *net)
362 {
363 rtnl_lock();
364 fib_rules_unregister(net->ipv6.fib6_rules_ops);
365 rtnl_unlock();
366 }
367
368 static struct pernet_operations fib6_rules_net_ops = {
369 .init = fib6_rules_net_init,
370 .exit = fib6_rules_net_exit,
371 };
372
373 int __init fib6_rules_init(void)
374 {
375 return register_pernet_subsys(&fib6_rules_net_ops);
376 }
377
378
379 void fib6_rules_cleanup(void)
380 {
381 unregister_pernet_subsys(&fib6_rules_net_ops);
382 }