1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/core/fib_rules.c Generic Routing Rules
5 * Authors: Thomas Graf <tgraf@suug.ch>
8 #include <linux/types.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <net/net_namespace.h>
15 #include <net/fib_rules.h>
16 #include <net/ip_tunnels.h>
17 #include <linux/indirect_call_wrapper.h>
19 #if defined(CONFIG_IPV6) && defined(CONFIG_IPV6_MULTIPLE_TABLES)
20 #ifdef CONFIG_IP_MULTIPLE_TABLES
21 #define INDIRECT_CALL_MT(f, f2, f1, ...) \
22 INDIRECT_CALL_INET(f, f2, f1, __VA_ARGS__)
24 #define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f2, __VA_ARGS__)
26 #elif defined(CONFIG_IP_MULTIPLE_TABLES)
27 #define INDIRECT_CALL_MT(f, f2, f1, ...) INDIRECT_CALL_1(f, f1, __VA_ARGS__)
29 #define INDIRECT_CALL_MT(f, f2, f1, ...) f(__VA_ARGS__)
32 static const struct fib_kuid_range fib_kuid_range_unset
= {
37 bool fib_rule_matchall(const struct fib_rule
*rule
)
39 if (rule
->iifindex
|| rule
->oifindex
|| rule
->mark
|| rule
->tun_id
||
42 if (rule
->suppress_ifgroup
!= -1 || rule
->suppress_prefixlen
!= -1)
44 if (!uid_eq(rule
->uid_range
.start
, fib_kuid_range_unset
.start
) ||
45 !uid_eq(rule
->uid_range
.end
, fib_kuid_range_unset
.end
))
47 if (fib_rule_port_range_set(&rule
->sport_range
))
49 if (fib_rule_port_range_set(&rule
->dport_range
))
53 EXPORT_SYMBOL_GPL(fib_rule_matchall
);
55 int fib_default_rule_add(struct fib_rules_ops
*ops
,
56 u32 pref
, u32 table
, u32 flags
)
60 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
64 refcount_set(&r
->refcnt
, 1);
65 r
->action
= FR_ACT_TO_TBL
;
69 r
->proto
= RTPROT_KERNEL
;
70 r
->fr_net
= ops
->fro_net
;
71 r
->uid_range
= fib_kuid_range_unset
;
73 r
->suppress_prefixlen
= -1;
74 r
->suppress_ifgroup
= -1;
76 /* The lock is not required here, the list in unreacheable
77 * at the moment this function is called */
78 list_add_tail(&r
->list
, &ops
->rules_list
);
81 EXPORT_SYMBOL(fib_default_rule_add
);
83 static u32
fib_default_rule_pref(struct fib_rules_ops
*ops
)
85 struct list_head
*pos
;
86 struct fib_rule
*rule
;
88 if (!list_empty(&ops
->rules_list
)) {
89 pos
= ops
->rules_list
.next
;
90 if (pos
->next
!= &ops
->rules_list
) {
91 rule
= list_entry(pos
->next
, struct fib_rule
, list
);
93 return rule
->pref
- 1;
100 static void notify_rule_change(int event
, struct fib_rule
*rule
,
101 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
104 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
106 struct fib_rules_ops
*ops
;
109 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
110 if (ops
->family
== family
) {
111 if (!try_module_get(ops
->owner
))
122 static void rules_ops_put(struct fib_rules_ops
*ops
)
125 module_put(ops
->owner
);
128 static void flush_route_cache(struct fib_rules_ops
*ops
)
130 if (ops
->flush_cache
)
131 ops
->flush_cache(ops
);
134 static int __fib_rules_register(struct fib_rules_ops
*ops
)
137 struct fib_rules_ops
*o
;
142 if (ops
->rule_size
< sizeof(struct fib_rule
))
145 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
146 ops
->compare
== NULL
|| ops
->fill
== NULL
||
150 spin_lock(&net
->rules_mod_lock
);
151 list_for_each_entry(o
, &net
->rules_ops
, list
)
152 if (ops
->family
== o
->family
)
155 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
158 spin_unlock(&net
->rules_mod_lock
);
163 struct fib_rules_ops
*
164 fib_rules_register(const struct fib_rules_ops
*tmpl
, struct net
*net
)
166 struct fib_rules_ops
*ops
;
169 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
171 return ERR_PTR(-ENOMEM
);
173 INIT_LIST_HEAD(&ops
->rules_list
);
176 err
= __fib_rules_register(ops
);
184 EXPORT_SYMBOL_GPL(fib_rules_register
);
186 static void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
188 struct fib_rule
*rule
, *tmp
;
190 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
191 list_del_rcu(&rule
->list
);
198 void fib_rules_unregister(struct fib_rules_ops
*ops
)
200 struct net
*net
= ops
->fro_net
;
202 spin_lock(&net
->rules_mod_lock
);
203 list_del_rcu(&ops
->list
);
204 spin_unlock(&net
->rules_mod_lock
);
206 fib_rules_cleanup_ops(ops
);
209 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
211 static int uid_range_set(struct fib_kuid_range
*range
)
213 return uid_valid(range
->start
) && uid_valid(range
->end
);
216 static struct fib_kuid_range
nla_get_kuid_range(struct nlattr
**tb
)
218 struct fib_rule_uid_range
*in
;
219 struct fib_kuid_range out
;
221 in
= (struct fib_rule_uid_range
*)nla_data(tb
[FRA_UID_RANGE
]);
223 out
.start
= make_kuid(current_user_ns(), in
->start
);
224 out
.end
= make_kuid(current_user_ns(), in
->end
);
229 static int nla_put_uid_range(struct sk_buff
*skb
, struct fib_kuid_range
*range
)
231 struct fib_rule_uid_range out
= {
232 from_kuid_munged(current_user_ns(), range
->start
),
233 from_kuid_munged(current_user_ns(), range
->end
)
236 return nla_put(skb
, FRA_UID_RANGE
, sizeof(out
), &out
);
239 static int nla_get_port_range(struct nlattr
*pattr
,
240 struct fib_rule_port_range
*port_range
)
242 const struct fib_rule_port_range
*pr
= nla_data(pattr
);
244 if (!fib_rule_port_range_valid(pr
))
247 port_range
->start
= pr
->start
;
248 port_range
->end
= pr
->end
;
253 static int nla_put_port_range(struct sk_buff
*skb
, int attrtype
,
254 struct fib_rule_port_range
*range
)
256 return nla_put(skb
, attrtype
, sizeof(*range
), range
);
259 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
260 struct flowi
*fl
, int flags
,
261 struct fib_lookup_arg
*arg
)
265 if (rule
->iifindex
&& (rule
->iifindex
!= fl
->flowi_iif
))
268 if (rule
->oifindex
&& (rule
->oifindex
!= fl
->flowi_oif
))
271 if ((rule
->mark
^ fl
->flowi_mark
) & rule
->mark_mask
)
274 if (rule
->tun_id
&& (rule
->tun_id
!= fl
->flowi_tun_key
.tun_id
))
277 if (rule
->l3mdev
&& !l3mdev_fib_rule_match(rule
->fr_net
, fl
, arg
))
280 if (uid_lt(fl
->flowi_uid
, rule
->uid_range
.start
) ||
281 uid_gt(fl
->flowi_uid
, rule
->uid_range
.end
))
284 ret
= INDIRECT_CALL_MT(ops
->match
,
289 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
292 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
293 int flags
, struct fib_lookup_arg
*arg
)
295 struct fib_rule
*rule
;
300 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
302 if (!fib_rule_match(rule
, ops
, fl
, flags
, arg
))
305 if (rule
->action
== FR_ACT_GOTO
) {
306 struct fib_rule
*target
;
308 target
= rcu_dereference(rule
->ctarget
);
309 if (target
== NULL
) {
315 } else if (rule
->action
== FR_ACT_NOP
)
318 err
= INDIRECT_CALL_MT(ops
->action
,
321 rule
, fl
, flags
, arg
);
323 if (!err
&& ops
->suppress
&& INDIRECT_CALL_MT(ops
->suppress
,
329 if (err
!= -EAGAIN
) {
330 if ((arg
->flags
& FIB_LOOKUP_NOREF
) ||
331 likely(refcount_inc_not_zero(&rule
->refcnt
))) {
345 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
347 static int call_fib_rule_notifier(struct notifier_block
*nb
,
348 enum fib_event_type event_type
,
349 struct fib_rule
*rule
, int family
,
350 struct netlink_ext_ack
*extack
)
352 struct fib_rule_notifier_info info
= {
353 .info
.family
= family
,
354 .info
.extack
= extack
,
358 return call_fib_notifier(nb
, event_type
, &info
.info
);
361 static int call_fib_rule_notifiers(struct net
*net
,
362 enum fib_event_type event_type
,
363 struct fib_rule
*rule
,
364 struct fib_rules_ops
*ops
,
365 struct netlink_ext_ack
*extack
)
367 struct fib_rule_notifier_info info
= {
368 .info
.family
= ops
->family
,
369 .info
.extack
= extack
,
373 ops
->fib_rules_seq
++;
374 return call_fib_notifiers(net
, event_type
, &info
.info
);
377 /* Called with rcu_read_lock() */
378 int fib_rules_dump(struct net
*net
, struct notifier_block
*nb
, int family
,
379 struct netlink_ext_ack
*extack
)
381 struct fib_rules_ops
*ops
;
382 struct fib_rule
*rule
;
385 ops
= lookup_rules_ops(net
, family
);
387 return -EAFNOSUPPORT
;
388 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
389 err
= call_fib_rule_notifier(nb
, FIB_EVENT_RULE_ADD
,
390 rule
, family
, extack
);
398 EXPORT_SYMBOL_GPL(fib_rules_dump
);
400 unsigned int fib_rules_seq_read(struct net
*net
, int family
)
402 unsigned int fib_rules_seq
;
403 struct fib_rules_ops
*ops
;
407 ops
= lookup_rules_ops(net
, family
);
410 fib_rules_seq
= ops
->fib_rules_seq
;
413 return fib_rules_seq
;
415 EXPORT_SYMBOL_GPL(fib_rules_seq_read
);
417 static struct fib_rule
*rule_find(struct fib_rules_ops
*ops
,
418 struct fib_rule_hdr
*frh
,
420 struct fib_rule
*rule
,
425 list_for_each_entry(r
, &ops
->rules_list
, list
) {
426 if (rule
->action
&& r
->action
!= rule
->action
)
429 if (rule
->table
&& r
->table
!= rule
->table
)
432 if (user_priority
&& r
->pref
!= rule
->pref
)
435 if (rule
->iifname
[0] &&
436 memcmp(r
->iifname
, rule
->iifname
, IFNAMSIZ
))
439 if (rule
->oifname
[0] &&
440 memcmp(r
->oifname
, rule
->oifname
, IFNAMSIZ
))
443 if (rule
->mark
&& r
->mark
!= rule
->mark
)
446 if (rule
->suppress_ifgroup
!= -1 &&
447 r
->suppress_ifgroup
!= rule
->suppress_ifgroup
)
450 if (rule
->suppress_prefixlen
!= -1 &&
451 r
->suppress_prefixlen
!= rule
->suppress_prefixlen
)
454 if (rule
->mark_mask
&& r
->mark_mask
!= rule
->mark_mask
)
457 if (rule
->tun_id
&& r
->tun_id
!= rule
->tun_id
)
460 if (r
->fr_net
!= rule
->fr_net
)
463 if (rule
->l3mdev
&& r
->l3mdev
!= rule
->l3mdev
)
466 if (uid_range_set(&rule
->uid_range
) &&
467 (!uid_eq(r
->uid_range
.start
, rule
->uid_range
.start
) ||
468 !uid_eq(r
->uid_range
.end
, rule
->uid_range
.end
)))
471 if (rule
->ip_proto
&& r
->ip_proto
!= rule
->ip_proto
)
474 if (rule
->proto
&& r
->proto
!= rule
->proto
)
477 if (fib_rule_port_range_set(&rule
->sport_range
) &&
478 !fib_rule_port_range_compare(&r
->sport_range
,
482 if (fib_rule_port_range_set(&rule
->dport_range
) &&
483 !fib_rule_port_range_compare(&r
->dport_range
,
487 if (!ops
->compare(r
, frh
, tb
))
495 #ifdef CONFIG_NET_L3_MASTER_DEV
496 static int fib_nl2rule_l3mdev(struct nlattr
*nla
, struct fib_rule
*nlrule
,
497 struct netlink_ext_ack
*extack
)
499 nlrule
->l3mdev
= nla_get_u8(nla
);
500 if (nlrule
->l3mdev
!= 1) {
501 NL_SET_ERR_MSG(extack
, "Invalid l3mdev attribute");
508 static int fib_nl2rule_l3mdev(struct nlattr
*nla
, struct fib_rule
*nlrule
,
509 struct netlink_ext_ack
*extack
)
511 NL_SET_ERR_MSG(extack
, "l3mdev support is not enabled in kernel");
516 static int fib_nl2rule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
517 struct netlink_ext_ack
*extack
,
518 struct fib_rules_ops
*ops
,
520 struct fib_rule
**rule
,
523 struct net
*net
= sock_net(skb
->sk
);
524 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
525 struct fib_rule
*nlrule
= NULL
;
530 frh
->src_len
> (ops
->addr_size
* 8) ||
531 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
) {
532 NL_SET_ERR_MSG(extack
, "Invalid source address");
538 frh
->dst_len
> (ops
->addr_size
* 8) ||
539 nla_len(tb
[FRA_DST
]) != ops
->addr_size
) {
540 NL_SET_ERR_MSG(extack
, "Invalid dst address");
544 nlrule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
549 refcount_set(&nlrule
->refcnt
, 1);
550 nlrule
->fr_net
= net
;
552 if (tb
[FRA_PRIORITY
]) {
553 nlrule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
554 *user_priority
= true;
556 nlrule
->pref
= fib_default_rule_pref(ops
);
559 nlrule
->proto
= tb
[FRA_PROTOCOL
] ?
560 nla_get_u8(tb
[FRA_PROTOCOL
]) : RTPROT_UNSPEC
;
562 if (tb
[FRA_IIFNAME
]) {
563 struct net_device
*dev
;
565 nlrule
->iifindex
= -1;
566 nla_strscpy(nlrule
->iifname
, tb
[FRA_IIFNAME
], IFNAMSIZ
);
567 dev
= __dev_get_by_name(net
, nlrule
->iifname
);
569 nlrule
->iifindex
= dev
->ifindex
;
572 if (tb
[FRA_OIFNAME
]) {
573 struct net_device
*dev
;
575 nlrule
->oifindex
= -1;
576 nla_strscpy(nlrule
->oifname
, tb
[FRA_OIFNAME
], IFNAMSIZ
);
577 dev
= __dev_get_by_name(net
, nlrule
->oifname
);
579 nlrule
->oifindex
= dev
->ifindex
;
582 if (tb
[FRA_FWMARK
]) {
583 nlrule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
585 /* compatibility: if the mark value is non-zero all bits
586 * are compared unless a mask is explicitly specified.
588 nlrule
->mark_mask
= 0xFFFFFFFF;
592 nlrule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
595 nlrule
->tun_id
= nla_get_be64(tb
[FRA_TUN_ID
]);
598 if (tb
[FRA_L3MDEV
] &&
599 fib_nl2rule_l3mdev(tb
[FRA_L3MDEV
], nlrule
, extack
) < 0)
602 nlrule
->action
= frh
->action
;
603 nlrule
->flags
= frh
->flags
;
604 nlrule
->table
= frh_get_table(frh
, tb
);
605 if (tb
[FRA_SUPPRESS_PREFIXLEN
])
606 nlrule
->suppress_prefixlen
= nla_get_u32(tb
[FRA_SUPPRESS_PREFIXLEN
]);
608 nlrule
->suppress_prefixlen
= -1;
610 if (tb
[FRA_SUPPRESS_IFGROUP
])
611 nlrule
->suppress_ifgroup
= nla_get_u32(tb
[FRA_SUPPRESS_IFGROUP
]);
613 nlrule
->suppress_ifgroup
= -1;
616 if (nlrule
->action
!= FR_ACT_GOTO
) {
617 NL_SET_ERR_MSG(extack
, "Unexpected goto");
621 nlrule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
622 /* Backward jumps are prohibited to avoid endless loops */
623 if (nlrule
->target
<= nlrule
->pref
) {
624 NL_SET_ERR_MSG(extack
, "Backward goto not supported");
627 } else if (nlrule
->action
== FR_ACT_GOTO
) {
628 NL_SET_ERR_MSG(extack
, "Missing goto target for action goto");
632 if (nlrule
->l3mdev
&& nlrule
->table
) {
633 NL_SET_ERR_MSG(extack
, "l3mdev and table are mutually exclusive");
637 if (tb
[FRA_UID_RANGE
]) {
638 if (current_user_ns() != net
->user_ns
) {
640 NL_SET_ERR_MSG(extack
, "No permission to set uid");
644 nlrule
->uid_range
= nla_get_kuid_range(tb
);
646 if (!uid_range_set(&nlrule
->uid_range
) ||
647 !uid_lte(nlrule
->uid_range
.start
, nlrule
->uid_range
.end
)) {
648 NL_SET_ERR_MSG(extack
, "Invalid uid range");
652 nlrule
->uid_range
= fib_kuid_range_unset
;
655 if (tb
[FRA_IP_PROTO
])
656 nlrule
->ip_proto
= nla_get_u8(tb
[FRA_IP_PROTO
]);
658 if (tb
[FRA_SPORT_RANGE
]) {
659 err
= nla_get_port_range(tb
[FRA_SPORT_RANGE
],
660 &nlrule
->sport_range
);
662 NL_SET_ERR_MSG(extack
, "Invalid sport range");
667 if (tb
[FRA_DPORT_RANGE
]) {
668 err
= nla_get_port_range(tb
[FRA_DPORT_RANGE
],
669 &nlrule
->dport_range
);
671 NL_SET_ERR_MSG(extack
, "Invalid dport range");
686 static int rule_exists(struct fib_rules_ops
*ops
, struct fib_rule_hdr
*frh
,
687 struct nlattr
**tb
, struct fib_rule
*rule
)
691 list_for_each_entry(r
, &ops
->rules_list
, list
) {
692 if (r
->action
!= rule
->action
)
695 if (r
->table
!= rule
->table
)
698 if (r
->pref
!= rule
->pref
)
701 if (memcmp(r
->iifname
, rule
->iifname
, IFNAMSIZ
))
704 if (memcmp(r
->oifname
, rule
->oifname
, IFNAMSIZ
))
707 if (r
->mark
!= rule
->mark
)
710 if (r
->suppress_ifgroup
!= rule
->suppress_ifgroup
)
713 if (r
->suppress_prefixlen
!= rule
->suppress_prefixlen
)
716 if (r
->mark_mask
!= rule
->mark_mask
)
719 if (r
->tun_id
!= rule
->tun_id
)
722 if (r
->fr_net
!= rule
->fr_net
)
725 if (r
->l3mdev
!= rule
->l3mdev
)
728 if (!uid_eq(r
->uid_range
.start
, rule
->uid_range
.start
) ||
729 !uid_eq(r
->uid_range
.end
, rule
->uid_range
.end
))
732 if (r
->ip_proto
!= rule
->ip_proto
)
735 if (r
->proto
!= rule
->proto
)
738 if (!fib_rule_port_range_compare(&r
->sport_range
,
742 if (!fib_rule_port_range_compare(&r
->dport_range
,
746 if (!ops
->compare(r
, frh
, tb
))
753 int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
754 struct netlink_ext_ack
*extack
)
756 struct net
*net
= sock_net(skb
->sk
);
757 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
758 struct fib_rules_ops
*ops
= NULL
;
759 struct fib_rule
*rule
= NULL
, *r
, *last
= NULL
;
760 struct nlattr
*tb
[FRA_MAX
+ 1];
761 int err
= -EINVAL
, unresolved
= 0;
762 bool user_priority
= false;
764 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
765 NL_SET_ERR_MSG(extack
, "Invalid msg length");
769 ops
= lookup_rules_ops(net
, frh
->family
);
772 NL_SET_ERR_MSG(extack
, "Rule family not supported");
776 err
= nlmsg_parse_deprecated(nlh
, sizeof(*frh
), tb
, FRA_MAX
,
777 ops
->policy
, extack
);
779 NL_SET_ERR_MSG(extack
, "Error parsing msg");
783 err
= fib_nl2rule(skb
, nlh
, extack
, ops
, tb
, &rule
, &user_priority
);
787 if ((nlh
->nlmsg_flags
& NLM_F_EXCL
) &&
788 rule_exists(ops
, frh
, tb
, rule
)) {
793 err
= ops
->configure(rule
, skb
, frh
, tb
, extack
);
797 err
= call_fib_rule_notifiers(net
, FIB_EVENT_RULE_ADD
, rule
, ops
,
802 list_for_each_entry(r
, &ops
->rules_list
, list
) {
803 if (r
->pref
== rule
->target
) {
804 RCU_INIT_POINTER(rule
->ctarget
, r
);
809 if (rcu_dereference_protected(rule
->ctarget
, 1) == NULL
)
812 list_for_each_entry(r
, &ops
->rules_list
, list
) {
813 if (r
->pref
> rule
->pref
)
819 list_add_rcu(&rule
->list
, &last
->list
);
821 list_add_rcu(&rule
->list
, &ops
->rules_list
);
823 if (ops
->unresolved_rules
) {
825 * There are unresolved goto rules in the list, check if
826 * any of them are pointing to this new rule.
828 list_for_each_entry(r
, &ops
->rules_list
, list
) {
829 if (r
->action
== FR_ACT_GOTO
&&
830 r
->target
== rule
->pref
&&
831 rtnl_dereference(r
->ctarget
) == NULL
) {
832 rcu_assign_pointer(r
->ctarget
, rule
);
833 if (--ops
->unresolved_rules
== 0)
839 if (rule
->action
== FR_ACT_GOTO
)
840 ops
->nr_goto_rules
++;
843 ops
->unresolved_rules
++;
846 ip_tunnel_need_metadata();
848 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).portid
);
849 flush_route_cache(ops
);
859 EXPORT_SYMBOL_GPL(fib_nl_newrule
);
861 int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
862 struct netlink_ext_ack
*extack
)
864 struct net
*net
= sock_net(skb
->sk
);
865 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
866 struct fib_rules_ops
*ops
= NULL
;
867 struct fib_rule
*rule
= NULL
, *r
, *nlrule
= NULL
;
868 struct nlattr
*tb
[FRA_MAX
+1];
870 bool user_priority
= false;
872 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
873 NL_SET_ERR_MSG(extack
, "Invalid msg length");
877 ops
= lookup_rules_ops(net
, frh
->family
);
880 NL_SET_ERR_MSG(extack
, "Rule family not supported");
884 err
= nlmsg_parse_deprecated(nlh
, sizeof(*frh
), tb
, FRA_MAX
,
885 ops
->policy
, extack
);
887 NL_SET_ERR_MSG(extack
, "Error parsing msg");
891 err
= fib_nl2rule(skb
, nlh
, extack
, ops
, tb
, &nlrule
, &user_priority
);
895 rule
= rule_find(ops
, frh
, tb
, nlrule
, user_priority
);
901 if (rule
->flags
& FIB_RULE_PERMANENT
) {
907 err
= ops
->delete(rule
);
913 ip_tunnel_unneed_metadata();
915 list_del_rcu(&rule
->list
);
917 if (rule
->action
== FR_ACT_GOTO
) {
918 ops
->nr_goto_rules
--;
919 if (rtnl_dereference(rule
->ctarget
) == NULL
)
920 ops
->unresolved_rules
--;
924 * Check if this rule is a target to any of them. If so,
925 * adjust to the next one with the same preference or
926 * disable them. As this operation is eventually very
927 * expensive, it is only performed if goto rules, except
928 * current if it is goto rule, have actually been added.
930 if (ops
->nr_goto_rules
> 0) {
933 n
= list_next_entry(rule
, list
);
934 if (&n
->list
== &ops
->rules_list
|| n
->pref
!= rule
->pref
)
936 list_for_each_entry(r
, &ops
->rules_list
, list
) {
937 if (rtnl_dereference(r
->ctarget
) != rule
)
939 rcu_assign_pointer(r
->ctarget
, n
);
941 ops
->unresolved_rules
++;
945 call_fib_rule_notifiers(net
, FIB_EVENT_RULE_DEL
, rule
, ops
,
947 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
948 NETLINK_CB(skb
).portid
);
950 flush_route_cache(ops
);
960 EXPORT_SYMBOL_GPL(fib_nl_delrule
);
962 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
963 struct fib_rule
*rule
)
965 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
966 + nla_total_size(IFNAMSIZ
) /* FRA_IIFNAME */
967 + nla_total_size(IFNAMSIZ
) /* FRA_OIFNAME */
968 + nla_total_size(4) /* FRA_PRIORITY */
969 + nla_total_size(4) /* FRA_TABLE */
970 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
971 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
972 + nla_total_size(4) /* FRA_FWMARK */
973 + nla_total_size(4) /* FRA_FWMASK */
974 + nla_total_size_64bit(8) /* FRA_TUN_ID */
975 + nla_total_size(sizeof(struct fib_kuid_range
))
976 + nla_total_size(1) /* FRA_PROTOCOL */
977 + nla_total_size(1) /* FRA_IP_PROTO */
978 + nla_total_size(sizeof(struct fib_rule_port_range
)) /* FRA_SPORT_RANGE */
979 + nla_total_size(sizeof(struct fib_rule_port_range
)); /* FRA_DPORT_RANGE */
981 if (ops
->nlmsg_payload
)
982 payload
+= ops
->nlmsg_payload(rule
);
987 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
988 u32 pid
, u32 seq
, int type
, int flags
,
989 struct fib_rules_ops
*ops
)
991 struct nlmsghdr
*nlh
;
992 struct fib_rule_hdr
*frh
;
994 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
998 frh
= nlmsg_data(nlh
);
999 frh
->family
= ops
->family
;
1000 frh
->table
= rule
->table
< 256 ? rule
->table
: RT_TABLE_COMPAT
;
1001 if (nla_put_u32(skb
, FRA_TABLE
, rule
->table
))
1002 goto nla_put_failure
;
1003 if (nla_put_u32(skb
, FRA_SUPPRESS_PREFIXLEN
, rule
->suppress_prefixlen
))
1004 goto nla_put_failure
;
1007 frh
->action
= rule
->action
;
1008 frh
->flags
= rule
->flags
;
1010 if (nla_put_u8(skb
, FRA_PROTOCOL
, rule
->proto
))
1011 goto nla_put_failure
;
1013 if (rule
->action
== FR_ACT_GOTO
&&
1014 rcu_access_pointer(rule
->ctarget
) == NULL
)
1015 frh
->flags
|= FIB_RULE_UNRESOLVED
;
1017 if (rule
->iifname
[0]) {
1018 if (nla_put_string(skb
, FRA_IIFNAME
, rule
->iifname
))
1019 goto nla_put_failure
;
1020 if (rule
->iifindex
== -1)
1021 frh
->flags
|= FIB_RULE_IIF_DETACHED
;
1024 if (rule
->oifname
[0]) {
1025 if (nla_put_string(skb
, FRA_OIFNAME
, rule
->oifname
))
1026 goto nla_put_failure
;
1027 if (rule
->oifindex
== -1)
1028 frh
->flags
|= FIB_RULE_OIF_DETACHED
;
1032 nla_put_u32(skb
, FRA_PRIORITY
, rule
->pref
)) ||
1034 nla_put_u32(skb
, FRA_FWMARK
, rule
->mark
)) ||
1035 ((rule
->mark_mask
|| rule
->mark
) &&
1036 nla_put_u32(skb
, FRA_FWMASK
, rule
->mark_mask
)) ||
1038 nla_put_u32(skb
, FRA_GOTO
, rule
->target
)) ||
1040 nla_put_be64(skb
, FRA_TUN_ID
, rule
->tun_id
, FRA_PAD
)) ||
1042 nla_put_u8(skb
, FRA_L3MDEV
, rule
->l3mdev
)) ||
1043 (uid_range_set(&rule
->uid_range
) &&
1044 nla_put_uid_range(skb
, &rule
->uid_range
)) ||
1045 (fib_rule_port_range_set(&rule
->sport_range
) &&
1046 nla_put_port_range(skb
, FRA_SPORT_RANGE
, &rule
->sport_range
)) ||
1047 (fib_rule_port_range_set(&rule
->dport_range
) &&
1048 nla_put_port_range(skb
, FRA_DPORT_RANGE
, &rule
->dport_range
)) ||
1049 (rule
->ip_proto
&& nla_put_u8(skb
, FRA_IP_PROTO
, rule
->ip_proto
)))
1050 goto nla_put_failure
;
1052 if (rule
->suppress_ifgroup
!= -1) {
1053 if (nla_put_u32(skb
, FRA_SUPPRESS_IFGROUP
, rule
->suppress_ifgroup
))
1054 goto nla_put_failure
;
1057 if (ops
->fill(rule
, skb
, frh
) < 0)
1058 goto nla_put_failure
;
1060 nlmsg_end(skb
, nlh
);
1064 nlmsg_cancel(skb
, nlh
);
1068 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
1069 struct fib_rules_ops
*ops
)
1072 struct fib_rule
*rule
;
1076 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
1077 if (idx
< cb
->args
[1])
1080 err
= fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).portid
,
1081 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
1095 static int fib_valid_dumprule_req(const struct nlmsghdr
*nlh
,
1096 struct netlink_ext_ack
*extack
)
1098 struct fib_rule_hdr
*frh
;
1100 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
1101 NL_SET_ERR_MSG(extack
, "Invalid header for fib rule dump request");
1105 frh
= nlmsg_data(nlh
);
1106 if (frh
->dst_len
|| frh
->src_len
|| frh
->tos
|| frh
->table
||
1107 frh
->res1
|| frh
->res2
|| frh
->action
|| frh
->flags
) {
1108 NL_SET_ERR_MSG(extack
,
1109 "Invalid values in header for fib rule dump request");
1113 if (nlmsg_attrlen(nlh
, sizeof(*frh
))) {
1114 NL_SET_ERR_MSG(extack
, "Invalid data after header in fib rule dump request");
1121 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1123 const struct nlmsghdr
*nlh
= cb
->nlh
;
1124 struct net
*net
= sock_net(skb
->sk
);
1125 struct fib_rules_ops
*ops
;
1126 int idx
= 0, family
;
1128 if (cb
->strict_check
) {
1129 int err
= fib_valid_dumprule_req(nlh
, cb
->extack
);
1135 family
= rtnl_msg_family(nlh
);
1136 if (family
!= AF_UNSPEC
) {
1137 /* Protocol specific dump request */
1138 ops
= lookup_rules_ops(net
, family
);
1140 return -EAFNOSUPPORT
;
1142 dump_rules(skb
, cb
, ops
);
1148 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
1149 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
1152 if (dump_rules(skb
, cb
, ops
) < 0)
1165 static void notify_rule_change(int event
, struct fib_rule
*rule
,
1166 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
1170 struct sk_buff
*skb
;
1174 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
1178 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
1180 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1181 WARN_ON(err
== -EMSGSIZE
);
1186 rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
1190 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
1193 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
1195 struct fib_rule
*rule
;
1197 list_for_each_entry(rule
, rules
, list
) {
1198 if (rule
->iifindex
== -1 &&
1199 strcmp(dev
->name
, rule
->iifname
) == 0)
1200 rule
->iifindex
= dev
->ifindex
;
1201 if (rule
->oifindex
== -1 &&
1202 strcmp(dev
->name
, rule
->oifname
) == 0)
1203 rule
->oifindex
= dev
->ifindex
;
1207 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
1209 struct fib_rule
*rule
;
1211 list_for_each_entry(rule
, rules
, list
) {
1212 if (rule
->iifindex
== dev
->ifindex
)
1213 rule
->iifindex
= -1;
1214 if (rule
->oifindex
== dev
->ifindex
)
1215 rule
->oifindex
= -1;
1220 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
1223 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1224 struct net
*net
= dev_net(dev
);
1225 struct fib_rules_ops
*ops
;
1230 case NETDEV_REGISTER
:
1231 list_for_each_entry(ops
, &net
->rules_ops
, list
)
1232 attach_rules(&ops
->rules_list
, dev
);
1235 case NETDEV_CHANGENAME
:
1236 list_for_each_entry(ops
, &net
->rules_ops
, list
) {
1237 detach_rules(&ops
->rules_list
, dev
);
1238 attach_rules(&ops
->rules_list
, dev
);
1242 case NETDEV_UNREGISTER
:
1243 list_for_each_entry(ops
, &net
->rules_ops
, list
)
1244 detach_rules(&ops
->rules_list
, dev
);
1251 static struct notifier_block fib_rules_notifier
= {
1252 .notifier_call
= fib_rules_event
,
1255 static int __net_init
fib_rules_net_init(struct net
*net
)
1257 INIT_LIST_HEAD(&net
->rules_ops
);
1258 spin_lock_init(&net
->rules_mod_lock
);
1262 static void __net_exit
fib_rules_net_exit(struct net
*net
)
1264 WARN_ON_ONCE(!list_empty(&net
->rules_ops
));
1267 static struct pernet_operations fib_rules_net_ops
= {
1268 .init
= fib_rules_net_init
,
1269 .exit
= fib_rules_net_exit
,
1272 static int __init
fib_rules_init(void)
1275 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
, 0);
1276 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
, 0);
1277 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
, 0);
1279 err
= register_pernet_subsys(&fib_rules_net_ops
);
1283 err
= register_netdevice_notifier(&fib_rules_notifier
);
1285 goto fail_unregister
;
1290 unregister_pernet_subsys(&fib_rules_net_ops
);
1292 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
1293 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
1294 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
1298 subsys_initcall(fib_rules_init
);