1
2
3
4
5
6
7
8
9
10
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/slab.h>
14#include <linux/list.h>
15#include <linux/module.h>
16#include <net/net_namespace.h>
17#include <net/sock.h>
18#include <net/fib_rules.h>
19#include <net/ip_tunnels.h>
20
21static const struct fib_kuid_range fib_kuid_range_unset = {
22 KUIDT_INIT(0),
23 KUIDT_INIT(~0),
24};
25
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 if (fib_rule_port_range_set(&rule->sport_range))
37 return false;
38 if (fib_rule_port_range_set(&rule->dport_range))
39 return false;
40 return true;
41}
42EXPORT_SYMBOL_GPL(fib_rule_matchall);
43
44int fib_default_rule_add(struct fib_rules_ops *ops,
45 u32 pref, u32 table, u32 flags)
46{
47 struct fib_rule *r;
48
49 r = kzalloc(ops->rule_size, GFP_KERNEL);
50 if (r == NULL)
51 return -ENOMEM;
52
53 refcount_set(&r->refcnt, 1);
54 r->action = FR_ACT_TO_TBL;
55 r->pref = pref;
56 r->table = table;
57 r->flags = flags;
58 r->proto = RTPROT_KERNEL;
59 r->fr_net = ops->fro_net;
60 r->uid_range = fib_kuid_range_unset;
61
62 r->suppress_prefixlen = -1;
63 r->suppress_ifgroup = -1;
64
65
66
67 list_add_tail(&r->list, &ops->rules_list);
68 return 0;
69}
70EXPORT_SYMBOL(fib_default_rule_add);
71
72static u32 fib_default_rule_pref(struct fib_rules_ops *ops)
73{
74 struct list_head *pos;
75 struct fib_rule *rule;
76
77 if (!list_empty(&ops->rules_list)) {
78 pos = ops->rules_list.next;
79 if (pos->next != &ops->rules_list) {
80 rule = list_entry(pos->next, struct fib_rule, list);
81 if (rule->pref)
82 return rule->pref - 1;
83 }
84 }
85
86 return 0;
87}
88
89static void notify_rule_change(int event, struct fib_rule *rule,
90 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
91 u32 pid);
92
93static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family)
94{
95 struct fib_rules_ops *ops;
96
97 rcu_read_lock();
98 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
99 if (ops->family == family) {
100 if (!try_module_get(ops->owner))
101 ops = NULL;
102 rcu_read_unlock();
103 return ops;
104 }
105 }
106 rcu_read_unlock();
107
108 return NULL;
109}
110
111static void rules_ops_put(struct fib_rules_ops *ops)
112{
113 if (ops)
114 module_put(ops->owner);
115}
116
117static void flush_route_cache(struct fib_rules_ops *ops)
118{
119 if (ops->flush_cache)
120 ops->flush_cache(ops);
121}
122
123static int __fib_rules_register(struct fib_rules_ops *ops)
124{
125 int err = -EEXIST;
126 struct fib_rules_ops *o;
127 struct net *net;
128
129 net = ops->fro_net;
130
131 if (ops->rule_size < sizeof(struct fib_rule))
132 return -EINVAL;
133
134 if (ops->match == NULL || ops->configure == NULL ||
135 ops->compare == NULL || ops->fill == NULL ||
136 ops->action == NULL)
137 return -EINVAL;
138
139 spin_lock(&net->rules_mod_lock);
140 list_for_each_entry(o, &net->rules_ops, list)
141 if (ops->family == o->family)
142 goto errout;
143
144 list_add_tail_rcu(&ops->list, &net->rules_ops);
145 err = 0;
146errout:
147 spin_unlock(&net->rules_mod_lock);
148
149 return err;
150}
151
152struct fib_rules_ops *
153fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net)
154{
155 struct fib_rules_ops *ops;
156 int err;
157
158 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
159 if (ops == NULL)
160 return ERR_PTR(-ENOMEM);
161
162 INIT_LIST_HEAD(&ops->rules_list);
163 ops->fro_net = net;
164
165 err = __fib_rules_register(ops);
166 if (err) {
167 kfree(ops);
168 ops = ERR_PTR(err);
169 }
170
171 return ops;
172}
173EXPORT_SYMBOL_GPL(fib_rules_register);
174
175static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
176{
177 struct fib_rule *rule, *tmp;
178
179 list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) {
180 list_del_rcu(&rule->list);
181 if (ops->delete)
182 ops->delete(rule);
183 fib_rule_put(rule);
184 }
185}
186
187void fib_rules_unregister(struct fib_rules_ops *ops)
188{
189 struct net *net = ops->fro_net;
190
191 spin_lock(&net->rules_mod_lock);
192 list_del_rcu(&ops->list);
193 spin_unlock(&net->rules_mod_lock);
194
195 fib_rules_cleanup_ops(ops);
196 kfree_rcu(ops, rcu);
197}
198EXPORT_SYMBOL_GPL(fib_rules_unregister);
199
200static int uid_range_set(struct fib_kuid_range *range)
201{
202 return uid_valid(range->start) && uid_valid(range->end);
203}
204
205static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb)
206{
207 struct fib_rule_uid_range *in;
208 struct fib_kuid_range out;
209
210 in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]);
211
212 out.start = make_kuid(current_user_ns(), in->start);
213 out.end = make_kuid(current_user_ns(), in->end);
214
215 return out;
216}
217
218static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range)
219{
220 struct fib_rule_uid_range out = {
221 from_kuid_munged(current_user_ns(), range->start),
222 from_kuid_munged(current_user_ns(), range->end)
223 };
224
225 return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out);
226}
227
228static int nla_get_port_range(struct nlattr *pattr,
229 struct fib_rule_port_range *port_range)
230{
231 const struct fib_rule_port_range *pr = nla_data(pattr);
232
233 if (!fib_rule_port_range_valid(pr))
234 return -EINVAL;
235
236 port_range->start = pr->start;
237 port_range->end = pr->end;
238
239 return 0;
240}
241
242static int nla_put_port_range(struct sk_buff *skb, int attrtype,
243 struct fib_rule_port_range *range)
244{
245 return nla_put(skb, attrtype, sizeof(*range), range);
246}
247
248static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops,
249 struct flowi *fl, int flags,
250 struct fib_lookup_arg *arg)
251{
252 int ret = 0;
253
254 if (rule->iifindex && (rule->iifindex != fl->flowi_iif))
255 goto out;
256
257 if (rule->oifindex && (rule->oifindex != fl->flowi_oif))
258 goto out;
259
260 if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask)
261 goto out;
262
263 if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id))
264 goto out;
265
266 if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg))
267 goto out;
268
269 if (uid_lt(fl->flowi_uid, rule->uid_range.start) ||
270 uid_gt(fl->flowi_uid, rule->uid_range.end))
271 goto out;
272
273 ret = ops->match(rule, fl, flags);
274out:
275 return (rule->flags & FIB_RULE_INVERT) ? !ret : ret;
276}
277
278int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
279 int flags, struct fib_lookup_arg *arg)
280{
281 struct fib_rule *rule;
282 int err;
283
284 rcu_read_lock();
285
286 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
287jumped:
288 if (!fib_rule_match(rule, ops, fl, flags, arg))
289 continue;
290
291 if (rule->action == FR_ACT_GOTO) {
292 struct fib_rule *target;
293
294 target = rcu_dereference(rule->ctarget);
295 if (target == NULL) {
296 continue;
297 } else {
298 rule = target;
299 goto jumped;
300 }
301 } else if (rule->action == FR_ACT_NOP)
302 continue;
303 else
304 err = ops->action(rule, fl, flags, arg);
305
306 if (!err && ops->suppress && ops->suppress(rule, arg))
307 continue;
308
309 if (err != -EAGAIN) {
310 if ((arg->flags & FIB_LOOKUP_NOREF) ||
311 likely(refcount_inc_not_zero(&rule->refcnt))) {
312 arg->rule = rule;
313 goto out;
314 }
315 break;
316 }
317 }
318
319 err = -ESRCH;
320out:
321 rcu_read_unlock();
322
323 return err;
324}
325EXPORT_SYMBOL_GPL(fib_rules_lookup);
326
327static int call_fib_rule_notifier(struct notifier_block *nb,
328 enum fib_event_type event_type,
329 struct fib_rule *rule, int family,
330 struct netlink_ext_ack *extack)
331{
332 struct fib_rule_notifier_info info = {
333 .info.family = family,
334 .info.extack = extack,
335 .rule = rule,
336 };
337
338 return call_fib_notifier(nb, event_type, &info.info);
339}
340
341static int call_fib_rule_notifiers(struct net *net,
342 enum fib_event_type event_type,
343 struct fib_rule *rule,
344 struct fib_rules_ops *ops,
345 struct netlink_ext_ack *extack)
346{
347 struct fib_rule_notifier_info info = {
348 .info.family = ops->family,
349 .info.extack = extack,
350 .rule = rule,
351 };
352
353 ops->fib_rules_seq++;
354 return call_fib_notifiers(net, event_type, &info.info);
355}
356
357
358int fib_rules_dump(struct net *net, struct notifier_block *nb, int family,
359 struct netlink_ext_ack *extack)
360{
361 struct fib_rules_ops *ops;
362 struct fib_rule *rule;
363 int err = 0;
364
365 ops = lookup_rules_ops(net, family);
366 if (!ops)
367 return -EAFNOSUPPORT;
368 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
369 err = call_fib_rule_notifier(nb, FIB_EVENT_RULE_ADD,
370 rule, family, extack);
371 if (err)
372 break;
373 }
374 rules_ops_put(ops);
375
376 return err;
377}
378EXPORT_SYMBOL_GPL(fib_rules_dump);
379
380unsigned int fib_rules_seq_read(struct net *net, int family)
381{
382 unsigned int fib_rules_seq;
383 struct fib_rules_ops *ops;
384
385 ASSERT_RTNL();
386
387 ops = lookup_rules_ops(net, family);
388 if (!ops)
389 return 0;
390 fib_rules_seq = ops->fib_rules_seq;
391 rules_ops_put(ops);
392
393 return fib_rules_seq;
394}
395EXPORT_SYMBOL_GPL(fib_rules_seq_read);
396
397static struct fib_rule *rule_find(struct fib_rules_ops *ops,
398 struct fib_rule_hdr *frh,
399 struct nlattr **tb,
400 struct fib_rule *rule,
401 bool user_priority)
402{
403 struct fib_rule *r;
404
405 list_for_each_entry(r, &ops->rules_list, list) {
406 if (rule->action && r->action != rule->action)
407 continue;
408
409 if (rule->table && r->table != rule->table)
410 continue;
411
412 if (user_priority && r->pref != rule->pref)
413 continue;
414
415 if (rule->iifname[0] &&
416 memcmp(r->iifname, rule->iifname, IFNAMSIZ))
417 continue;
418
419 if (rule->oifname[0] &&
420 memcmp(r->oifname, rule->oifname, IFNAMSIZ))
421 continue;
422
423 if (rule->mark && r->mark != rule->mark)
424 continue;
425
426 if (rule->suppress_ifgroup != -1 &&
427 r->suppress_ifgroup != rule->suppress_ifgroup)
428 continue;
429
430 if (rule->suppress_prefixlen != -1 &&
431 r->suppress_prefixlen != rule->suppress_prefixlen)
432 continue;
433
434 if (rule->mark_mask && r->mark_mask != rule->mark_mask)
435 continue;
436
437 if (rule->tun_id && r->tun_id != rule->tun_id)
438 continue;
439
440 if (r->fr_net != rule->fr_net)
441 continue;
442
443 if (rule->l3mdev && r->l3mdev != rule->l3mdev)
444 continue;
445
446 if (uid_range_set(&rule->uid_range) &&
447 (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
448 !uid_eq(r->uid_range.end, rule->uid_range.end)))
449 continue;
450
451 if (rule->ip_proto && r->ip_proto != rule->ip_proto)
452 continue;
453
454 if (rule->proto && r->proto != rule->proto)
455 continue;
456
457 if (fib_rule_port_range_set(&rule->sport_range) &&
458 !fib_rule_port_range_compare(&r->sport_range,
459 &rule->sport_range))
460 continue;
461
462 if (fib_rule_port_range_set(&rule->dport_range) &&
463 !fib_rule_port_range_compare(&r->dport_range,
464 &rule->dport_range))
465 continue;
466
467 if (!ops->compare(r, frh, tb))
468 continue;
469 return r;
470 }
471
472 return NULL;
473}
474
475#ifdef CONFIG_NET_L3_MASTER_DEV
476static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
477 struct netlink_ext_ack *extack)
478{
479 nlrule->l3mdev = nla_get_u8(nla);
480 if (nlrule->l3mdev != 1) {
481 NL_SET_ERR_MSG(extack, "Invalid l3mdev attribute");
482 return -1;
483 }
484
485 return 0;
486}
487#else
488static int fib_nl2rule_l3mdev(struct nlattr *nla, struct fib_rule *nlrule,
489 struct netlink_ext_ack *extack)
490{
491 NL_SET_ERR_MSG(extack, "l3mdev support is not enabled in kernel");
492 return -1;
493}
494#endif
495
496static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
497 struct netlink_ext_ack *extack,
498 struct fib_rules_ops *ops,
499 struct nlattr *tb[],
500 struct fib_rule **rule,
501 bool *user_priority)
502{
503 struct net *net = sock_net(skb->sk);
504 struct fib_rule_hdr *frh = nlmsg_data(nlh);
505 struct fib_rule *nlrule = NULL;
506 int err = -EINVAL;
507
508 if (frh->src_len)
509 if (!tb[FRA_SRC] ||
510 frh->src_len > (ops->addr_size * 8) ||
511 nla_len(tb[FRA_SRC]) != ops->addr_size) {
512 NL_SET_ERR_MSG(extack, "Invalid source address");
513 goto errout;
514 }
515
516 if (frh->dst_len)
517 if (!tb[FRA_DST] ||
518 frh->dst_len > (ops->addr_size * 8) ||
519 nla_len(tb[FRA_DST]) != ops->addr_size) {
520 NL_SET_ERR_MSG(extack, "Invalid dst address");
521 goto errout;
522 }
523
524 nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
525 if (!nlrule) {
526 err = -ENOMEM;
527 goto errout;
528 }
529 refcount_set(&nlrule->refcnt, 1);
530 nlrule->fr_net = net;
531
532 if (tb[FRA_PRIORITY]) {
533 nlrule->pref = nla_get_u32(tb[FRA_PRIORITY]);
534 *user_priority = true;
535 } else {
536 nlrule->pref = fib_default_rule_pref(ops);
537 }
538
539 nlrule->proto = tb[FRA_PROTOCOL] ?
540 nla_get_u8(tb[FRA_PROTOCOL]) : RTPROT_UNSPEC;
541
542 if (tb[FRA_IIFNAME]) {
543 struct net_device *dev;
544
545 nlrule->iifindex = -1;
546 nla_strscpy(nlrule->iifname, tb[FRA_IIFNAME], IFNAMSIZ);
547 dev = __dev_get_by_name(net, nlrule->iifname);
548 if (dev)
549 nlrule->iifindex = dev->ifindex;
550 }
551
552 if (tb[FRA_OIFNAME]) {
553 struct net_device *dev;
554
555 nlrule->oifindex = -1;
556 nla_strscpy(nlrule->oifname, tb[FRA_OIFNAME], IFNAMSIZ);
557 dev = __dev_get_by_name(net, nlrule->oifname);
558 if (dev)
559 nlrule->oifindex = dev->ifindex;
560 }
561
562 if (tb[FRA_FWMARK]) {
563 nlrule->mark = nla_get_u32(tb[FRA_FWMARK]);
564 if (nlrule->mark)
565
566
567
568 nlrule->mark_mask = 0xFFFFFFFF;
569 }
570
571 if (tb[FRA_FWMASK])
572 nlrule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
573
574 if (tb[FRA_TUN_ID])
575 nlrule->tun_id = nla_get_be64(tb[FRA_TUN_ID]);
576
577 err = -EINVAL;
578 if (tb[FRA_L3MDEV] &&
579 fib_nl2rule_l3mdev(tb[FRA_L3MDEV], nlrule, extack) < 0)
580 goto errout_free;
581
582 nlrule->action = frh->action;
583 nlrule->flags = frh->flags;
584 nlrule->table = frh_get_table(frh, tb);
585 if (tb[FRA_SUPPRESS_PREFIXLEN])
586 nlrule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]);
587 else
588 nlrule->suppress_prefixlen = -1;
589
590 if (tb[FRA_SUPPRESS_IFGROUP])
591 nlrule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]);
592 else
593 nlrule->suppress_ifgroup = -1;
594
595 if (tb[FRA_GOTO]) {
596 if (nlrule->action != FR_ACT_GOTO) {
597 NL_SET_ERR_MSG(extack, "Unexpected goto");
598 goto errout_free;
599 }
600
601 nlrule->target = nla_get_u32(tb[FRA_GOTO]);
602
603 if (nlrule->target <= nlrule->pref) {
604 NL_SET_ERR_MSG(extack, "Backward goto not supported");
605 goto errout_free;
606 }
607 } else if (nlrule->action == FR_ACT_GOTO) {
608 NL_SET_ERR_MSG(extack, "Missing goto target for action goto");
609 goto errout_free;
610 }
611
612 if (nlrule->l3mdev && nlrule->table) {
613 NL_SET_ERR_MSG(extack, "l3mdev and table are mutually exclusive");
614 goto errout_free;
615 }
616
617 if (tb[FRA_UID_RANGE]) {
618 if (current_user_ns() != net->user_ns) {
619 err = -EPERM;
620 NL_SET_ERR_MSG(extack, "No permission to set uid");
621 goto errout_free;
622 }
623
624 nlrule->uid_range = nla_get_kuid_range(tb);
625
626 if (!uid_range_set(&nlrule->uid_range) ||
627 !uid_lte(nlrule->uid_range.start, nlrule->uid_range.end)) {
628 NL_SET_ERR_MSG(extack, "Invalid uid range");
629 goto errout_free;
630 }
631 } else {
632 nlrule->uid_range = fib_kuid_range_unset;
633 }
634
635 if (tb[FRA_IP_PROTO])
636 nlrule->ip_proto = nla_get_u8(tb[FRA_IP_PROTO]);
637
638 if (tb[FRA_SPORT_RANGE]) {
639 err = nla_get_port_range(tb[FRA_SPORT_RANGE],
640 &nlrule->sport_range);
641 if (err) {
642 NL_SET_ERR_MSG(extack, "Invalid sport range");
643 goto errout_free;
644 }
645 }
646
647 if (tb[FRA_DPORT_RANGE]) {
648 err = nla_get_port_range(tb[FRA_DPORT_RANGE],
649 &nlrule->dport_range);
650 if (err) {
651 NL_SET_ERR_MSG(extack, "Invalid dport range");
652 goto errout_free;
653 }
654 }
655
656 *rule = nlrule;
657
658 return 0;
659
660errout_free:
661 kfree(nlrule);
662errout:
663 return err;
664}
665
666static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh,
667 struct nlattr **tb, struct fib_rule *rule)
668{
669 struct fib_rule *r;
670
671 list_for_each_entry(r, &ops->rules_list, list) {
672 if (r->action != rule->action)
673 continue;
674
675 if (r->table != rule->table)
676 continue;
677
678 if (r->pref != rule->pref)
679 continue;
680
681 if (memcmp(r->iifname, rule->iifname, IFNAMSIZ))
682 continue;
683
684 if (memcmp(r->oifname, rule->oifname, IFNAMSIZ))
685 continue;
686
687 if (r->mark != rule->mark)
688 continue;
689
690 if (r->suppress_ifgroup != rule->suppress_ifgroup)
691 continue;
692
693 if (r->suppress_prefixlen != rule->suppress_prefixlen)
694 continue;
695
696 if (r->mark_mask != rule->mark_mask)
697 continue;
698
699 if (r->tun_id != rule->tun_id)
700 continue;
701
702 if (r->fr_net != rule->fr_net)
703 continue;
704
705 if (r->l3mdev != rule->l3mdev)
706 continue;
707
708 if (!uid_eq(r->uid_range.start, rule->uid_range.start) ||
709 !uid_eq(r->uid_range.end, rule->uid_range.end))
710 continue;
711
712 if (r->ip_proto != rule->ip_proto)
713 continue;
714
715 if (r->proto != rule->proto)
716 continue;
717
718 if (!fib_rule_port_range_compare(&r->sport_range,
719 &rule->sport_range))
720 continue;
721
722 if (!fib_rule_port_range_compare(&r->dport_range,
723 &rule->dport_range))
724 continue;
725
726 if (!ops->compare(r, frh, tb))
727 continue;
728 return 1;
729 }
730 return 0;
731}
732
733int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh,
734 struct netlink_ext_ack *extack)
735{
736 struct net *net = sock_net(skb->sk);
737 struct fib_rule_hdr *frh = nlmsg_data(nlh);
738 struct fib_rules_ops *ops = NULL;
739 struct fib_rule *rule = NULL, *r, *last = NULL;
740 struct nlattr *tb[FRA_MAX + 1];
741 int err = -EINVAL, unresolved = 0;
742 bool user_priority = false;
743
744 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
745 NL_SET_ERR_MSG(extack, "Invalid msg length");
746 goto errout;
747 }
748
749 ops = lookup_rules_ops(net, frh->family);
750 if (!ops) {
751 err = -EAFNOSUPPORT;
752 NL_SET_ERR_MSG(extack, "Rule family not supported");
753 goto errout;
754 }
755
756 err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
757 ops->policy, extack);
758 if (err < 0) {
759 NL_SET_ERR_MSG(extack, "Error parsing msg");
760 goto errout;
761 }
762
763 err = fib_nl2rule(skb, nlh, extack, ops, tb, &rule, &user_priority);
764 if (err)
765 goto errout;
766
767 if ((nlh->nlmsg_flags & NLM_F_EXCL) &&
768 rule_exists(ops, frh, tb, rule)) {
769 err = -EEXIST;
770 goto errout_free;
771 }
772
773 err = ops->configure(rule, skb, frh, tb, extack);
774 if (err < 0)
775 goto errout_free;
776
777 err = call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule, ops,
778 extack);
779 if (err < 0)
780 goto errout_free;
781
782 list_for_each_entry(r, &ops->rules_list, list) {
783 if (r->pref == rule->target) {
784 RCU_INIT_POINTER(rule->ctarget, r);
785 break;
786 }
787 }
788
789 if (rcu_dereference_protected(rule->ctarget, 1) == NULL)
790 unresolved = 1;
791
792 list_for_each_entry(r, &ops->rules_list, list) {
793 if (r->pref > rule->pref)
794 break;
795 last = r;
796 }
797
798 if (last)
799 list_add_rcu(&rule->list, &last->list);
800 else
801 list_add_rcu(&rule->list, &ops->rules_list);
802
803 if (ops->unresolved_rules) {
804
805
806
807
808 list_for_each_entry(r, &ops->rules_list, list) {
809 if (r->action == FR_ACT_GOTO &&
810 r->target == rule->pref &&
811 rtnl_dereference(r->ctarget) == NULL) {
812 rcu_assign_pointer(r->ctarget, rule);
813 if (--ops->unresolved_rules == 0)
814 break;
815 }
816 }
817 }
818
819 if (rule->action == FR_ACT_GOTO)
820 ops->nr_goto_rules++;
821
822 if (unresolved)
823 ops->unresolved_rules++;
824
825 if (rule->tun_id)
826 ip_tunnel_need_metadata();
827
828 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid);
829 flush_route_cache(ops);
830 rules_ops_put(ops);
831 return 0;
832
833errout_free:
834 kfree(rule);
835errout:
836 rules_ops_put(ops);
837 return err;
838}
839EXPORT_SYMBOL_GPL(fib_nl_newrule);
840
841int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh,
842 struct netlink_ext_ack *extack)
843{
844 struct net *net = sock_net(skb->sk);
845 struct fib_rule_hdr *frh = nlmsg_data(nlh);
846 struct fib_rules_ops *ops = NULL;
847 struct fib_rule *rule = NULL, *r, *nlrule = NULL;
848 struct nlattr *tb[FRA_MAX+1];
849 int err = -EINVAL;
850 bool user_priority = false;
851
852 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
853 NL_SET_ERR_MSG(extack, "Invalid msg length");
854 goto errout;
855 }
856
857 ops = lookup_rules_ops(net, frh->family);
858 if (ops == NULL) {
859 err = -EAFNOSUPPORT;
860 NL_SET_ERR_MSG(extack, "Rule family not supported");
861 goto errout;
862 }
863
864 err = nlmsg_parse_deprecated(nlh, sizeof(*frh), tb, FRA_MAX,
865 ops->policy, extack);
866 if (err < 0) {
867 NL_SET_ERR_MSG(extack, "Error parsing msg");
868 goto errout;
869 }
870
871 err = fib_nl2rule(skb, nlh, extack, ops, tb, &nlrule, &user_priority);
872 if (err)
873 goto errout;
874
875 rule = rule_find(ops, frh, tb, nlrule, user_priority);
876 if (!rule) {
877 err = -ENOENT;
878 goto errout;
879 }
880
881 if (rule->flags & FIB_RULE_PERMANENT) {
882 err = -EPERM;
883 goto errout;
884 }
885
886 if (ops->delete) {
887 err = ops->delete(rule);
888 if (err)
889 goto errout;
890 }
891
892 if (rule->tun_id)
893 ip_tunnel_unneed_metadata();
894
895 list_del_rcu(&rule->list);
896
897 if (rule->action == FR_ACT_GOTO) {
898 ops->nr_goto_rules--;
899 if (rtnl_dereference(rule->ctarget) == NULL)
900 ops->unresolved_rules--;
901 }
902
903
904
905
906
907
908
909
910 if (ops->nr_goto_rules > 0) {
911 struct fib_rule *n;
912
913 n = list_next_entry(rule, list);
914 if (&n->list == &ops->rules_list || n->pref != rule->pref)
915 n = NULL;
916 list_for_each_entry(r, &ops->rules_list, list) {
917 if (rtnl_dereference(r->ctarget) != rule)
918 continue;
919 rcu_assign_pointer(r->ctarget, n);
920 if (!n)
921 ops->unresolved_rules++;
922 }
923 }
924
925 call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule, ops,
926 NULL);
927 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
928 NETLINK_CB(skb).portid);
929 fib_rule_put(rule);
930 flush_route_cache(ops);
931 rules_ops_put(ops);
932 kfree(nlrule);
933 return 0;
934
935errout:
936 if (nlrule)
937 kfree(nlrule);
938 rules_ops_put(ops);
939 return err;
940}
941EXPORT_SYMBOL_GPL(fib_nl_delrule);
942
943static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
944 struct fib_rule *rule)
945{
946 size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
947 + nla_total_size(IFNAMSIZ)
948 + nla_total_size(IFNAMSIZ)
949 + nla_total_size(4)
950 + nla_total_size(4)
951 + nla_total_size(4)
952 + nla_total_size(4)
953 + nla_total_size(4)
954 + nla_total_size(4)
955 + nla_total_size_64bit(8)
956 + nla_total_size(sizeof(struct fib_kuid_range))
957 + nla_total_size(1)
958 + nla_total_size(1)
959 + nla_total_size(sizeof(struct fib_rule_port_range))
960 + nla_total_size(sizeof(struct fib_rule_port_range));
961
962 if (ops->nlmsg_payload)
963 payload += ops->nlmsg_payload(rule);
964
965 return payload;
966}
967
968static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
969 u32 pid, u32 seq, int type, int flags,
970 struct fib_rules_ops *ops)
971{
972 struct nlmsghdr *nlh;
973 struct fib_rule_hdr *frh;
974
975 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
976 if (nlh == NULL)
977 return -EMSGSIZE;
978
979 frh = nlmsg_data(nlh);
980 frh->family = ops->family;
981 frh->table = rule->table;
982 if (nla_put_u32(skb, FRA_TABLE, rule->table))
983 goto nla_put_failure;
984 if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen))
985 goto nla_put_failure;
986 frh->res1 = 0;
987 frh->res2 = 0;
988 frh->action = rule->action;
989 frh->flags = rule->flags;
990
991 if (nla_put_u8(skb, FRA_PROTOCOL, rule->proto))
992 goto nla_put_failure;
993
994 if (rule->action == FR_ACT_GOTO &&
995 rcu_access_pointer(rule->ctarget) == NULL)
996 frh->flags |= FIB_RULE_UNRESOLVED;
997
998 if (rule->iifname[0]) {
999 if (nla_put_string(skb, FRA_IIFNAME, rule->iifname))
1000 goto nla_put_failure;
1001 if (rule->iifindex == -1)
1002 frh->flags |= FIB_RULE_IIF_DETACHED;
1003 }
1004
1005 if (rule->oifname[0]) {
1006 if (nla_put_string(skb, FRA_OIFNAME, rule->oifname))
1007 goto nla_put_failure;
1008 if (rule->oifindex == -1)
1009 frh->flags |= FIB_RULE_OIF_DETACHED;
1010 }
1011
1012 if ((rule->pref &&
1013 nla_put_u32(skb, FRA_PRIORITY, rule->pref)) ||
1014 (rule->mark &&
1015 nla_put_u32(skb, FRA_FWMARK, rule->mark)) ||
1016 ((rule->mark_mask || rule->mark) &&
1017 nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) ||
1018 (rule->target &&
1019 nla_put_u32(skb, FRA_GOTO, rule->target)) ||
1020 (rule->tun_id &&
1021 nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) ||
1022 (rule->l3mdev &&
1023 nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) ||
1024 (uid_range_set(&rule->uid_range) &&
1025 nla_put_uid_range(skb, &rule->uid_range)) ||
1026 (fib_rule_port_range_set(&rule->sport_range) &&
1027 nla_put_port_range(skb, FRA_SPORT_RANGE, &rule->sport_range)) ||
1028 (fib_rule_port_range_set(&rule->dport_range) &&
1029 nla_put_port_range(skb, FRA_DPORT_RANGE, &rule->dport_range)) ||
1030 (rule->ip_proto && nla_put_u8(skb, FRA_IP_PROTO, rule->ip_proto)))
1031 goto nla_put_failure;
1032
1033 if (rule->suppress_ifgroup != -1) {
1034 if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup))
1035 goto nla_put_failure;
1036 }
1037
1038 if (ops->fill(rule, skb, frh) < 0)
1039 goto nla_put_failure;
1040
1041 nlmsg_end(skb, nlh);
1042 return 0;
1043
1044nla_put_failure:
1045 nlmsg_cancel(skb, nlh);
1046 return -EMSGSIZE;
1047}
1048
1049static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
1050 struct fib_rules_ops *ops)
1051{
1052 int idx = 0;
1053 struct fib_rule *rule;
1054 int err = 0;
1055
1056 rcu_read_lock();
1057 list_for_each_entry_rcu(rule, &ops->rules_list, list) {
1058 if (idx < cb->args[1])
1059 goto skip;
1060
1061 err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid,
1062 cb->nlh->nlmsg_seq, RTM_NEWRULE,
1063 NLM_F_MULTI, ops);
1064 if (err)
1065 break;
1066skip:
1067 idx++;
1068 }
1069 rcu_read_unlock();
1070 cb->args[1] = idx;
1071 rules_ops_put(ops);
1072
1073 return err;
1074}
1075
1076static int fib_valid_dumprule_req(const struct nlmsghdr *nlh,
1077 struct netlink_ext_ack *extack)
1078{
1079 struct fib_rule_hdr *frh;
1080
1081 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) {
1082 NL_SET_ERR_MSG(extack, "Invalid header for fib rule dump request");
1083 return -EINVAL;
1084 }
1085
1086 frh = nlmsg_data(nlh);
1087 if (frh->dst_len || frh->src_len || frh->tos || frh->table ||
1088 frh->res1 || frh->res2 || frh->action || frh->flags) {
1089 NL_SET_ERR_MSG(extack,
1090 "Invalid values in header for fib rule dump request");
1091 return -EINVAL;
1092 }
1093
1094 if (nlmsg_attrlen(nlh, sizeof(*frh))) {
1095 NL_SET_ERR_MSG(extack, "Invalid data after header in fib rule dump request");
1096 return -EINVAL;
1097 }
1098
1099 return 0;
1100}
1101
1102static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
1103{
1104 const struct nlmsghdr *nlh = cb->nlh;
1105 struct net *net = sock_net(skb->sk);
1106 struct fib_rules_ops *ops;
1107 int idx = 0, family;
1108
1109 if (cb->strict_check) {
1110 int err = fib_valid_dumprule_req(nlh, cb->extack);
1111
1112 if (err < 0)
1113 return err;
1114 }
1115
1116 family = rtnl_msg_family(nlh);
1117 if (family != AF_UNSPEC) {
1118
1119 ops = lookup_rules_ops(net, family);
1120 if (ops == NULL)
1121 return -EAFNOSUPPORT;
1122
1123 dump_rules(skb, cb, ops);
1124
1125 return skb->len;
1126 }
1127
1128 rcu_read_lock();
1129 list_for_each_entry_rcu(ops, &net->rules_ops, list) {
1130 if (idx < cb->args[0] || !try_module_get(ops->owner))
1131 goto skip;
1132
1133 if (dump_rules(skb, cb, ops) < 0)
1134 break;
1135
1136 cb->args[1] = 0;
1137skip:
1138 idx++;
1139 }
1140 rcu_read_unlock();
1141 cb->args[0] = idx;
1142
1143 return skb->len;
1144}
1145
1146static void notify_rule_change(int event, struct fib_rule *rule,
1147 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
1148 u32 pid)
1149{
1150 struct net *net;
1151 struct sk_buff *skb;
1152 int err = -ENOBUFS;
1153
1154 net = ops->fro_net;
1155 skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
1156 if (skb == NULL)
1157 goto errout;
1158
1159 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
1160 if (err < 0) {
1161
1162 WARN_ON(err == -EMSGSIZE);
1163 kfree_skb(skb);
1164 goto errout;
1165 }
1166
1167 rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL);
1168 return;
1169errout:
1170 if (err < 0)
1171 rtnl_set_sk_err(net, ops->nlgroup, err);
1172}
1173
1174static void attach_rules(struct list_head *rules, struct net_device *dev)
1175{
1176 struct fib_rule *rule;
1177
1178 list_for_each_entry(rule, rules, list) {
1179 if (rule->iifindex == -1 &&
1180 strcmp(dev->name, rule->iifname) == 0)
1181 rule->iifindex = dev->ifindex;
1182 if (rule->oifindex == -1 &&
1183 strcmp(dev->name, rule->oifname) == 0)
1184 rule->oifindex = dev->ifindex;
1185 }
1186}
1187
1188static void detach_rules(struct list_head *rules, struct net_device *dev)
1189{
1190 struct fib_rule *rule;
1191
1192 list_for_each_entry(rule, rules, list) {
1193 if (rule->iifindex == dev->ifindex)
1194 rule->iifindex = -1;
1195 if (rule->oifindex == dev->ifindex)
1196 rule->oifindex = -1;
1197 }
1198}
1199
1200
1201static int fib_rules_event(struct notifier_block *this, unsigned long event,
1202 void *ptr)
1203{
1204 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1205 struct net *net = dev_net(dev);
1206 struct fib_rules_ops *ops;
1207
1208 ASSERT_RTNL();
1209
1210 switch (event) {
1211 case NETDEV_REGISTER:
1212 list_for_each_entry(ops, &net->rules_ops, list)
1213 attach_rules(&ops->rules_list, dev);
1214 break;
1215
1216 case NETDEV_CHANGENAME:
1217 list_for_each_entry(ops, &net->rules_ops, list) {
1218 detach_rules(&ops->rules_list, dev);
1219 attach_rules(&ops->rules_list, dev);
1220 }
1221 break;
1222
1223 case NETDEV_UNREGISTER:
1224 list_for_each_entry(ops, &net->rules_ops, list)
1225 detach_rules(&ops->rules_list, dev);
1226 break;
1227 }
1228
1229 return NOTIFY_DONE;
1230}
1231
1232static struct notifier_block fib_rules_notifier = {
1233 .notifier_call = fib_rules_event,
1234};
1235
1236static int __net_init fib_rules_net_init(struct net *net)
1237{
1238 INIT_LIST_HEAD(&net->rules_ops);
1239 spin_lock_init(&net->rules_mod_lock);
1240 return 0;
1241}
1242
1243static void __net_exit fib_rules_net_exit(struct net *net)
1244{
1245 WARN_ON_ONCE(!list_empty(&net->rules_ops));
1246}
1247
1248static struct pernet_operations fib_rules_net_ops = {
1249 .init = fib_rules_net_init,
1250 .exit = fib_rules_net_exit,
1251};
1252
1253static int __init fib_rules_init(void)
1254{
1255 int err;
1256 rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, 0);
1257 rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, 0);
1258 rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, 0);
1259
1260 err = register_pernet_subsys(&fib_rules_net_ops);
1261 if (err < 0)
1262 goto fail;
1263
1264 err = register_netdevice_notifier(&fib_rules_notifier);
1265 if (err < 0)
1266 goto fail_unregister;
1267
1268 return 0;
1269
1270fail_unregister:
1271 unregister_pernet_subsys(&fib_rules_net_ops);
1272fail:
1273 rtnl_unregister(PF_UNSPEC, RTM_NEWRULE);
1274 rtnl_unregister(PF_UNSPEC, RTM_DELRULE);
1275 rtnl_unregister(PF_UNSPEC, RTM_GETRULE);
1276 return err;
1277}
1278
1279subsys_initcall(fib_rules_init);
1280