]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/fib_rules.c
[NETNS]: Pass fib_rules_ops into default_pref method.
[net-next-2.6.git] / net / core / fib_rules.c
CommitLineData
14c0b97d
TG
1/*
2 * net/core/fib_rules.c Generic Routing Rules
3 *
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.
7 *
8 * Authors: Thomas Graf <tgraf@suug.ch>
9 */
10
14c0b97d
TG
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
e9dc8653 14#include <net/net_namespace.h>
881d966b 15#include <net/sock.h>
14c0b97d
TG
16#include <net/fib_rules.h>
17
18static LIST_HEAD(rules_ops);
19static DEFINE_SPINLOCK(rules_mod_lock);
20
2994c638
DL
21int fib_default_rule_add(struct fib_rules_ops *ops,
22 u32 pref, u32 table, u32 flags)
23{
24 struct fib_rule *r;
25
26 r = kzalloc(ops->rule_size, GFP_KERNEL);
27 if (r == NULL)
28 return -ENOMEM;
29
30 atomic_set(&r->refcnt, 1);
31 r->action = FR_ACT_TO_TBL;
32 r->pref = pref;
33 r->table = table;
34 r->flags = flags;
35
36 /* The lock is not required here, the list in unreacheable
37 * at the moment this function is called */
38 list_add_tail(&r->list, &ops->rules_list);
39 return 0;
40}
41EXPORT_SYMBOL(fib_default_rule_add);
42
14c0b97d 43static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
44 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
45 u32 pid);
14c0b97d
TG
46
47static struct fib_rules_ops *lookup_rules_ops(int family)
48{
49 struct fib_rules_ops *ops;
50
51 rcu_read_lock();
52 list_for_each_entry_rcu(ops, &rules_ops, list) {
53 if (ops->family == family) {
54 if (!try_module_get(ops->owner))
55 ops = NULL;
56 rcu_read_unlock();
57 return ops;
58 }
59 }
60 rcu_read_unlock();
61
62 return NULL;
63}
64
65static void rules_ops_put(struct fib_rules_ops *ops)
66{
67 if (ops)
68 module_put(ops->owner);
69}
70
73417f61
TG
71static void flush_route_cache(struct fib_rules_ops *ops)
72{
73 if (ops->flush_cache)
74 ops->flush_cache();
75}
76
f8c26b8d 77int fib_rules_register(struct net *net, struct fib_rules_ops *ops)
14c0b97d
TG
78{
79 int err = -EEXIST;
80 struct fib_rules_ops *o;
81
82 if (ops->rule_size < sizeof(struct fib_rule))
83 return -EINVAL;
84
85 if (ops->match == NULL || ops->configure == NULL ||
86 ops->compare == NULL || ops->fill == NULL ||
87 ops->action == NULL)
88 return -EINVAL;
89
90 spin_lock(&rules_mod_lock);
91 list_for_each_entry(o, &rules_ops, list)
92 if (ops->family == o->family)
93 goto errout;
94
95 list_add_tail_rcu(&ops->list, &rules_ops);
96 err = 0;
97errout:
98 spin_unlock(&rules_mod_lock);
99
100 return err;
101}
102
103EXPORT_SYMBOL_GPL(fib_rules_register);
104
9eb87f3f 105void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
14c0b97d
TG
106{
107 struct fib_rule *rule, *tmp;
108
76c72d4f 109 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
14c0b97d
TG
110 list_del_rcu(&rule->list);
111 fib_rule_put(rule);
112 }
113}
9eb87f3f 114EXPORT_SYMBOL_GPL(fib_rules_cleanup_ops);
14c0b97d 115
f8c26b8d 116int fib_rules_unregister(struct net *net, struct fib_rules_ops *ops)
14c0b97d
TG
117{
118 int err = 0;
119 struct fib_rules_ops *o;
120
121 spin_lock(&rules_mod_lock);
122 list_for_each_entry(o, &rules_ops, list) {
123 if (o == ops) {
124 list_del_rcu(&o->list);
9eb87f3f 125 fib_rules_cleanup_ops(ops);
14c0b97d
TG
126 goto out;
127 }
128 }
129
130 err = -ENOENT;
131out:
132 spin_unlock(&rules_mod_lock);
133
134 synchronize_rcu();
135
136 return err;
137}
138
139EXPORT_SYMBOL_GPL(fib_rules_unregister);
140
3dfbcc41
TG
141static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
142 struct flowi *fl, int flags)
143{
144 int ret = 0;
145
146 if (rule->ifindex && (rule->ifindex != fl->iif))
147 goto out;
148
149 if ((rule->mark ^ fl->mark) & rule->mark_mask)
150 goto out;
151
152 ret = ops->match(rule, fl, flags);
153out:
154 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
155}
156
14c0b97d
TG
157int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
158 int flags, struct fib_lookup_arg *arg)
159{
160 struct fib_rule *rule;
161 int err;
162
163 rcu_read_lock();
164
76c72d4f 165 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
0947c9fe 166jumped:
3dfbcc41 167 if (!fib_rule_match(rule, ops, fl, flags))
14c0b97d
TG
168 continue;
169
0947c9fe
TG
170 if (rule->action == FR_ACT_GOTO) {
171 struct fib_rule *target;
172
173 target = rcu_dereference(rule->ctarget);
174 if (target == NULL) {
175 continue;
176 } else {
177 rule = target;
178 goto jumped;
179 }
fa0b2d1d
TG
180 } else if (rule->action == FR_ACT_NOP)
181 continue;
182 else
0947c9fe
TG
183 err = ops->action(rule, fl, flags, arg);
184
14c0b97d
TG
185 if (err != -EAGAIN) {
186 fib_rule_get(rule);
187 arg->rule = rule;
188 goto out;
189 }
190 }
191
83886b6b 192 err = -ESRCH;
14c0b97d
TG
193out:
194 rcu_read_unlock();
195
196 return err;
197}
198
199EXPORT_SYMBOL_GPL(fib_rules_lookup);
200
e1701c68
TG
201static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
202 struct fib_rules_ops *ops)
203{
204 int err = -EINVAL;
205
206 if (frh->src_len)
207 if (tb[FRA_SRC] == NULL ||
208 frh->src_len > (ops->addr_size * 8) ||
209 nla_len(tb[FRA_SRC]) != ops->addr_size)
210 goto errout;
211
212 if (frh->dst_len)
213 if (tb[FRA_DST] == NULL ||
214 frh->dst_len > (ops->addr_size * 8) ||
215 nla_len(tb[FRA_DST]) != ops->addr_size)
216 goto errout;
217
218 err = 0;
219errout:
220 return err;
221}
222
9d9e6a58 223static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
14c0b97d 224{
881d966b 225 struct net *net = skb->sk->sk_net;
14c0b97d
TG
226 struct fib_rule_hdr *frh = nlmsg_data(nlh);
227 struct fib_rules_ops *ops = NULL;
228 struct fib_rule *rule, *r, *last = NULL;
229 struct nlattr *tb[FRA_MAX+1];
0947c9fe 230 int err = -EINVAL, unresolved = 0;
14c0b97d 231
b854272b
DL
232 if (net != &init_net)
233 return -EINVAL;
234
14c0b97d
TG
235 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
236 goto errout;
237
238 ops = lookup_rules_ops(frh->family);
239 if (ops == NULL) {
240 err = EAFNOSUPPORT;
241 goto errout;
242 }
243
244 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
245 if (err < 0)
246 goto errout;
247
e1701c68
TG
248 err = validate_rulemsg(frh, tb, ops);
249 if (err < 0)
250 goto errout;
251
14c0b97d
TG
252 rule = kzalloc(ops->rule_size, GFP_KERNEL);
253 if (rule == NULL) {
254 err = -ENOMEM;
255 goto errout;
256 }
257
258 if (tb[FRA_PRIORITY])
259 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
260
261 if (tb[FRA_IFNAME]) {
262 struct net_device *dev;
263
264 rule->ifindex = -1;
5176f91e 265 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
881d966b 266 dev = __dev_get_by_name(net, rule->ifname);
14c0b97d
TG
267 if (dev)
268 rule->ifindex = dev->ifindex;
269 }
270
b8964ed9
TG
271 if (tb[FRA_FWMARK]) {
272 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
273 if (rule->mark)
274 /* compatibility: if the mark value is non-zero all bits
275 * are compared unless a mask is explicitly specified.
276 */
277 rule->mark_mask = 0xFFFFFFFF;
278 }
279
280 if (tb[FRA_FWMASK])
281 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
282
14c0b97d
TG
283 rule->action = frh->action;
284 rule->flags = frh->flags;
9e762a4a 285 rule->table = frh_get_table(frh, tb);
14c0b97d
TG
286
287 if (!rule->pref && ops->default_pref)
868d13ac 288 rule->pref = ops->default_pref(ops);
14c0b97d 289
0947c9fe
TG
290 err = -EINVAL;
291 if (tb[FRA_GOTO]) {
292 if (rule->action != FR_ACT_GOTO)
293 goto errout_free;
294
295 rule->target = nla_get_u32(tb[FRA_GOTO]);
296 /* Backward jumps are prohibited to avoid endless loops */
297 if (rule->target <= rule->pref)
298 goto errout_free;
299
76c72d4f 300 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe
TG
301 if (r->pref == rule->target) {
302 rule->ctarget = r;
303 break;
304 }
305 }
306
307 if (rule->ctarget == NULL)
308 unresolved = 1;
309 } else if (rule->action == FR_ACT_GOTO)
310 goto errout_free;
311
14c0b97d
TG
312 err = ops->configure(rule, skb, nlh, frh, tb);
313 if (err < 0)
314 goto errout_free;
315
76c72d4f 316 list_for_each_entry(r, &ops->rules_list, list) {
14c0b97d
TG
317 if (r->pref > rule->pref)
318 break;
319 last = r;
320 }
321
322 fib_rule_get(rule);
323
0947c9fe
TG
324 if (ops->unresolved_rules) {
325 /*
326 * There are unresolved goto rules in the list, check if
327 * any of them are pointing to this new rule.
328 */
76c72d4f 329 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe
TG
330 if (r->action == FR_ACT_GOTO &&
331 r->target == rule->pref) {
332 BUG_ON(r->ctarget != NULL);
333 rcu_assign_pointer(r->ctarget, rule);
334 if (--ops->unresolved_rules == 0)
335 break;
336 }
337 }
338 }
339
340 if (rule->action == FR_ACT_GOTO)
341 ops->nr_goto_rules++;
342
343 if (unresolved)
344 ops->unresolved_rules++;
345
14c0b97d
TG
346 if (last)
347 list_add_rcu(&rule->list, &last->list);
348 else
76c72d4f 349 list_add_rcu(&rule->list, &ops->rules_list);
14c0b97d 350
c17084d2 351 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
73417f61 352 flush_route_cache(ops);
14c0b97d
TG
353 rules_ops_put(ops);
354 return 0;
355
356errout_free:
357 kfree(rule);
358errout:
359 rules_ops_put(ops);
360 return err;
361}
362
9d9e6a58 363static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
14c0b97d 364{
b854272b 365 struct net *net = skb->sk->sk_net;
14c0b97d
TG
366 struct fib_rule_hdr *frh = nlmsg_data(nlh);
367 struct fib_rules_ops *ops = NULL;
0947c9fe 368 struct fib_rule *rule, *tmp;
14c0b97d
TG
369 struct nlattr *tb[FRA_MAX+1];
370 int err = -EINVAL;
371
b854272b
DL
372 if (net != &init_net)
373 return -EINVAL;
374
14c0b97d
TG
375 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
376 goto errout;
377
378 ops = lookup_rules_ops(frh->family);
379 if (ops == NULL) {
380 err = EAFNOSUPPORT;
381 goto errout;
382 }
383
384 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
385 if (err < 0)
386 goto errout;
387
e1701c68
TG
388 err = validate_rulemsg(frh, tb, ops);
389 if (err < 0)
390 goto errout;
391
76c72d4f 392 list_for_each_entry(rule, &ops->rules_list, list) {
14c0b97d
TG
393 if (frh->action && (frh->action != rule->action))
394 continue;
395
9e762a4a 396 if (frh->table && (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
397 continue;
398
399 if (tb[FRA_PRIORITY] &&
400 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
401 continue;
402
403 if (tb[FRA_IFNAME] &&
404 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
405 continue;
406
b8964ed9
TG
407 if (tb[FRA_FWMARK] &&
408 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
409 continue;
410
411 if (tb[FRA_FWMASK] &&
412 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
413 continue;
414
14c0b97d
TG
415 if (!ops->compare(rule, frh, tb))
416 continue;
417
418 if (rule->flags & FIB_RULE_PERMANENT) {
419 err = -EPERM;
420 goto errout;
421 }
422
423 list_del_rcu(&rule->list);
0947c9fe
TG
424
425 if (rule->action == FR_ACT_GOTO)
426 ops->nr_goto_rules--;
427
428 /*
429 * Check if this rule is a target to any of them. If so,
430 * disable them. As this operation is eventually very
431 * expensive, it is only performed if goto rules have
432 * actually been added.
433 */
434 if (ops->nr_goto_rules > 0) {
76c72d4f 435 list_for_each_entry(tmp, &ops->rules_list, list) {
0947c9fe
TG
436 if (tmp->ctarget == rule) {
437 rcu_assign_pointer(tmp->ctarget, NULL);
438 ops->unresolved_rules++;
439 }
440 }
441 }
442
14c0b97d 443 synchronize_rcu();
c17084d2
TG
444 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
445 NETLINK_CB(skb).pid);
14c0b97d 446 fib_rule_put(rule);
73417f61 447 flush_route_cache(ops);
14c0b97d
TG
448 rules_ops_put(ops);
449 return 0;
450 }
451
452 err = -ENOENT;
453errout:
454 rules_ops_put(ops);
455 return err;
456}
457
339bf98f
TG
458static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
459 struct fib_rule *rule)
460{
461 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
462 + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
463 + nla_total_size(4) /* FRA_PRIORITY */
464 + nla_total_size(4) /* FRA_TABLE */
465 + nla_total_size(4) /* FRA_FWMARK */
466 + nla_total_size(4); /* FRA_FWMASK */
467
468 if (ops->nlmsg_payload)
469 payload += ops->nlmsg_payload(rule);
470
471 return payload;
472}
473
14c0b97d
TG
474static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
475 u32 pid, u32 seq, int type, int flags,
476 struct fib_rules_ops *ops)
477{
478 struct nlmsghdr *nlh;
479 struct fib_rule_hdr *frh;
480
481 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
482 if (nlh == NULL)
26932566 483 return -EMSGSIZE;
14c0b97d
TG
484
485 frh = nlmsg_data(nlh);
486 frh->table = rule->table;
9e762a4a 487 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
14c0b97d
TG
488 frh->res1 = 0;
489 frh->res2 = 0;
490 frh->action = rule->action;
491 frh->flags = rule->flags;
492
0947c9fe
TG
493 if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
494 frh->flags |= FIB_RULE_UNRESOLVED;
495
2b443683 496 if (rule->ifname[0]) {
14c0b97d
TG
497 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
498
2b443683
TG
499 if (rule->ifindex == -1)
500 frh->flags |= FIB_RULE_DEV_DETACHED;
501 }
502
14c0b97d
TG
503 if (rule->pref)
504 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
505
b8964ed9
TG
506 if (rule->mark)
507 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
508
509 if (rule->mark_mask || rule->mark)
510 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
511
0947c9fe
TG
512 if (rule->target)
513 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
514
14c0b97d
TG
515 if (ops->fill(rule, skb, nlh, frh) < 0)
516 goto nla_put_failure;
517
518 return nlmsg_end(skb, nlh);
519
520nla_put_failure:
26932566
PM
521 nlmsg_cancel(skb, nlh);
522 return -EMSGSIZE;
14c0b97d
TG
523}
524
c454673d
TG
525static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
526 struct fib_rules_ops *ops)
14c0b97d
TG
527{
528 int idx = 0;
529 struct fib_rule *rule;
14c0b97d 530
76c72d4f 531 list_for_each_entry(rule, &ops->rules_list, list) {
c454673d 532 if (idx < cb->args[1])
14c0b97d
TG
533 goto skip;
534
535 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
536 cb->nlh->nlmsg_seq, RTM_NEWRULE,
537 NLM_F_MULTI, ops) < 0)
538 break;
539skip:
540 idx++;
541 }
c454673d 542 cb->args[1] = idx;
14c0b97d
TG
543 rules_ops_put(ops);
544
545 return skb->len;
546}
547
c454673d
TG
548static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
549{
b854272b 550 struct net *net = skb->sk->sk_net;
c454673d
TG
551 struct fib_rules_ops *ops;
552 int idx = 0, family;
553
b854272b
DL
554 if (net != &init_net)
555 return -EINVAL;
556
c454673d
TG
557 family = rtnl_msg_family(cb->nlh);
558 if (family != AF_UNSPEC) {
559 /* Protocol specific dump request */
560 ops = lookup_rules_ops(family);
561 if (ops == NULL)
562 return -EAFNOSUPPORT;
563
564 return dump_rules(skb, cb, ops);
565 }
566
567 rcu_read_lock();
568 list_for_each_entry_rcu(ops, &rules_ops, list) {
569 if (idx < cb->args[0] || !try_module_get(ops->owner))
570 goto skip;
571
572 if (dump_rules(skb, cb, ops) < 0)
573 break;
574
575 cb->args[1] = 0;
576 skip:
577 idx++;
578 }
579 rcu_read_unlock();
580 cb->args[0] = idx;
581
582 return skb->len;
583}
14c0b97d
TG
584
585static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
586 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
587 u32 pid)
14c0b97d 588{
c17084d2
TG
589 struct sk_buff *skb;
590 int err = -ENOBUFS;
14c0b97d 591
339bf98f 592 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
14c0b97d 593 if (skb == NULL)
c17084d2
TG
594 goto errout;
595
596 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
26932566
PM
597 if (err < 0) {
598 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
599 WARN_ON(err == -EMSGSIZE);
600 kfree_skb(skb);
601 goto errout;
602 }
97c53cac 603 err = rtnl_notify(skb, &init_net, pid, ops->nlgroup, nlh, GFP_KERNEL);
c17084d2
TG
604errout:
605 if (err < 0)
97c53cac 606 rtnl_set_sk_err(&init_net, ops->nlgroup, err);
14c0b97d
TG
607}
608
609static void attach_rules(struct list_head *rules, struct net_device *dev)
610{
611 struct fib_rule *rule;
612
613 list_for_each_entry(rule, rules, list) {
614 if (rule->ifindex == -1 &&
615 strcmp(dev->name, rule->ifname) == 0)
616 rule->ifindex = dev->ifindex;
617 }
618}
619
620static void detach_rules(struct list_head *rules, struct net_device *dev)
621{
622 struct fib_rule *rule;
623
624 list_for_each_entry(rule, rules, list)
625 if (rule->ifindex == dev->ifindex)
626 rule->ifindex = -1;
627}
628
629
630static int fib_rules_event(struct notifier_block *this, unsigned long event,
631 void *ptr)
632{
633 struct net_device *dev = ptr;
634 struct fib_rules_ops *ops;
635
e9dc8653
EB
636 if (dev->nd_net != &init_net)
637 return NOTIFY_DONE;
638
14c0b97d
TG
639 ASSERT_RTNL();
640 rcu_read_lock();
641
642 switch (event) {
643 case NETDEV_REGISTER:
644 list_for_each_entry(ops, &rules_ops, list)
76c72d4f 645 attach_rules(&ops->rules_list, dev);
14c0b97d
TG
646 break;
647
648 case NETDEV_UNREGISTER:
649 list_for_each_entry(ops, &rules_ops, list)
76c72d4f 650 detach_rules(&ops->rules_list, dev);
14c0b97d
TG
651 break;
652 }
653
654 rcu_read_unlock();
655
656 return NOTIFY_DONE;
657}
658
659static struct notifier_block fib_rules_notifier = {
660 .notifier_call = fib_rules_event,
661};
662
663static int __init fib_rules_init(void)
664{
9d9e6a58
TG
665 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
666 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
c454673d 667 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
9d9e6a58 668
14c0b97d
TG
669 return register_netdevice_notifier(&fib_rules_notifier);
670}
671
672subsys_initcall(fib_rules_init);