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) {
135 if (!fib_rule_match(rule, ops, fl, flags))
138 err = ops->action(rule, fl, flags, arg);
139 if (err != -EAGAIN) {
153 EXPORT_SYMBOL_GPL(fib_rules_lookup);
155 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
157 struct fib_rule_hdr *frh = nlmsg_data(nlh);
158 struct fib_rules_ops *ops = NULL;
159 struct fib_rule *rule, *r, *last = NULL;
160 struct nlattr *tb[FRA_MAX+1];
163 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
166 ops = lookup_rules_ops(frh->family);
172 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
176 rule = kzalloc(ops->rule_size, GFP_KERNEL);
182 if (tb[FRA_PRIORITY])
183 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
185 if (tb[FRA_IFNAME]) {
186 struct net_device *dev;
189 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
190 dev = __dev_get_by_name(rule->ifname);
192 rule->ifindex = dev->ifindex;
195 if (tb[FRA_FWMARK]) {
196 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
198 /* compatibility: if the mark value is non-zero all bits
199 * are compared unless a mask is explicitly specified.
201 rule->mark_mask = 0xFFFFFFFF;
205 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
207 rule->action = frh->action;
208 rule->flags = frh->flags;
209 rule->table = frh_get_table(frh, tb);
211 if (!rule->pref && ops->default_pref)
212 rule->pref = ops->default_pref();
214 err = ops->configure(rule, skb, nlh, frh, tb);
218 list_for_each_entry(r, ops->rules_list, list) {
219 if (r->pref > rule->pref)
227 list_add_rcu(&rule->list, &last->list);
229 list_add_rcu(&rule->list, ops->rules_list);
231 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
242 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
244 struct fib_rule_hdr *frh = nlmsg_data(nlh);
245 struct fib_rules_ops *ops = NULL;
246 struct fib_rule *rule;
247 struct nlattr *tb[FRA_MAX+1];
250 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
253 ops = lookup_rules_ops(frh->family);
259 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
263 list_for_each_entry(rule, ops->rules_list, list) {
264 if (frh->action && (frh->action != rule->action))
267 if (frh->table && (frh_get_table(frh, tb) != rule->table))
270 if (tb[FRA_PRIORITY] &&
271 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
274 if (tb[FRA_IFNAME] &&
275 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
278 if (tb[FRA_FWMARK] &&
279 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
282 if (tb[FRA_FWMASK] &&
283 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
286 if (!ops->compare(rule, frh, tb))
289 if (rule->flags & FIB_RULE_PERMANENT) {
294 list_del_rcu(&rule->list);
296 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
297 NETLINK_CB(skb).pid);
309 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
310 struct fib_rule *rule)
312 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
313 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
314 + nla_total_size(4) /* FRA_PRIORITY */
315 + nla_total_size(4) /* FRA_TABLE */
316 + nla_total_size(4) /* FRA_FWMARK */
317 + nla_total_size(4); /* FRA_FWMASK */
319 if (ops->nlmsg_payload)
320 payload += ops->nlmsg_payload(rule);
325 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
326 u32 pid, u32 seq, int type, int flags,
327 struct fib_rules_ops *ops)
329 struct nlmsghdr *nlh;
330 struct fib_rule_hdr *frh;
332 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
336 frh = nlmsg_data(nlh);
337 frh->table = rule->table;
338 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
341 frh->action = rule->action;
342 frh->flags = rule->flags;
345 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
348 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
351 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
353 if (rule->mark_mask || rule->mark)
354 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
356 if (ops->fill(rule, skb, nlh, frh) < 0)
357 goto nla_put_failure;
359 return nlmsg_end(skb, nlh);
362 nlmsg_cancel(skb, nlh);
366 int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
369 struct fib_rule *rule;
370 struct fib_rules_ops *ops;
372 ops = lookup_rules_ops(family);
374 return -EAFNOSUPPORT;
377 list_for_each_entry(rule, ops->rules_list, list) {
378 if (idx < cb->args[0])
381 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
382 cb->nlh->nlmsg_seq, RTM_NEWRULE,
383 NLM_F_MULTI, ops) < 0)
395 EXPORT_SYMBOL_GPL(fib_rules_dump);
397 static void notify_rule_change(int event, struct fib_rule *rule,
398 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
404 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
408 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
410 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
411 WARN_ON(err == -EMSGSIZE);
415 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
418 rtnl_set_sk_err(ops->nlgroup, err);
421 static void attach_rules(struct list_head *rules, struct net_device *dev)
423 struct fib_rule *rule;
425 list_for_each_entry(rule, rules, list) {
426 if (rule->ifindex == -1 &&
427 strcmp(dev->name, rule->ifname) == 0)
428 rule->ifindex = dev->ifindex;
432 static void detach_rules(struct list_head *rules, struct net_device *dev)
434 struct fib_rule *rule;
436 list_for_each_entry(rule, rules, list)
437 if (rule->ifindex == dev->ifindex)
442 static int fib_rules_event(struct notifier_block *this, unsigned long event,
445 struct net_device *dev = ptr;
446 struct fib_rules_ops *ops;
452 case NETDEV_REGISTER:
453 list_for_each_entry(ops, &rules_ops, list)
454 attach_rules(ops->rules_list, dev);
457 case NETDEV_UNREGISTER:
458 list_for_each_entry(ops, &rules_ops, list)
459 detach_rules(ops->rules_list, dev);
468 static struct notifier_block fib_rules_notifier = {
469 .notifier_call = fib_rules_event,
472 static int __init fib_rules_init(void)
474 return register_netdevice_notifier(&fib_rules_notifier);
477 subsys_initcall(fib_rules_init);