]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/core/fib_rules.c
[NET]: Make rtnetlink infrastructure network namespace aware (v3)
[mirror_ubuntu-artful-kernel.git] / net / core / fib_rules.c
1 /*
2 * net/core/fib_rules.c Generic Routing Rules
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2.
7 *
8 * Authors: Thomas Graf <tgraf@suug.ch>
9 */
10
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <net/net_namespace.h>
15 #include <net/sock.h>
16 #include <net/fib_rules.h>
17
18 static LIST_HEAD(rules_ops);
19 static DEFINE_SPINLOCK(rules_mod_lock);
20
21 int fib_default_rule_add(struct fib_rules_ops *ops,
22 u32 pref, u32 table, u32 flags)
23 {
24 struct fib_rule *r;
25
26 r = kzalloc(ops->rule_size, GFP_KERNEL);
27 if (r == NULL)
28 return -ENOMEM;
29
30 atomic_set(&r->refcnt, 1);
31 r->action = FR_ACT_TO_TBL;
32 r->pref = pref;
33 r->table = table;
34 r->flags = flags;
35
36 /* The lock is not required here, the list in unreacheable
37 * at the moment this function is called */
38 list_add_tail(&r->list, &ops->rules_list);
39 return 0;
40 }
41 EXPORT_SYMBOL(fib_default_rule_add);
42
43 static void notify_rule_change(int event, struct fib_rule *rule,
44 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
45 u32 pid);
46
47 static struct fib_rules_ops *lookup_rules_ops(int family)
48 {
49 struct fib_rules_ops *ops;
50
51 rcu_read_lock();
52 list_for_each_entry_rcu(ops, &rules_ops, list) {
53 if (ops->family == family) {
54 if (!try_module_get(ops->owner))
55 ops = NULL;
56 rcu_read_unlock();
57 return ops;
58 }
59 }
60 rcu_read_unlock();
61
62 return NULL;
63 }
64
65 static void rules_ops_put(struct fib_rules_ops *ops)
66 {
67 if (ops)
68 module_put(ops->owner);
69 }
70
71 static void flush_route_cache(struct fib_rules_ops *ops)
72 {
73 if (ops->flush_cache)
74 ops->flush_cache();
75 }
76
77 int fib_rules_register(struct fib_rules_ops *ops)
78 {
79 int err = -EEXIST;
80 struct fib_rules_ops *o;
81
82 if (ops->rule_size < sizeof(struct fib_rule))
83 return -EINVAL;
84
85 if (ops->match == NULL || ops->configure == NULL ||
86 ops->compare == NULL || ops->fill == NULL ||
87 ops->action == NULL)
88 return -EINVAL;
89
90 spin_lock(&rules_mod_lock);
91 list_for_each_entry(o, &rules_ops, list)
92 if (ops->family == o->family)
93 goto errout;
94
95 list_add_tail_rcu(&ops->list, &rules_ops);
96 err = 0;
97 errout:
98 spin_unlock(&rules_mod_lock);
99
100 return err;
101 }
102
103 EXPORT_SYMBOL_GPL(fib_rules_register);
104
105 static void cleanup_ops(struct fib_rules_ops *ops)
106 {
107 struct fib_rule *rule, *tmp;
108
109 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
110 list_del_rcu(&rule->list);
111 fib_rule_put(rule);
112 }
113 }
114
115 int fib_rules_unregister(struct fib_rules_ops *ops)
116 {
117 int err = 0;
118 struct fib_rules_ops *o;
119
120 spin_lock(&rules_mod_lock);
121 list_for_each_entry(o, &rules_ops, list) {
122 if (o == ops) {
123 list_del_rcu(&o->list);
124 cleanup_ops(ops);
125 goto out;
126 }
127 }
128
129 err = -ENOENT;
130 out:
131 spin_unlock(&rules_mod_lock);
132
133 synchronize_rcu();
134
135 return err;
136 }
137
138 EXPORT_SYMBOL_GPL(fib_rules_unregister);
139
140 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
141 struct flowi *fl, int flags)
142 {
143 int ret = 0;
144
145 if (rule->ifindex && (rule->ifindex != fl->iif))
146 goto out;
147
148 if ((rule->mark ^ fl->mark) & rule->mark_mask)
149 goto out;
150
151 ret = ops->match(rule, fl, flags);
152 out:
153 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
154 }
155
156 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
157 int flags, struct fib_lookup_arg *arg)
158 {
159 struct fib_rule *rule;
160 int err;
161
162 rcu_read_lock();
163
164 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
165 jumped:
166 if (!fib_rule_match(rule, ops, fl, flags))
167 continue;
168
169 if (rule->action == FR_ACT_GOTO) {
170 struct fib_rule *target;
171
172 target = rcu_dereference(rule->ctarget);
173 if (target == NULL) {
174 continue;
175 } else {
176 rule = target;
177 goto jumped;
178 }
179 } else if (rule->action == FR_ACT_NOP)
180 continue;
181 else
182 err = ops->action(rule, fl, flags, arg);
183
184 if (err != -EAGAIN) {
185 fib_rule_get(rule);
186 arg->rule = rule;
187 goto out;
188 }
189 }
190
191 err = -ESRCH;
192 out:
193 rcu_read_unlock();
194
195 return err;
196 }
197
198 EXPORT_SYMBOL_GPL(fib_rules_lookup);
199
200 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
201 struct fib_rules_ops *ops)
202 {
203 int err = -EINVAL;
204
205 if (frh->src_len)
206 if (tb[FRA_SRC] == NULL ||
207 frh->src_len > (ops->addr_size * 8) ||
208 nla_len(tb[FRA_SRC]) != ops->addr_size)
209 goto errout;
210
211 if (frh->dst_len)
212 if (tb[FRA_DST] == NULL ||
213 frh->dst_len > (ops->addr_size * 8) ||
214 nla_len(tb[FRA_DST]) != ops->addr_size)
215 goto errout;
216
217 err = 0;
218 errout:
219 return err;
220 }
221
222 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
223 {
224 struct net *net = skb->sk->sk_net;
225 struct fib_rule_hdr *frh = nlmsg_data(nlh);
226 struct fib_rules_ops *ops = NULL;
227 struct fib_rule *rule, *r, *last = NULL;
228 struct nlattr *tb[FRA_MAX+1];
229 int err = -EINVAL, unresolved = 0;
230
231 if (net != &init_net)
232 return -EINVAL;
233
234 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
235 goto errout;
236
237 ops = lookup_rules_ops(frh->family);
238 if (ops == NULL) {
239 err = EAFNOSUPPORT;
240 goto errout;
241 }
242
243 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
244 if (err < 0)
245 goto errout;
246
247 err = validate_rulemsg(frh, tb, ops);
248 if (err < 0)
249 goto errout;
250
251 rule = kzalloc(ops->rule_size, GFP_KERNEL);
252 if (rule == NULL) {
253 err = -ENOMEM;
254 goto errout;
255 }
256
257 if (tb[FRA_PRIORITY])
258 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
259
260 if (tb[FRA_IFNAME]) {
261 struct net_device *dev;
262
263 rule->ifindex = -1;
264 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
265 dev = __dev_get_by_name(net, rule->ifname);
266 if (dev)
267 rule->ifindex = dev->ifindex;
268 }
269
270 if (tb[FRA_FWMARK]) {
271 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
272 if (rule->mark)
273 /* compatibility: if the mark value is non-zero all bits
274 * are compared unless a mask is explicitly specified.
275 */
276 rule->mark_mask = 0xFFFFFFFF;
277 }
278
279 if (tb[FRA_FWMASK])
280 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
281
282 rule->action = frh->action;
283 rule->flags = frh->flags;
284 rule->table = frh_get_table(frh, tb);
285
286 if (!rule->pref && ops->default_pref)
287 rule->pref = ops->default_pref();
288
289 err = -EINVAL;
290 if (tb[FRA_GOTO]) {
291 if (rule->action != FR_ACT_GOTO)
292 goto errout_free;
293
294 rule->target = nla_get_u32(tb[FRA_GOTO]);
295 /* Backward jumps are prohibited to avoid endless loops */
296 if (rule->target <= rule->pref)
297 goto errout_free;
298
299 list_for_each_entry(r, &ops->rules_list, list) {
300 if (r->pref == rule->target) {
301 rule->ctarget = r;
302 break;
303 }
304 }
305
306 if (rule->ctarget == NULL)
307 unresolved = 1;
308 } else if (rule->action == FR_ACT_GOTO)
309 goto errout_free;
310
311 err = ops->configure(rule, skb, nlh, frh, tb);
312 if (err < 0)
313 goto errout_free;
314
315 list_for_each_entry(r, &ops->rules_list, list) {
316 if (r->pref > rule->pref)
317 break;
318 last = r;
319 }
320
321 fib_rule_get(rule);
322
323 if (ops->unresolved_rules) {
324 /*
325 * There are unresolved goto rules in the list, check if
326 * any of them are pointing to this new rule.
327 */
328 list_for_each_entry(r, &ops->rules_list, list) {
329 if (r->action == FR_ACT_GOTO &&
330 r->target == rule->pref) {
331 BUG_ON(r->ctarget != NULL);
332 rcu_assign_pointer(r->ctarget, rule);
333 if (--ops->unresolved_rules == 0)
334 break;
335 }
336 }
337 }
338
339 if (rule->action == FR_ACT_GOTO)
340 ops->nr_goto_rules++;
341
342 if (unresolved)
343 ops->unresolved_rules++;
344
345 if (last)
346 list_add_rcu(&rule->list, &last->list);
347 else
348 list_add_rcu(&rule->list, &ops->rules_list);
349
350 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
351 flush_route_cache(ops);
352 rules_ops_put(ops);
353 return 0;
354
355 errout_free:
356 kfree(rule);
357 errout:
358 rules_ops_put(ops);
359 return err;
360 }
361
362 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
363 {
364 struct net *net = skb->sk->sk_net;
365 struct fib_rule_hdr *frh = nlmsg_data(nlh);
366 struct fib_rules_ops *ops = NULL;
367 struct fib_rule *rule, *tmp;
368 struct nlattr *tb[FRA_MAX+1];
369 int err = -EINVAL;
370
371 if (net != &init_net)
372 return -EINVAL;
373
374 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
375 goto errout;
376
377 ops = lookup_rules_ops(frh->family);
378 if (ops == NULL) {
379 err = EAFNOSUPPORT;
380 goto errout;
381 }
382
383 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
384 if (err < 0)
385 goto errout;
386
387 err = validate_rulemsg(frh, tb, ops);
388 if (err < 0)
389 goto errout;
390
391 list_for_each_entry(rule, &ops->rules_list, list) {
392 if (frh->action && (frh->action != rule->action))
393 continue;
394
395 if (frh->table && (frh_get_table(frh, tb) != rule->table))
396 continue;
397
398 if (tb[FRA_PRIORITY] &&
399 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
400 continue;
401
402 if (tb[FRA_IFNAME] &&
403 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
404 continue;
405
406 if (tb[FRA_FWMARK] &&
407 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
408 continue;
409
410 if (tb[FRA_FWMASK] &&
411 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
412 continue;
413
414 if (!ops->compare(rule, frh, tb))
415 continue;
416
417 if (rule->flags & FIB_RULE_PERMANENT) {
418 err = -EPERM;
419 goto errout;
420 }
421
422 list_del_rcu(&rule->list);
423
424 if (rule->action == FR_ACT_GOTO)
425 ops->nr_goto_rules--;
426
427 /*
428 * Check if this rule is a target to any of them. If so,
429 * disable them. As this operation is eventually very
430 * expensive, it is only performed if goto rules have
431 * actually been added.
432 */
433 if (ops->nr_goto_rules > 0) {
434 list_for_each_entry(tmp, &ops->rules_list, list) {
435 if (tmp->ctarget == rule) {
436 rcu_assign_pointer(tmp->ctarget, NULL);
437 ops->unresolved_rules++;
438 }
439 }
440 }
441
442 synchronize_rcu();
443 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
444 NETLINK_CB(skb).pid);
445 fib_rule_put(rule);
446 flush_route_cache(ops);
447 rules_ops_put(ops);
448 return 0;
449 }
450
451 err = -ENOENT;
452 errout:
453 rules_ops_put(ops);
454 return err;
455 }
456
457 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
458 struct fib_rule *rule)
459 {
460 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
461 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
462 + nla_total_size(4) /* FRA_PRIORITY */
463 + nla_total_size(4) /* FRA_TABLE */
464 + nla_total_size(4) /* FRA_FWMARK */
465 + nla_total_size(4); /* FRA_FWMASK */
466
467 if (ops->nlmsg_payload)
468 payload += ops->nlmsg_payload(rule);
469
470 return payload;
471 }
472
473 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
474 u32 pid, u32 seq, int type, int flags,
475 struct fib_rules_ops *ops)
476 {
477 struct nlmsghdr *nlh;
478 struct fib_rule_hdr *frh;
479
480 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
481 if (nlh == NULL)
482 return -EMSGSIZE;
483
484 frh = nlmsg_data(nlh);
485 frh->table = rule->table;
486 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
487 frh->res1 = 0;
488 frh->res2 = 0;
489 frh->action = rule->action;
490 frh->flags = rule->flags;
491
492 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
493 frh->flags |= FIB_RULE_UNRESOLVED;
494
495 if (rule->ifname[0]) {
496 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
497
498 if (rule->ifindex == -1)
499 frh->flags |= FIB_RULE_DEV_DETACHED;
500 }
501
502 if (rule->pref)
503 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
504
505 if (rule->mark)
506 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
507
508 if (rule->mark_mask || rule->mark)
509 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
510
511 if (rule->target)
512 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
513
514 if (ops->fill(rule, skb, nlh, frh) < 0)
515 goto nla_put_failure;
516
517 return nlmsg_end(skb, nlh);
518
519 nla_put_failure:
520 nlmsg_cancel(skb, nlh);
521 return -EMSGSIZE;
522 }
523
524 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
525 struct fib_rules_ops *ops)
526 {
527 int idx = 0;
528 struct fib_rule *rule;
529
530 list_for_each_entry(rule, &ops->rules_list, list) {
531 if (idx < cb->args[1])
532 goto skip;
533
534 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
535 cb->nlh->nlmsg_seq, RTM_NEWRULE,
536 NLM_F_MULTI, ops) < 0)
537 break;
538 skip:
539 idx++;
540 }
541 cb->args[1] = idx;
542 rules_ops_put(ops);
543
544 return skb->len;
545 }
546
547 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
548 {
549 struct net *net = skb->sk->sk_net;
550 struct fib_rules_ops *ops;
551 int idx = 0, family;
552
553 if (net != &init_net)
554 return -EINVAL;
555
556 family = rtnl_msg_family(cb->nlh);
557 if (family != AF_UNSPEC) {
558 /* Protocol specific dump request */
559 ops = lookup_rules_ops(family);
560 if (ops == NULL)
561 return -EAFNOSUPPORT;
562
563 return dump_rules(skb, cb, ops);
564 }
565
566 rcu_read_lock();
567 list_for_each_entry_rcu(ops, &rules_ops, list) {
568 if (idx < cb->args[0] || !try_module_get(ops->owner))
569 goto skip;
570
571 if (dump_rules(skb, cb, ops) < 0)
572 break;
573
574 cb->args[1] = 0;
575 skip:
576 idx++;
577 }
578 rcu_read_unlock();
579 cb->args[0] = idx;
580
581 return skb->len;
582 }
583
584 static void notify_rule_change(int event, struct fib_rule *rule,
585 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
586 u32 pid)
587 {
588 struct sk_buff *skb;
589 int err = -ENOBUFS;
590
591 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
592 if (skb == NULL)
593 goto errout;
594
595 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
596 if (err < 0) {
597 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
598 WARN_ON(err == -EMSGSIZE);
599 kfree_skb(skb);
600 goto errout;
601 }
602 err = rtnl_notify(skb, &init_net, pid, ops->nlgroup, nlh, GFP_KERNEL);
603 errout:
604 if (err < 0)
605 rtnl_set_sk_err(&init_net, ops->nlgroup, err);
606 }
607
608 static void attach_rules(struct list_head *rules, struct net_device *dev)
609 {
610 struct fib_rule *rule;
611
612 list_for_each_entry(rule, rules, list) {
613 if (rule->ifindex == -1 &&
614 strcmp(dev->name, rule->ifname) == 0)
615 rule->ifindex = dev->ifindex;
616 }
617 }
618
619 static void detach_rules(struct list_head *rules, struct net_device *dev)
620 {
621 struct fib_rule *rule;
622
623 list_for_each_entry(rule, rules, list)
624 if (rule->ifindex == dev->ifindex)
625 rule->ifindex = -1;
626 }
627
628
629 static int fib_rules_event(struct notifier_block *this, unsigned long event,
630 void *ptr)
631 {
632 struct net_device *dev = ptr;
633 struct fib_rules_ops *ops;
634
635 if (dev->nd_net != &init_net)
636 return NOTIFY_DONE;
637
638 ASSERT_RTNL();
639 rcu_read_lock();
640
641 switch (event) {
642 case NETDEV_REGISTER:
643 list_for_each_entry(ops, &rules_ops, list)
644 attach_rules(&ops->rules_list, dev);
645 break;
646
647 case NETDEV_UNREGISTER:
648 list_for_each_entry(ops, &rules_ops, list)
649 detach_rules(&ops->rules_list, dev);
650 break;
651 }
652
653 rcu_read_unlock();
654
655 return NOTIFY_DONE;
656 }
657
658 static struct notifier_block fib_rules_notifier = {
659 .notifier_call = fib_rules_event,
660 };
661
662 static int __init fib_rules_init(void)
663 {
664 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
665 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
666 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
667
668 return register_netdevice_notifier(&fib_rules_notifier);
669 }
670
671 subsys_initcall(fib_rules_init);