netfilter: nf_conntrack: add support for "conntrack zones"
[linux/fpc-iii.git] / net / core / fib_rules.c
blob9a24377146bf0dc25163e96bfb3c9b6c0701ab40
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/net_namespace.h>
15 #include <net/sock.h>
16 #include <net/fib_rules.h>
18 int fib_default_rule_add(struct fib_rules_ops *ops,
19 u32 pref, u32 table, u32 flags)
21 struct fib_rule *r;
23 r = kzalloc(ops->rule_size, GFP_KERNEL);
24 if (r == NULL)
25 return -ENOMEM;
27 atomic_set(&r->refcnt, 1);
28 r->action = FR_ACT_TO_TBL;
29 r->pref = pref;
30 r->table = table;
31 r->flags = flags;
32 r->fr_net = hold_net(ops->fro_net);
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(struct net *net, int family)
47 struct fib_rules_ops *ops;
49 rcu_read_lock();
50 list_for_each_entry_rcu(ops, &net->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(ops);
75 static int __fib_rules_register(struct fib_rules_ops *ops)
77 int err = -EEXIST;
78 struct fib_rules_ops *o;
79 struct net *net;
81 net = ops->fro_net;
83 if (ops->rule_size < sizeof(struct fib_rule))
84 return -EINVAL;
86 if (ops->match == NULL || ops->configure == NULL ||
87 ops->compare == NULL || ops->fill == NULL ||
88 ops->action == NULL)
89 return -EINVAL;
91 spin_lock(&net->rules_mod_lock);
92 list_for_each_entry(o, &net->rules_ops, list)
93 if (ops->family == o->family)
94 goto errout;
96 hold_net(net);
97 list_add_tail_rcu(&ops->list, &net->rules_ops);
98 err = 0;
99 errout:
100 spin_unlock(&net->rules_mod_lock);
102 return err;
105 struct fib_rules_ops *
106 fib_rules_register(struct fib_rules_ops *tmpl, struct net *net)
108 struct fib_rules_ops *ops;
109 int err;
111 ops = kmemdup(tmpl, sizeof (*ops), GFP_KERNEL);
112 if (ops == NULL)
113 return ERR_PTR(-ENOMEM);
115 INIT_LIST_HEAD(&ops->rules_list);
116 ops->fro_net = net;
118 err = __fib_rules_register(ops);
119 if (err) {
120 kfree(ops);
121 ops = ERR_PTR(err);
124 return ops;
127 EXPORT_SYMBOL_GPL(fib_rules_register);
129 void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
131 struct fib_rule *rule, *tmp;
133 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
134 list_del_rcu(&rule->list);
135 fib_rule_put(rule);
138 EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
140 static void fib_rules_put_rcu(struct rcu_head *head)
142 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
143 struct net *net = ops->fro_net;
145 release_net(net);
146 kfree(ops);
149 void fib_rules_unregister(struct fib_rules_ops *ops)
151 struct net *net = ops->fro_net;
153 spin_lock(&net->rules_mod_lock);
154 list_del_rcu(&ops->list);
155 fib_rules_cleanup_ops(ops);
156 spin_unlock(&net->rules_mod_lock);
158 call_rcu(&ops->rcu, fib_rules_put_rcu);
161 EXPORT_SYMBOL_GPL(fib_rules_unregister);
163 static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
164 struct flowi *fl, int flags)
166 int ret = 0;
168 if (rule->iifindex && (rule->iifindex != fl->iif))
169 goto out;
171 if (rule->oifindex && (rule->oifindex != fl->oif))
172 goto out;
174 if ((rule->mark ^ fl->mark) & rule->mark_mask)
175 goto out;
177 ret = ops->match(rule, fl, flags);
178 out:
179 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
182 int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
183 int flags, struct fib_lookup_arg *arg)
185 struct fib_rule *rule;
186 int err;
188 rcu_read_lock();
190 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
191 jumped:
192 if (!fib_rule_match(rule, ops, fl, flags))
193 continue;
195 if (rule->action == FR_ACT_GOTO) {
196 struct fib_rule *target;
198 target = rcu_dereference(rule->ctarget);
199 if (target == NULL) {
200 continue;
201 } else {
202 rule = target;
203 goto jumped;
205 } else if (rule->action == FR_ACT_NOP)
206 continue;
207 else
208 err = ops->action(rule, fl, flags, arg);
210 if (err != -EAGAIN) {
211 fib_rule_get(rule);
212 arg->rule = rule;
213 goto out;
217 err = -ESRCH;
218 out:
219 rcu_read_unlock();
221 return err;
224 EXPORT_SYMBOL_GPL(fib_rules_lookup);
226 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
227 struct fib_rules_ops *ops)
229 int err = -EINVAL;
231 if (frh->src_len)
232 if (tb[FRA_SRC] == NULL ||
233 frh->src_len > (ops->addr_size * 8) ||
234 nla_len(tb[FRA_SRC]) != ops->addr_size)
235 goto errout;
237 if (frh->dst_len)
238 if (tb[FRA_DST] == NULL ||
239 frh->dst_len > (ops->addr_size * 8) ||
240 nla_len(tb[FRA_DST]) != ops->addr_size)
241 goto errout;
243 err = 0;
244 errout:
245 return err;
248 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
250 struct net *net = sock_net(skb->sk);
251 struct fib_rule_hdr *frh = nlmsg_data(nlh);
252 struct fib_rules_ops *ops = NULL;
253 struct fib_rule *rule, *r, *last = NULL;
254 struct nlattr *tb[FRA_MAX+1];
255 int err = -EINVAL, unresolved = 0;
257 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
258 goto errout;
260 ops = lookup_rules_ops(net, frh->family);
261 if (ops == NULL) {
262 err = -EAFNOSUPPORT;
263 goto errout;
266 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
267 if (err < 0)
268 goto errout;
270 err = validate_rulemsg(frh, tb, ops);
271 if (err < 0)
272 goto errout;
274 rule = kzalloc(ops->rule_size, GFP_KERNEL);
275 if (rule == NULL) {
276 err = -ENOMEM;
277 goto errout;
279 rule->fr_net = hold_net(net);
281 if (tb[FRA_PRIORITY])
282 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
284 if (tb[FRA_IIFNAME]) {
285 struct net_device *dev;
287 rule->iifindex = -1;
288 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
289 dev = __dev_get_by_name(net, rule->iifname);
290 if (dev)
291 rule->iifindex = dev->ifindex;
294 if (tb[FRA_OIFNAME]) {
295 struct net_device *dev;
297 rule->oifindex = -1;
298 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
299 dev = __dev_get_by_name(net, rule->oifname);
300 if (dev)
301 rule->oifindex = dev->ifindex;
304 if (tb[FRA_FWMARK]) {
305 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
306 if (rule->mark)
307 /* compatibility: if the mark value is non-zero all bits
308 * are compared unless a mask is explicitly specified.
310 rule->mark_mask = 0xFFFFFFFF;
313 if (tb[FRA_FWMASK])
314 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
316 rule->action = frh->action;
317 rule->flags = frh->flags;
318 rule->table = frh_get_table(frh, tb);
320 if (!tb[FRA_PRIORITY] && ops->default_pref)
321 rule->pref = ops->default_pref(ops);
323 err = -EINVAL;
324 if (tb[FRA_GOTO]) {
325 if (rule->action != FR_ACT_GOTO)
326 goto errout_free;
328 rule->target = nla_get_u32(tb[FRA_GOTO]);
329 /* Backward jumps are prohibited to avoid endless loops */
330 if (rule->target <= rule->pref)
331 goto errout_free;
333 list_for_each_entry(r, &ops->rules_list, list) {
334 if (r->pref == rule->target) {
335 rule->ctarget = r;
336 break;
340 if (rule->ctarget == NULL)
341 unresolved = 1;
342 } else if (rule->action == FR_ACT_GOTO)
343 goto errout_free;
345 err = ops->configure(rule, skb, frh, tb);
346 if (err < 0)
347 goto errout_free;
349 list_for_each_entry(r, &ops->rules_list, list) {
350 if (r->pref > rule->pref)
351 break;
352 last = r;
355 fib_rule_get(rule);
357 if (ops->unresolved_rules) {
359 * There are unresolved goto rules in the list, check if
360 * any of them are pointing to this new rule.
362 list_for_each_entry(r, &ops->rules_list, list) {
363 if (r->action == FR_ACT_GOTO &&
364 r->target == rule->pref) {
365 BUG_ON(r->ctarget != NULL);
366 rcu_assign_pointer(r->ctarget, rule);
367 if (--ops->unresolved_rules == 0)
368 break;
373 if (rule->action == FR_ACT_GOTO)
374 ops->nr_goto_rules++;
376 if (unresolved)
377 ops->unresolved_rules++;
379 if (last)
380 list_add_rcu(&rule->list, &last->list);
381 else
382 list_add_rcu(&rule->list, &ops->rules_list);
384 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
385 flush_route_cache(ops);
386 rules_ops_put(ops);
387 return 0;
389 errout_free:
390 release_net(rule->fr_net);
391 kfree(rule);
392 errout:
393 rules_ops_put(ops);
394 return err;
397 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
399 struct net *net = sock_net(skb->sk);
400 struct fib_rule_hdr *frh = nlmsg_data(nlh);
401 struct fib_rules_ops *ops = NULL;
402 struct fib_rule *rule, *tmp;
403 struct nlattr *tb[FRA_MAX+1];
404 int err = -EINVAL;
406 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
407 goto errout;
409 ops = lookup_rules_ops(net, frh->family);
410 if (ops == NULL) {
411 err = -EAFNOSUPPORT;
412 goto errout;
415 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
416 if (err < 0)
417 goto errout;
419 err = validate_rulemsg(frh, tb, ops);
420 if (err < 0)
421 goto errout;
423 list_for_each_entry(rule, &ops->rules_list, list) {
424 if (frh->action && (frh->action != rule->action))
425 continue;
427 if (frh->table && (frh_get_table(frh, tb) != rule->table))
428 continue;
430 if (tb[FRA_PRIORITY] &&
431 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
432 continue;
434 if (tb[FRA_IIFNAME] &&
435 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
436 continue;
438 if (tb[FRA_OIFNAME] &&
439 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
440 continue;
442 if (tb[FRA_FWMARK] &&
443 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
444 continue;
446 if (tb[FRA_FWMASK] &&
447 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
448 continue;
450 if (!ops->compare(rule, frh, tb))
451 continue;
453 if (rule->flags & FIB_RULE_PERMANENT) {
454 err = -EPERM;
455 goto errout;
458 list_del_rcu(&rule->list);
460 if (rule->action == FR_ACT_GOTO)
461 ops->nr_goto_rules--;
464 * Check if this rule is a target to any of them. If so,
465 * disable them. As this operation is eventually very
466 * expensive, it is only performed if goto rules have
467 * actually been added.
469 if (ops->nr_goto_rules > 0) {
470 list_for_each_entry(tmp, &ops->rules_list, list) {
471 if (tmp->ctarget == rule) {
472 rcu_assign_pointer(tmp->ctarget, NULL);
473 ops->unresolved_rules++;
478 synchronize_rcu();
479 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
480 NETLINK_CB(skb).pid);
481 fib_rule_put(rule);
482 flush_route_cache(ops);
483 rules_ops_put(ops);
484 return 0;
487 err = -ENOENT;
488 errout:
489 rules_ops_put(ops);
490 return err;
493 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
494 struct fib_rule *rule)
496 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
497 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
498 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
499 + nla_total_size(4) /* FRA_PRIORITY */
500 + nla_total_size(4) /* FRA_TABLE */
501 + nla_total_size(4) /* FRA_FWMARK */
502 + nla_total_size(4); /* FRA_FWMASK */
504 if (ops->nlmsg_payload)
505 payload += ops->nlmsg_payload(rule);
507 return payload;
510 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
511 u32 pid, u32 seq, int type, int flags,
512 struct fib_rules_ops *ops)
514 struct nlmsghdr *nlh;
515 struct fib_rule_hdr *frh;
517 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
518 if (nlh == NULL)
519 return -EMSGSIZE;
521 frh = nlmsg_data(nlh);
522 frh->table = rule->table;
523 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
524 frh->res1 = 0;
525 frh->res2 = 0;
526 frh->action = rule->action;
527 frh->flags = rule->flags;
529 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
530 frh->flags |= FIB_RULE_UNRESOLVED;
532 if (rule->iifname[0]) {
533 NLA_PUT_STRING(skb, FRA_IIFNAME, rule->iifname);
535 if (rule->iifindex == -1)
536 frh->flags |= FIB_RULE_IIF_DETACHED;
539 if (rule->oifname[0]) {
540 NLA_PUT_STRING(skb, FRA_OIFNAME, rule->oifname);
542 if (rule->oifindex == -1)
543 frh->flags |= FIB_RULE_OIF_DETACHED;
546 if (rule->pref)
547 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
549 if (rule->mark)
550 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
552 if (rule->mark_mask || rule->mark)
553 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
555 if (rule->target)
556 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
558 if (ops->fill(rule, skb, frh) < 0)
559 goto nla_put_failure;
561 return nlmsg_end(skb, nlh);
563 nla_put_failure:
564 nlmsg_cancel(skb, nlh);
565 return -EMSGSIZE;
568 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
569 struct fib_rules_ops *ops)
571 int idx = 0;
572 struct fib_rule *rule;
574 list_for_each_entry(rule, &ops->rules_list, list) {
575 if (idx < cb->args[1])
576 goto skip;
578 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
579 cb->nlh->nlmsg_seq, RTM_NEWRULE,
580 NLM_F_MULTI, ops) < 0)
581 break;
582 skip:
583 idx++;
585 cb->args[1] = idx;
586 rules_ops_put(ops);
588 return skb->len;
591 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
593 struct net *net = sock_net(skb->sk);
594 struct fib_rules_ops *ops;
595 int idx = 0, family;
597 family = rtnl_msg_family(cb->nlh);
598 if (family != AF_UNSPEC) {
599 /* Protocol specific dump request */
600 ops = lookup_rules_ops(net, family);
601 if (ops == NULL)
602 return -EAFNOSUPPORT;
604 return dump_rules(skb, cb, ops);
607 rcu_read_lock();
608 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
609 if (idx < cb->args[0] || !try_module_get(ops->owner))
610 goto skip;
612 if (dump_rules(skb, cb, ops) < 0)
613 break;
615 cb->args[1] = 0;
616 skip:
617 idx++;
619 rcu_read_unlock();
620 cb->args[0] = idx;
622 return skb->len;
625 static void notify_rule_change(int event, struct fib_rule *rule,
626 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
627 u32 pid)
629 struct net *net;
630 struct sk_buff *skb;
631 int err = -ENOBUFS;
633 net = ops->fro_net;
634 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
635 if (skb == NULL)
636 goto errout;
638 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
639 if (err < 0) {
640 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
641 WARN_ON(err == -EMSGSIZE);
642 kfree_skb(skb);
643 goto errout;
646 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
647 return;
648 errout:
649 if (err < 0)
650 rtnl_set_sk_err(net, ops->nlgroup, err);
653 static void attach_rules(struct list_head *rules, struct net_device *dev)
655 struct fib_rule *rule;
657 list_for_each_entry(rule, rules, list) {
658 if (rule->iifindex == -1 &&
659 strcmp(dev->name, rule->iifname) == 0)
660 rule->iifindex = dev->ifindex;
661 if (rule->oifindex == -1 &&
662 strcmp(dev->name, rule->oifname) == 0)
663 rule->oifindex = dev->ifindex;
667 static void detach_rules(struct list_head *rules, struct net_device *dev)
669 struct fib_rule *rule;
671 list_for_each_entry(rule, rules, list) {
672 if (rule->iifindex == dev->ifindex)
673 rule->iifindex = -1;
674 if (rule->oifindex == dev->ifindex)
675 rule->oifindex = -1;
680 static int fib_rules_event(struct notifier_block *this, unsigned long event,
681 void *ptr)
683 struct net_device *dev = ptr;
684 struct net *net = dev_net(dev);
685 struct fib_rules_ops *ops;
687 ASSERT_RTNL();
688 rcu_read_lock();
690 switch (event) {
691 case NETDEV_REGISTER:
692 list_for_each_entry(ops, &net->rules_ops, list)
693 attach_rules(&ops->rules_list, dev);
694 break;
696 case NETDEV_UNREGISTER:
697 list_for_each_entry(ops, &net->rules_ops, list)
698 detach_rules(&ops->rules_list, dev);
699 break;
702 rcu_read_unlock();
704 return NOTIFY_DONE;
707 static struct notifier_block fib_rules_notifier = {
708 .notifier_call = fib_rules_event,
711 static int __net_init fib_rules_net_init(struct net *net)
713 INIT_LIST_HEAD(&net->rules_ops);
714 spin_lock_init(&net->rules_mod_lock);
715 return 0;
718 static struct pernet_operations fib_rules_net_ops = {
719 .init = fib_rules_net_init,
722 static int __init fib_rules_init(void)
724 int err;
725 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
726 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
727 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
729 err = register_pernet_subsys(&fib_rules_net_ops);
730 if (err < 0)
731 goto fail;
733 err = register_netdevice_notifier(&fib_rules_notifier);
734 if (err < 0)
735 goto fail_unregister;
737 return 0;
739 fail_unregister:
740 unregister_pernet_subsys(&fib_rules_net_ops);
741 fail:
742 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
743 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
744 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
745 return err;
748 subsys_initcall(fib_rules_init);