]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/core/fib_rules.c
Merge remote-tracking branch 'regulator/fix/max77802' into regulator-linus
[mirror_ubuntu-artful-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
49 atomic_set(&r->refcnt, 1);
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
ED
285 if ((arg->flags & FIB_LOOKUP_NOREF) ||
286 likely(atomic_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
e1701c68
TG
302static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
303 struct fib_rules_ops *ops)
304{
305 int err = -EINVAL;
306
307 if (frh->src_len)
308 if (tb[FRA_SRC] == NULL ||
309 frh->src_len > (ops->addr_size * 8) ||
310 nla_len(tb[FRA_SRC]) != ops->addr_size)
311 goto errout;
312
313 if (frh->dst_len)
314 if (tb[FRA_DST] == NULL ||
315 frh->dst_len > (ops->addr_size * 8) ||
316 nla_len(tb[FRA_DST]) != ops->addr_size)
317 goto errout;
318
319 err = 0;
320errout:
321 return err;
322}
323
153380ec
MB
324static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
325 struct nlattr **tb, struct fib_rule *rule)
326{
327 struct fib_rule *r;
328
329 list_for_each_entry(r, &ops->rules_list, list) {
330 if (r->action != rule->action)
331 continue;
332
333 if (r->table != rule->table)
334 continue;
335
336 if (r->pref != rule->pref)
337 continue;
338
339 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
340 continue;
341
342 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
343 continue;
344
345 if (r->mark != rule->mark)
346 continue;
347
348 if (r->mark_mask != rule->mark_mask)
349 continue;
350
351 if (r->tun_id != rule->tun_id)
352 continue;
353
354 if (r->fr_net != rule->fr_net)
355 continue;
356
357 if (r->l3mdev != rule->l3mdev)
358 continue;
359
35b80733
LC
360 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
361 !uid_eq(r->uid_range.end, rule->uid_range.end))
362 continue;
363
153380ec
MB
364 if (!ops->compare(r, frh, tb))
365 continue;
366 return 1;
367 }
368 return 0;
369}
370
c21ef3e3
DA
371int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
372 struct netlink_ext_ack *extack)
14c0b97d 373{
3b1e0a65 374 struct net *net = sock_net(skb->sk);
14c0b97d
TG
375 struct fib_rule_hdr *frh = nlmsg_data(nlh);
376 struct fib_rules_ops *ops = NULL;
377 struct fib_rule *rule, *r, *last = NULL;
378 struct nlattr *tb[FRA_MAX+1];
0947c9fe 379 int err = -EINVAL, unresolved = 0;
14c0b97d
TG
380
381 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
382 goto errout;
383
5fd30ee7 384 ops = lookup_rules_ops(net, frh->family);
14c0b97d 385 if (ops == NULL) {
2fe195cf 386 err = -EAFNOSUPPORT;
14c0b97d
TG
387 goto errout;
388 }
389
c21ef3e3 390 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
14c0b97d
TG
391 if (err < 0)
392 goto errout;
393
e1701c68
TG
394 err = validate_rulemsg(frh, tb, ops);
395 if (err < 0)
396 goto errout;
397
14c0b97d
TG
398 rule = kzalloc(ops->rule_size, GFP_KERNEL);
399 if (rule == NULL) {
400 err = -ENOMEM;
401 goto errout;
402 }
efd7ef1c 403 rule->fr_net = net;
14c0b97d 404
f53de1e9
PS
405 rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY])
406 : fib_default_rule_pref(ops);
14c0b97d 407
491deb24 408 if (tb[FRA_IIFNAME]) {
14c0b97d
TG
409 struct net_device *dev;
410
491deb24
PM
411 rule->iifindex = -1;
412 nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
413 dev = __dev_get_by_name(net, rule->iifname);
14c0b97d 414 if (dev)
491deb24 415 rule->iifindex = dev->ifindex;
14c0b97d
TG
416 }
417
1b038a5e
PM
418 if (tb[FRA_OIFNAME]) {
419 struct net_device *dev;
420
421 rule->oifindex = -1;
422 nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
423 dev = __dev_get_by_name(net, rule->oifname);
424 if (dev)
425 rule->oifindex = dev->ifindex;
426 }
427
b8964ed9
TG
428 if (tb[FRA_FWMARK]) {
429 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
430 if (rule->mark)
431 /* compatibility: if the mark value is non-zero all bits
432 * are compared unless a mask is explicitly specified.
433 */
434 rule->mark_mask = 0xFFFFFFFF;
435 }
436
437 if (tb[FRA_FWMASK])
438 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
439
e7030878
TG
440 if (tb[FRA_TUN_ID])
441 rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
442
adeb45cb 443 err = -EINVAL;
96c63fa7
DA
444 if (tb[FRA_L3MDEV]) {
445#ifdef CONFIG_NET_L3_MASTER_DEV
446 rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]);
447 if (rule->l3mdev != 1)
448#endif
449 goto errout_free;
450 }
451
14c0b97d
TG
452 rule->action = frh->action;
453 rule->flags = frh->flags;
9e762a4a 454 rule->table = frh_get_table(frh, tb);
73f5698e
ST
455 if (tb[FRA_SUPPRESS_PREFIXLEN])
456 rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
457 else
458 rule->suppress_prefixlen = -1;
14c0b97d 459
6ef94cfa
ST
460 if (tb[FRA_SUPPRESS_IFGROUP])
461 rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
73f5698e
ST
462 else
463 rule->suppress_ifgroup = -1;
6ef94cfa 464
0947c9fe
TG
465 if (tb[FRA_GOTO]) {
466 if (rule->action != FR_ACT_GOTO)
467 goto errout_free;
468
469 rule->target = nla_get_u32(tb[FRA_GOTO]);
470 /* Backward jumps are prohibited to avoid endless loops */
471 if (rule->target <= rule->pref)
472 goto errout_free;
473
76c72d4f 474 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 475 if (r->pref == rule->target) {
7a2b03c5 476 RCU_INIT_POINTER(rule->ctarget, r);
0947c9fe
TG
477 break;
478 }
479 }
480
7a2b03c5 481 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
0947c9fe
TG
482 unresolved = 1;
483 } else if (rule->action == FR_ACT_GOTO)
484 goto errout_free;
485
96c63fa7
DA
486 if (rule->l3mdev && rule->table)
487 goto errout_free;
488
622ec2c9
LC
489 if (tb[FRA_UID_RANGE]) {
490 if (current_user_ns() != net->user_ns) {
491 err = -EPERM;
492 goto errout_free;
493 }
494
495 rule->uid_range = nla_get_kuid_range(tb);
496
497 if (!uid_range_set(&rule->uid_range) ||
498 !uid_lte(rule->uid_range.start, rule->uid_range.end))
499 goto errout_free;
500 } else {
501 rule->uid_range = fib_kuid_range_unset;
502 }
503
153380ec
MB
504 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
505 rule_exists(ops, frh, tb, rule)) {
506 err = -EEXIST;
507 goto errout_free;
508 }
509
8b3521ee 510 err = ops->configure(rule, skb, frh, tb);
14c0b97d
TG
511 if (err < 0)
512 goto errout_free;
513
76c72d4f 514 list_for_each_entry(r, &ops->rules_list, list) {
14c0b97d
TG
515 if (r->pref > rule->pref)
516 break;
517 last = r;
518 }
519
520 fib_rule_get(rule);
521
ebb9fed2
ED
522 if (last)
523 list_add_rcu(&rule->list, &last->list);
524 else
525 list_add_rcu(&rule->list, &ops->rules_list);
526
0947c9fe
TG
527 if (ops->unresolved_rules) {
528 /*
529 * There are unresolved goto rules in the list, check if
530 * any of them are pointing to this new rule.
531 */
76c72d4f 532 list_for_each_entry(r, &ops->rules_list, list) {
0947c9fe 533 if (r->action == FR_ACT_GOTO &&
561dac2d
G
534 r->target == rule->pref &&
535 rtnl_dereference(r->ctarget) == NULL) {
0947c9fe
TG
536 rcu_assign_pointer(r->ctarget, rule);
537 if (--ops->unresolved_rules == 0)
538 break;
539 }
540 }
541 }
542
543 if (rule->action == FR_ACT_GOTO)
544 ops->nr_goto_rules++;
545
546 if (unresolved)
547 ops->unresolved_rules++;
548
e7030878
TG
549 if (rule->tun_id)
550 ip_tunnel_need_metadata();
551
15e47304 552 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
73417f61 553 flush_route_cache(ops);
14c0b97d
TG
554 rules_ops_put(ops);
555 return 0;
556
557errout_free:
558 kfree(rule);
559errout:
560 rules_ops_put(ops);
561 return err;
562}
96c63fa7 563EXPORT_SYMBOL_GPL(fib_nl_newrule);
14c0b97d 564
c21ef3e3
DA
565int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
566 struct netlink_ext_ack *extack)
14c0b97d 567{
3b1e0a65 568 struct net *net = sock_net(skb->sk);
14c0b97d
TG
569 struct fib_rule_hdr *frh = nlmsg_data(nlh);
570 struct fib_rules_ops *ops = NULL;
bdaf32c3 571 struct fib_rule *rule, *r;
14c0b97d 572 struct nlattr *tb[FRA_MAX+1];
622ec2c9 573 struct fib_kuid_range range;
14c0b97d
TG
574 int err = -EINVAL;
575
576 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
577 goto errout;
578
5fd30ee7 579 ops = lookup_rules_ops(net, frh->family);
14c0b97d 580 if (ops == NULL) {
2fe195cf 581 err = -EAFNOSUPPORT;
14c0b97d
TG
582 goto errout;
583 }
584
c21ef3e3 585 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, extack);
14c0b97d
TG
586 if (err < 0)
587 goto errout;
588
e1701c68
TG
589 err = validate_rulemsg(frh, tb, ops);
590 if (err < 0)
591 goto errout;
592
622ec2c9
LC
593 if (tb[FRA_UID_RANGE]) {
594 range = nla_get_kuid_range(tb);
adeb45cb
WY
595 if (!uid_range_set(&range)) {
596 err = -EINVAL;
622ec2c9 597 goto errout;
adeb45cb 598 }
622ec2c9
LC
599 } else {
600 range = fib_kuid_range_unset;
601 }
602
76c72d4f 603 list_for_each_entry(rule, &ops->rules_list, list) {
14c0b97d
TG
604 if (frh->action && (frh->action != rule->action))
605 continue;
606
13eb2ab2
AH
607 if (frh_get_table(frh, tb) &&
608 (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
609 continue;
610
611 if (tb[FRA_PRIORITY] &&
612 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
613 continue;
614
491deb24
PM
615 if (tb[FRA_IIFNAME] &&
616 nla_strcmp(tb[FRA_IIFNAME], rule->iifname))
14c0b97d
TG
617 continue;
618
1b038a5e
PM
619 if (tb[FRA_OIFNAME] &&
620 nla_strcmp(tb[FRA_OIFNAME], rule->oifname))
621 continue;
622
b8964ed9
TG
623 if (tb[FRA_FWMARK] &&
624 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
625 continue;
626
627 if (tb[FRA_FWMASK] &&
628 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
629 continue;
630
e7030878
TG
631 if (tb[FRA_TUN_ID] &&
632 (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID])))
633 continue;
634
96c63fa7
DA
635 if (tb[FRA_L3MDEV] &&
636 (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV])))
637 continue;
638
622ec2c9
LC
639 if (uid_range_set(&range) &&
640 (!uid_eq(rule->uid_range.start, range.start) ||
641 !uid_eq(rule->uid_range.end, range.end)))
642 continue;
643
14c0b97d
TG
644 if (!ops->compare(rule, frh, tb))
645 continue;
646
647 if (rule->flags & FIB_RULE_PERMANENT) {
648 err = -EPERM;
649 goto errout;
650 }
651
0ddcf43d
AD
652 if (ops->delete) {
653 err = ops->delete(rule);
654 if (err)
655 goto errout;
656 }
657
e7030878
TG
658 if (rule->tun_id)
659 ip_tunnel_unneed_metadata();
660
14c0b97d 661 list_del_rcu(&rule->list);
0947c9fe 662
afaef734 663 if (rule->action == FR_ACT_GOTO) {
0947c9fe 664 ops->nr_goto_rules--;
afaef734
YZ
665 if (rtnl_dereference(rule->ctarget) == NULL)
666 ops->unresolved_rules--;
667 }
0947c9fe
TG
668
669 /*
670 * Check if this rule is a target to any of them. If so,
bdaf32c3 671 * adjust to the next one with the same preference or
0947c9fe 672 * disable them. As this operation is eventually very
bdaf32c3
SP
673 * expensive, it is only performed if goto rules, except
674 * current if it is goto rule, have actually been added.
0947c9fe
TG
675 */
676 if (ops->nr_goto_rules > 0) {
bdaf32c3
SP
677 struct fib_rule *n;
678
679 n = list_next_entry(rule, list);
680 if (&n->list == &ops->rules_list || n->pref != rule->pref)
681 n = NULL;
682 list_for_each_entry(r, &ops->rules_list, list) {
683 if (rtnl_dereference(r->ctarget) != rule)
684 continue;
685 rcu_assign_pointer(r->ctarget, n);
686 if (!n)
0947c9fe 687 ops->unresolved_rules++;
0947c9fe
TG
688 }
689 }
690
9e3a5487 691 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
15e47304 692 NETLINK_CB(skb).portid);
14c0b97d 693 fib_rule_put(rule);
73417f61 694 flush_route_cache(ops);
14c0b97d
TG
695 rules_ops_put(ops);
696 return 0;
697 }
698
699 err = -ENOENT;
700errout:
701 rules_ops_put(ops);
702 return err;
703}
96c63fa7 704EXPORT_SYMBOL_GPL(fib_nl_delrule);
14c0b97d 705
339bf98f
TG
706static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
707 struct fib_rule *rule)
708{
709 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
491deb24 710 + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */
1b038a5e 711 + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */
339bf98f
TG
712 + nla_total_size(4) /* FRA_PRIORITY */
713 + nla_total_size(4) /* FRA_TABLE */
73f5698e 714 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
6ef94cfa 715 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
339bf98f 716 + nla_total_size(4) /* FRA_FWMARK */
e7030878 717 + nla_total_size(4) /* FRA_FWMASK */
622ec2c9
LC
718 + nla_total_size_64bit(8) /* FRA_TUN_ID */
719 + nla_total_size(sizeof(struct fib_kuid_range));
339bf98f
TG
720
721 if (ops->nlmsg_payload)
722 payload += ops->nlmsg_payload(rule);
723
724 return payload;
725}
726
14c0b97d
TG
727static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
728 u32 pid, u32 seq, int type, int flags,
729 struct fib_rules_ops *ops)
730{
731 struct nlmsghdr *nlh;
732 struct fib_rule_hdr *frh;
733
734 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
735 if (nlh == NULL)
26932566 736 return -EMSGSIZE;
14c0b97d
TG
737
738 frh = nlmsg_data(nlh);
28bb1726 739 frh->family = ops->family;
14c0b97d 740 frh->table = rule->table;
0e3cea7b
DM
741 if (nla_put_u32(skb, FRA_TABLE, rule->table))
742 goto nla_put_failure;
73f5698e 743 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
7764a45a 744 goto nla_put_failure;
14c0b97d
TG
745 frh->res1 = 0;
746 frh->res2 = 0;
747 frh->action = rule->action;
748 frh->flags = rule->flags;
749
7a2b03c5 750 if (rule->action == FR_ACT_GOTO &&
33d480ce 751 rcu_access_pointer(rule->ctarget) == NULL)
0947c9fe
TG
752 frh->flags |= FIB_RULE_UNRESOLVED;
753
491deb24 754 if (rule->iifname[0]) {
0e3cea7b
DM
755 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
756 goto nla_put_failure;
491deb24
PM
757 if (rule->iifindex == -1)
758 frh->flags |= FIB_RULE_IIF_DETACHED;
2b443683
TG
759 }
760
1b038a5e 761 if (rule->oifname[0]) {
0e3cea7b
DM
762 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
763 goto nla_put_failure;
1b038a5e
PM
764 if (rule->oifindex == -1)
765 frh->flags |= FIB_RULE_OIF_DETACHED;
766 }
767
0e3cea7b
DM
768 if ((rule->pref &&
769 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
770 (rule->mark &&
771 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
772 ((rule->mark_mask || rule->mark) &&
773 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
774 (rule->target &&
e7030878
TG
775 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
776 (rule->tun_id &&
96c63fa7
DA
777 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
778 (rule->l3mdev &&
622ec2c9
LC
779 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
780 (uid_range_set(&rule->uid_range) &&
781 nla_put_uid_range(skb, &rule->uid_range)))
0e3cea7b 782 goto nla_put_failure;
6ef94cfa
ST
783
784 if (rule->suppress_ifgroup != -1) {
785 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
786 goto nla_put_failure;
787 }
788
04af8cf6 789 if (ops->fill(rule, skb, frh) < 0)
14c0b97d
TG
790 goto nla_put_failure;
791
053c095a
JB
792 nlmsg_end(skb, nlh);
793 return 0;
14c0b97d
TG
794
795nla_put_failure:
26932566
PM
796 nlmsg_cancel(skb, nlh);
797 return -EMSGSIZE;
14c0b97d
TG
798}
799
c454673d
TG
800static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
801 struct fib_rules_ops *ops)
14c0b97d
TG
802{
803 int idx = 0;
804 struct fib_rule *rule;
41fc0143 805 int err = 0;
14c0b97d 806
e67f88dd
ED
807 rcu_read_lock();
808 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
c454673d 809 if (idx < cb->args[1])
14c0b97d
TG
810 goto skip;
811
41fc0143
WK
812 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
813 cb->nlh->nlmsg_seq, RTM_NEWRULE,
814 NLM_F_MULTI, ops);
815 if (err)
14c0b97d
TG
816 break;
817skip:
818 idx++;
819 }
2907c35f 820 rcu_read_unlock();
c454673d 821 cb->args[1] = idx;
14c0b97d
TG
822 rules_ops_put(ops);
823
41fc0143 824 return err;
14c0b97d
TG
825}
826
c454673d
TG
827static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
828{
3b1e0a65 829 struct net *net = sock_net(skb->sk);
c454673d
TG
830 struct fib_rules_ops *ops;
831 int idx = 0, family;
832
833 family = rtnl_msg_family(cb->nlh);
834 if (family != AF_UNSPEC) {
835 /* Protocol specific dump request */
5fd30ee7 836 ops = lookup_rules_ops(net, family);
c454673d
TG
837 if (ops == NULL)
838 return -EAFNOSUPPORT;
839
41fc0143
WK
840 dump_rules(skb, cb, ops);
841
842 return skb->len;
c454673d
TG
843 }
844
845 rcu_read_lock();
5fd30ee7 846 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
c454673d
TG
847 if (idx < cb->args[0] || !try_module_get(ops->owner))
848 goto skip;
849
850 if (dump_rules(skb, cb, ops) < 0)
851 break;
852
853 cb->args[1] = 0;
2fb3573d 854skip:
c454673d
TG
855 idx++;
856 }
857 rcu_read_unlock();
858 cb->args[0] = idx;
859
860 return skb->len;
861}
14c0b97d 862
9e3a5487 863static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
864 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
865 u32 pid)
14c0b97d 866{
9e3a5487 867 struct net *net;
c17084d2
TG
868 struct sk_buff *skb;
869 int err = -ENOBUFS;
14c0b97d 870
9e3a5487 871 net = ops->fro_net;
339bf98f 872 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
14c0b97d 873 if (skb == NULL)
c17084d2
TG
874 goto errout;
875
876 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
26932566
PM
877 if (err < 0) {
878 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
879 WARN_ON(err == -EMSGSIZE);
880 kfree_skb(skb);
881 goto errout;
882 }
9e3a5487 883
1ce85fe4
PNA
884 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
885 return;
c17084d2
TG
886errout:
887 if (err < 0)
5fd30ee7 888 rtnl_set_sk_err(net, ops->nlgroup, err);
14c0b97d
TG
889}
890
891static void attach_rules(struct list_head *rules, struct net_device *dev)
892{
893 struct fib_rule *rule;
894
895 list_for_each_entry(rule, rules, list) {
491deb24
PM
896 if (rule->iifindex == -1 &&
897 strcmp(dev->name, rule->iifname) == 0)
898 rule->iifindex = dev->ifindex;
1b038a5e
PM
899 if (rule->oifindex == -1 &&
900 strcmp(dev->name, rule->oifname) == 0)
901 rule->oifindex = dev->ifindex;
14c0b97d
TG
902 }
903}
904
905static void detach_rules(struct list_head *rules, struct net_device *dev)
906{
907 struct fib_rule *rule;
908
1b038a5e 909 list_for_each_entry(rule, rules, list) {
491deb24
PM
910 if (rule->iifindex == dev->ifindex)
911 rule->iifindex = -1;
1b038a5e
PM
912 if (rule->oifindex == dev->ifindex)
913 rule->oifindex = -1;
914 }
14c0b97d
TG
915}
916
917
918static int fib_rules_event(struct notifier_block *this, unsigned long event,
351638e7 919 void *ptr)
14c0b97d 920{
351638e7 921 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
c346dca1 922 struct net *net = dev_net(dev);
14c0b97d
TG
923 struct fib_rules_ops *ops;
924
748e2d93 925 ASSERT_RTNL();
14c0b97d
TG
926
927 switch (event) {
928 case NETDEV_REGISTER:
5fd30ee7 929 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 930 attach_rules(&ops->rules_list, dev);
14c0b97d
TG
931 break;
932
946c032e
933 case NETDEV_CHANGENAME:
934 list_for_each_entry(ops, &net->rules_ops, list) {
935 detach_rules(&ops->rules_list, dev);
936 attach_rules(&ops->rules_list, dev);
937 }
938 break;
939
14c0b97d 940 case NETDEV_UNREGISTER:
5fd30ee7 941 list_for_each_entry(ops, &net->rules_ops, list)
76c72d4f 942 detach_rules(&ops->rules_list, dev);
14c0b97d
TG
943 break;
944 }
945
14c0b97d
TG
946 return NOTIFY_DONE;
947}
948
949static struct notifier_block fib_rules_notifier = {
950 .notifier_call = fib_rules_event,
951};
952
2c8c1e72 953static int __net_init fib_rules_net_init(struct net *net)
5fd30ee7
DL
954{
955 INIT_LIST_HEAD(&net->rules_ops);
956 spin_lock_init(&net->rules_mod_lock);
957 return 0;
958}
959
960static struct pernet_operations fib_rules_net_ops = {
961 .init = fib_rules_net_init,
962};
963
14c0b97d
TG
964static int __init fib_rules_init(void)
965{
5fd30ee7 966 int err;
c7ac8679
GR
967 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL);
968 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL);
969 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL);
9d9e6a58 970
5d6d4809 971 err = register_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
972 if (err < 0)
973 goto fail;
974
5d6d4809 975 err = register_netdevice_notifier(&fib_rules_notifier);
5fd30ee7
DL
976 if (err < 0)
977 goto fail_unregister;
5d6d4809 978
5fd30ee7
DL
979 return 0;
980
981fail_unregister:
5d6d4809 982 unregister_pernet_subsys(&fib_rules_net_ops);
5fd30ee7
DL
983fail:
984 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
985 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
986 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
987 return err;
14c0b97d
TG
988}
989
990subsys_initcall(fib_rules_init);