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/list.h>
14 #include <net/net_namespace.h>
16 #include <net/fib_rules.h>
18 static LIST_HEAD(rules_ops);
19 static DEFINE_SPINLOCK(rules_mod_lock);
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;
36 /* The lock is not required here, the list in unreacheable
37 * at the moment this function is called */
38 list_add_tail(&r->list, &ops->rules_list);
41 EXPORT_SYMBOL(fib_default_rule_add);
43 static void notify_rule_change(int event, struct fib_rule *rule,
44 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
47 static struct fib_rules_ops *lookup_rules_ops(int family)
49 struct fib_rules_ops *ops;
52 list_for_each_entry_rcu(ops, &rules_ops, list) {
53 if (ops->family == family) {
54 if (!try_module_get(ops->owner))
65 static void rules_ops_put(struct fib_rules_ops *ops)
68 module_put(ops->owner);
71 static void flush_route_cache(struct fib_rules_ops *ops)
77 int fib_rules_register(struct fib_rules_ops *ops)
80 struct fib_rules_ops *o;
82 if (ops->rule_size < sizeof(struct fib_rule))
85 if (ops->match == NULL || ops->configure == NULL ||
86 ops->compare == NULL || ops->fill == NULL ||
90 spin_lock(&rules_mod_lock);
91 list_for_each_entry(o, &rules_ops, list)
92 if (ops->family == o->family)
95 list_add_tail_rcu(&ops->list, &rules_ops);
98 spin_unlock(&rules_mod_lock);
103 EXPORT_SYMBOL_GPL(fib_rules_register);
105 static void cleanup_ops(struct fib_rules_ops *ops)
107 struct fib_rule *rule, *tmp;
109 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
110 list_del_rcu(&rule->list);
115 int fib_rules_unregister(struct fib_rules_ops *ops)
118 struct fib_rules_ops *o;
120 spin_lock(&rules_mod_lock);
121 list_for_each_entry(o, &rules_ops, list) {
123 list_del_rcu(&o->list);
131 spin_unlock(&rules_mod_lock);
138 EXPORT_SYMBOL_GPL(fib_rules_unregister);
140 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
141 struct flowi *fl, int flags)
145 if (rule->ifindex && (rule->ifindex != fl->iif))
148 if ((rule->mark ^ fl->mark) & rule->mark_mask)
151 ret = ops->match(rule, fl, flags);
153 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
156 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
157 int flags, struct fib_lookup_arg *arg)
159 struct fib_rule *rule;
164 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
166 if (!fib_rule_match(rule, ops, fl, flags))
169 if (rule->action == FR_ACT_GOTO) {
170 struct fib_rule *target;
172 target = rcu_dereference(rule->ctarget);
173 if (target == NULL) {
179 } else if (rule->action == FR_ACT_NOP)
182 err = ops->action(rule, fl, flags, arg);
184 if (err != -EAGAIN) {
198 EXPORT_SYMBOL_GPL(fib_rules_lookup);
200 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
201 struct fib_rules_ops *ops)
206 if (tb[FRA_SRC] == NULL ||
207 frh->src_len > (ops->addr_size * 8) ||
208 nla_len(tb[FRA_SRC]) != ops->addr_size)
212 if (tb[FRA_DST] == NULL ||
213 frh->dst_len > (ops->addr_size * 8) ||
214 nla_len(tb[FRA_DST]) != ops->addr_size)
222 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
224 struct net *net = skb->sk->sk_net;
225 struct fib_rule_hdr *frh = nlmsg_data(nlh);
226 struct fib_rules_ops *ops = NULL;
227 struct fib_rule *rule, *r, *last = NULL;
228 struct nlattr *tb[FRA_MAX+1];
229 int err = -EINVAL, unresolved = 0;
231 if (net != &init_net)
234 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
237 ops = lookup_rules_ops(frh->family);
243 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
247 err = validate_rulemsg(frh, tb, ops);
251 rule = kzalloc(ops->rule_size, GFP_KERNEL);
257 if (tb[FRA_PRIORITY])
258 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
260 if (tb[FRA_IFNAME]) {
261 struct net_device *dev;
264 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
265 dev = __dev_get_by_name(net, rule->ifname);
267 rule->ifindex = dev->ifindex;
270 if (tb[FRA_FWMARK]) {
271 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
273 /* compatibility: if the mark value is non-zero all bits
274 * are compared unless a mask is explicitly specified.
276 rule->mark_mask = 0xFFFFFFFF;
280 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
282 rule->action = frh->action;
283 rule->flags = frh->flags;
284 rule->table = frh_get_table(frh, tb);
286 if (!rule->pref && ops->default_pref)
287 rule->pref = ops->default_pref();
291 if (rule->action != FR_ACT_GOTO)
294 rule->target = nla_get_u32(tb[FRA_GOTO]);
295 /* Backward jumps are prohibited to avoid endless loops */
296 if (rule->target <= rule->pref)
299 list_for_each_entry(r, &ops->rules_list, list) {
300 if (r->pref == rule->target) {
306 if (rule->ctarget == NULL)
308 } else if (rule->action == FR_ACT_GOTO)
311 err = ops->configure(rule, skb, nlh, frh, tb);
315 list_for_each_entry(r, &ops->rules_list, list) {
316 if (r->pref > rule->pref)
323 if (ops->unresolved_rules) {
325 * There are unresolved goto rules in the list, check if
326 * any of them are pointing to this new rule.
328 list_for_each_entry(r, &ops->rules_list, list) {
329 if (r->action == FR_ACT_GOTO &&
330 r->target == rule->pref) {
331 BUG_ON(r->ctarget != NULL);
332 rcu_assign_pointer(r->ctarget, rule);
333 if (--ops->unresolved_rules == 0)
339 if (rule->action == FR_ACT_GOTO)
340 ops->nr_goto_rules++;
343 ops->unresolved_rules++;
346 list_add_rcu(&rule->list, &last->list);
348 list_add_rcu(&rule->list, &ops->rules_list);
350 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
351 flush_route_cache(ops);
362 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
364 struct net *net = skb->sk->sk_net;
365 struct fib_rule_hdr *frh = nlmsg_data(nlh);
366 struct fib_rules_ops *ops = NULL;
367 struct fib_rule *rule, *tmp;
368 struct nlattr *tb[FRA_MAX+1];
371 if (net != &init_net)
374 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
377 ops = lookup_rules_ops(frh->family);
383 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
387 err = validate_rulemsg(frh, tb, ops);
391 list_for_each_entry(rule, &ops->rules_list, list) {
392 if (frh->action && (frh->action != rule->action))
395 if (frh->table && (frh_get_table(frh, tb) != rule->table))
398 if (tb[FRA_PRIORITY] &&
399 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
402 if (tb[FRA_IFNAME] &&
403 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
406 if (tb[FRA_FWMARK] &&
407 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
410 if (tb[FRA_FWMASK] &&
411 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
414 if (!ops->compare(rule, frh, tb))
417 if (rule->flags & FIB_RULE_PERMANENT) {
422 list_del_rcu(&rule->list);
424 if (rule->action == FR_ACT_GOTO)
425 ops->nr_goto_rules--;
428 * Check if this rule is a target to any of them. If so,
429 * disable them. As this operation is eventually very
430 * expensive, it is only performed if goto rules have
431 * actually been added.
433 if (ops->nr_goto_rules > 0) {
434 list_for_each_entry(tmp, &ops->rules_list, list) {
435 if (tmp->ctarget == rule) {
436 rcu_assign_pointer(tmp->ctarget, NULL);
437 ops->unresolved_rules++;
443 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
444 NETLINK_CB(skb).pid);
446 flush_route_cache(ops);
457 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
458 struct fib_rule *rule)
460 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
461 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
462 + nla_total_size(4) /* FRA_PRIORITY */
463 + nla_total_size(4) /* FRA_TABLE */
464 + nla_total_size(4) /* FRA_FWMARK */
465 + nla_total_size(4); /* FRA_FWMASK */
467 if (ops->nlmsg_payload)
468 payload += ops->nlmsg_payload(rule);
473 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
474 u32 pid, u32 seq, int type, int flags,
475 struct fib_rules_ops *ops)
477 struct nlmsghdr *nlh;
478 struct fib_rule_hdr *frh;
480 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
484 frh = nlmsg_data(nlh);
485 frh->table = rule->table;
486 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
489 frh->action = rule->action;
490 frh->flags = rule->flags;
492 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
493 frh->flags |= FIB_RULE_UNRESOLVED;
495 if (rule->ifname[0]) {
496 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
498 if (rule->ifindex == -1)
499 frh->flags |= FIB_RULE_DEV_DETACHED;
503 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
506 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
508 if (rule->mark_mask || rule->mark)
509 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
512 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
514 if (ops->fill(rule, skb, nlh, frh) < 0)
515 goto nla_put_failure;
517 return nlmsg_end(skb, nlh);
520 nlmsg_cancel(skb, nlh);
524 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
525 struct fib_rules_ops *ops)
528 struct fib_rule *rule;
530 list_for_each_entry(rule, &ops->rules_list, list) {
531 if (idx < cb->args[1])
534 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
535 cb->nlh->nlmsg_seq, RTM_NEWRULE,
536 NLM_F_MULTI, ops) < 0)
547 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
549 struct net *net = skb->sk->sk_net;
550 struct fib_rules_ops *ops;
553 if (net != &init_net)
556 family = rtnl_msg_family(cb->nlh);
557 if (family != AF_UNSPEC) {
558 /* Protocol specific dump request */
559 ops = lookup_rules_ops(family);
561 return -EAFNOSUPPORT;
563 return dump_rules(skb, cb, ops);
567 list_for_each_entry_rcu(ops, &rules_ops, list) {
568 if (idx < cb->args[0] || !try_module_get(ops->owner))
571 if (dump_rules(skb, cb, ops) < 0)
584 static void notify_rule_change(int event, struct fib_rule *rule,
585 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
591 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
595 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
597 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
598 WARN_ON(err == -EMSGSIZE);
602 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
605 rtnl_set_sk_err(ops->nlgroup, err);
608 static void attach_rules(struct list_head *rules, struct net_device *dev)
610 struct fib_rule *rule;
612 list_for_each_entry(rule, rules, list) {
613 if (rule->ifindex == -1 &&
614 strcmp(dev->name, rule->ifname) == 0)
615 rule->ifindex = dev->ifindex;
619 static void detach_rules(struct list_head *rules, struct net_device *dev)
621 struct fib_rule *rule;
623 list_for_each_entry(rule, rules, list)
624 if (rule->ifindex == dev->ifindex)
629 static int fib_rules_event(struct notifier_block *this, unsigned long event,
632 struct net_device *dev = ptr;
633 struct fib_rules_ops *ops;
635 if (dev->nd_net != &init_net)
642 case NETDEV_REGISTER:
643 list_for_each_entry(ops, &rules_ops, list)
644 attach_rules(&ops->rules_list, dev);
647 case NETDEV_UNREGISTER:
648 list_for_each_entry(ops, &rules_ops, list)
649 detach_rules(&ops->rules_list, dev);
658 static struct notifier_block fib_rules_notifier = {
659 .notifier_call = fib_rules_event,
662 static int __init fib_rules_init(void)
664 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
665 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
666 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
668 return register_netdevice_notifier(&fib_rules_notifier);
671 subsys_initcall(fib_rules_init);