K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / net / core / fib_rules.c
blob6205087a407bc79b572ac5dc9c959ffe6c377077
1 /*
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>
9 */
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 int fib_default_rule_add(struct fib_rules_ops *ops,
20 u32 pref, u32 table, u32 flags)
22 struct fib_rule *r;
24 r = kzalloc(ops->rule_size, GFP_KERNEL);
25 if (r == NULL)
26 return -ENOMEM;
28 atomic_set(&r->refcnt, 1);
29 r->action = FR_ACT_TO_TBL;
30 r->pref = pref;
31 r->table = table;
32 r->flags = flags;
34 /* The lock is not required here, the list in unreacheable
35 * at the moment this function is called */
36 list_add_tail(&r->list, &ops->rules_list);
37 return 0;
39 EXPORT_SYMBOL(fib_default_rule_add);
41 static void notify_rule_change(int event, struct fib_rule *rule,
42 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
43 u32 pid);
45 static struct fib_rules_ops *lookup_rules_ops(int family)
47 struct fib_rules_ops *ops;
49 rcu_read_lock();
50 list_for_each_entry_rcu(ops, &rules_ops, list) {
51 if (ops->family == family) {
52 if (!try_module_get(ops->owner))
53 ops = NULL;
54 rcu_read_unlock();
55 return ops;
58 rcu_read_unlock();
60 return NULL;
63 static void rules_ops_put(struct fib_rules_ops *ops)
65 if (ops)
66 module_put(ops->owner);
69 static void flush_route_cache(struct fib_rules_ops *ops)
71 if (ops->flush_cache)
72 ops->flush_cache();
75 int fib_rules_register(struct fib_rules_ops *ops)
77 int err = -EEXIST;
78 struct fib_rules_ops *o;
80 if (ops->rule_size < sizeof(struct fib_rule))
81 return -EINVAL;
83 if (ops->match == NULL || ops->configure == NULL ||
84 ops->compare == NULL || ops->fill == NULL ||
85 ops->action == NULL)
86 return -EINVAL;
88 spin_lock(&rules_mod_lock);
89 list_for_each_entry(o, &rules_ops, list)
90 if (ops->family == o->family)
91 goto errout;
93 list_add_tail_rcu(&ops->list, &rules_ops);
94 err = 0;
95 errout:
96 spin_unlock(&rules_mod_lock);
98 return err;
101 EXPORT_SYMBOL_GPL(fib_rules_register);
103 static void cleanup_ops(struct fib_rules_ops *ops)
105 struct fib_rule *rule, *tmp;
107 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
108 list_del_rcu(&rule->list);
109 fib_rule_put(rule);
113 int fib_rules_unregister(struct fib_rules_ops *ops)
115 int err = 0;
116 struct fib_rules_ops *o;
118 spin_lock(&rules_mod_lock);
119 list_for_each_entry(o, &rules_ops, list) {
120 if (o == ops) {
121 list_del_rcu(&o->list);
122 cleanup_ops(ops);
123 goto out;
127 err = -ENOENT;
128 out:
129 spin_unlock(&rules_mod_lock);
131 synchronize_rcu();
133 return err;
136 EXPORT_SYMBOL_GPL(fib_rules_unregister);
138 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
139 struct flowi *fl, int flags)
141 int ret = 0;
143 if (rule->ifindex && (rule->ifindex != fl->iif))
144 goto out;
146 if ((rule->mark ^ fl->mark) & rule->mark_mask)
147 goto out;
149 ret = ops->match(rule, fl, flags);
150 out:
151 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
154 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
155 int flags, struct fib_lookup_arg *arg)
157 struct fib_rule *rule;
158 int err;
160 rcu_read_lock();
162 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
163 jumped:
164 if (!fib_rule_match(rule, ops, fl, flags))
165 continue;
167 if (rule->action == FR_ACT_GOTO) {
168 struct fib_rule *target;
170 target = rcu_dereference(rule->ctarget);
171 if (target == NULL) {
172 continue;
173 } else {
174 rule = target;
175 goto jumped;
177 } else if (rule->action == FR_ACT_NOP)
178 continue;
179 else
180 err = ops->action(rule, fl, flags, arg);
182 if (err != -EAGAIN) {
183 fib_rule_get(rule);
184 arg->rule = rule;
185 goto out;
189 err = -ESRCH;
190 out:
191 rcu_read_unlock();
193 return err;
196 EXPORT_SYMBOL_GPL(fib_rules_lookup);
198 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
199 struct fib_rules_ops *ops)
201 int err = -EINVAL;
203 if (frh->src_len)
204 if (tb[FRA_SRC] == NULL ||
205 frh->src_len > (ops->addr_size * 8) ||
206 nla_len(tb[FRA_SRC]) != ops->addr_size)
207 goto errout;
209 if (frh->dst_len)
210 if (tb[FRA_DST] == NULL ||
211 frh->dst_len > (ops->addr_size * 8) ||
212 nla_len(tb[FRA_DST]) != ops->addr_size)
213 goto errout;
215 err = 0;
216 errout:
217 return err;
220 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
222 struct fib_rule_hdr *frh = nlmsg_data(nlh);
223 struct fib_rules_ops *ops = NULL;
224 struct fib_rule *rule, *r, *last = NULL;
225 struct nlattr *tb[FRA_MAX+1];
226 int err = -EINVAL, unresolved = 0;
228 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
229 goto errout;
231 ops = lookup_rules_ops(frh->family);
232 if (ops == NULL) {
233 err = EAFNOSUPPORT;
234 goto errout;
237 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
238 if (err < 0)
239 goto errout;
241 err = validate_rulemsg(frh, tb, ops);
242 if (err < 0)
243 goto errout;
245 rule = kzalloc(ops->rule_size, GFP_KERNEL);
246 if (rule == NULL) {
247 err = -ENOMEM;
248 goto errout;
251 if (tb[FRA_PRIORITY])
252 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
254 if (tb[FRA_IFNAME]) {
255 struct net_device *dev;
257 rule->ifindex = -1;
258 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
259 dev = __dev_get_by_name(rule->ifname);
260 if (dev)
261 rule->ifindex = dev->ifindex;
264 if (tb[FRA_FWMARK]) {
265 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
266 if (rule->mark)
267 /* compatibility: if the mark value is non-zero all bits
268 * are compared unless a mask is explicitly specified.
270 rule->mark_mask = 0xFFFFFFFF;
273 if (tb[FRA_FWMASK])
274 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
276 rule->action = frh->action;
277 rule->flags = frh->flags;
278 rule->table = frh_get_table(frh, tb);
280 if (!rule->pref && ops->default_pref)
281 rule->pref = ops->default_pref();
283 err = -EINVAL;
284 if (tb[FRA_GOTO]) {
285 if (rule->action != FR_ACT_GOTO)
286 goto errout_free;
288 rule->target = nla_get_u32(tb[FRA_GOTO]);
289 /* Backward jumps are prohibited to avoid endless loops */
290 if (rule->target <= rule->pref)
291 goto errout_free;
293 list_for_each_entry(r, &ops->rules_list, list) {
294 if (r->pref == rule->target) {
295 rule->ctarget = r;
296 break;
300 if (rule->ctarget == NULL)
301 unresolved = 1;
302 } else if (rule->action == FR_ACT_GOTO)
303 goto errout_free;
305 err = ops->configure(rule, skb, nlh, frh, tb);
306 if (err < 0)
307 goto errout_free;
309 list_for_each_entry(r, &ops->rules_list, list) {
310 if (r->pref > rule->pref)
311 break;
312 last = r;
315 fib_rule_get(rule);
317 if (ops->unresolved_rules) {
319 * There are unresolved goto rules in the list, check if
320 * any of them are pointing to this new rule.
322 list_for_each_entry(r, &ops->rules_list, list) {
323 if (r->action == FR_ACT_GOTO &&
324 r->target == rule->pref) {
325 BUG_ON(r->ctarget != NULL);
326 rcu_assign_pointer(r->ctarget, rule);
327 if (--ops->unresolved_rules == 0)
328 break;
333 if (rule->action == FR_ACT_GOTO)
334 ops->nr_goto_rules++;
336 if (unresolved)
337 ops->unresolved_rules++;
339 if (last)
340 list_add_rcu(&rule->list, &last->list);
341 else
342 list_add_rcu(&rule->list, &ops->rules_list);
344 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
345 flush_route_cache(ops);
346 rules_ops_put(ops);
347 return 0;
349 errout_free:
350 kfree(rule);
351 errout:
352 rules_ops_put(ops);
353 return err;
356 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
358 struct fib_rule_hdr *frh = nlmsg_data(nlh);
359 struct fib_rules_ops *ops = NULL;
360 struct fib_rule *rule, *tmp;
361 struct nlattr *tb[FRA_MAX+1];
362 int err = -EINVAL;
364 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
365 goto errout;
367 ops = lookup_rules_ops(frh->family);
368 if (ops == NULL) {
369 err = EAFNOSUPPORT;
370 goto errout;
373 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
374 if (err < 0)
375 goto errout;
377 err = validate_rulemsg(frh, tb, ops);
378 if (err < 0)
379 goto errout;
381 list_for_each_entry(rule, &ops->rules_list, list) {
382 if (frh->action && (frh->action != rule->action))
383 continue;
385 if (frh->table && (frh_get_table(frh, tb) != rule->table))
386 continue;
388 if (tb[FRA_PRIORITY] &&
389 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
390 continue;
392 if (tb[FRA_IFNAME] &&
393 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
394 continue;
396 if (tb[FRA_FWMARK] &&
397 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
398 continue;
400 if (tb[FRA_FWMASK] &&
401 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
402 continue;
404 if (!ops->compare(rule, frh, tb))
405 continue;
407 if (rule->flags & FIB_RULE_PERMANENT) {
408 err = -EPERM;
409 goto errout;
412 list_del_rcu(&rule->list);
414 if (rule->action == FR_ACT_GOTO)
415 ops->nr_goto_rules--;
418 * Check if this rule is a target to any of them. If so,
419 * disable them. As this operation is eventually very
420 * expensive, it is only performed if goto rules have
421 * actually been added.
423 if (ops->nr_goto_rules > 0) {
424 list_for_each_entry(tmp, &ops->rules_list, list) {
425 if (tmp->ctarget == rule) {
426 rcu_assign_pointer(tmp->ctarget, NULL);
427 ops->unresolved_rules++;
432 synchronize_rcu();
433 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
434 NETLINK_CB(skb).pid);
435 fib_rule_put(rule);
436 flush_route_cache(ops);
437 rules_ops_put(ops);
438 return 0;
441 err = -ENOENT;
442 errout:
443 rules_ops_put(ops);
444 return err;
447 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
448 struct fib_rule *rule)
450 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
451 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
452 + nla_total_size(4) /* FRA_PRIORITY */
453 + nla_total_size(4) /* FRA_TABLE */
454 + nla_total_size(4) /* FRA_FWMARK */
455 + nla_total_size(4); /* FRA_FWMASK */
457 if (ops->nlmsg_payload)
458 payload += ops->nlmsg_payload(rule);
460 return payload;
463 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
464 u32 pid, u32 seq, int type, int flags,
465 struct fib_rules_ops *ops)
467 struct nlmsghdr *nlh;
468 struct fib_rule_hdr *frh;
470 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
471 if (nlh == NULL)
472 return -EMSGSIZE;
474 frh = nlmsg_data(nlh);
475 frh->table = rule->table;
476 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
477 frh->res1 = 0;
478 frh->res2 = 0;
479 frh->action = rule->action;
480 frh->flags = rule->flags;
482 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
483 frh->flags |= FIB_RULE_UNRESOLVED;
485 if (rule->ifname[0]) {
486 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
488 if (rule->ifindex == -1)
489 frh->flags |= FIB_RULE_DEV_DETACHED;
492 if (rule->pref)
493 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
495 if (rule->mark)
496 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
498 if (rule->mark_mask || rule->mark)
499 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
501 if (rule->target)
502 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
504 if (ops->fill(rule, skb, nlh, frh) < 0)
505 goto nla_put_failure;
507 return nlmsg_end(skb, nlh);
509 nla_put_failure:
510 nlmsg_cancel(skb, nlh);
511 return -EMSGSIZE;
514 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
515 struct fib_rules_ops *ops)
517 int idx = 0;
518 struct fib_rule *rule;
520 list_for_each_entry(rule, &ops->rules_list, list) {
521 if (idx < cb->args[1])
522 goto skip;
524 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
525 cb->nlh->nlmsg_seq, RTM_NEWRULE,
526 NLM_F_MULTI, ops) < 0)
527 break;
528 skip:
529 idx++;
531 cb->args[1] = idx;
532 rules_ops_put(ops);
534 return skb->len;
537 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
539 struct fib_rules_ops *ops;
540 int idx = 0, family;
542 family = rtnl_msg_family(cb->nlh);
543 if (family != AF_UNSPEC) {
544 /* Protocol specific dump request */
545 ops = lookup_rules_ops(family);
546 if (ops == NULL)
547 return -EAFNOSUPPORT;
549 return dump_rules(skb, cb, ops);
552 rcu_read_lock();
553 list_for_each_entry_rcu(ops, &rules_ops, list) {
554 if (idx < cb->args[0] || !try_module_get(ops->owner))
555 goto skip;
557 if (dump_rules(skb, cb, ops) < 0)
558 break;
560 cb->args[1] = 0;
561 skip:
562 idx++;
564 rcu_read_unlock();
565 cb->args[0] = idx;
567 return skb->len;
570 static void notify_rule_change(int event, struct fib_rule *rule,
571 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
572 u32 pid)
574 struct sk_buff *skb;
575 int err = -ENOBUFS;
577 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
578 if (skb == NULL)
579 goto errout;
581 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
582 if (err < 0) {
583 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
584 WARN_ON(err == -EMSGSIZE);
585 kfree_skb(skb);
586 goto errout;
588 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
589 errout:
590 if (err < 0)
591 rtnl_set_sk_err(ops->nlgroup, err);
594 static void attach_rules(struct list_head *rules, struct net_device *dev)
596 struct fib_rule *rule;
598 list_for_each_entry(rule, rules, list) {
599 if (rule->ifindex == -1 &&
600 strcmp(dev->name, rule->ifname) == 0)
601 rule->ifindex = dev->ifindex;
605 static void detach_rules(struct list_head *rules, struct net_device *dev)
607 struct fib_rule *rule;
609 list_for_each_entry(rule, rules, list)
610 if (rule->ifindex == dev->ifindex)
611 rule->ifindex = -1;
615 static int fib_rules_event(struct notifier_block *this, unsigned long event,
616 void *ptr)
618 struct net_device *dev = ptr;
619 struct fib_rules_ops *ops;
621 ASSERT_RTNL();
622 rcu_read_lock();
624 switch (event) {
625 case NETDEV_REGISTER:
626 list_for_each_entry(ops, &rules_ops, list)
627 attach_rules(&ops->rules_list, dev);
628 break;
630 case NETDEV_UNREGISTER:
631 list_for_each_entry(ops, &rules_ops, list)
632 detach_rules(&ops->rules_list, dev);
633 break;
636 rcu_read_unlock();
638 return NOTIFY_DONE;
641 static struct notifier_block fib_rules_notifier = {
642 .notifier_call = fib_rules_event,
645 static int __init fib_rules_init(void)
647 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
648 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
649 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
651 return register_netdevice_notifier(&fib_rules_notifier);
654 subsys_initcall(fib_rules_init);