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