]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/core/fib_rules.c
UBUNTU: [Debian] config-check -- Make it easier to find annotations syntax errors
[mirror_ubuntu-zesty-kernel.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 18#include <net/fib_rules.h>
e7030878 19#include <net/ip_tunnels.h>
14c0b97d 20
622ec2c9
LC
21static const struct fib_kuid_range fib_kuid_range_unset = {
22 KUIDT_INIT(0),
23 KUIDT_INIT(~0),
24};
25
2994c638
DL
26int fib_default_rule_add(struct fib_rules_ops *ops,
27 u32 pref, u32 table, u32 flags)
28{
29 struct fib_rule *r;
30
31 r = kzalloc(ops->rule_size, GFP_KERNEL);
32 if (r == NULL)
33 return -ENOMEM;
34
35 atomic_set(&r->refcnt, 1);
36 r->action = FR_ACT_TO_TBL;
37 r->pref = pref;
38 r->table = table;
39 r->flags = flags;
efd7ef1c 40 r->fr_net = ops->fro_net;
622ec2c9 41 r->uid_range = fib_kuid_range_unset;
2994c638 42
73f5698e
ST
43 r->suppress_prefixlen = -1;
44 r->suppress_ifgroup = -1;
45
2994c638
DL
46 /* The lock is not required here, the list in unreacheable
47 * at the moment this function is called */
48 list_add_tail(&r->list, &ops->rules_list);
49 return 0;
50}
51EXPORT_SYMBOL(fib_default_rule_add);
52
f53de1e9 53static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
d8a566be
PM
54{
55 struct list_head *pos;
56 struct fib_rule *rule;
57
58 if (!list_empty(&ops->rules_list)) {
59 pos = ops->rules_list.next;
60 if (pos->next != &ops->rules_list) {
61 rule = list_entry(pos->next, struct fib_rule, list);
62 if (rule->pref)
63 return rule->pref - 1;
64 }
65 }
66
67 return 0;
68}
d8a566be 69
9e3a5487 70static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
71 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
72 u32 pid);
14c0b97d 73
5fd30ee7 74static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
14c0b97d
TG
75{
76 struct fib_rules_ops *ops;
77
78 rcu_read_lock();
5fd30ee7 79 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
14c0b97d
TG
80 if (ops->family == family) {
81 if (!try_module_get(ops->owner))
82 ops = NULL;
83 rcu_read_unlock();
84 return ops;
85 }
86 }
87 rcu_read_unlock();
88
89 return NULL;
90}
91
92static void rules_ops_put(struct fib_rules_ops *ops)
93{
94 if (ops)
95 module_put(ops->owner);
96}
97
73417f61
TG
98static void flush_route_cache(struct fib_rules_ops *ops)
99{
100 if (ops->flush_cache)
ae299fc0 101 ops->flush_cache(ops);
73417f61
TG
102}
103
e9c5158a 104static int __fib_rules_register(struct fib_rules_ops *ops)
14c0b97d
TG
105{
106 int err = -EEXIST;
107 struct fib_rules_ops *o;
9e3a5487
DL
108 struct net *net;
109
110 net = ops->fro_net;
14c0b97d
TG
111
112 if (ops->rule_size < sizeof(struct fib_rule))
113 return -EINVAL;
114
115 if (ops->match == NULL || ops->configure == NULL ||
116 ops->compare == NULL || ops->fill == NULL ||
117 ops->action == NULL)
118 return -EINVAL;
119
5fd30ee7
DL
120 spin_lock(&net->rules_mod_lock);
121 list_for_each_entry(o, &net->rules_ops, list)
14c0b97d
TG
122 if (ops->family == o->family)
123 goto errout;
124
5fd30ee7 125 list_add_tail_rcu(&ops->list, &net->rules_ops);
14c0b97d
TG
126 err = 0;
127errout:
5fd30ee7 128 spin_unlock(&net->rules_mod_lock);
14c0b97d
TG
129
130 return err;
131}
132
e9c5158a 133struct fib_rules_ops *
3d0c9c4e 134fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
e9c5158a
EB
135{
136 struct fib_rules_ops *ops;
137 int err;
138
2fb3573d 139 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
e9c5158a
EB
140 if (ops == NULL)
141 return ERR_PTR(-ENOMEM);
142
143 INIT_LIST_HEAD(&ops->rules_list);
144 ops->fro_net = net;
145
146 err = __fib_rules_register(ops);
147 if (err) {
148 kfree(ops);
149 ops = ERR_PTR(err);
150 }
151
152 return ops;
153}
14c0b97d
TG
154EXPORT_SYMBOL_GPL(fib_rules_register);
155
1df9916e 156static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
14c0b97d
TG
157{
158 struct fib_rule *rule, *tmp;
159
76c72d4f 160 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
14c0b97d 161 list_del_rcu(&rule->list);
7a9bc9b8
DM
162 if (ops->delete)
163 ops->delete(rule);
14c0b97d
TG
164 fib_rule_put(rule);
165 }
166}
167
9e3a5487 168void fib_rules_unregister(struct fib_rules_ops *ops)
14c0b97d 169{
9e3a5487 170 struct net *net = ops->fro_net;
14c0b97d 171
5fd30ee7 172 spin_lock(&net->rules_mod_lock);
72132c1b 173 list_del_rcu(&ops->list);
5fd30ee7 174 spin_unlock(&net->rules_mod_lock);
14c0b97d 175
419df12f 176 fib_rules_cleanup_ops(ops);
efd7ef1c 177 kfree_rcu(ops, rcu);
14c0b97d 178}
14c0b97d
TG
179EXPORT_SYMBOL_GPL(fib_rules_unregister);
180
622ec2c9
LC
181static int uid_range_set(struct fib_kuid_range *range)
182{
183 return uid_valid(range->start) && uid_valid(range->end);
184}
185
186static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
187{
188 struct fib_rule_uid_range *in;
189 struct fib_kuid_range out;
190
191 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
192
193 out.start = make_kuid(current_user_ns(), in->start);
194 out.end = make_kuid(current_user_ns(), in->end);
195
196 return out;
197}
198
199static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
200{
201 struct fib_rule_uid_range out = {
202 from_kuid_munged(current_user_ns(), range->start),
203 from_kuid_munged(current_user_ns(), range->end)
204 };
205
206 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
207}
208
3dfbcc41 209static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
96c63fa7
DA
210 struct flowi *fl, int flags,
211 struct fib_lookup_arg *arg)
3dfbcc41
TG
212{
213 int ret = 0;
214
1d28f42c 215 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
3dfbcc41
TG
216 goto out;
217
1d28f42c 218 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
1b038a5e
PM
219 goto out;
220
1d28f42c 221 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
3dfbcc41
TG
222 goto out;
223
e7030878
TG
224 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
225 goto out;
226
96c63fa7
DA
227 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
228 goto out;
229
622ec2c9
LC
230 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
231 uid_gt(fl->flowi_uid, rule->uid_range.end))
232 goto out;
233
3dfbcc41
TG
234 ret = ops->match(rule, fl, flags);
235out:
236 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
237}
238
14c0b97d
TG
239int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
240 int flags, struct fib_lookup_arg *arg)
241{
242 struct fib_rule *rule;
243 int err;
244
245 rcu_read_lock();
246
76c72d4f 247 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
0947c9fe 248jumped:
96c63fa7 249 if (!fib_rule_match(rule, ops, fl, flags, arg))
14c0b97d
TG
250 continue;
251
0947c9fe
TG
252 if (rule->action == FR_ACT_GOTO) {
253 struct fib_rule *target;
254
255 target = rcu_dereference(rule->ctarget);
256 if (target == NULL) {
257 continue;
258 } else {
259 rule = target;
260 goto jumped;
261 }
fa0b2d1d
TG
262 } else if (rule->action == FR_ACT_NOP)
263 continue;
264 else
0947c9fe
TG
265 err = ops->action(rule, fl, flags, arg);
266
7764a45a
ST
267 if (!err && ops->suppress && ops->suppress(rule, arg))
268 continue;
269
14c0b97d 270 if (err != -EAGAIN) {
ebc0ffae
ED
271 if ((arg->flags & FIB_LOOKUP_NOREF) ||
272 likely(atomic_inc_not_zero(&rule->refcnt))) {
7fa7cb71
ED
273 arg->rule = rule;
274 goto out;
275 }
276 break;
14c0b97d
TG
277 }
278 }
279
83886b6b 280 err = -ESRCH;
14c0b97d
TG
281out:
282 rcu_read_unlock();
283
284 return err;
285}
14c0b97d
TG
286EXPORT_SYMBOL_GPL(fib_rules_lookup);
287
e1701c68
TG
288static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
289 struct fib_rules_ops *ops)
290{
291 int err = -EINVAL;
292
293 if (frh->src_len)
294 if (tb[FRA_SRC] == NULL ||
295 frh->src_len > (ops->addr_size * 8) ||
296 nla_len(tb[FRA_SRC]) != ops->addr_size)
297 goto errout;
298
299 if (frh->dst_len)
300 if (tb[FRA_DST] == NULL ||
301 frh->dst_len > (ops->addr_size * 8) ||
302 nla_len(tb[FRA_DST]) != ops->addr_size)
303 goto errout;
304
305 err = 0;
306errout:
307 return err;
308}
309
153380ec
MB
310static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
311 struct nlattr **tb, struct fib_rule *rule)
312{
313 struct fib_rule *r;
314
315 list_for_each_entry(r, &ops->rules_list, list) {
316 if (r->action != rule->action)
317 continue;
318
319 if (r->table != rule->table)
320 continue;
321
322 if (r->pref != rule->pref)
323 continue;
324
325 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
326 continue;
327
328 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
329 continue;
330
331 if (r->mark != rule->mark)
332 continue;
333
334 if (r->mark_mask != rule->mark_mask)
335 continue;
336
337 if (r->tun_id != rule->tun_id)
338 continue;
339
340 if (r->fr_net != rule->fr_net)
341 continue;
342
343 if (r->l3mdev != rule->l3mdev)
344 continue;
345
35b80733
LC
346 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
347 !uid_eq(r->uid_range.end, rule->uid_range.end))
348 continue;
349
153380ec
MB
350 if (!ops->compare(r, frh, tb))
351 continue;
352 return 1;
353 }
354 return 0;
355}
356
96c63fa7 357int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh)
14c0b97d 358{
3b1e0a65 359 struct net *net = sock_net(skb->sk);
14c0b97d
TG
360 struct fib_rule_hdr *frh = nlmsg_data(nlh);
361 struct fib_rules_ops *ops = NULL;
362 struct fib_rule *rule, *r, *last = NULL;
363 struct nlattr *tb[FRA_MAX+1];
0947c9fe 364 int err = -EINVAL, unresolved = 0;
14c0b97d
TG
365
366 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
367 goto errout;
368
5fd30ee7 369 ops = lookup_rules_ops(net, frh->family);
14c0b97d 370 if (ops == NULL) {
2fe195cf 371 err = -EAFNOSUPPORT;
14c0b97d
TG
372 goto errout;
373 }
374
375 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
376 if (err < 0)
377 goto errout;
378
e1701c68
TG
379 err = validate_rulemsg(frh, tb, ops);
380 if (err < 0)
381 goto errout;
382
14c0b97d
TG
383 rule = kzalloc(ops->rule_size, GFP_KERNEL);
384 if (rule == NULL) {
385 err = -ENOMEM;
386 goto errout;
387 }
efd7ef1c 388 rule->fr_net = net;
14c0b97d 389
f53de1e9
PS
390 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
391 : fib_default_rule_pref(ops);
14c0b97d 392
491deb24 393 if (tb[FRA_IIFNAME]) {
14c0b97d
TG
394 struct net_device *dev;
395
491deb24
PM
396 rule->iifindex = -1;
397 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
398 dev = __dev_get_by_name(net, rule->iifname);
14c0b97d 399 if (dev)
491deb24 400 rule->iifindex = dev->ifindex;
14c0b97d
TG
401 }
402
1b038a5e
PM
403 if (tb[FRA_OIFNAME]) {
404 struct net_device *dev;
405
406 rule->oifindex = -1;
407 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
408 dev = __dev_get_by_name(net, rule->oifname);
409 if (dev)
410 rule->oifindex = dev->ifindex;
411 }
412
b8964ed9
TG
413 if (tb[FRA_FWMARK]) {
414 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
415 if (rule->mark)
416 /* compatibility: if the mark value is non-zero all bits
417 * are compared unless a mask is explicitly specified.
418 */
419 rule->mark_mask = 0xFFFFFFFF;
420 }
421
422 if (tb[FRA_FWMASK])
423 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
424
e7030878
TG
425 if (tb[FRA_TUN_ID])
426 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
427
96c63fa7
DA
428 if (tb[FRA_L3MDEV]) {
429#ifdef CONFIG_NET_L3_MASTER_DEV
430 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
431 if (rule->l3mdev != 1)
432#endif
433 goto errout_free;
434 }
435
14c0b97d
TG
436 rule->action = frh->action;
437 rule->flags = frh->flags;
9e762a4a 438 rule->table = frh_get_table(frh, tb);
73f5698e
ST
439 if (tb[FRA_SUPPRESS_PREFIXLEN])
440 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
441 else
442 rule->suppress_prefixlen = -1;
14c0b97d 443
6ef94cfa
ST
444 if (tb[FRA_SUPPRESS_IFGROUP])
445 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
73f5698e
ST
446 else
447 rule->suppress_ifgroup = -1;
6ef94cfa 448
0947c9fe
TG
449 err = -EINVAL;
450 if (tb[FRA_GOTO]) {
451 if (rule->action != FR_ACT_GOTO)
452 goto errout_free;
453
454 rule->target = nla_get_u32(tb[FRA_GOTO]);
455 /* Backward jumps are prohibited to avoid endless loops */
456 if (rule->target <= rule->pref)
457 goto errout_free;
458
76c72d4f 459 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 460 if (r->pref == rule->target) {
7a2b03c5 461 RCU_INIT_POINTER(rule->ctarget, r);
0947c9fe
TG
462 break;
463 }
464 }
465
7a2b03c5 466 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
0947c9fe
TG
467 unresolved = 1;
468 } else if (rule->action == FR_ACT_GOTO)
469 goto errout_free;
470
96c63fa7
DA
471 if (rule->l3mdev && rule->table)
472 goto errout_free;
473
622ec2c9
LC
474 if (tb[FRA_UID_RANGE]) {
475 if (current_user_ns() != net->user_ns) {
476 err = -EPERM;
477 goto errout_free;
478 }
479
480 rule->uid_range = nla_get_kuid_range(tb);
481
482 if (!uid_range_set(&rule->uid_range) ||
483 !uid_lte(rule->uid_range.start, rule->uid_range.end))
484 goto errout_free;
485 } else {
486 rule->uid_range = fib_kuid_range_unset;
487 }
488
153380ec
MB
489 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
490 rule_exists(ops, frh, tb, rule)) {
491 err = -EEXIST;
492 goto errout_free;
493 }
494
8b3521ee 495 err = ops->configure(rule, skb, frh, tb);
14c0b97d
TG
496 if (err < 0)
497 goto errout_free;
498
76c72d4f 499 list_for_each_entry(r, &ops->rules_list, list) {
14c0b97d
TG
500 if (r->pref > rule->pref)
501 break;
502 last = r;
503 }
504
505 fib_rule_get(rule);
506
ebb9fed2
ED
507 if (last)
508 list_add_rcu(&rule->list, &last->list);
509 else
510 list_add_rcu(&rule->list, &ops->rules_list);
511
0947c9fe
TG
512 if (ops->unresolved_rules) {
513 /*
514 * There are unresolved goto rules in the list, check if
515 * any of them are pointing to this new rule.
516 */
76c72d4f 517 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 518 if (r->action == FR_ACT_GOTO &&
561dac2d
G
519 r->target == rule->pref &&
520 rtnl_dereference(r->ctarget) == NULL) {
0947c9fe
TG
521 rcu_assign_pointer(r->ctarget, rule);
522 if (--ops->unresolved_rules == 0)
523 break;
524 }
525 }
526 }
527
528 if (rule->action == FR_ACT_GOTO)
529 ops->nr_goto_rules++;
530
531 if (unresolved)
532 ops->unresolved_rules++;
533
e7030878
TG
534 if (rule->tun_id)
535 ip_tunnel_need_metadata();
536
15e47304 537 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
73417f61 538 flush_route_cache(ops);
14c0b97d
TG
539 rules_ops_put(ops);
540 return 0;
541
542errout_free:
543 kfree(rule);
544errout:
545 rules_ops_put(ops);
546 return err;
547}
96c63fa7 548EXPORT_SYMBOL_GPL(fib_nl_newrule);
14c0b97d 549
96c63fa7 550int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh)
14c0b97d 551{
3b1e0a65 552 struct net *net = sock_net(skb->sk);
14c0b97d
TG
553 struct fib_rule_hdr *frh = nlmsg_data(nlh);
554 struct fib_rules_ops *ops = NULL;
0947c9fe 555 struct fib_rule *rule, *tmp;
14c0b97d 556 struct nlattr *tb[FRA_MAX+1];
622ec2c9 557 struct fib_kuid_range range;
14c0b97d
TG
558 int err = -EINVAL;
559
560 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
561 goto errout;
562
5fd30ee7 563 ops = lookup_rules_ops(net, frh->family);
14c0b97d 564 if (ops == NULL) {
2fe195cf 565 err = -EAFNOSUPPORT;
14c0b97d
TG
566 goto errout;
567 }
568
569 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
570 if (err < 0)
571 goto errout;
572
e1701c68
TG
573 err = validate_rulemsg(frh, tb, ops);
574 if (err < 0)
575 goto errout;
576
622ec2c9
LC
577 if (tb[FRA_UID_RANGE]) {
578 range = nla_get_kuid_range(tb);
579 if (!uid_range_set(&range))
580 goto errout;
581 } else {
582 range = fib_kuid_range_unset;
583 }
584
76c72d4f 585 list_for_each_entry(rule, &ops->rules_list, list) {
14c0b97d
TG
586 if (frh->action && (frh->action != rule->action))
587 continue;
588
13eb2ab2
AH
589 if (frh_get_table(frh, tb) &&
590 (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
591 continue;
592
593 if (tb[FRA_PRIORITY] &&
594 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
595 continue;
596
491deb24
PM
597 if (tb[FRA_IIFNAME] &&
598 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
14c0b97d
TG
599 continue;
600
1b038a5e
PM
601 if (tb[FRA_OIFNAME] &&
602 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
603 continue;
604
b8964ed9
TG
605 if (tb[FRA_FWMARK] &&
606 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
607 continue;
608
609 if (tb[FRA_FWMASK] &&
610 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
611 continue;
612
e7030878
TG
613 if (tb[FRA_TUN_ID] &&
614 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
615 continue;
616
96c63fa7
DA
617 if (tb[FRA_L3MDEV] &&
618 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
619 continue;
620
622ec2c9
LC
621 if (uid_range_set(&range) &&
622 (!uid_eq(rule->uid_range.start, range.start) ||
623 !uid_eq(rule->uid_range.end, range.end)))
624 continue;
625
14c0b97d
TG
626 if (!ops->compare(rule, frh, tb))
627 continue;
628
629 if (rule->flags & FIB_RULE_PERMANENT) {
630 err = -EPERM;
631 goto errout;
632 }
633
0ddcf43d
AD
634 if (ops->delete) {
635 err = ops->delete(rule);
636 if (err)
637 goto errout;
638 }
639
e7030878
TG
640 if (rule->tun_id)
641 ip_tunnel_unneed_metadata();
642
14c0b97d 643 list_del_rcu(&rule->list);
0947c9fe 644
afaef734 645 if (rule->action == FR_ACT_GOTO) {
0947c9fe 646 ops->nr_goto_rules--;
afaef734
YZ
647 if (rtnl_dereference(rule->ctarget) == NULL)
648 ops->unresolved_rules--;
649 }
0947c9fe
TG
650
651 /*
652 * Check if this rule is a target to any of them. If so,
653 * disable them. As this operation is eventually very
654 * expensive, it is only performed if goto rules have
655 * actually been added.
656 */
657 if (ops->nr_goto_rules > 0) {
76c72d4f 658 list_for_each_entry(tmp, &ops->rules_list, list) {
7a2b03c5 659 if (rtnl_dereference(tmp->ctarget) == rule) {
a9b3cd7f 660 RCU_INIT_POINTER(tmp->ctarget, NULL);
0947c9fe
TG
661 ops->unresolved_rules++;
662 }
663 }
664 }
665
9e3a5487 666 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
15e47304 667 NETLINK_CB(skb).portid);
14c0b97d 668 fib_rule_put(rule);
73417f61 669 flush_route_cache(ops);
14c0b97d
TG
670 rules_ops_put(ops);
671 return 0;
672 }
673
674 err = -ENOENT;
675errout:
676 rules_ops_put(ops);
677 return err;
678}
96c63fa7 679EXPORT_SYMBOL_GPL(fib_nl_delrule);
14c0b97d 680
339bf98f
TG
681static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
682 struct fib_rule *rule)
683{
684 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
491deb24 685 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
1b038a5e 686 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
339bf98f
TG
687 + nla_total_size(4) /* FRA_PRIORITY */
688 + nla_total_size(4) /* FRA_TABLE */
73f5698e 689 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
6ef94cfa 690 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
339bf98f 691 + nla_total_size(4) /* FRA_FWMARK */
e7030878 692 + nla_total_size(4) /* FRA_FWMASK */
622ec2c9
LC
693 + nla_total_size_64bit(8) /* FRA_TUN_ID */
694 + nla_total_size(sizeof(struct fib_kuid_range));
339bf98f
TG
695
696 if (ops->nlmsg_payload)
697 payload += ops->nlmsg_payload(rule);
698
699 return payload;
700}
701
14c0b97d
TG
702static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
703 u32 pid, u32 seq, int type, int flags,
704 struct fib_rules_ops *ops)
705{
706 struct nlmsghdr *nlh;
707 struct fib_rule_hdr *frh;
708
709 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
710 if (nlh == NULL)
26932566 711 return -EMSGSIZE;
14c0b97d
TG
712
713 frh = nlmsg_data(nlh);
28bb1726 714 frh->family = ops->family;
14c0b97d 715 frh->table = rule->table;
0e3cea7b
DM
716 if (nla_put_u32(skb, FRA_TABLE, rule->table))
717 goto nla_put_failure;
73f5698e 718 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
7764a45a 719 goto nla_put_failure;
14c0b97d
TG
720 frh->res1 = 0;
721 frh->res2 = 0;
722 frh->action = rule->action;
723 frh->flags = rule->flags;
724
7a2b03c5 725 if (rule->action == FR_ACT_GOTO &&
33d480ce 726 rcu_access_pointer(rule->ctarget) == NULL)
0947c9fe
TG
727 frh->flags |= FIB_RULE_UNRESOLVED;
728
491deb24 729 if (rule->iifname[0]) {
0e3cea7b
DM
730 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
731 goto nla_put_failure;
491deb24
PM
732 if (rule->iifindex == -1)
733 frh->flags |= FIB_RULE_IIF_DETACHED;
2b443683
TG
734 }
735
1b038a5e 736 if (rule->oifname[0]) {
0e3cea7b
DM
737 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
738 goto nla_put_failure;
1b038a5e
PM
739 if (rule->oifindex == -1)
740 frh->flags |= FIB_RULE_OIF_DETACHED;
741 }
742
0e3cea7b
DM
743 if ((rule->pref &&
744 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
745 (rule->mark &&
746 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
747 ((rule->mark_mask || rule->mark) &&
748 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
749 (rule->target &&
e7030878
TG
750 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
751 (rule->tun_id &&
96c63fa7
DA
752 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
753 (rule->l3mdev &&
622ec2c9
LC
754 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
755 (uid_range_set(&rule->uid_range) &&
756 nla_put_uid_range(skb, &rule->uid_range)))
0e3cea7b 757 goto nla_put_failure;
6ef94cfa
ST
758
759 if (rule->suppress_ifgroup != -1) {
760 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
761 goto nla_put_failure;
762 }
763
04af8cf6 764 if (ops->fill(rule, skb, frh) < 0)
14c0b97d
TG
765 goto nla_put_failure;
766
053c095a
JB
767 nlmsg_end(skb, nlh);
768 return 0;
14c0b97d
TG
769
770nla_put_failure:
26932566
PM
771 nlmsg_cancel(skb, nlh);
772 return -EMSGSIZE;
14c0b97d
TG
773}
774
c454673d
TG
775static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
776 struct fib_rules_ops *ops)
14c0b97d
TG
777{
778 int idx = 0;
779 struct fib_rule *rule;
41fc0143 780 int err = 0;
14c0b97d 781
e67f88dd
ED
782 rcu_read_lock();
783 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
c454673d 784 if (idx < cb->args[1])
14c0b97d
TG
785 goto skip;
786
41fc0143
WK
787 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
788 cb->nlh->nlmsg_seq, RTM_NEWRULE,
789 NLM_F_MULTI, ops);
790 if (err)
14c0b97d
TG
791 break;
792skip:
793 idx++;
794 }
2907c35f 795 rcu_read_unlock();
c454673d 796 cb->args[1] = idx;
14c0b97d
TG
797 rules_ops_put(ops);
798
41fc0143 799 return err;
14c0b97d
TG
800}
801
c454673d
TG
802static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
803{
3b1e0a65 804 struct net *net = sock_net(skb->sk);
c454673d
TG
805 struct fib_rules_ops *ops;
806 int idx = 0, family;
807
808 family = rtnl_msg_family(cb->nlh);
809 if (family != AF_UNSPEC) {
810 /* Protocol specific dump request */
5fd30ee7 811 ops = lookup_rules_ops(net, family);
c454673d
TG
812 if (ops == NULL)
813 return -EAFNOSUPPORT;
814
41fc0143
WK
815 dump_rules(skb, cb, ops);
816
817 return skb->len;
c454673d
TG
818 }
819
820 rcu_read_lock();
5fd30ee7 821 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
c454673d
TG
822 if (idx < cb->args[0] || !try_module_get(ops->owner))
823 goto skip;
824
825 if (dump_rules(skb, cb, ops) < 0)
826 break;
827
828 cb->args[1] = 0;
2fb3573d 829skip:
c454673d
TG
830 idx++;
831 }
832 rcu_read_unlock();
833 cb->args[0] = idx;
834
835 return skb->len;
836}
14c0b97d 837
9e3a5487 838static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
839 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
840 u32 pid)
14c0b97d 841{
9e3a5487 842 struct net *net;
c17084d2
TG
843 struct sk_buff *skb;
844 int err = -ENOBUFS;
14c0b97d 845
9e3a5487 846 net = ops->fro_net;
339bf98f 847 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
14c0b97d 848 if (skb == NULL)
c17084d2
TG
849 goto errout;
850
851 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
26932566
PM
852 if (err < 0) {
853 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
854 WARN_ON(err == -EMSGSIZE);
855 kfree_skb(skb);
856 goto errout;
857 }
9e3a5487 858
1ce85fe4
PNA
859 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
860 return;
c17084d2
TG
861errout:
862 if (err < 0)
5fd30ee7 863 rtnl_set_sk_err(net, ops->nlgroup, err);
14c0b97d
TG
864}
865
866static void attach_rules(struct list_head *rules, struct net_device *dev)
867{
868 struct fib_rule *rule;
869
870 list_for_each_entry(rule, rules, list) {
491deb24
PM
871 if (rule->iifindex == -1 &&
872 strcmp(dev->name, rule->iifname) == 0)
873 rule->iifindex = dev->ifindex;
1b038a5e
PM
874 if (rule->oifindex == -1 &&
875 strcmp(dev->name, rule->oifname) == 0)
876 rule->oifindex = dev->ifindex;
14c0b97d
TG
877 }
878}
879
880static void detach_rules(struct list_head *rules, struct net_device *dev)
881{
882 struct fib_rule *rule;
883
1b038a5e 884 list_for_each_entry(rule, rules, list) {
491deb24
PM
885 if (rule->iifindex == dev->ifindex)
886 rule->iifindex = -1;
1b038a5e
PM
887 if (rule->oifindex == dev->ifindex)
888 rule->oifindex = -1;
889 }
14c0b97d
TG
890}
891
892
893static int fib_rules_event(struct notifier_block *this, unsigned long event,
351638e7 894 void *ptr)
14c0b97d 895{
351638e7 896 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c346dca1 897 struct net *net = dev_net(dev);
14c0b97d
TG
898 struct fib_rules_ops *ops;
899
748e2d93 900 ASSERT_RTNL();
14c0b97d
TG
901
902 switch (event) {
903 case NETDEV_REGISTER:
5fd30ee7 904 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 905 attach_rules(&ops->rules_list, dev);
14c0b97d
TG
906 break;
907
946c032e
908 case NETDEV_CHANGENAME:
909 list_for_each_entry(ops, &net->rules_ops, list) {
910 detach_rules(&ops->rules_list, dev);
911 attach_rules(&ops->rules_list, dev);
912 }
913 break;
914
14c0b97d 915 case NETDEV_UNREGISTER:
5fd30ee7 916 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 917 detach_rules(&ops->rules_list, dev);
14c0b97d
TG
918 break;
919 }
920
14c0b97d
TG
921 return NOTIFY_DONE;
922}
923
924static struct notifier_block fib_rules_notifier = {
925 .notifier_call = fib_rules_event,
926};
927
2c8c1e72 928static int __net_init fib_rules_net_init(struct net *net)
5fd30ee7
DL
929{
930 INIT_LIST_HEAD(&net->rules_ops);
931 spin_lock_init(&net->rules_mod_lock);
932 return 0;
933}
934
935static struct pernet_operations fib_rules_net_ops = {
936 .init = fib_rules_net_init,
937};
938
14c0b97d
TG
939static int __init fib_rules_init(void)
940{
5fd30ee7 941 int err;
c7ac8679
GR
942 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
943 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
944 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
9d9e6a58 945
5d6d4809 946 err = register_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
947 if (err < 0)
948 goto fail;
949
5d6d4809 950 err = register_netdevice_notifier(&fib_rules_notifier);
5fd30ee7
DL
951 if (err < 0)
952 goto fail_unregister;
5d6d4809 953
5fd30ee7
DL
954 return 0;
955
956fail_unregister:
5d6d4809 957 unregister_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
958fail:
959 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
960 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
961 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
962 return err;
14c0b97d
TG
963}
964
965subsys_initcall(fib_rules_init);