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/fib_rules.h>
16 static LIST_HEAD(rules_ops);
17 static DEFINE_SPINLOCK(rules_mod_lock);
19 static void notify_rule_change(int event, struct fib_rule *rule,
20 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
23 static struct fib_rules_ops *lookup_rules_ops(int family)
25 struct fib_rules_ops *ops;
28 list_for_each_entry_rcu(ops, &rules_ops, list) {
29 if (ops->family == family) {
30 if (!try_module_get(ops->owner))
41 static void rules_ops_put(struct fib_rules_ops *ops)
44 module_put(ops->owner);
47 int fib_rules_register(struct fib_rules_ops *ops)
50 struct fib_rules_ops *o;
52 if (ops->rule_size < sizeof(struct fib_rule))
55 if (ops->match == NULL || ops->configure == NULL ||
56 ops->compare == NULL || ops->fill == NULL ||
60 spin_lock(&rules_mod_lock);
61 list_for_each_entry(o, &rules_ops, list)
62 if (ops->family == o->family)
65 list_add_tail_rcu(&ops->list, &rules_ops);
68 spin_unlock(&rules_mod_lock);
73 EXPORT_SYMBOL_GPL(fib_rules_register);
75 static void cleanup_ops(struct fib_rules_ops *ops)
77 struct fib_rule *rule, *tmp;
79 list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
80 list_del_rcu(&rule->list);
85 int fib_rules_unregister(struct fib_rules_ops *ops)
88 struct fib_rules_ops *o;
90 spin_lock(&rules_mod_lock);
91 list_for_each_entry(o, &rules_ops, list) {
93 list_del_rcu(&o->list);
101 spin_unlock(&rules_mod_lock);
108 EXPORT_SYMBOL_GPL(fib_rules_unregister);
110 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
111 struct flowi *fl, int flags)
115 if (rule->ifindex && (rule->ifindex != fl->iif))
118 if ((rule->mark ^ fl->mark) & rule->mark_mask)
121 ret = ops->match(rule, fl, flags);
123 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
126 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
127 int flags, struct fib_lookup_arg *arg)
129 struct fib_rule *rule;
134 list_for_each_entry_rcu(rule, ops->rules_list, list) {
136 if (!fib_rule_match(rule, ops, fl, flags))
139 if (rule->action == FR_ACT_GOTO) {
140 struct fib_rule *target;
142 target = rcu_dereference(rule->ctarget);
143 if (target == NULL) {
150 err = ops->action(rule, fl, flags, arg);
152 if (err != -EAGAIN) {
166 EXPORT_SYMBOL_GPL(fib_rules_lookup);
168 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
169 struct fib_rules_ops *ops)
174 if (tb[FRA_SRC] == NULL ||
175 frh->src_len > (ops->addr_size * 8) ||
176 nla_len(tb[FRA_SRC]) != ops->addr_size)
180 if (tb[FRA_DST] == NULL ||
181 frh->dst_len > (ops->addr_size * 8) ||
182 nla_len(tb[FRA_DST]) != ops->addr_size)
190 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
192 struct fib_rule_hdr *frh = nlmsg_data(nlh);
193 struct fib_rules_ops *ops = NULL;
194 struct fib_rule *rule, *r, *last = NULL;
195 struct nlattr *tb[FRA_MAX+1];
196 int err = -EINVAL, unresolved = 0;
198 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
201 ops = lookup_rules_ops(frh->family);
207 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
211 err = validate_rulemsg(frh, tb, ops);
215 rule = kzalloc(ops->rule_size, GFP_KERNEL);
221 if (tb[FRA_PRIORITY])
222 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
224 if (tb[FRA_IFNAME]) {
225 struct net_device *dev;
228 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
229 dev = __dev_get_by_name(rule->ifname);
231 rule->ifindex = dev->ifindex;
234 if (tb[FRA_FWMARK]) {
235 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
237 /* compatibility: if the mark value is non-zero all bits
238 * are compared unless a mask is explicitly specified.
240 rule->mark_mask = 0xFFFFFFFF;
244 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
246 rule->action = frh->action;
247 rule->flags = frh->flags;
248 rule->table = frh_get_table(frh, tb);
250 if (!rule->pref && ops->default_pref)
251 rule->pref = ops->default_pref();
255 if (rule->action != FR_ACT_GOTO)
258 rule->target = nla_get_u32(tb[FRA_GOTO]);
259 /* Backward jumps are prohibited to avoid endless loops */
260 if (rule->target <= rule->pref)
263 list_for_each_entry(r, ops->rules_list, list) {
264 if (r->pref == rule->target) {
270 if (rule->ctarget == NULL)
272 } else if (rule->action == FR_ACT_GOTO)
275 err = ops->configure(rule, skb, nlh, frh, tb);
279 list_for_each_entry(r, ops->rules_list, list) {
280 if (r->pref > rule->pref)
287 if (ops->unresolved_rules) {
289 * There are unresolved goto rules in the list, check if
290 * any of them are pointing to this new rule.
292 list_for_each_entry(r, ops->rules_list, list) {
293 if (r->action == FR_ACT_GOTO &&
294 r->target == rule->pref) {
295 BUG_ON(r->ctarget != NULL);
296 rcu_assign_pointer(r->ctarget, rule);
297 if (--ops->unresolved_rules == 0)
303 if (rule->action == FR_ACT_GOTO)
304 ops->nr_goto_rules++;
307 ops->unresolved_rules++;
310 list_add_rcu(&rule->list, &last->list);
312 list_add_rcu(&rule->list, ops->rules_list);
314 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
325 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
327 struct fib_rule_hdr *frh = nlmsg_data(nlh);
328 struct fib_rules_ops *ops = NULL;
329 struct fib_rule *rule, *tmp;
330 struct nlattr *tb[FRA_MAX+1];
333 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
336 ops = lookup_rules_ops(frh->family);
342 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
346 err = validate_rulemsg(frh, tb, ops);
350 list_for_each_entry(rule, ops->rules_list, list) {
351 if (frh->action && (frh->action != rule->action))
354 if (frh->table && (frh_get_table(frh, tb) != rule->table))
357 if (tb[FRA_PRIORITY] &&
358 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
361 if (tb[FRA_IFNAME] &&
362 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
365 if (tb[FRA_FWMARK] &&
366 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
369 if (tb[FRA_FWMASK] &&
370 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
373 if (!ops->compare(rule, frh, tb))
376 if (rule->flags & FIB_RULE_PERMANENT) {
381 list_del_rcu(&rule->list);
383 if (rule->action == FR_ACT_GOTO)
384 ops->nr_goto_rules--;
387 * Check if this rule is a target to any of them. If so,
388 * disable them. As this operation is eventually very
389 * expensive, it is only performed if goto rules have
390 * actually been added.
392 if (ops->nr_goto_rules > 0) {
393 list_for_each_entry(tmp, ops->rules_list, list) {
394 if (tmp->ctarget == rule) {
395 rcu_assign_pointer(tmp->ctarget, NULL);
396 ops->unresolved_rules++;
402 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
403 NETLINK_CB(skb).pid);
415 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
416 struct fib_rule *rule)
418 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
419 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
420 + nla_total_size(4) /* FRA_PRIORITY */
421 + nla_total_size(4) /* FRA_TABLE */
422 + nla_total_size(4) /* FRA_FWMARK */
423 + nla_total_size(4); /* FRA_FWMASK */
425 if (ops->nlmsg_payload)
426 payload += ops->nlmsg_payload(rule);
431 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
432 u32 pid, u32 seq, int type, int flags,
433 struct fib_rules_ops *ops)
435 struct nlmsghdr *nlh;
436 struct fib_rule_hdr *frh;
438 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
442 frh = nlmsg_data(nlh);
443 frh->table = rule->table;
444 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
447 frh->action = rule->action;
448 frh->flags = rule->flags;
450 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
451 frh->flags |= FIB_RULE_UNRESOLVED;
454 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
457 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
460 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
462 if (rule->mark_mask || rule->mark)
463 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
466 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
468 if (ops->fill(rule, skb, nlh, frh) < 0)
469 goto nla_put_failure;
471 return nlmsg_end(skb, nlh);
474 nlmsg_cancel(skb, nlh);
478 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
479 struct fib_rules_ops *ops)
482 struct fib_rule *rule;
485 list_for_each_entry_rcu(rule, ops->rules_list, list) {
486 if (idx < cb->args[1])
489 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
490 cb->nlh->nlmsg_seq, RTM_NEWRULE,
491 NLM_F_MULTI, ops) < 0)
503 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
505 struct fib_rules_ops *ops;
508 family = rtnl_msg_family(cb->nlh);
509 if (family != AF_UNSPEC) {
510 /* Protocol specific dump request */
511 ops = lookup_rules_ops(family);
513 return -EAFNOSUPPORT;
515 return dump_rules(skb, cb, ops);
519 list_for_each_entry_rcu(ops, &rules_ops, list) {
520 if (idx < cb->args[0] || !try_module_get(ops->owner))
523 if (dump_rules(skb, cb, ops) < 0)
536 static void notify_rule_change(int event, struct fib_rule *rule,
537 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
543 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
547 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
549 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
550 WARN_ON(err == -EMSGSIZE);
554 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
557 rtnl_set_sk_err(ops->nlgroup, err);
560 static void attach_rules(struct list_head *rules, struct net_device *dev)
562 struct fib_rule *rule;
564 list_for_each_entry(rule, rules, list) {
565 if (rule->ifindex == -1 &&
566 strcmp(dev->name, rule->ifname) == 0)
567 rule->ifindex = dev->ifindex;
571 static void detach_rules(struct list_head *rules, struct net_device *dev)
573 struct fib_rule *rule;
575 list_for_each_entry(rule, rules, list)
576 if (rule->ifindex == dev->ifindex)
581 static int fib_rules_event(struct notifier_block *this, unsigned long event,
584 struct net_device *dev = ptr;
585 struct fib_rules_ops *ops;
591 case NETDEV_REGISTER:
592 list_for_each_entry(ops, &rules_ops, list)
593 attach_rules(ops->rules_list, dev);
596 case NETDEV_UNREGISTER:
597 list_for_each_entry(ops, &rules_ops, list)
598 detach_rules(ops->rules_list, dev);
607 static struct notifier_block fib_rules_notifier = {
608 .notifier_call = fib_rules_event,
611 static int __init fib_rules_init(void)
613 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
614 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
615 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
617 return register_netdevice_notifier(&fib_rules_notifier);
620 subsys_initcall(fib_rules_init);