2 * net/core/fib_rules.c Generic Routing Rules
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.
8 * Authors: Thomas Graf <tgraf@suug.ch>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/list.h>
15 #include <net/net_namespace.h>
17 #include <net/fib_rules.h>
19 int fib_default_rule_add(struct fib_rules_ops *ops,
20 u32 pref, u32 table, u32 flags)
24 r = kzalloc(ops->rule_size, GFP_KERNEL);
28 atomic_set(&r->refcnt, 1);
29 r->action = FR_ACT_TO_TBL;
33 r->fr_net = hold_net(ops->fro_net);
35 /* The lock is not required here, the list in unreacheable
36 * at the moment this function is called */
37 list_add_tail(&r->list, &ops->rules_list);
40 EXPORT_SYMBOL(fib_default_rule_add);
42 static void notify_rule_change(int event, struct fib_rule *rule,
43 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
46 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
48 struct fib_rules_ops *ops;
51 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
52 if (ops->family == family) {
53 if (!try_module_get(ops->owner))
64 static void rules_ops_put(struct fib_rules_ops *ops)
67 module_put(ops->owner);
70 static void flush_route_cache(struct fib_rules_ops *ops)
73 ops->flush_cache(ops);
76 static int __fib_rules_register(struct fib_rules_ops *ops)
79 struct fib_rules_ops *o;
84 if (ops->rule_size < sizeof(struct fib_rule))
87 if (ops->match == NULL || ops->configure == NULL ||
88 ops->compare == NULL || ops->fill == NULL ||
92 spin_lock(&net->rules_mod_lock);
93 list_for_each_entry(o, &net->rules_ops, list)
94 if (ops->family == o->family)
98 list_add_tail_rcu(&ops->list, &net->rules_ops);
101 spin_unlock(&net->rules_mod_lock);
106 struct fib_rules_ops *
107 fib_rules_register(struct fib_rules_ops *tmpl, struct net *net)
109 struct fib_rules_ops *ops;
112 ops = kmemdup(tmpl, sizeof (*ops), GFP_KERNEL);
114 return ERR_PTR(-ENOMEM);
116 INIT_LIST_HEAD(&ops->rules_list);
119 err = __fib_rules_register(ops);
128 EXPORT_SYMBOL_GPL(fib_rules_register);
130 void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
132 struct fib_rule *rule, *tmp;
134 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
135 list_del_rcu(&rule->list);
139 EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
141 static void fib_rules_put_rcu(struct rcu_head *head)
143 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
144 struct net *net = ops->fro_net;
150 void fib_rules_unregister(struct fib_rules_ops *ops)
152 struct net *net = ops->fro_net;
154 spin_lock(&net->rules_mod_lock);
155 list_del_rcu(&ops->list);
156 fib_rules_cleanup_ops(ops);
157 spin_unlock(&net->rules_mod_lock);
159 call_rcu(&ops->rcu, fib_rules_put_rcu);
162 EXPORT_SYMBOL_GPL(fib_rules_unregister);
164 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
165 struct flowi *fl, int flags)
169 if (rule->iifindex && (rule->iifindex != fl->iif))
172 if (rule->oifindex && (rule->oifindex != fl->oif))
175 if ((rule->mark ^ fl->mark) & rule->mark_mask)
178 ret = ops->match(rule, fl, flags);
180 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
183 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
184 int flags, struct fib_lookup_arg *arg)
186 struct fib_rule *rule;
191 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
193 if (!fib_rule_match(rule, ops, fl, flags))
196 if (rule->action == FR_ACT_GOTO) {
197 struct fib_rule *target;
199 target = rcu_dereference(rule->ctarget);
200 if (target == NULL) {
206 } else if (rule->action == FR_ACT_NOP)
209 err = ops->action(rule, fl, flags, arg);
211 if (err != -EAGAIN) {
225 EXPORT_SYMBOL_GPL(fib_rules_lookup);
227 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
228 struct fib_rules_ops *ops)
233 if (tb[FRA_SRC] == NULL ||
234 frh->src_len > (ops->addr_size * 8) ||
235 nla_len(tb[FRA_SRC]) != ops->addr_size)
239 if (tb[FRA_DST] == NULL ||
240 frh->dst_len > (ops->addr_size * 8) ||
241 nla_len(tb[FRA_DST]) != ops->addr_size)
249 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
251 struct net *net = sock_net(skb->sk);
252 struct fib_rule_hdr *frh = nlmsg_data(nlh);
253 struct fib_rules_ops *ops = NULL;
254 struct fib_rule *rule, *r, *last = NULL;
255 struct nlattr *tb[FRA_MAX+1];
256 int err = -EINVAL, unresolved = 0;
258 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
261 ops = lookup_rules_ops(net, frh->family);
267 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
271 err = validate_rulemsg(frh, tb, ops);
275 rule = kzalloc(ops->rule_size, GFP_KERNEL);
280 rule->fr_net = hold_net(net);
282 if (tb[FRA_PRIORITY])
283 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
285 if (tb[FRA_IIFNAME]) {
286 struct net_device *dev;
289 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
290 dev = __dev_get_by_name(net, rule->iifname);
292 rule->iifindex = dev->ifindex;
295 if (tb[FRA_OIFNAME]) {
296 struct net_device *dev;
299 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
300 dev = __dev_get_by_name(net, rule->oifname);
302 rule->oifindex = dev->ifindex;
305 if (tb[FRA_FWMARK]) {
306 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
308 /* compatibility: if the mark value is non-zero all bits
309 * are compared unless a mask is explicitly specified.
311 rule->mark_mask = 0xFFFFFFFF;
315 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
317 rule->action = frh->action;
318 rule->flags = frh->flags;
319 rule->table = frh_get_table(frh, tb);
321 if (!tb[FRA_PRIORITY] && ops->default_pref)
322 rule->pref = ops->default_pref(ops);
326 if (rule->action != FR_ACT_GOTO)
329 rule->target = nla_get_u32(tb[FRA_GOTO]);
330 /* Backward jumps are prohibited to avoid endless loops */
331 if (rule->target <= rule->pref)
334 list_for_each_entry(r, &ops->rules_list, list) {
335 if (r->pref == rule->target) {
341 if (rule->ctarget == NULL)
343 } else if (rule->action == FR_ACT_GOTO)
346 err = ops->configure(rule, skb, frh, tb);
350 list_for_each_entry(r, &ops->rules_list, list) {
351 if (r->pref > rule->pref)
358 if (ops->unresolved_rules) {
360 * There are unresolved goto rules in the list, check if
361 * any of them are pointing to this new rule.
363 list_for_each_entry(r, &ops->rules_list, list) {
364 if (r->action == FR_ACT_GOTO &&
365 r->target == rule->pref) {
366 BUG_ON(r->ctarget != NULL);
367 rcu_assign_pointer(r->ctarget, rule);
368 if (--ops->unresolved_rules == 0)
374 if (rule->action == FR_ACT_GOTO)
375 ops->nr_goto_rules++;
378 ops->unresolved_rules++;
381 list_add_rcu(&rule->list, &last->list);
383 list_add_rcu(&rule->list, &ops->rules_list);
385 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
386 flush_route_cache(ops);
391 release_net(rule->fr_net);
398 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
400 struct net *net = sock_net(skb->sk);
401 struct fib_rule_hdr *frh = nlmsg_data(nlh);
402 struct fib_rules_ops *ops = NULL;
403 struct fib_rule *rule, *tmp;
404 struct nlattr *tb[FRA_MAX+1];
407 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
410 ops = lookup_rules_ops(net, frh->family);
416 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
420 err = validate_rulemsg(frh, tb, ops);
424 list_for_each_entry(rule, &ops->rules_list, list) {
425 if (frh->action && (frh->action != rule->action))
428 if (frh->table && (frh_get_table(frh, tb) != rule->table))
431 if (tb[FRA_PRIORITY] &&
432 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
435 if (tb[FRA_IIFNAME] &&
436 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
439 if (tb[FRA_OIFNAME] &&
440 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
443 if (tb[FRA_FWMARK] &&
444 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
447 if (tb[FRA_FWMASK] &&
448 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
451 if (!ops->compare(rule, frh, tb))
454 if (rule->flags & FIB_RULE_PERMANENT) {
459 list_del_rcu(&rule->list);
461 if (rule->action == FR_ACT_GOTO)
462 ops->nr_goto_rules--;
465 * Check if this rule is a target to any of them. If so,
466 * disable them. As this operation is eventually very
467 * expensive, it is only performed if goto rules have
468 * actually been added.
470 if (ops->nr_goto_rules > 0) {
471 list_for_each_entry(tmp, &ops->rules_list, list) {
472 if (tmp->ctarget == rule) {
473 rcu_assign_pointer(tmp->ctarget, NULL);
474 ops->unresolved_rules++;
480 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
481 NETLINK_CB(skb).pid);
483 flush_route_cache(ops);
494 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
495 struct fib_rule *rule)
497 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
498 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
499 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
500 + nla_total_size(4) /* FRA_PRIORITY */
501 + nla_total_size(4) /* FRA_TABLE */
502 + nla_total_size(4) /* FRA_FWMARK */
503 + nla_total_size(4); /* FRA_FWMASK */
505 if (ops->nlmsg_payload)
506 payload += ops->nlmsg_payload(rule);
511 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
512 u32 pid, u32 seq, int type, int flags,
513 struct fib_rules_ops *ops)
515 struct nlmsghdr *nlh;
516 struct fib_rule_hdr *frh;
518 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
522 frh = nlmsg_data(nlh);
523 frh->table = rule->table;
524 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
527 frh->action = rule->action;
528 frh->flags = rule->flags;
530 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
531 frh->flags |= FIB_RULE_UNRESOLVED;
533 if (rule->iifname[0]) {
534 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
536 if (rule->iifindex == -1)
537 frh->flags |= FIB_RULE_IIF_DETACHED;
540 if (rule->oifname[0]) {
541 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
543 if (rule->oifindex == -1)
544 frh->flags |= FIB_RULE_OIF_DETACHED;
548 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
551 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
553 if (rule->mark_mask || rule->mark)
554 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
557 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
559 if (ops->fill(rule, skb, frh) < 0)
560 goto nla_put_failure;
562 return nlmsg_end(skb, nlh);
565 nlmsg_cancel(skb, nlh);
569 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
570 struct fib_rules_ops *ops)
573 struct fib_rule *rule;
575 list_for_each_entry(rule, &ops->rules_list, list) {
576 if (idx < cb->args[1])
579 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
580 cb->nlh->nlmsg_seq, RTM_NEWRULE,
581 NLM_F_MULTI, ops) < 0)
592 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
594 struct net *net = sock_net(skb->sk);
595 struct fib_rules_ops *ops;
598 family = rtnl_msg_family(cb->nlh);
599 if (family != AF_UNSPEC) {
600 /* Protocol specific dump request */
601 ops = lookup_rules_ops(net, family);
603 return -EAFNOSUPPORT;
605 return dump_rules(skb, cb, ops);
609 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
610 if (idx < cb->args[0] || !try_module_get(ops->owner))
613 if (dump_rules(skb, cb, ops) < 0)
626 static void notify_rule_change(int event, struct fib_rule *rule,
627 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
635 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
639 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
641 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
642 WARN_ON(err == -EMSGSIZE);
647 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
651 rtnl_set_sk_err(net, ops->nlgroup, err);
654 static void attach_rules(struct list_head *rules, struct net_device *dev)
656 struct fib_rule *rule;
658 list_for_each_entry(rule, rules, list) {
659 if (rule->iifindex == -1 &&
660 strcmp(dev->name, rule->iifname) == 0)
661 rule->iifindex = dev->ifindex;
662 if (rule->oifindex == -1 &&
663 strcmp(dev->name, rule->oifname) == 0)
664 rule->oifindex = dev->ifindex;
668 static void detach_rules(struct list_head *rules, struct net_device *dev)
670 struct fib_rule *rule;
672 list_for_each_entry(rule, rules, list) {
673 if (rule->iifindex == dev->ifindex)
675 if (rule->oifindex == dev->ifindex)
681 static int fib_rules_event(struct notifier_block *this, unsigned long event,
684 struct net_device *dev = ptr;
685 struct net *net = dev_net(dev);
686 struct fib_rules_ops *ops;
692 case NETDEV_REGISTER:
693 list_for_each_entry(ops, &net->rules_ops, list)
694 attach_rules(&ops->rules_list, dev);
697 case NETDEV_UNREGISTER:
698 list_for_each_entry(ops, &net->rules_ops, list)
699 detach_rules(&ops->rules_list, dev);
708 static struct notifier_block fib_rules_notifier = {
709 .notifier_call = fib_rules_event,
712 static int __net_init fib_rules_net_init(struct net *net)
714 INIT_LIST_HEAD(&net->rules_ops);
715 spin_lock_init(&net->rules_mod_lock);
719 static struct pernet_operations fib_rules_net_ops = {
720 .init = fib_rules_net_init,
723 static int __init fib_rules_init(void)
726 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
727 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
728 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
730 err = register_pernet_subsys(&fib_rules_net_ops);
734 err = register_netdevice_notifier(&fib_rules_notifier);
736 goto fail_unregister;
741 unregister_pernet_subsys(&fib_rules_net_ops);
743 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
744 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
745 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
749 subsys_initcall(fib_rules_init);