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