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