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.
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>
18 #include <net/fib_rules.h>
19 #include <net/ip_tunnels.h>
21 int fib_default_rule_add(struct fib_rules_ops *ops,
22 u32 pref, u32 table, u32 flags)
26 r = kzalloc(ops->rule_size, GFP_KERNEL);
30 atomic_set(&r->refcnt, 1);
31 r->action = FR_ACT_TO_TBL;
35 r->fr_net = ops->fro_net;
37 r->suppress_prefixlen = -1;
38 r->suppress_ifgroup = -1;
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);
45 EXPORT_SYMBOL(fib_default_rule_add);
47 u32 fib_default_rule_pref(struct fib_rules_ops *ops)
49 struct list_head *pos;
50 struct fib_rule *rule;
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);
57 return rule->pref - 1;
63 EXPORT_SYMBOL(fib_default_rule_pref);
65 static void notify_rule_change(int event, struct fib_rule *rule,
66 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
69 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
71 struct fib_rules_ops *ops;
74 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
75 if (ops->family == family) {
76 if (!try_module_get(ops->owner))
87 static void rules_ops_put(struct fib_rules_ops *ops)
90 module_put(ops->owner);
93 static void flush_route_cache(struct fib_rules_ops *ops)
96 ops->flush_cache(ops);
99 static int __fib_rules_register(struct fib_rules_ops *ops)
102 struct fib_rules_ops *o;
107 if (ops->rule_size < sizeof(struct fib_rule))
110 if (ops->match == NULL || ops->configure == NULL ||
111 ops->compare == NULL || ops->fill == NULL ||
115 spin_lock(&net->rules_mod_lock);
116 list_for_each_entry(o, &net->rules_ops, list)
117 if (ops->family == o->family)
120 list_add_tail_rcu(&ops->list, &net->rules_ops);
123 spin_unlock(&net->rules_mod_lock);
128 struct fib_rules_ops *
129 fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
131 struct fib_rules_ops *ops;
134 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
136 return ERR_PTR(-ENOMEM);
138 INIT_LIST_HEAD(&ops->rules_list);
141 err = __fib_rules_register(ops);
149 EXPORT_SYMBOL_GPL(fib_rules_register);
151 static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
153 struct fib_rule *rule, *tmp;
155 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
156 list_del_rcu(&rule->list);
163 void fib_rules_unregister(struct fib_rules_ops *ops)
165 struct net *net = ops->fro_net;
167 spin_lock(&net->rules_mod_lock);
168 list_del_rcu(&ops->list);
169 spin_unlock(&net->rules_mod_lock);
171 fib_rules_cleanup_ops(ops);
174 EXPORT_SYMBOL_GPL(fib_rules_unregister);
176 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
177 struct flowi *fl, int flags)
181 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
184 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
187 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
190 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
193 ret = ops->match(rule, fl, flags);
195 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
198 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
199 int flags, struct fib_lookup_arg *arg)
201 struct fib_rule *rule;
206 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
208 if (!fib_rule_match(rule, ops, fl, flags))
211 if (rule->action == FR_ACT_GOTO) {
212 struct fib_rule *target;
214 target = rcu_dereference(rule->ctarget);
215 if (target == NULL) {
221 } else if (rule->action == FR_ACT_NOP)
224 err = ops->action(rule, fl, flags, arg);
226 if (!err && ops->suppress && ops->suppress(rule, arg))
229 if (err != -EAGAIN) {
230 if ((arg->flags & FIB_LOOKUP_NOREF) ||
231 likely(atomic_inc_not_zero(&rule->refcnt))) {
245 EXPORT_SYMBOL_GPL(fib_rules_lookup);
247 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
248 struct fib_rules_ops *ops)
253 if (tb[FRA_SRC] == NULL ||
254 frh->src_len > (ops->addr_size * 8) ||
255 nla_len(tb[FRA_SRC]) != ops->addr_size)
259 if (tb[FRA_DST] == NULL ||
260 frh->dst_len > (ops->addr_size * 8) ||
261 nla_len(tb[FRA_DST]) != ops->addr_size)
269 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
271 struct net *net = sock_net(skb->sk);
272 struct fib_rule_hdr *frh = nlmsg_data(nlh);
273 struct fib_rules_ops *ops = NULL;
274 struct fib_rule *rule, *r, *last = NULL;
275 struct nlattr *tb[FRA_MAX+1];
276 int err = -EINVAL, unresolved = 0;
278 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
281 ops = lookup_rules_ops(net, frh->family);
287 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
291 err = validate_rulemsg(frh, tb, ops);
295 rule = kzalloc(ops->rule_size, GFP_KERNEL);
302 if (tb[FRA_PRIORITY])
303 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
305 if (tb[FRA_IIFNAME]) {
306 struct net_device *dev;
309 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
310 dev = __dev_get_by_name(net, rule->iifname);
312 rule->iifindex = dev->ifindex;
315 if (tb[FRA_OIFNAME]) {
316 struct net_device *dev;
319 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
320 dev = __dev_get_by_name(net, rule->oifname);
322 rule->oifindex = dev->ifindex;
325 if (tb[FRA_FWMARK]) {
326 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
328 /* compatibility: if the mark value is non-zero all bits
329 * are compared unless a mask is explicitly specified.
331 rule->mark_mask = 0xFFFFFFFF;
335 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
338 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
340 rule->action = frh->action;
341 rule->flags = frh->flags;
342 rule->table = frh_get_table(frh, tb);
343 if (tb[FRA_SUPPRESS_PREFIXLEN])
344 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
346 rule->suppress_prefixlen = -1;
348 if (tb[FRA_SUPPRESS_IFGROUP])
349 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
351 rule->suppress_ifgroup = -1;
353 if (!tb[FRA_PRIORITY] && ops->default_pref)
354 rule->pref = ops->default_pref(ops);
358 if (rule->action != FR_ACT_GOTO)
361 rule->target = nla_get_u32(tb[FRA_GOTO]);
362 /* Backward jumps are prohibited to avoid endless loops */
363 if (rule->target <= rule->pref)
366 list_for_each_entry(r, &ops->rules_list, list) {
367 if (r->pref == rule->target) {
368 RCU_INIT_POINTER(rule->ctarget, r);
373 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
375 } else if (rule->action == FR_ACT_GOTO)
378 err = ops->configure(rule, skb, frh, tb);
382 list_for_each_entry(r, &ops->rules_list, list) {
383 if (r->pref > rule->pref)
391 list_add_rcu(&rule->list, &last->list);
393 list_add_rcu(&rule->list, &ops->rules_list);
395 if (ops->unresolved_rules) {
397 * There are unresolved goto rules in the list, check if
398 * any of them are pointing to this new rule.
400 list_for_each_entry(r, &ops->rules_list, list) {
401 if (r->action == FR_ACT_GOTO &&
402 r->target == rule->pref &&
403 rtnl_dereference(r->ctarget) == NULL) {
404 rcu_assign_pointer(r->ctarget, rule);
405 if (--ops->unresolved_rules == 0)
411 if (rule->action == FR_ACT_GOTO)
412 ops->nr_goto_rules++;
415 ops->unresolved_rules++;
418 ip_tunnel_need_metadata();
420 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
421 flush_route_cache(ops);
432 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
434 struct net *net = sock_net(skb->sk);
435 struct fib_rule_hdr *frh = nlmsg_data(nlh);
436 struct fib_rules_ops *ops = NULL;
437 struct fib_rule *rule, *tmp;
438 struct nlattr *tb[FRA_MAX+1];
441 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
444 ops = lookup_rules_ops(net, frh->family);
450 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
454 err = validate_rulemsg(frh, tb, ops);
458 list_for_each_entry(rule, &ops->rules_list, list) {
459 if (frh->action && (frh->action != rule->action))
462 if (frh_get_table(frh, tb) &&
463 (frh_get_table(frh, tb) != rule->table))
466 if (tb[FRA_PRIORITY] &&
467 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
470 if (tb[FRA_IIFNAME] &&
471 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
474 if (tb[FRA_OIFNAME] &&
475 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
478 if (tb[FRA_FWMARK] &&
479 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
482 if (tb[FRA_FWMASK] &&
483 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
486 if (tb[FRA_TUN_ID] &&
487 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
490 if (!ops->compare(rule, frh, tb))
493 if (rule->flags & FIB_RULE_PERMANENT) {
499 err = ops->delete(rule);
505 ip_tunnel_unneed_metadata();
507 list_del_rcu(&rule->list);
509 if (rule->action == FR_ACT_GOTO) {
510 ops->nr_goto_rules--;
511 if (rtnl_dereference(rule->ctarget) == NULL)
512 ops->unresolved_rules--;
516 * Check if this rule is a target to any of them. If so,
517 * disable them. As this operation is eventually very
518 * expensive, it is only performed if goto rules have
519 * actually been added.
521 if (ops->nr_goto_rules > 0) {
522 list_for_each_entry(tmp, &ops->rules_list, list) {
523 if (rtnl_dereference(tmp->ctarget) == rule) {
524 RCU_INIT_POINTER(tmp->ctarget, NULL);
525 ops->unresolved_rules++;
530 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
531 NETLINK_CB(skb).portid);
533 flush_route_cache(ops);
544 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
545 struct fib_rule *rule)
547 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
548 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
549 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
550 + nla_total_size(4) /* FRA_PRIORITY */
551 + nla_total_size(4) /* FRA_TABLE */
552 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
553 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
554 + nla_total_size(4) /* FRA_FWMARK */
555 + nla_total_size(4) /* FRA_FWMASK */
556 + nla_total_size(8); /* FRA_TUN_ID */
558 if (ops->nlmsg_payload)
559 payload += ops->nlmsg_payload(rule);
564 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
565 u32 pid, u32 seq, int type, int flags,
566 struct fib_rules_ops *ops)
568 struct nlmsghdr *nlh;
569 struct fib_rule_hdr *frh;
571 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
575 frh = nlmsg_data(nlh);
576 frh->family = ops->family;
577 frh->table = rule->table;
578 if (nla_put_u32(skb, FRA_TABLE, rule->table))
579 goto nla_put_failure;
580 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
581 goto nla_put_failure;
584 frh->action = rule->action;
585 frh->flags = rule->flags;
587 if (rule->action == FR_ACT_GOTO &&
588 rcu_access_pointer(rule->ctarget) == NULL)
589 frh->flags |= FIB_RULE_UNRESOLVED;
591 if (rule->iifname[0]) {
592 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
593 goto nla_put_failure;
594 if (rule->iifindex == -1)
595 frh->flags |= FIB_RULE_IIF_DETACHED;
598 if (rule->oifname[0]) {
599 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
600 goto nla_put_failure;
601 if (rule->oifindex == -1)
602 frh->flags |= FIB_RULE_OIF_DETACHED;
606 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
608 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
609 ((rule->mark_mask || rule->mark) &&
610 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
612 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
614 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id)))
615 goto nla_put_failure;
617 if (rule->suppress_ifgroup != -1) {
618 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
619 goto nla_put_failure;
622 if (ops->fill(rule, skb, frh) < 0)
623 goto nla_put_failure;
629 nlmsg_cancel(skb, nlh);
633 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
634 struct fib_rules_ops *ops)
637 struct fib_rule *rule;
640 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
641 if (idx < cb->args[1])
644 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
645 cb->nlh->nlmsg_seq, RTM_NEWRULE,
646 NLM_F_MULTI, ops) < 0)
658 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
660 struct net *net = sock_net(skb->sk);
661 struct fib_rules_ops *ops;
664 family = rtnl_msg_family(cb->nlh);
665 if (family != AF_UNSPEC) {
666 /* Protocol specific dump request */
667 ops = lookup_rules_ops(net, family);
669 return -EAFNOSUPPORT;
671 return dump_rules(skb, cb, ops);
675 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
676 if (idx < cb->args[0] || !try_module_get(ops->owner))
679 if (dump_rules(skb, cb, ops) < 0)
692 static void notify_rule_change(int event, struct fib_rule *rule,
693 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
701 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
705 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
707 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
708 WARN_ON(err == -EMSGSIZE);
713 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
717 rtnl_set_sk_err(net, ops->nlgroup, err);
720 static void attach_rules(struct list_head *rules, struct net_device *dev)
722 struct fib_rule *rule;
724 list_for_each_entry(rule, rules, list) {
725 if (rule->iifindex == -1 &&
726 strcmp(dev->name, rule->iifname) == 0)
727 rule->iifindex = dev->ifindex;
728 if (rule->oifindex == -1 &&
729 strcmp(dev->name, rule->oifname) == 0)
730 rule->oifindex = dev->ifindex;
734 static void detach_rules(struct list_head *rules, struct net_device *dev)
736 struct fib_rule *rule;
738 list_for_each_entry(rule, rules, list) {
739 if (rule->iifindex == dev->ifindex)
741 if (rule->oifindex == dev->ifindex)
747 static int fib_rules_event(struct notifier_block *this, unsigned long event,
750 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
751 struct net *net = dev_net(dev);
752 struct fib_rules_ops *ops;
757 case NETDEV_REGISTER:
758 list_for_each_entry(ops, &net->rules_ops, list)
759 attach_rules(&ops->rules_list, dev);
762 case NETDEV_CHANGENAME:
763 list_for_each_entry(ops, &net->rules_ops, list) {
764 detach_rules(&ops->rules_list, dev);
765 attach_rules(&ops->rules_list, dev);
769 case NETDEV_UNREGISTER:
770 list_for_each_entry(ops, &net->rules_ops, list)
771 detach_rules(&ops->rules_list, dev);
778 static struct notifier_block fib_rules_notifier = {
779 .notifier_call = fib_rules_event,
782 static int __net_init fib_rules_net_init(struct net *net)
784 INIT_LIST_HEAD(&net->rules_ops);
785 spin_lock_init(&net->rules_mod_lock);
789 static struct pernet_operations fib_rules_net_ops = {
790 .init = fib_rules_net_init,
793 static int __init fib_rules_init(void)
796 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
797 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
798 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
800 err = register_pernet_subsys(&fib_rules_net_ops);
804 err = register_netdevice_notifier(&fib_rules_notifier);
806 goto fail_unregister;
811 unregister_pernet_subsys(&fib_rules_net_ops);
813 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
814 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
815 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
819 subsys_initcall(fib_rules_init);