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);
127 EXPORT_SYMBOL_GPL(fib_rules_register);
129 void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
131 struct fib_rule *rule, *tmp;
133 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
134 list_del_rcu(&rule->list);
138 EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
140 static void fib_rules_put_rcu(struct rcu_head *head)
142 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
143 struct net *net = ops->fro_net;
149 void fib_rules_unregister(struct fib_rules_ops *ops)
151 struct net *net = ops->fro_net;
153 spin_lock(&net->rules_mod_lock);
154 list_del_rcu(&ops->list);
155 fib_rules_cleanup_ops(ops);
156 spin_unlock(&net->rules_mod_lock);
158 call_rcu(&ops->rcu, fib_rules_put_rcu);
160 EXPORT_SYMBOL_GPL(fib_rules_unregister);
162 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
163 struct flowi *fl, int flags)
167 if (rule->iifindex && (rule->iifindex != fl->iif))
170 if (rule->oifindex && (rule->oifindex != fl->oif))
173 if ((rule->mark ^ fl->mark) & rule->mark_mask)
176 ret = ops->match(rule, fl, flags);
178 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
181 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
182 int flags, struct fib_lookup_arg *arg)
184 struct fib_rule *rule;
189 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
191 if (!fib_rule_match(rule, ops, fl, flags))
194 if (rule->action == FR_ACT_GOTO) {
195 struct fib_rule *target;
197 target = rcu_dereference(rule->ctarget);
198 if (target == NULL) {
204 } else if (rule->action == FR_ACT_NOP)
207 err = ops->action(rule, fl, flags, arg);
209 if (err != -EAGAIN) {
222 EXPORT_SYMBOL_GPL(fib_rules_lookup);
224 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
225 struct fib_rules_ops *ops)
230 if (tb[FRA_SRC] == NULL ||
231 frh->src_len > (ops->addr_size * 8) ||
232 nla_len(tb[FRA_SRC]) != ops->addr_size)
236 if (tb[FRA_DST] == NULL ||
237 frh->dst_len > (ops->addr_size * 8) ||
238 nla_len(tb[FRA_DST]) != ops->addr_size)
246 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
248 struct net *net = sock_net(skb->sk);
249 struct fib_rule_hdr *frh = nlmsg_data(nlh);
250 struct fib_rules_ops *ops = NULL;
251 struct fib_rule *rule, *r, *last = NULL;
252 struct nlattr *tb[FRA_MAX+1];
253 int err = -EINVAL, unresolved = 0;
255 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
258 ops = lookup_rules_ops(net, frh->family);
264 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
268 err = validate_rulemsg(frh, tb, ops);
272 rule = kzalloc(ops->rule_size, GFP_KERNEL);
277 rule->fr_net = hold_net(net);
279 if (tb[FRA_PRIORITY])
280 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
282 if (tb[FRA_IIFNAME]) {
283 struct net_device *dev;
286 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
287 dev = __dev_get_by_name(net, rule->iifname);
289 rule->iifindex = dev->ifindex;
292 if (tb[FRA_OIFNAME]) {
293 struct net_device *dev;
296 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
297 dev = __dev_get_by_name(net, rule->oifname);
299 rule->oifindex = dev->ifindex;
302 if (tb[FRA_FWMARK]) {
303 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
305 /* compatibility: if the mark value is non-zero all bits
306 * are compared unless a mask is explicitly specified.
308 rule->mark_mask = 0xFFFFFFFF;
312 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
314 rule->action = frh->action;
315 rule->flags = frh->flags;
316 rule->table = frh_get_table(frh, tb);
318 if (!tb[FRA_PRIORITY] && ops->default_pref)
319 rule->pref = ops->default_pref(ops);
323 if (rule->action != FR_ACT_GOTO)
326 rule->target = nla_get_u32(tb[FRA_GOTO]);
327 /* Backward jumps are prohibited to avoid endless loops */
328 if (rule->target <= rule->pref)
331 list_for_each_entry(r, &ops->rules_list, list) {
332 if (r->pref == rule->target) {
338 if (rule->ctarget == NULL)
340 } else if (rule->action == FR_ACT_GOTO)
343 err = ops->configure(rule, skb, frh, tb);
347 list_for_each_entry(r, &ops->rules_list, list) {
348 if (r->pref > rule->pref)
355 if (ops->unresolved_rules) {
357 * There are unresolved goto rules in the list, check if
358 * any of them are pointing to this new rule.
360 list_for_each_entry(r, &ops->rules_list, list) {
361 if (r->action == FR_ACT_GOTO &&
362 r->target == rule->pref) {
363 BUG_ON(r->ctarget != NULL);
364 rcu_assign_pointer(r->ctarget, rule);
365 if (--ops->unresolved_rules == 0)
371 if (rule->action == FR_ACT_GOTO)
372 ops->nr_goto_rules++;
375 ops->unresolved_rules++;
378 list_add_rcu(&rule->list, &last->list);
380 list_add_rcu(&rule->list, &ops->rules_list);
382 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
383 flush_route_cache(ops);
388 release_net(rule->fr_net);
395 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
397 struct net *net = sock_net(skb->sk);
398 struct fib_rule_hdr *frh = nlmsg_data(nlh);
399 struct fib_rules_ops *ops = NULL;
400 struct fib_rule *rule, *tmp;
401 struct nlattr *tb[FRA_MAX+1];
404 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
407 ops = lookup_rules_ops(net, frh->family);
413 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
417 err = validate_rulemsg(frh, tb, ops);
421 list_for_each_entry(rule, &ops->rules_list, list) {
422 if (frh->action && (frh->action != rule->action))
425 if (frh->table && (frh_get_table(frh, tb) != rule->table))
428 if (tb[FRA_PRIORITY] &&
429 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
432 if (tb[FRA_IIFNAME] &&
433 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
436 if (tb[FRA_OIFNAME] &&
437 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
440 if (tb[FRA_FWMARK] &&
441 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
444 if (tb[FRA_FWMASK] &&
445 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
448 if (!ops->compare(rule, frh, tb))
451 if (rule->flags & FIB_RULE_PERMANENT) {
456 list_del_rcu(&rule->list);
458 if (rule->action == FR_ACT_GOTO)
459 ops->nr_goto_rules--;
462 * Check if this rule is a target to any of them. If so,
463 * disable them. As this operation is eventually very
464 * expensive, it is only performed if goto rules have
465 * actually been added.
467 if (ops->nr_goto_rules > 0) {
468 list_for_each_entry(tmp, &ops->rules_list, list) {
469 if (tmp->ctarget == rule) {
470 rcu_assign_pointer(tmp->ctarget, NULL);
471 ops->unresolved_rules++;
477 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
478 NETLINK_CB(skb).pid);
480 flush_route_cache(ops);
491 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
492 struct fib_rule *rule)
494 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
495 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
496 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
497 + nla_total_size(4) /* FRA_PRIORITY */
498 + nla_total_size(4) /* FRA_TABLE */
499 + nla_total_size(4) /* FRA_FWMARK */
500 + nla_total_size(4); /* FRA_FWMASK */
502 if (ops->nlmsg_payload)
503 payload += ops->nlmsg_payload(rule);
508 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
509 u32 pid, u32 seq, int type, int flags,
510 struct fib_rules_ops *ops)
512 struct nlmsghdr *nlh;
513 struct fib_rule_hdr *frh;
515 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
519 frh = nlmsg_data(nlh);
520 frh->table = rule->table;
521 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
524 frh->action = rule->action;
525 frh->flags = rule->flags;
527 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
528 frh->flags |= FIB_RULE_UNRESOLVED;
530 if (rule->iifname[0]) {
531 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
533 if (rule->iifindex == -1)
534 frh->flags |= FIB_RULE_IIF_DETACHED;
537 if (rule->oifname[0]) {
538 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
540 if (rule->oifindex == -1)
541 frh->flags |= FIB_RULE_OIF_DETACHED;
545 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
548 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
550 if (rule->mark_mask || rule->mark)
551 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
554 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
556 if (ops->fill(rule, skb, frh) < 0)
557 goto nla_put_failure;
559 return nlmsg_end(skb, nlh);
562 nlmsg_cancel(skb, nlh);
566 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
567 struct fib_rules_ops *ops)
570 struct fib_rule *rule;
572 list_for_each_entry(rule, &ops->rules_list, list) {
573 if (idx < cb->args[1])
576 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
577 cb->nlh->nlmsg_seq, RTM_NEWRULE,
578 NLM_F_MULTI, ops) < 0)
589 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
591 struct net *net = sock_net(skb->sk);
592 struct fib_rules_ops *ops;
595 family = rtnl_msg_family(cb->nlh);
596 if (family != AF_UNSPEC) {
597 /* Protocol specific dump request */
598 ops = lookup_rules_ops(net, family);
600 return -EAFNOSUPPORT;
602 return dump_rules(skb, cb, ops);
606 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
607 if (idx < cb->args[0] || !try_module_get(ops->owner))
610 if (dump_rules(skb, cb, ops) < 0)
623 static void notify_rule_change(int event, struct fib_rule *rule,
624 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
632 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
636 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
638 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
639 WARN_ON(err == -EMSGSIZE);
644 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
648 rtnl_set_sk_err(net, ops->nlgroup, err);
651 static void attach_rules(struct list_head *rules, struct net_device *dev)
653 struct fib_rule *rule;
655 list_for_each_entry(rule, rules, list) {
656 if (rule->iifindex == -1 &&
657 strcmp(dev->name, rule->iifname) == 0)
658 rule->iifindex = dev->ifindex;
659 if (rule->oifindex == -1 &&
660 strcmp(dev->name, rule->oifname) == 0)
661 rule->oifindex = dev->ifindex;
665 static void detach_rules(struct list_head *rules, struct net_device *dev)
667 struct fib_rule *rule;
669 list_for_each_entry(rule, rules, list) {
670 if (rule->iifindex == dev->ifindex)
672 if (rule->oifindex == dev->ifindex)
678 static int fib_rules_event(struct notifier_block *this, unsigned long event,
681 struct net_device *dev = ptr;
682 struct net *net = dev_net(dev);
683 struct fib_rules_ops *ops;
688 case NETDEV_REGISTER:
689 list_for_each_entry(ops, &net->rules_ops, list)
690 attach_rules(&ops->rules_list, dev);
693 case NETDEV_UNREGISTER:
694 list_for_each_entry(ops, &net->rules_ops, list)
695 detach_rules(&ops->rules_list, dev);
702 static struct notifier_block fib_rules_notifier = {
703 .notifier_call = fib_rules_event,
706 static int __net_init fib_rules_net_init(struct net *net)
708 INIT_LIST_HEAD(&net->rules_ops);
709 spin_lock_init(&net->rules_mod_lock);
713 static struct pernet_operations fib_rules_net_ops = {
714 .init = fib_rules_net_init,
717 static int __init fib_rules_init(void)
720 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
721 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
722 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
724 err = register_pernet_subsys(&fib_rules_net_ops);
728 err = register_netdevice_notifier(&fib_rules_notifier);
730 goto fail_unregister;
735 unregister_pernet_subsys(&fib_rules_net_ops);
737 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
738 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
739 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
743 subsys_initcall(fib_rules_init);