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/config.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <net/fib_rules.h>
17 static LIST_HEAD(rules_ops);
18 static DEFINE_SPINLOCK(rules_mod_lock);
20 static void notify_rule_change(int event, struct fib_rule *rule,
21 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
24 static struct fib_rules_ops *lookup_rules_ops(int family)
26 struct fib_rules_ops *ops;
29 list_for_each_entry_rcu(ops, &rules_ops, list) {
30 if (ops->family == family) {
31 if (!try_module_get(ops->owner))
42 static void rules_ops_put(struct fib_rules_ops *ops)
45 module_put(ops->owner);
48 int fib_rules_register(struct fib_rules_ops *ops)
51 struct fib_rules_ops *o;
53 if (ops->rule_size < sizeof(struct fib_rule))
56 if (ops->match == NULL || ops->configure == NULL ||
57 ops->compare == NULL || ops->fill == NULL ||
61 spin_lock(&rules_mod_lock);
62 list_for_each_entry(o, &rules_ops, list)
63 if (ops->family == o->family)
66 list_add_tail_rcu(&ops->list, &rules_ops);
69 spin_unlock(&rules_mod_lock);
74 EXPORT_SYMBOL_GPL(fib_rules_register);
76 static void cleanup_ops(struct fib_rules_ops *ops)
78 struct fib_rule *rule, *tmp;
80 list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
81 list_del_rcu(&rule->list);
86 int fib_rules_unregister(struct fib_rules_ops *ops)
89 struct fib_rules_ops *o;
91 spin_lock(&rules_mod_lock);
92 list_for_each_entry(o, &rules_ops, list) {
94 list_del_rcu(&o->list);
102 spin_unlock(&rules_mod_lock);
109 EXPORT_SYMBOL_GPL(fib_rules_unregister);
111 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
112 int flags, struct fib_lookup_arg *arg)
114 struct fib_rule *rule;
119 list_for_each_entry_rcu(rule, ops->rules_list, list) {
120 if (rule->ifindex && (rule->ifindex != fl->iif))
123 if (!ops->match(rule, fl, flags))
126 err = ops->action(rule, fl, flags, arg);
127 if (err != -EAGAIN) {
141 EXPORT_SYMBOL_GPL(fib_rules_lookup);
143 int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
145 struct fib_rule_hdr *frh = nlmsg_data(nlh);
146 struct fib_rules_ops *ops = NULL;
147 struct fib_rule *rule, *r, *last = NULL;
148 struct nlattr *tb[FRA_MAX+1];
151 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
154 ops = lookup_rules_ops(frh->family);
160 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
164 rule = kzalloc(ops->rule_size, GFP_KERNEL);
170 if (tb[FRA_PRIORITY])
171 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
173 if (tb[FRA_IFNAME]) {
174 struct net_device *dev;
177 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
178 dev = __dev_get_by_name(rule->ifname);
180 rule->ifindex = dev->ifindex;
183 rule->action = frh->action;
184 rule->flags = frh->flags;
185 rule->table = frh_get_table(frh, tb);
187 if (!rule->pref && ops->default_pref)
188 rule->pref = ops->default_pref();
190 err = ops->configure(rule, skb, nlh, frh, tb);
194 list_for_each_entry(r, ops->rules_list, list) {
195 if (r->pref > rule->pref)
203 list_add_rcu(&rule->list, &last->list);
205 list_add_rcu(&rule->list, ops->rules_list);
207 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
218 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
220 struct fib_rule_hdr *frh = nlmsg_data(nlh);
221 struct fib_rules_ops *ops = NULL;
222 struct fib_rule *rule;
223 struct nlattr *tb[FRA_MAX+1];
226 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
229 ops = lookup_rules_ops(frh->family);
235 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
239 list_for_each_entry(rule, ops->rules_list, list) {
240 if (frh->action && (frh->action != rule->action))
243 if (frh->table && (frh_get_table(frh, tb) != rule->table))
246 if (tb[FRA_PRIORITY] &&
247 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
250 if (tb[FRA_IFNAME] &&
251 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
254 if (!ops->compare(rule, frh, tb))
257 if (rule->flags & FIB_RULE_PERMANENT) {
262 list_del_rcu(&rule->list);
264 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
265 NETLINK_CB(skb).pid);
277 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
278 u32 pid, u32 seq, int type, int flags,
279 struct fib_rules_ops *ops)
281 struct nlmsghdr *nlh;
282 struct fib_rule_hdr *frh;
284 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
288 frh = nlmsg_data(nlh);
289 frh->table = rule->table;
290 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
293 frh->action = rule->action;
294 frh->flags = rule->flags;
297 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
300 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
302 if (ops->fill(rule, skb, nlh, frh) < 0)
303 goto nla_put_failure;
305 return nlmsg_end(skb, nlh);
308 return nlmsg_cancel(skb, nlh);
311 int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
314 struct fib_rule *rule;
315 struct fib_rules_ops *ops;
317 ops = lookup_rules_ops(family);
319 return -EAFNOSUPPORT;
322 list_for_each_entry(rule, ops->rules_list, list) {
323 if (idx < cb->args[0])
326 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
327 cb->nlh->nlmsg_seq, RTM_NEWRULE,
328 NLM_F_MULTI, ops) < 0)
340 EXPORT_SYMBOL_GPL(fib_rules_dump);
342 static void notify_rule_change(int event, struct fib_rule *rule,
343 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
349 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
353 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
359 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
362 rtnl_set_sk_err(ops->nlgroup, err);
365 static void attach_rules(struct list_head *rules, struct net_device *dev)
367 struct fib_rule *rule;
369 list_for_each_entry(rule, rules, list) {
370 if (rule->ifindex == -1 &&
371 strcmp(dev->name, rule->ifname) == 0)
372 rule->ifindex = dev->ifindex;
376 static void detach_rules(struct list_head *rules, struct net_device *dev)
378 struct fib_rule *rule;
380 list_for_each_entry(rule, rules, list)
381 if (rule->ifindex == dev->ifindex)
386 static int fib_rules_event(struct notifier_block *this, unsigned long event,
389 struct net_device *dev = ptr;
390 struct fib_rules_ops *ops;
396 case NETDEV_REGISTER:
397 list_for_each_entry(ops, &rules_ops, list)
398 attach_rules(ops->rules_list, dev);
401 case NETDEV_UNREGISTER:
402 list_for_each_entry(ops, &rules_ops, list)
403 detach_rules(ops->rules_list, dev);
412 static struct notifier_block fib_rules_notifier = {
413 .notifier_call = fib_rules_event,
416 static int __init fib_rules_init(void)
418 return register_netdevice_notifier(&fib_rules_notifier);
421 subsys_initcall(fib_rules_init);