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