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 <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;
36 r->uid_start = INVALID_UID;
37 r->uid_end = INVALID_UID;
39 r->suppress_prefixlen = -1;
40 r->suppress_ifgroup = -1;
42 /* The lock is not required here, the list in unreacheable
43 * at the moment this function is called */
44 list_add_tail(&r->list, &ops->rules_list);
47 EXPORT_SYMBOL(fib_default_rule_add);
49 static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
51 struct list_head *pos;
52 struct fib_rule *rule;
54 if (!list_empty(&ops->rules_list)) {
55 pos = ops->rules_list.next;
56 if (pos->next != &ops->rules_list) {
57 rule = list_entry(pos->next, struct fib_rule, list);
59 return rule->pref - 1;
66 static void notify_rule_change(int event, struct fib_rule *rule,
67 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
70 static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
72 struct fib_rules_ops *ops;
75 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
76 if (ops->family == family) {
77 if (!try_module_get(ops->owner))
88 static void rules_ops_put(struct fib_rules_ops *ops)
91 module_put(ops->owner);
94 static void flush_route_cache(struct fib_rules_ops *ops)
97 ops->flush_cache(ops);
100 static int __fib_rules_register(struct fib_rules_ops *ops)
103 struct fib_rules_ops *o;
108 if (ops->rule_size < sizeof(struct fib_rule))
111 if (ops->match == NULL || ops->configure == NULL ||
112 ops->compare == NULL || ops->fill == NULL ||
116 spin_lock(&net->rules_mod_lock);
117 list_for_each_entry(o, &net->rules_ops, list)
118 if (ops->family == o->family)
121 list_add_tail_rcu(&ops->list, &net->rules_ops);
124 spin_unlock(&net->rules_mod_lock);
129 struct fib_rules_ops *
130 fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
132 struct fib_rules_ops *ops;
135 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
137 return ERR_PTR(-ENOMEM);
139 INIT_LIST_HEAD(&ops->rules_list);
142 err = __fib_rules_register(ops);
150 EXPORT_SYMBOL_GPL(fib_rules_register);
152 static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
154 struct fib_rule *rule, *tmp;
156 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
157 list_del_rcu(&rule->list);
164 void fib_rules_unregister(struct fib_rules_ops *ops)
166 struct net *net = ops->fro_net;
168 spin_lock(&net->rules_mod_lock);
169 list_del_rcu(&ops->list);
170 spin_unlock(&net->rules_mod_lock);
172 fib_rules_cleanup_ops(ops);
175 EXPORT_SYMBOL_GPL(fib_rules_unregister);
177 static inline kuid_t fib_nl_uid(struct nlattr *nla)
179 return make_kuid(current_user_ns(), nla_get_u32(nla));
182 static int nla_put_uid(struct sk_buff *skb, int idx, kuid_t uid)
184 return nla_put_u32(skb, idx, from_kuid_munged(current_user_ns(), uid));
187 static int fib_uid_range_match(struct flowi *fl, struct fib_rule *rule)
189 return (!uid_valid(rule->uid_start) && !uid_valid(rule->uid_end)) ||
190 (uid_gte(fl->flowi_uid, rule->uid_start) &&
191 uid_lte(fl->flowi_uid, rule->uid_end));
194 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
195 struct flowi *fl, int flags)
199 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
202 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
205 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
208 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
211 if (!fib_uid_range_match(fl, rule))
214 ret = ops->match(rule, fl, flags);
216 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
219 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
220 int flags, struct fib_lookup_arg *arg)
222 struct fib_rule *rule;
227 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
229 if (!fib_rule_match(rule, ops, fl, flags))
232 if (rule->action == FR_ACT_GOTO) {
233 struct fib_rule *target;
235 target = rcu_dereference(rule->ctarget);
236 if (target == NULL) {
242 } else if (rule->action == FR_ACT_NOP)
245 err = ops->action(rule, fl, flags, arg);
247 if (!err && ops->suppress && ops->suppress(rule, arg))
250 if (err != -EAGAIN) {
251 if ((arg->flags & FIB_LOOKUP_NOREF) ||
252 likely(atomic_inc_not_zero(&rule->refcnt))) {
266 EXPORT_SYMBOL_GPL(fib_rules_lookup);
268 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
269 struct fib_rules_ops *ops)
274 if (tb[FRA_SRC] == NULL ||
275 frh->src_len > (ops->addr_size * 8) ||
276 nla_len(tb[FRA_SRC]) != ops->addr_size)
280 if (tb[FRA_DST] == NULL ||
281 frh->dst_len > (ops->addr_size * 8) ||
282 nla_len(tb[FRA_DST]) != ops->addr_size)
290 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
292 struct net *net = sock_net(skb->sk);
293 struct fib_rule_hdr *frh = nlmsg_data(nlh);
294 struct fib_rules_ops *ops = NULL;
295 struct fib_rule *rule, *r, *last = NULL;
296 struct nlattr *tb[FRA_MAX+1];
297 int err = -EINVAL, unresolved = 0;
299 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
302 ops = lookup_rules_ops(net, frh->family);
308 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
312 err = validate_rulemsg(frh, tb, ops);
316 rule = kzalloc(ops->rule_size, GFP_KERNEL);
323 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
324 : fib_default_rule_pref(ops);
326 if (tb[FRA_IIFNAME]) {
327 struct net_device *dev;
330 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
331 dev = __dev_get_by_name(net, rule->iifname);
333 rule->iifindex = dev->ifindex;
336 if (tb[FRA_OIFNAME]) {
337 struct net_device *dev;
340 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
341 dev = __dev_get_by_name(net, rule->oifname);
343 rule->oifindex = dev->ifindex;
346 if (tb[FRA_FWMARK]) {
347 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
349 /* compatibility: if the mark value is non-zero all bits
350 * are compared unless a mask is explicitly specified.
352 rule->mark_mask = 0xFFFFFFFF;
356 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
359 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
361 rule->action = frh->action;
362 rule->flags = frh->flags;
363 rule->table = frh_get_table(frh, tb);
364 if (tb[FRA_SUPPRESS_PREFIXLEN])
365 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
367 rule->suppress_prefixlen = -1;
369 if (tb[FRA_SUPPRESS_IFGROUP])
370 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
372 rule->suppress_ifgroup = -1;
376 if (rule->action != FR_ACT_GOTO)
379 rule->target = nla_get_u32(tb[FRA_GOTO]);
380 /* Backward jumps are prohibited to avoid endless loops */
381 if (rule->target <= rule->pref)
384 list_for_each_entry(r, &ops->rules_list, list) {
385 if (r->pref == rule->target) {
386 RCU_INIT_POINTER(rule->ctarget, r);
391 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
393 } else if (rule->action == FR_ACT_GOTO)
396 /* UID start and end must either both be valid or both unspecified. */
397 rule->uid_start = rule->uid_end = INVALID_UID;
398 if (tb[FRA_UID_START] || tb[FRA_UID_END]) {
399 if (tb[FRA_UID_START] && tb[FRA_UID_END]) {
400 rule->uid_start = fib_nl_uid(tb[FRA_UID_START]);
401 rule->uid_end = fib_nl_uid(tb[FRA_UID_END]);
403 if (!uid_valid(rule->uid_start) ||
404 !uid_valid(rule->uid_end) ||
405 !uid_lte(rule->uid_start, rule->uid_end))
409 err = ops->configure(rule, skb, frh, tb);
413 list_for_each_entry(r, &ops->rules_list, list) {
414 if (r->pref > rule->pref)
422 list_add_rcu(&rule->list, &last->list);
424 list_add_rcu(&rule->list, &ops->rules_list);
426 if (ops->unresolved_rules) {
428 * There are unresolved goto rules in the list, check if
429 * any of them are pointing to this new rule.
431 list_for_each_entry(r, &ops->rules_list, list) {
432 if (r->action == FR_ACT_GOTO &&
433 r->target == rule->pref &&
434 rtnl_dereference(r->ctarget) == NULL) {
435 rcu_assign_pointer(r->ctarget, rule);
436 if (--ops->unresolved_rules == 0)
442 if (rule->action == FR_ACT_GOTO)
443 ops->nr_goto_rules++;
446 ops->unresolved_rules++;
449 ip_tunnel_need_metadata();
451 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
452 flush_route_cache(ops);
463 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
465 struct net *net = sock_net(skb->sk);
466 struct fib_rule_hdr *frh = nlmsg_data(nlh);
467 struct fib_rules_ops *ops = NULL;
468 struct fib_rule *rule, *tmp;
469 struct nlattr *tb[FRA_MAX+1];
472 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
475 ops = lookup_rules_ops(net, frh->family);
481 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
485 err = validate_rulemsg(frh, tb, ops);
489 list_for_each_entry(rule, &ops->rules_list, list) {
490 if (frh->action && (frh->action != rule->action))
493 if (frh_get_table(frh, tb) &&
494 (frh_get_table(frh, tb) != rule->table))
497 if (tb[FRA_PRIORITY] &&
498 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
501 if (tb[FRA_IIFNAME] &&
502 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
505 if (tb[FRA_OIFNAME] &&
506 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
509 if (tb[FRA_FWMARK] &&
510 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
513 if (tb[FRA_FWMASK] &&
514 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
517 if (tb[FRA_TUN_ID] &&
518 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
521 if (tb[FRA_UID_START] &&
522 !uid_eq(rule->uid_start, fib_nl_uid(tb[FRA_UID_START])))
525 if (tb[FRA_UID_END] &&
526 !uid_eq(rule->uid_end, fib_nl_uid(tb[FRA_UID_END])))
529 if (!ops->compare(rule, frh, tb))
532 if (rule->flags & FIB_RULE_PERMANENT) {
538 err = ops->delete(rule);
544 ip_tunnel_unneed_metadata();
546 list_del_rcu(&rule->list);
548 if (rule->action == FR_ACT_GOTO) {
549 ops->nr_goto_rules--;
550 if (rtnl_dereference(rule->ctarget) == NULL)
551 ops->unresolved_rules--;
555 * Check if this rule is a target to any of them. If so,
556 * disable them. As this operation is eventually very
557 * expensive, it is only performed if goto rules have
558 * actually been added.
560 if (ops->nr_goto_rules > 0) {
561 list_for_each_entry(tmp, &ops->rules_list, list) {
562 if (rtnl_dereference(tmp->ctarget) == rule) {
563 RCU_INIT_POINTER(tmp->ctarget, NULL);
564 ops->unresolved_rules++;
569 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
570 NETLINK_CB(skb).portid);
572 flush_route_cache(ops);
583 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
584 struct fib_rule *rule)
586 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
587 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
588 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
589 + nla_total_size(4) /* FRA_PRIORITY */
590 + nla_total_size(4) /* FRA_TABLE */
591 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
592 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
593 + nla_total_size(4) /* FRA_FWMARK */
594 + nla_total_size(4) /* FRA_FWMASK */
595 + nla_total_size(8) /* FRA_TUN_ID */
596 + nla_total_size(4) /* FRA_UID_START */
597 + nla_total_size(4); /* FRA_UID_END */
599 if (ops->nlmsg_payload)
600 payload += ops->nlmsg_payload(rule);
605 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
606 u32 pid, u32 seq, int type, int flags,
607 struct fib_rules_ops *ops)
609 struct nlmsghdr *nlh;
610 struct fib_rule_hdr *frh;
612 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
616 frh = nlmsg_data(nlh);
617 frh->family = ops->family;
618 frh->table = rule->table;
619 if (nla_put_u32(skb, FRA_TABLE, rule->table))
620 goto nla_put_failure;
621 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
622 goto nla_put_failure;
625 frh->action = rule->action;
626 frh->flags = rule->flags;
628 if (rule->action == FR_ACT_GOTO &&
629 rcu_access_pointer(rule->ctarget) == NULL)
630 frh->flags |= FIB_RULE_UNRESOLVED;
632 if (rule->iifname[0]) {
633 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
634 goto nla_put_failure;
635 if (rule->iifindex == -1)
636 frh->flags |= FIB_RULE_IIF_DETACHED;
639 if (rule->oifname[0]) {
640 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
641 goto nla_put_failure;
642 if (rule->oifindex == -1)
643 frh->flags |= FIB_RULE_OIF_DETACHED;
647 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
649 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
650 ((rule->mark_mask || rule->mark) &&
651 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
653 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
655 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id)) ||
656 (uid_valid(rule->uid_start) &&
657 nla_put_uid(skb, FRA_UID_START, rule->uid_start)) ||
658 (uid_valid(rule->uid_end) &&
659 nla_put_uid(skb, FRA_UID_END, rule->uid_end)))
660 goto nla_put_failure;
662 if (rule->suppress_ifgroup != -1) {
663 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
664 goto nla_put_failure;
667 if (ops->fill(rule, skb, frh) < 0)
668 goto nla_put_failure;
674 nlmsg_cancel(skb, nlh);
678 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
679 struct fib_rules_ops *ops)
682 struct fib_rule *rule;
686 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
687 if (idx < cb->args[1])
690 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
691 cb->nlh->nlmsg_seq, RTM_NEWRULE,
705 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
707 struct net *net = sock_net(skb->sk);
708 struct fib_rules_ops *ops;
711 family = rtnl_msg_family(cb->nlh);
712 if (family != AF_UNSPEC) {
713 /* Protocol specific dump request */
714 ops = lookup_rules_ops(net, family);
716 return -EAFNOSUPPORT;
718 dump_rules(skb, cb, ops);
724 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
725 if (idx < cb->args[0] || !try_module_get(ops->owner))
728 if (dump_rules(skb, cb, ops) < 0)
741 static void notify_rule_change(int event, struct fib_rule *rule,
742 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
750 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
754 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
756 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
757 WARN_ON(err == -EMSGSIZE);
762 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
766 rtnl_set_sk_err(net, ops->nlgroup, err);
769 static void attach_rules(struct list_head *rules, struct net_device *dev)
771 struct fib_rule *rule;
773 list_for_each_entry(rule, rules, list) {
774 if (rule->iifindex == -1 &&
775 strcmp(dev->name, rule->iifname) == 0)
776 rule->iifindex = dev->ifindex;
777 if (rule->oifindex == -1 &&
778 strcmp(dev->name, rule->oifname) == 0)
779 rule->oifindex = dev->ifindex;
783 static void detach_rules(struct list_head *rules, struct net_device *dev)
785 struct fib_rule *rule;
787 list_for_each_entry(rule, rules, list) {
788 if (rule->iifindex == dev->ifindex)
790 if (rule->oifindex == dev->ifindex)
796 static int fib_rules_event(struct notifier_block *this, unsigned long event,
799 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
800 struct net *net = dev_net(dev);
801 struct fib_rules_ops *ops;
806 case NETDEV_REGISTER:
807 list_for_each_entry(ops, &net->rules_ops, list)
808 attach_rules(&ops->rules_list, dev);
811 case NETDEV_CHANGENAME:
812 list_for_each_entry(ops, &net->rules_ops, list) {
813 detach_rules(&ops->rules_list, dev);
814 attach_rules(&ops->rules_list, dev);
818 case NETDEV_UNREGISTER:
819 list_for_each_entry(ops, &net->rules_ops, list)
820 detach_rules(&ops->rules_list, dev);
827 static struct notifier_block fib_rules_notifier = {
828 .notifier_call = fib_rules_event,
831 static int __net_init fib_rules_net_init(struct net *net)
833 INIT_LIST_HEAD(&net->rules_ops);
834 spin_lock_init(&net->rules_mod_lock);
838 static struct pernet_operations fib_rules_net_ops = {
839 .init = fib_rules_net_init,
842 static int __init fib_rules_init(void)
845 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
846 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
847 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
849 err = register_pernet_subsys(&fib_rules_net_ops);
853 err = register_netdevice_notifier(&fib_rules_notifier);
855 goto fail_unregister;
860 unregister_pernet_subsys(&fib_rules_net_ops);
862 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
863 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
864 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
868 subsys_initcall(fib_rules_init);