net: Add l3mdev rule
[deliverable/linux.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/slab.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <net/net_namespace.h>
17 #include <net/sock.h>
18 #include <net/fib_rules.h>
19 #include <net/ip_tunnels.h>
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 r->fr_net = ops->fro_net;
36
37 r->suppress_prefixlen = -1;
38 r->suppress_ifgroup = -1;
39
40 /* The lock is not required here, the list in unreacheable
41 * at the moment this function is called */
42 list_add_tail(&r->list, &ops->rules_list);
43 return 0;
44 }
45 EXPORT_SYMBOL(fib_default_rule_add);
46
47 static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
48 {
49 struct list_head *pos;
50 struct fib_rule *rule;
51
52 if (!list_empty(&ops->rules_list)) {
53 pos = ops->rules_list.next;
54 if (pos->next != &ops->rules_list) {
55 rule = list_entry(pos->next, struct fib_rule, list);
56 if (rule->pref)
57 return rule->pref - 1;
58 }
59 }
60
61 return 0;
62 }
63
64 static void notify_rule_change(int event, struct fib_rule *rule,
65 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
66 u32 pid);
67
68 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
69 {
70 struct fib_rules_ops *ops;
71
72 rcu_read_lock();
73 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
74 if (ops->family == family) {
75 if (!try_module_get(ops->owner))
76 ops = NULL;
77 rcu_read_unlock();
78 return ops;
79 }
80 }
81 rcu_read_unlock();
82
83 return NULL;
84 }
85
86 static void rules_ops_put(struct fib_rules_ops *ops)
87 {
88 if (ops)
89 module_put(ops->owner);
90 }
91
92 static void flush_route_cache(struct fib_rules_ops *ops)
93 {
94 if (ops->flush_cache)
95 ops->flush_cache(ops);
96 }
97
98 static int __fib_rules_register(struct fib_rules_ops *ops)
99 {
100 int err = -EEXIST;
101 struct fib_rules_ops *o;
102 struct net *net;
103
104 net = ops->fro_net;
105
106 if (ops->rule_size < sizeof(struct fib_rule))
107 return -EINVAL;
108
109 if (ops->match == NULL || ops->configure == NULL ||
110 ops->compare == NULL || ops->fill == NULL ||
111 ops->action == NULL)
112 return -EINVAL;
113
114 spin_lock(&net->rules_mod_lock);
115 list_for_each_entry(o, &net->rules_ops, list)
116 if (ops->family == o->family)
117 goto errout;
118
119 list_add_tail_rcu(&ops->list, &net->rules_ops);
120 err = 0;
121 errout:
122 spin_unlock(&net->rules_mod_lock);
123
124 return err;
125 }
126
127 struct fib_rules_ops *
128 fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
129 {
130 struct fib_rules_ops *ops;
131 int err;
132
133 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
134 if (ops == NULL)
135 return ERR_PTR(-ENOMEM);
136
137 INIT_LIST_HEAD(&ops->rules_list);
138 ops->fro_net = net;
139
140 err = __fib_rules_register(ops);
141 if (err) {
142 kfree(ops);
143 ops = ERR_PTR(err);
144 }
145
146 return ops;
147 }
148 EXPORT_SYMBOL_GPL(fib_rules_register);
149
150 static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
151 {
152 struct fib_rule *rule, *tmp;
153
154 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
155 list_del_rcu(&rule->list);
156 if (ops->delete)
157 ops->delete(rule);
158 fib_rule_put(rule);
159 }
160 }
161
162 void fib_rules_unregister(struct fib_rules_ops *ops)
163 {
164 struct net *net = ops->fro_net;
165
166 spin_lock(&net->rules_mod_lock);
167 list_del_rcu(&ops->list);
168 spin_unlock(&net->rules_mod_lock);
169
170 fib_rules_cleanup_ops(ops);
171 kfree_rcu(ops, rcu);
172 }
173 EXPORT_SYMBOL_GPL(fib_rules_unregister);
174
175 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
176 struct flowi *fl, int flags,
177 struct fib_lookup_arg *arg)
178 {
179 int ret = 0;
180
181 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
182 goto out;
183
184 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
185 goto out;
186
187 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
188 goto out;
189
190 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
191 goto out;
192
193 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
194 goto out;
195
196 ret = ops->match(rule, fl, flags);
197 out:
198 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
199 }
200
201 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
202 int flags, struct fib_lookup_arg *arg)
203 {
204 struct fib_rule *rule;
205 int err;
206
207 rcu_read_lock();
208
209 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
210 jumped:
211 if (!fib_rule_match(rule, ops, fl, flags, arg))
212 continue;
213
214 if (rule->action == FR_ACT_GOTO) {
215 struct fib_rule *target;
216
217 target = rcu_dereference(rule->ctarget);
218 if (target == NULL) {
219 continue;
220 } else {
221 rule = target;
222 goto jumped;
223 }
224 } else if (rule->action == FR_ACT_NOP)
225 continue;
226 else
227 err = ops->action(rule, fl, flags, arg);
228
229 if (!err && ops->suppress && ops->suppress(rule, arg))
230 continue;
231
232 if (err != -EAGAIN) {
233 if ((arg->flags & FIB_LOOKUP_NOREF) ||
234 likely(atomic_inc_not_zero(&rule->refcnt))) {
235 arg->rule = rule;
236 goto out;
237 }
238 break;
239 }
240 }
241
242 err = -ESRCH;
243 out:
244 rcu_read_unlock();
245
246 return err;
247 }
248 EXPORT_SYMBOL_GPL(fib_rules_lookup);
249
250 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
251 struct fib_rules_ops *ops)
252 {
253 int err = -EINVAL;
254
255 if (frh->src_len)
256 if (tb[FRA_SRC] == NULL ||
257 frh->src_len > (ops->addr_size * 8) ||
258 nla_len(tb[FRA_SRC]) != ops->addr_size)
259 goto errout;
260
261 if (frh->dst_len)
262 if (tb[FRA_DST] == NULL ||
263 frh->dst_len > (ops->addr_size * 8) ||
264 nla_len(tb[FRA_DST]) != ops->addr_size)
265 goto errout;
266
267 err = 0;
268 errout:
269 return err;
270 }
271
272 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
273 {
274 struct net *net = sock_net(skb->sk);
275 struct fib_rule_hdr *frh = nlmsg_data(nlh);
276 struct fib_rules_ops *ops = NULL;
277 struct fib_rule *rule, *r, *last = NULL;
278 struct nlattr *tb[FRA_MAX+1];
279 int err = -EINVAL, unresolved = 0;
280
281 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
282 goto errout;
283
284 ops = lookup_rules_ops(net, frh->family);
285 if (ops == NULL) {
286 err = -EAFNOSUPPORT;
287 goto errout;
288 }
289
290 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
291 if (err < 0)
292 goto errout;
293
294 err = validate_rulemsg(frh, tb, ops);
295 if (err < 0)
296 goto errout;
297
298 rule = kzalloc(ops->rule_size, GFP_KERNEL);
299 if (rule == NULL) {
300 err = -ENOMEM;
301 goto errout;
302 }
303 rule->fr_net = net;
304
305 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
306 : fib_default_rule_pref(ops);
307
308 if (tb[FRA_IIFNAME]) {
309 struct net_device *dev;
310
311 rule->iifindex = -1;
312 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
313 dev = __dev_get_by_name(net, rule->iifname);
314 if (dev)
315 rule->iifindex = dev->ifindex;
316 }
317
318 if (tb[FRA_OIFNAME]) {
319 struct net_device *dev;
320
321 rule->oifindex = -1;
322 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
323 dev = __dev_get_by_name(net, rule->oifname);
324 if (dev)
325 rule->oifindex = dev->ifindex;
326 }
327
328 if (tb[FRA_FWMARK]) {
329 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
330 if (rule->mark)
331 /* compatibility: if the mark value is non-zero all bits
332 * are compared unless a mask is explicitly specified.
333 */
334 rule->mark_mask = 0xFFFFFFFF;
335 }
336
337 if (tb[FRA_FWMASK])
338 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
339
340 if (tb[FRA_TUN_ID])
341 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
342
343 if (tb[FRA_L3MDEV]) {
344 #ifdef CONFIG_NET_L3_MASTER_DEV
345 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
346 if (rule->l3mdev != 1)
347 #endif
348 goto errout_free;
349 }
350
351 rule->action = frh->action;
352 rule->flags = frh->flags;
353 rule->table = frh_get_table(frh, tb);
354 if (tb[FRA_SUPPRESS_PREFIXLEN])
355 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
356 else
357 rule->suppress_prefixlen = -1;
358
359 if (tb[FRA_SUPPRESS_IFGROUP])
360 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
361 else
362 rule->suppress_ifgroup = -1;
363
364 err = -EINVAL;
365 if (tb[FRA_GOTO]) {
366 if (rule->action != FR_ACT_GOTO)
367 goto errout_free;
368
369 rule->target = nla_get_u32(tb[FRA_GOTO]);
370 /* Backward jumps are prohibited to avoid endless loops */
371 if (rule->target <= rule->pref)
372 goto errout_free;
373
374 list_for_each_entry(r, &ops->rules_list, list) {
375 if (r->pref == rule->target) {
376 RCU_INIT_POINTER(rule->ctarget, r);
377 break;
378 }
379 }
380
381 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
382 unresolved = 1;
383 } else if (rule->action == FR_ACT_GOTO)
384 goto errout_free;
385
386 if (rule->l3mdev && rule->table)
387 goto errout_free;
388
389 err = ops->configure(rule, skb, frh, tb);
390 if (err < 0)
391 goto errout_free;
392
393 list_for_each_entry(r, &ops->rules_list, list) {
394 if (r->pref > rule->pref)
395 break;
396 last = r;
397 }
398
399 fib_rule_get(rule);
400
401 if (last)
402 list_add_rcu(&rule->list, &last->list);
403 else
404 list_add_rcu(&rule->list, &ops->rules_list);
405
406 if (ops->unresolved_rules) {
407 /*
408 * There are unresolved goto rules in the list, check if
409 * any of them are pointing to this new rule.
410 */
411 list_for_each_entry(r, &ops->rules_list, list) {
412 if (r->action == FR_ACT_GOTO &&
413 r->target == rule->pref &&
414 rtnl_dereference(r->ctarget) == NULL) {
415 rcu_assign_pointer(r->ctarget, rule);
416 if (--ops->unresolved_rules == 0)
417 break;
418 }
419 }
420 }
421
422 if (rule->action == FR_ACT_GOTO)
423 ops->nr_goto_rules++;
424
425 if (unresolved)
426 ops->unresolved_rules++;
427
428 if (rule->tun_id)
429 ip_tunnel_need_metadata();
430
431 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
432 flush_route_cache(ops);
433 rules_ops_put(ops);
434 return 0;
435
436 errout_free:
437 kfree(rule);
438 errout:
439 rules_ops_put(ops);
440 return err;
441 }
442 EXPORT_SYMBOL_GPL(fib_nl_newrule);
443
444 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
445 {
446 struct net *net = sock_net(skb->sk);
447 struct fib_rule_hdr *frh = nlmsg_data(nlh);
448 struct fib_rules_ops *ops = NULL;
449 struct fib_rule *rule, *tmp;
450 struct nlattr *tb[FRA_MAX+1];
451 int err = -EINVAL;
452
453 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
454 goto errout;
455
456 ops = lookup_rules_ops(net, frh->family);
457 if (ops == NULL) {
458 err = -EAFNOSUPPORT;
459 goto errout;
460 }
461
462 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
463 if (err < 0)
464 goto errout;
465
466 err = validate_rulemsg(frh, tb, ops);
467 if (err < 0)
468 goto errout;
469
470 list_for_each_entry(rule, &ops->rules_list, list) {
471 if (frh->action && (frh->action != rule->action))
472 continue;
473
474 if (frh_get_table(frh, tb) &&
475 (frh_get_table(frh, tb) != rule->table))
476 continue;
477
478 if (tb[FRA_PRIORITY] &&
479 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
480 continue;
481
482 if (tb[FRA_IIFNAME] &&
483 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
484 continue;
485
486 if (tb[FRA_OIFNAME] &&
487 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
488 continue;
489
490 if (tb[FRA_FWMARK] &&
491 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
492 continue;
493
494 if (tb[FRA_FWMASK] &&
495 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
496 continue;
497
498 if (tb[FRA_TUN_ID] &&
499 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
500 continue;
501
502 if (tb[FRA_L3MDEV] &&
503 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
504 continue;
505
506 if (!ops->compare(rule, frh, tb))
507 continue;
508
509 if (rule->flags & FIB_RULE_PERMANENT) {
510 err = -EPERM;
511 goto errout;
512 }
513
514 if (ops->delete) {
515 err = ops->delete(rule);
516 if (err)
517 goto errout;
518 }
519
520 if (rule->tun_id)
521 ip_tunnel_unneed_metadata();
522
523 list_del_rcu(&rule->list);
524
525 if (rule->action == FR_ACT_GOTO) {
526 ops->nr_goto_rules--;
527 if (rtnl_dereference(rule->ctarget) == NULL)
528 ops->unresolved_rules--;
529 }
530
531 /*
532 * Check if this rule is a target to any of them. If so,
533 * disable them. As this operation is eventually very
534 * expensive, it is only performed if goto rules have
535 * actually been added.
536 */
537 if (ops->nr_goto_rules > 0) {
538 list_for_each_entry(tmp, &ops->rules_list, list) {
539 if (rtnl_dereference(tmp->ctarget) == rule) {
540 RCU_INIT_POINTER(tmp->ctarget, NULL);
541 ops->unresolved_rules++;
542 }
543 }
544 }
545
546 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
547 NETLINK_CB(skb).portid);
548 fib_rule_put(rule);
549 flush_route_cache(ops);
550 rules_ops_put(ops);
551 return 0;
552 }
553
554 err = -ENOENT;
555 errout:
556 rules_ops_put(ops);
557 return err;
558 }
559 EXPORT_SYMBOL_GPL(fib_nl_delrule);
560
561 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
562 struct fib_rule *rule)
563 {
564 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
565 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
566 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
567 + nla_total_size(4) /* FRA_PRIORITY */
568 + nla_total_size(4) /* FRA_TABLE */
569 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
570 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
571 + nla_total_size(4) /* FRA_FWMARK */
572 + nla_total_size(4) /* FRA_FWMASK */
573 + nla_total_size_64bit(8); /* FRA_TUN_ID */
574
575 if (ops->nlmsg_payload)
576 payload += ops->nlmsg_payload(rule);
577
578 return payload;
579 }
580
581 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
582 u32 pid, u32 seq, int type, int flags,
583 struct fib_rules_ops *ops)
584 {
585 struct nlmsghdr *nlh;
586 struct fib_rule_hdr *frh;
587
588 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
589 if (nlh == NULL)
590 return -EMSGSIZE;
591
592 frh = nlmsg_data(nlh);
593 frh->family = ops->family;
594 frh->table = rule->table;
595 if (nla_put_u32(skb, FRA_TABLE, rule->table))
596 goto nla_put_failure;
597 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
598 goto nla_put_failure;
599 frh->res1 = 0;
600 frh->res2 = 0;
601 frh->action = rule->action;
602 frh->flags = rule->flags;
603
604 if (rule->action == FR_ACT_GOTO &&
605 rcu_access_pointer(rule->ctarget) == NULL)
606 frh->flags |= FIB_RULE_UNRESOLVED;
607
608 if (rule->iifname[0]) {
609 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
610 goto nla_put_failure;
611 if (rule->iifindex == -1)
612 frh->flags |= FIB_RULE_IIF_DETACHED;
613 }
614
615 if (rule->oifname[0]) {
616 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
617 goto nla_put_failure;
618 if (rule->oifindex == -1)
619 frh->flags |= FIB_RULE_OIF_DETACHED;
620 }
621
622 if ((rule->pref &&
623 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
624 (rule->mark &&
625 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
626 ((rule->mark_mask || rule->mark) &&
627 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
628 (rule->target &&
629 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
630 (rule->tun_id &&
631 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
632 (rule->l3mdev &&
633 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)))
634 goto nla_put_failure;
635
636 if (rule->suppress_ifgroup != -1) {
637 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
638 goto nla_put_failure;
639 }
640
641 if (ops->fill(rule, skb, frh) < 0)
642 goto nla_put_failure;
643
644 nlmsg_end(skb, nlh);
645 return 0;
646
647 nla_put_failure:
648 nlmsg_cancel(skb, nlh);
649 return -EMSGSIZE;
650 }
651
652 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
653 struct fib_rules_ops *ops)
654 {
655 int idx = 0;
656 struct fib_rule *rule;
657 int err = 0;
658
659 rcu_read_lock();
660 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
661 if (idx < cb->args[1])
662 goto skip;
663
664 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
665 cb->nlh->nlmsg_seq, RTM_NEWRULE,
666 NLM_F_MULTI, ops);
667 if (err)
668 break;
669 skip:
670 idx++;
671 }
672 rcu_read_unlock();
673 cb->args[1] = idx;
674 rules_ops_put(ops);
675
676 return err;
677 }
678
679 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
680 {
681 struct net *net = sock_net(skb->sk);
682 struct fib_rules_ops *ops;
683 int idx = 0, family;
684
685 family = rtnl_msg_family(cb->nlh);
686 if (family != AF_UNSPEC) {
687 /* Protocol specific dump request */
688 ops = lookup_rules_ops(net, family);
689 if (ops == NULL)
690 return -EAFNOSUPPORT;
691
692 dump_rules(skb, cb, ops);
693
694 return skb->len;
695 }
696
697 rcu_read_lock();
698 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
699 if (idx < cb->args[0] || !try_module_get(ops->owner))
700 goto skip;
701
702 if (dump_rules(skb, cb, ops) < 0)
703 break;
704
705 cb->args[1] = 0;
706 skip:
707 idx++;
708 }
709 rcu_read_unlock();
710 cb->args[0] = idx;
711
712 return skb->len;
713 }
714
715 static void notify_rule_change(int event, struct fib_rule *rule,
716 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
717 u32 pid)
718 {
719 struct net *net;
720 struct sk_buff *skb;
721 int err = -ENOBUFS;
722
723 net = ops->fro_net;
724 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
725 if (skb == NULL)
726 goto errout;
727
728 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
729 if (err < 0) {
730 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
731 WARN_ON(err == -EMSGSIZE);
732 kfree_skb(skb);
733 goto errout;
734 }
735
736 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
737 return;
738 errout:
739 if (err < 0)
740 rtnl_set_sk_err(net, ops->nlgroup, err);
741 }
742
743 static void attach_rules(struct list_head *rules, struct net_device *dev)
744 {
745 struct fib_rule *rule;
746
747 list_for_each_entry(rule, rules, list) {
748 if (rule->iifindex == -1 &&
749 strcmp(dev->name, rule->iifname) == 0)
750 rule->iifindex = dev->ifindex;
751 if (rule->oifindex == -1 &&
752 strcmp(dev->name, rule->oifname) == 0)
753 rule->oifindex = dev->ifindex;
754 }
755 }
756
757 static void detach_rules(struct list_head *rules, struct net_device *dev)
758 {
759 struct fib_rule *rule;
760
761 list_for_each_entry(rule, rules, list) {
762 if (rule->iifindex == dev->ifindex)
763 rule->iifindex = -1;
764 if (rule->oifindex == dev->ifindex)
765 rule->oifindex = -1;
766 }
767 }
768
769
770 static int fib_rules_event(struct notifier_block *this, unsigned long event,
771 void *ptr)
772 {
773 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
774 struct net *net = dev_net(dev);
775 struct fib_rules_ops *ops;
776
777 ASSERT_RTNL();
778
779 switch (event) {
780 case NETDEV_REGISTER:
781 list_for_each_entry(ops, &net->rules_ops, list)
782 attach_rules(&ops->rules_list, dev);
783 break;
784
785 case NETDEV_CHANGENAME:
786 list_for_each_entry(ops, &net->rules_ops, list) {
787 detach_rules(&ops->rules_list, dev);
788 attach_rules(&ops->rules_list, dev);
789 }
790 break;
791
792 case NETDEV_UNREGISTER:
793 list_for_each_entry(ops, &net->rules_ops, list)
794 detach_rules(&ops->rules_list, dev);
795 break;
796 }
797
798 return NOTIFY_DONE;
799 }
800
801 static struct notifier_block fib_rules_notifier = {
802 .notifier_call = fib_rules_event,
803 };
804
805 static int __net_init fib_rules_net_init(struct net *net)
806 {
807 INIT_LIST_HEAD(&net->rules_ops);
808 spin_lock_init(&net->rules_mod_lock);
809 return 0;
810 }
811
812 static struct pernet_operations fib_rules_net_ops = {
813 .init = fib_rules_net_init,
814 };
815
816 static int __init fib_rules_init(void)
817 {
818 int err;
819 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
820 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
821 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
822
823 err = register_pernet_subsys(&fib_rules_net_ops);
824 if (err < 0)
825 goto fail;
826
827 err = register_netdevice_notifier(&fib_rules_notifier);
828 if (err < 0)
829 goto fail_unregister;
830
831 return 0;
832
833 fail_unregister:
834 unregister_pernet_subsys(&fib_rules_net_ops);
835 fail:
836 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
837 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
838 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
839 return err;
840 }
841
842 subsys_initcall(fib_rules_init);
This page took 0.050754 seconds and 5 git commands to generate.