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