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