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 if (tb[FRA_IFNAME] && nla_len(tb[FRA_IFNAME]) > IFNAMSIZ)
167 rule = kzalloc(ops->rule_size, GFP_KERNEL);
173 if (tb[FRA_PRIORITY])
174 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
176 if (tb[FRA_IFNAME]) {
177 struct net_device *dev;
180 if (nla_strlcpy(rule->ifname, tb[FRA_IFNAME],
181 IFNAMSIZ) >= IFNAMSIZ)
184 dev = __dev_get_by_name(rule->ifname);
186 rule->ifindex = dev->ifindex;
189 rule->action = frh->action;
190 rule->flags = frh->flags;
191 rule->table = frh_get_table(frh, tb);
193 if (!rule->pref && ops->default_pref)
194 rule->pref = ops->default_pref();
196 err = ops->configure(rule, skb, nlh, frh, tb);
200 list_for_each_entry(r, ops->rules_list, list) {
201 if (r->pref > rule->pref)
209 list_add_rcu(&rule->list, &last->list);
211 list_add_rcu(&rule->list, ops->rules_list);
213 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
224 int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
226 struct fib_rule_hdr *frh = nlmsg_data(nlh);
227 struct fib_rules_ops *ops = NULL;
228 struct fib_rule *rule;
229 struct nlattr *tb[FRA_MAX+1];
232 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
235 ops = lookup_rules_ops(frh->family);
241 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
245 list_for_each_entry(rule, ops->rules_list, list) {
246 if (frh->action && (frh->action != rule->action))
249 if (frh->table && (frh_get_table(frh, tb) != rule->table))
252 if (tb[FRA_PRIORITY] &&
253 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
256 if (tb[FRA_IFNAME] &&
257 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
260 if (!ops->compare(rule, frh, tb))
263 if (rule->flags & FIB_RULE_PERMANENT) {
268 list_del_rcu(&rule->list);
270 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
271 NETLINK_CB(skb).pid);
283 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
284 u32 pid, u32 seq, int type, int flags,
285 struct fib_rules_ops *ops)
287 struct nlmsghdr *nlh;
288 struct fib_rule_hdr *frh;
290 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
294 frh = nlmsg_data(nlh);
295 frh->table = rule->table;
296 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
299 frh->action = rule->action;
300 frh->flags = rule->flags;
303 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
306 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
308 if (ops->fill(rule, skb, nlh, frh) < 0)
309 goto nla_put_failure;
311 return nlmsg_end(skb, nlh);
314 return nlmsg_cancel(skb, nlh);
317 int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
320 struct fib_rule *rule;
321 struct fib_rules_ops *ops;
323 ops = lookup_rules_ops(family);
325 return -EAFNOSUPPORT;
328 list_for_each_entry(rule, ops->rules_list, list) {
329 if (idx < cb->args[0])
332 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
333 cb->nlh->nlmsg_seq, RTM_NEWRULE,
334 NLM_F_MULTI, ops) < 0)
346 EXPORT_SYMBOL_GPL(fib_rules_dump);
348 static void notify_rule_change(int event, struct fib_rule *rule,
349 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
355 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
359 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
365 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
368 rtnl_set_sk_err(ops->nlgroup, err);
371 static void attach_rules(struct list_head *rules, struct net_device *dev)
373 struct fib_rule *rule;
375 list_for_each_entry(rule, rules, list) {
376 if (rule->ifindex == -1 &&
377 strcmp(dev->name, rule->ifname) == 0)
378 rule->ifindex = dev->ifindex;
382 static void detach_rules(struct list_head *rules, struct net_device *dev)
384 struct fib_rule *rule;
386 list_for_each_entry(rule, rules, list)
387 if (rule->ifindex == dev->ifindex)
392 static int fib_rules_event(struct notifier_block *this, unsigned long event,
395 struct net_device *dev = ptr;
396 struct fib_rules_ops *ops;
402 case NETDEV_REGISTER:
403 list_for_each_entry(ops, &rules_ops, list)
404 attach_rules(ops->rules_list, dev);
407 case NETDEV_UNREGISTER:
408 list_for_each_entry(ops, &rules_ops, list)
409 detach_rules(ops->rules_list, dev);
418 static struct notifier_block fib_rules_notifier = {
419 .notifier_call = fib_rules_event,
422 static int __init fib_rules_init(void)
424 return register_netdevice_notifier(&fib_rules_notifier);
427 subsys_initcall(fib_rules_init);