Merge branch 'rhashtable-cleanups'
[deliverable/linux.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>
5a0e3ad6 13#include <linux/slab.h>
14c0b97d 14#include <linux/list.h>
3a9a231d 15#include <linux/module.h>
e9dc8653 16#include <net/net_namespace.h>
881d966b 17#include <net/sock.h>
14c0b97d
TG
18#include <net/fib_rules.h>
19
2994c638
DL
20int fib_default_rule_add(struct fib_rules_ops *ops,
21 u32 pref, u32 table, u32 flags)
22{
23 struct fib_rule *r;
24
25 r = kzalloc(ops->rule_size, GFP_KERNEL);
26 if (r == NULL)
27 return -ENOMEM;
28
29 atomic_set(&r->refcnt, 1);
30 r->action = FR_ACT_TO_TBL;
31 r->pref = pref;
32 r->table = table;
33 r->flags = flags;
3661a910 34 r->fr_net = hold_net(ops->fro_net);
2994c638 35
73f5698e
ST
36 r->suppress_prefixlen = -1;
37 r->suppress_ifgroup = -1;
38
2994c638
DL
39 /* The lock is not required here, the list in unreacheable
40 * at the moment this function is called */
41 list_add_tail(&r->list, &ops->rules_list);
42 return 0;
43}
44EXPORT_SYMBOL(fib_default_rule_add);
45
d8a566be
PM
46u32 fib_default_rule_pref(struct fib_rules_ops *ops)
47{
48 struct list_head *pos;
49 struct fib_rule *rule;
50
51 if (!list_empty(&ops->rules_list)) {
52 pos = ops->rules_list.next;
53 if (pos->next != &ops->rules_list) {
54 rule = list_entry(pos->next, struct fib_rule, list);
55 if (rule->pref)
56 return rule->pref - 1;
57 }
58 }
59
60 return 0;
61}
62EXPORT_SYMBOL(fib_default_rule_pref);
63
9e3a5487 64static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
65 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
66 u32 pid);
14c0b97d 67
5fd30ee7 68static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
14c0b97d
TG
69{
70 struct fib_rules_ops *ops;
71
72 rcu_read_lock();
5fd30ee7 73 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
14c0b97d
TG
74 if (ops->family == family) {
75 if (!try_module_get(ops->owner))
76 ops = NULL;
77 rcu_read_unlock();
78 return ops;
79 }
80 }
81 rcu_read_unlock();
82
83 return NULL;
84}
85
86static void rules_ops_put(struct fib_rules_ops *ops)
87{
88 if (ops)
89 module_put(ops->owner);
90}
91
73417f61
TG
92static void flush_route_cache(struct fib_rules_ops *ops)
93{
94 if (ops->flush_cache)
ae299fc0 95 ops->flush_cache(ops);
73417f61
TG
96}
97
e9c5158a 98static int __fib_rules_register(struct fib_rules_ops *ops)
14c0b97d
TG
99{
100 int err = -EEXIST;
101 struct fib_rules_ops *o;
9e3a5487
DL
102 struct net *net;
103
104 net = ops->fro_net;
14c0b97d
TG
105
106 if (ops->rule_size < sizeof(struct fib_rule))
107 return -EINVAL;
108
109 if (ops->match == NULL || ops->configure == NULL ||
110 ops->compare == NULL || ops->fill == NULL ||
111 ops->action == NULL)
112 return -EINVAL;
113
5fd30ee7
DL
114 spin_lock(&net->rules_mod_lock);
115 list_for_each_entry(o, &net->rules_ops, list)
14c0b97d
TG
116 if (ops->family == o->family)
117 goto errout;
118
5fd30ee7
DL
119 hold_net(net);
120 list_add_tail_rcu(&ops->list, &net->rules_ops);
14c0b97d
TG
121 err = 0;
122errout:
5fd30ee7 123 spin_unlock(&net->rules_mod_lock);
14c0b97d
TG
124
125 return err;
126}
127
e9c5158a 128struct fib_rules_ops *
3d0c9c4e 129fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
e9c5158a
EB
130{
131 struct fib_rules_ops *ops;
132 int err;
133
2fb3573d 134 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
e9c5158a
EB
135 if (ops == NULL)
136 return ERR_PTR(-ENOMEM);
137
138 INIT_LIST_HEAD(&ops->rules_list);
139 ops->fro_net = net;
140
141 err = __fib_rules_register(ops);
142 if (err) {
143 kfree(ops);
144 ops = ERR_PTR(err);
145 }
146
147 return ops;
148}
14c0b97d
TG
149EXPORT_SYMBOL_GPL(fib_rules_register);
150
1df9916e 151static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
14c0b97d
TG
152{
153 struct fib_rule *rule, *tmp;
154
76c72d4f 155 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
14c0b97d 156 list_del_rcu(&rule->list);
7a9bc9b8
DM
157 if (ops->delete)
158 ops->delete(rule);
14c0b97d
TG
159 fib_rule_put(rule);
160 }
161}
162
e9c5158a
EB
163static void fib_rules_put_rcu(struct rcu_head *head)
164{
165 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
166 struct net *net = ops->fro_net;
167
168 release_net(net);
169 kfree(ops);
170}
171
9e3a5487 172void fib_rules_unregister(struct fib_rules_ops *ops)
14c0b97d 173{
9e3a5487 174 struct net *net = ops->fro_net;
14c0b97d 175
5fd30ee7 176 spin_lock(&net->rules_mod_lock);
72132c1b
DL
177 list_del_rcu(&ops->list);
178 fib_rules_cleanup_ops(ops);
5fd30ee7 179 spin_unlock(&net->rules_mod_lock);
14c0b97d 180
e9c5158a 181 call_rcu(&ops->rcu, fib_rules_put_rcu);
14c0b97d 182}
14c0b97d
TG
183EXPORT_SYMBOL_GPL(fib_rules_unregister);
184
3dfbcc41
TG
185static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
186 struct flowi *fl, int flags)
187{
188 int ret = 0;
189
1d28f42c 190 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
3dfbcc41
TG
191 goto out;
192
1d28f42c 193 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
1b038a5e
PM
194 goto out;
195
1d28f42c 196 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
3dfbcc41
TG
197 goto out;
198
199 ret = ops->match(rule, fl, flags);
200out:
201 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
202}
203
14c0b97d
TG
204int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
205 int flags, struct fib_lookup_arg *arg)
206{
207 struct fib_rule *rule;
208 int err;
209
210 rcu_read_lock();
211
76c72d4f 212 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
0947c9fe 213jumped:
3dfbcc41 214 if (!fib_rule_match(rule, ops, fl, flags))
14c0b97d
TG
215 continue;
216
0947c9fe
TG
217 if (rule->action == FR_ACT_GOTO) {
218 struct fib_rule *target;
219
220 target = rcu_dereference(rule->ctarget);
221 if (target == NULL) {
222 continue;
223 } else {
224 rule = target;
225 goto jumped;
226 }
fa0b2d1d
TG
227 } else if (rule->action == FR_ACT_NOP)
228 continue;
229 else
0947c9fe
TG
230 err = ops->action(rule, fl, flags, arg);
231
7764a45a
ST
232 if (!err && ops->suppress && ops->suppress(rule, arg))
233 continue;
234
14c0b97d 235 if (err != -EAGAIN) {
ebc0ffae
ED
236 if ((arg->flags & FIB_LOOKUP_NOREF) ||
237 likely(atomic_inc_not_zero(&rule->refcnt))) {
7fa7cb71
ED
238 arg->rule = rule;
239 goto out;
240 }
241 break;
14c0b97d
TG
242 }
243 }
244
83886b6b 245 err = -ESRCH;
14c0b97d
TG
246out:
247 rcu_read_unlock();
248
249 return err;
250}
14c0b97d
TG
251EXPORT_SYMBOL_GPL(fib_rules_lookup);
252
e1701c68
TG
253static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
254 struct fib_rules_ops *ops)
255{
256 int err = -EINVAL;
257
258 if (frh->src_len)
259 if (tb[FRA_SRC] == NULL ||
260 frh->src_len > (ops->addr_size * 8) ||
261 nla_len(tb[FRA_SRC]) != ops->addr_size)
262 goto errout;
263
264 if (frh->dst_len)
265 if (tb[FRA_DST] == NULL ||
266 frh->dst_len > (ops->addr_size * 8) ||
267 nla_len(tb[FRA_DST]) != ops->addr_size)
268 goto errout;
269
270 err = 0;
271errout:
272 return err;
273}
274
661d2967 275static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
14c0b97d 276{
3b1e0a65 277 struct net *net = sock_net(skb->sk);
14c0b97d
TG
278 struct fib_rule_hdr *frh = nlmsg_data(nlh);
279 struct fib_rules_ops *ops = NULL;
280 struct fib_rule *rule, *r, *last = NULL;
281 struct nlattr *tb[FRA_MAX+1];
0947c9fe 282 int err = -EINVAL, unresolved = 0;
14c0b97d
TG
283
284 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
285 goto errout;
286
5fd30ee7 287 ops = lookup_rules_ops(net, frh->family);
14c0b97d 288 if (ops == NULL) {
2fe195cf 289 err = -EAFNOSUPPORT;
14c0b97d
TG
290 goto errout;
291 }
292
293 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
294 if (err < 0)
295 goto errout;
296
e1701c68
TG
297 err = validate_rulemsg(frh, tb, ops);
298 if (err < 0)
299 goto errout;
300
14c0b97d
TG
301 rule = kzalloc(ops->rule_size, GFP_KERNEL);
302 if (rule == NULL) {
303 err = -ENOMEM;
304 goto errout;
305 }
3661a910 306 rule->fr_net = hold_net(net);
14c0b97d
TG
307
308 if (tb[FRA_PRIORITY])
309 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
310
491deb24 311 if (tb[FRA_IIFNAME]) {
14c0b97d
TG
312 struct net_device *dev;
313
491deb24
PM
314 rule->iifindex = -1;
315 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
316 dev = __dev_get_by_name(net, rule->iifname);
14c0b97d 317 if (dev)
491deb24 318 rule->iifindex = dev->ifindex;
14c0b97d
TG
319 }
320
1b038a5e
PM
321 if (tb[FRA_OIFNAME]) {
322 struct net_device *dev;
323
324 rule->oifindex = -1;
325 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
326 dev = __dev_get_by_name(net, rule->oifname);
327 if (dev)
328 rule->oifindex = dev->ifindex;
329 }
330
b8964ed9
TG
331 if (tb[FRA_FWMARK]) {
332 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
333 if (rule->mark)
334 /* compatibility: if the mark value is non-zero all bits
335 * are compared unless a mask is explicitly specified.
336 */
337 rule->mark_mask = 0xFFFFFFFF;
338 }
339
340 if (tb[FRA_FWMASK])
341 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
342
14c0b97d
TG
343 rule->action = frh->action;
344 rule->flags = frh->flags;
9e762a4a 345 rule->table = frh_get_table(frh, tb);
73f5698e
ST
346 if (tb[FRA_SUPPRESS_PREFIXLEN])
347 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
348 else
349 rule->suppress_prefixlen = -1;
14c0b97d 350
6ef94cfa
ST
351 if (tb[FRA_SUPPRESS_IFGROUP])
352 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
73f5698e
ST
353 else
354 rule->suppress_ifgroup = -1;
6ef94cfa 355
5adef180 356 if (!tb[FRA_PRIORITY] && ops->default_pref)
868d13ac 357 rule->pref = ops->default_pref(ops);
14c0b97d 358
0947c9fe
TG
359 err = -EINVAL;
360 if (tb[FRA_GOTO]) {
361 if (rule->action != FR_ACT_GOTO)
362 goto errout_free;
363
364 rule->target = nla_get_u32(tb[FRA_GOTO]);
365 /* Backward jumps are prohibited to avoid endless loops */
366 if (rule->target <= rule->pref)
367 goto errout_free;
368
76c72d4f 369 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 370 if (r->pref == rule->target) {
7a2b03c5 371 RCU_INIT_POINTER(rule->ctarget, r);
0947c9fe
TG
372 break;
373 }
374 }
375
7a2b03c5 376 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
0947c9fe
TG
377 unresolved = 1;
378 } else if (rule->action == FR_ACT_GOTO)
379 goto errout_free;
380
8b3521ee 381 err = ops->configure(rule, skb, frh, tb);
14c0b97d
TG
382 if (err < 0)
383 goto errout_free;
384
76c72d4f 385 list_for_each_entry(r, &ops->rules_list, list) {
14c0b97d
TG
386 if (r->pref > rule->pref)
387 break;
388 last = r;
389 }
390
391 fib_rule_get(rule);
392
ebb9fed2
ED
393 if (last)
394 list_add_rcu(&rule->list, &last->list);
395 else
396 list_add_rcu(&rule->list, &ops->rules_list);
397
0947c9fe
TG
398 if (ops->unresolved_rules) {
399 /*
400 * There are unresolved goto rules in the list, check if
401 * any of them are pointing to this new rule.
402 */
76c72d4f 403 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 404 if (r->action == FR_ACT_GOTO &&
561dac2d
G
405 r->target == rule->pref &&
406 rtnl_dereference(r->ctarget) == NULL) {
0947c9fe
TG
407 rcu_assign_pointer(r->ctarget, rule);
408 if (--ops->unresolved_rules == 0)
409 break;
410 }
411 }
412 }
413
414 if (rule->action == FR_ACT_GOTO)
415 ops->nr_goto_rules++;
416
417 if (unresolved)
418 ops->unresolved_rules++;
419
15e47304 420 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
73417f61 421 flush_route_cache(ops);
14c0b97d
TG
422 rules_ops_put(ops);
423 return 0;
424
425errout_free:
3661a910 426 release_net(rule->fr_net);
14c0b97d
TG
427 kfree(rule);
428errout:
429 rules_ops_put(ops);
430 return err;
431}
432
661d2967 433static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
14c0b97d 434{
3b1e0a65 435 struct net *net = sock_net(skb->sk);
14c0b97d
TG
436 struct fib_rule_hdr *frh = nlmsg_data(nlh);
437 struct fib_rules_ops *ops = NULL;
0947c9fe 438 struct fib_rule *rule, *tmp;
14c0b97d
TG
439 struct nlattr *tb[FRA_MAX+1];
440 int err = -EINVAL;
441
442 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
443 goto errout;
444
5fd30ee7 445 ops = lookup_rules_ops(net, frh->family);
14c0b97d 446 if (ops == NULL) {
2fe195cf 447 err = -EAFNOSUPPORT;
14c0b97d
TG
448 goto errout;
449 }
450
451 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
452 if (err < 0)
453 goto errout;
454
e1701c68
TG
455 err = validate_rulemsg(frh, tb, ops);
456 if (err < 0)
457 goto errout;
458
76c72d4f 459 list_for_each_entry(rule, &ops->rules_list, list) {
14c0b97d
TG
460 if (frh->action && (frh->action != rule->action))
461 continue;
462
13eb2ab2
AH
463 if (frh_get_table(frh, tb) &&
464 (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
465 continue;
466
467 if (tb[FRA_PRIORITY] &&
468 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
469 continue;
470
491deb24
PM
471 if (tb[FRA_IIFNAME] &&
472 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
14c0b97d
TG
473 continue;
474
1b038a5e
PM
475 if (tb[FRA_OIFNAME] &&
476 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
477 continue;
478
b8964ed9
TG
479 if (tb[FRA_FWMARK] &&
480 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
481 continue;
482
483 if (tb[FRA_FWMASK] &&
484 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
485 continue;
486
14c0b97d
TG
487 if (!ops->compare(rule, frh, tb))
488 continue;
489
490 if (rule->flags & FIB_RULE_PERMANENT) {
491 err = -EPERM;
492 goto errout;
493 }
494
0ddcf43d
AD
495 if (ops->delete) {
496 err = ops->delete(rule);
497 if (err)
498 goto errout;
499 }
500
14c0b97d 501 list_del_rcu(&rule->list);
0947c9fe 502
afaef734 503 if (rule->action == FR_ACT_GOTO) {
0947c9fe 504 ops->nr_goto_rules--;
afaef734
YZ
505 if (rtnl_dereference(rule->ctarget) == NULL)
506 ops->unresolved_rules--;
507 }
0947c9fe
TG
508
509 /*
510 * Check if this rule is a target to any of them. If so,
511 * disable them. As this operation is eventually very
512 * expensive, it is only performed if goto rules have
513 * actually been added.
514 */
515 if (ops->nr_goto_rules > 0) {
76c72d4f 516 list_for_each_entry(tmp, &ops->rules_list, list) {
7a2b03c5 517 if (rtnl_dereference(tmp->ctarget) == rule) {
a9b3cd7f 518 RCU_INIT_POINTER(tmp->ctarget, NULL);
0947c9fe
TG
519 ops->unresolved_rules++;
520 }
521 }
522 }
523
9e3a5487 524 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
15e47304 525 NETLINK_CB(skb).portid);
14c0b97d 526 fib_rule_put(rule);
73417f61 527 flush_route_cache(ops);
14c0b97d
TG
528 rules_ops_put(ops);
529 return 0;
530 }
531
532 err = -ENOENT;
533errout:
534 rules_ops_put(ops);
535 return err;
536}
537
339bf98f
TG
538static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
539 struct fib_rule *rule)
540{
541 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
491deb24 542 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
1b038a5e 543 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
339bf98f
TG
544 + nla_total_size(4) /* FRA_PRIORITY */
545 + nla_total_size(4) /* FRA_TABLE */
73f5698e 546 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
6ef94cfa 547 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
339bf98f
TG
548 + nla_total_size(4) /* FRA_FWMARK */
549 + nla_total_size(4); /* FRA_FWMASK */
550
551 if (ops->nlmsg_payload)
552 payload += ops->nlmsg_payload(rule);
553
554 return payload;
555}
556
14c0b97d
TG
557static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
558 u32 pid, u32 seq, int type, int flags,
559 struct fib_rules_ops *ops)
560{
561 struct nlmsghdr *nlh;
562 struct fib_rule_hdr *frh;
563
564 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
565 if (nlh == NULL)
26932566 566 return -EMSGSIZE;
14c0b97d
TG
567
568 frh = nlmsg_data(nlh);
28bb1726 569 frh->family = ops->family;
14c0b97d 570 frh->table = rule->table;
0e3cea7b
DM
571 if (nla_put_u32(skb, FRA_TABLE, rule->table))
572 goto nla_put_failure;
73f5698e 573 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
7764a45a 574 goto nla_put_failure;
14c0b97d
TG
575 frh->res1 = 0;
576 frh->res2 = 0;
577 frh->action = rule->action;
578 frh->flags = rule->flags;
579
7a2b03c5 580 if (rule->action == FR_ACT_GOTO &&
33d480ce 581 rcu_access_pointer(rule->ctarget) == NULL)
0947c9fe
TG
582 frh->flags |= FIB_RULE_UNRESOLVED;
583
491deb24 584 if (rule->iifname[0]) {
0e3cea7b
DM
585 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
586 goto nla_put_failure;
491deb24
PM
587 if (rule->iifindex == -1)
588 frh->flags |= FIB_RULE_IIF_DETACHED;
2b443683
TG
589 }
590
1b038a5e 591 if (rule->oifname[0]) {
0e3cea7b
DM
592 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
593 goto nla_put_failure;
1b038a5e
PM
594 if (rule->oifindex == -1)
595 frh->flags |= FIB_RULE_OIF_DETACHED;
596 }
597
0e3cea7b
DM
598 if ((rule->pref &&
599 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
600 (rule->mark &&
601 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
602 ((rule->mark_mask || rule->mark) &&
603 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
604 (rule->target &&
605 nla_put_u32(skb, FRA_GOTO, rule->target)))
606 goto nla_put_failure;
6ef94cfa
ST
607
608 if (rule->suppress_ifgroup != -1) {
609 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
610 goto nla_put_failure;
611 }
612
04af8cf6 613 if (ops->fill(rule, skb, frh) < 0)
14c0b97d
TG
614 goto nla_put_failure;
615
053c095a
JB
616 nlmsg_end(skb, nlh);
617 return 0;
14c0b97d
TG
618
619nla_put_failure:
26932566
PM
620 nlmsg_cancel(skb, nlh);
621 return -EMSGSIZE;
14c0b97d
TG
622}
623
c454673d
TG
624static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
625 struct fib_rules_ops *ops)
14c0b97d
TG
626{
627 int idx = 0;
628 struct fib_rule *rule;
14c0b97d 629
e67f88dd
ED
630 rcu_read_lock();
631 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
c454673d 632 if (idx < cb->args[1])
14c0b97d
TG
633 goto skip;
634
15e47304 635 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
14c0b97d
TG
636 cb->nlh->nlmsg_seq, RTM_NEWRULE,
637 NLM_F_MULTI, ops) < 0)
638 break;
639skip:
640 idx++;
641 }
2907c35f 642 rcu_read_unlock();
c454673d 643 cb->args[1] = idx;
14c0b97d
TG
644 rules_ops_put(ops);
645
646 return skb->len;
647}
648
c454673d
TG
649static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
650{
3b1e0a65 651 struct net *net = sock_net(skb->sk);
c454673d
TG
652 struct fib_rules_ops *ops;
653 int idx = 0, family;
654
655 family = rtnl_msg_family(cb->nlh);
656 if (family != AF_UNSPEC) {
657 /* Protocol specific dump request */
5fd30ee7 658 ops = lookup_rules_ops(net, family);
c454673d
TG
659 if (ops == NULL)
660 return -EAFNOSUPPORT;
661
662 return dump_rules(skb, cb, ops);
663 }
664
665 rcu_read_lock();
5fd30ee7 666 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
c454673d
TG
667 if (idx < cb->args[0] || !try_module_get(ops->owner))
668 goto skip;
669
670 if (dump_rules(skb, cb, ops) < 0)
671 break;
672
673 cb->args[1] = 0;
2fb3573d 674skip:
c454673d
TG
675 idx++;
676 }
677 rcu_read_unlock();
678 cb->args[0] = idx;
679
680 return skb->len;
681}
14c0b97d 682
9e3a5487 683static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
684 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
685 u32 pid)
14c0b97d 686{
9e3a5487 687 struct net *net;
c17084d2
TG
688 struct sk_buff *skb;
689 int err = -ENOBUFS;
14c0b97d 690
9e3a5487 691 net = ops->fro_net;
339bf98f 692 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
14c0b97d 693 if (skb == NULL)
c17084d2
TG
694 goto errout;
695
696 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
26932566
PM
697 if (err < 0) {
698 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
699 WARN_ON(err == -EMSGSIZE);
700 kfree_skb(skb);
701 goto errout;
702 }
9e3a5487 703
1ce85fe4
PNA
704 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
705 return;
c17084d2
TG
706errout:
707 if (err < 0)
5fd30ee7 708 rtnl_set_sk_err(net, ops->nlgroup, err);
14c0b97d
TG
709}
710
711static void attach_rules(struct list_head *rules, struct net_device *dev)
712{
713 struct fib_rule *rule;
714
715 list_for_each_entry(rule, rules, list) {
491deb24
PM
716 if (rule->iifindex == -1 &&
717 strcmp(dev->name, rule->iifname) == 0)
718 rule->iifindex = dev->ifindex;
1b038a5e
PM
719 if (rule->oifindex == -1 &&
720 strcmp(dev->name, rule->oifname) == 0)
721 rule->oifindex = dev->ifindex;
14c0b97d
TG
722 }
723}
724
725static void detach_rules(struct list_head *rules, struct net_device *dev)
726{
727 struct fib_rule *rule;
728
1b038a5e 729 list_for_each_entry(rule, rules, list) {
491deb24
PM
730 if (rule->iifindex == dev->ifindex)
731 rule->iifindex = -1;
1b038a5e
PM
732 if (rule->oifindex == dev->ifindex)
733 rule->oifindex = -1;
734 }
14c0b97d
TG
735}
736
737
738static int fib_rules_event(struct notifier_block *this, unsigned long event,
351638e7 739 void *ptr)
14c0b97d 740{
351638e7 741 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c346dca1 742 struct net *net = dev_net(dev);
14c0b97d
TG
743 struct fib_rules_ops *ops;
744
748e2d93 745 ASSERT_RTNL();
14c0b97d
TG
746
747 switch (event) {
748 case NETDEV_REGISTER:
5fd30ee7 749 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 750 attach_rules(&ops->rules_list, dev);
14c0b97d
TG
751 break;
752
946c032e
753 case NETDEV_CHANGENAME:
754 list_for_each_entry(ops, &net->rules_ops, list) {
755 detach_rules(&ops->rules_list, dev);
756 attach_rules(&ops->rules_list, dev);
757 }
758 break;
759
14c0b97d 760 case NETDEV_UNREGISTER:
5fd30ee7 761 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 762 detach_rules(&ops->rules_list, dev);
14c0b97d
TG
763 break;
764 }
765
14c0b97d
TG
766 return NOTIFY_DONE;
767}
768
769static struct notifier_block fib_rules_notifier = {
770 .notifier_call = fib_rules_event,
771};
772
2c8c1e72 773static int __net_init fib_rules_net_init(struct net *net)
5fd30ee7
DL
774{
775 INIT_LIST_HEAD(&net->rules_ops);
776 spin_lock_init(&net->rules_mod_lock);
777 return 0;
778}
779
780static struct pernet_operations fib_rules_net_ops = {
781 .init = fib_rules_net_init,
782};
783
14c0b97d
TG
784static int __init fib_rules_init(void)
785{
5fd30ee7 786 int err;
c7ac8679
GR
787 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
788 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
789 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
9d9e6a58 790
5d6d4809 791 err = register_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
792 if (err < 0)
793 goto fail;
794
5d6d4809 795 err = register_netdevice_notifier(&fib_rules_notifier);
5fd30ee7
DL
796 if (err < 0)
797 goto fail_unregister;
5d6d4809 798
5fd30ee7
DL
799 return 0;
800
801fail_unregister:
5d6d4809 802 unregister_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
803fail:
804 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
805 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
806 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
807 return err;
14c0b97d
TG
808}
809
810subsys_initcall(fib_rules_init);
This page took 0.697344 seconds and 5 git commands to generate.