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