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>
18 static const struct fib_kuid_range fib_kuid_range_unset
= {
23 bool fib_rule_matchall(const struct fib_rule
*rule
)
25 if (rule
->iifindex
|| rule
->oifindex
|| rule
->mark
|| rule
->tun_id
||
28 if (rule
->suppress_ifgroup
!= -1 || rule
->suppress_prefixlen
!= -1)
30 if (!uid_eq(rule
->uid_range
.start
, fib_kuid_range_unset
.start
) ||
31 !uid_eq(rule
->uid_range
.end
, fib_kuid_range_unset
.end
))
33 if (fib_rule_port_range_set(&rule
->sport_range
))
35 if (fib_rule_port_range_set(&rule
->dport_range
))
39 EXPORT_SYMBOL_GPL(fib_rule_matchall
);
41 int fib_default_rule_add(struct fib_rules_ops
*ops
,
42 u32 pref
, u32 table
, u32 flags
)
46 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
50 refcount_set(&r
->refcnt
, 1);
51 r
->action
= FR_ACT_TO_TBL
;
55 r
->proto
= RTPROT_KERNEL
;
56 r
->fr_net
= ops
->fro_net
;
57 r
->uid_range
= fib_kuid_range_unset
;
59 r
->suppress_prefixlen
= -1;
60 r
->suppress_ifgroup
= -1;
62 /* The lock is not required here, the list in unreacheable
63 * at the moment this function is called */
64 list_add_tail(&r
->list
, &ops
->rules_list
);
67 EXPORT_SYMBOL(fib_default_rule_add
);
69 static u32
fib_default_rule_pref(struct fib_rules_ops
*ops
)
71 struct list_head
*pos
;
72 struct fib_rule
*rule
;
74 if (!list_empty(&ops
->rules_list
)) {
75 pos
= ops
->rules_list
.next
;
76 if (pos
->next
!= &ops
->rules_list
) {
77 rule
= list_entry(pos
->next
, struct fib_rule
, list
);
79 return rule
->pref
- 1;
86 static void notify_rule_change(int event
, struct fib_rule
*rule
,
87 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
90 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
92 struct fib_rules_ops
*ops
;
95 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
96 if (ops
->family
== family
) {
97 if (!try_module_get(ops
->owner
))
108 static void rules_ops_put(struct fib_rules_ops
*ops
)
111 module_put(ops
->owner
);
114 static void flush_route_cache(struct fib_rules_ops
*ops
)
116 if (ops
->flush_cache
)
117 ops
->flush_cache(ops
);
120 static int __fib_rules_register(struct fib_rules_ops
*ops
)
123 struct fib_rules_ops
*o
;
128 if (ops
->rule_size
< sizeof(struct fib_rule
))
131 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
132 ops
->compare
== NULL
|| ops
->fill
== NULL
||
136 spin_lock(&net
->rules_mod_lock
);
137 list_for_each_entry(o
, &net
->rules_ops
, list
)
138 if (ops
->family
== o
->family
)
141 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
144 spin_unlock(&net
->rules_mod_lock
);
149 struct fib_rules_ops
*
150 fib_rules_register(const struct fib_rules_ops
*tmpl
, struct net
*net
)
152 struct fib_rules_ops
*ops
;
155 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
157 return ERR_PTR(-ENOMEM
);
159 INIT_LIST_HEAD(&ops
->rules_list
);
162 err
= __fib_rules_register(ops
);
170 EXPORT_SYMBOL_GPL(fib_rules_register
);
172 static void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
174 struct fib_rule
*rule
, *tmp
;
176 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
177 list_del_rcu(&rule
->list
);
184 void fib_rules_unregister(struct fib_rules_ops
*ops
)
186 struct net
*net
= ops
->fro_net
;
188 spin_lock(&net
->rules_mod_lock
);
189 list_del_rcu(&ops
->list
);
190 spin_unlock(&net
->rules_mod_lock
);
192 fib_rules_cleanup_ops(ops
);
195 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
197 static int uid_range_set(struct fib_kuid_range
*range
)
199 return uid_valid(range
->start
) && uid_valid(range
->end
);
202 static struct fib_kuid_range
nla_get_kuid_range(struct nlattr
**tb
)
204 struct fib_rule_uid_range
*in
;
205 struct fib_kuid_range out
;
207 in
= (struct fib_rule_uid_range
*)nla_data(tb
[FRA_UID_RANGE
]);
209 out
.start
= make_kuid(current_user_ns(), in
->start
);
210 out
.end
= make_kuid(current_user_ns(), in
->end
);
215 static int nla_put_uid_range(struct sk_buff
*skb
, struct fib_kuid_range
*range
)
217 struct fib_rule_uid_range out
= {
218 from_kuid_munged(current_user_ns(), range
->start
),
219 from_kuid_munged(current_user_ns(), range
->end
)
222 return nla_put(skb
, FRA_UID_RANGE
, sizeof(out
), &out
);
225 static int nla_get_port_range(struct nlattr
*pattr
,
226 struct fib_rule_port_range
*port_range
)
228 const struct fib_rule_port_range
*pr
= nla_data(pattr
);
230 if (!fib_rule_port_range_valid(pr
))
233 port_range
->start
= pr
->start
;
234 port_range
->end
= pr
->end
;
239 static int nla_put_port_range(struct sk_buff
*skb
, int attrtype
,
240 struct fib_rule_port_range
*range
)
242 return nla_put(skb
, attrtype
, sizeof(*range
), range
);
245 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
246 struct flowi
*fl
, int flags
,
247 struct fib_lookup_arg
*arg
)
251 if (rule
->iifindex
&& (rule
->iifindex
!= fl
->flowi_iif
))
254 if (rule
->oifindex
&& (rule
->oifindex
!= fl
->flowi_oif
))
257 if ((rule
->mark
^ fl
->flowi_mark
) & rule
->mark_mask
)
260 if (rule
->tun_id
&& (rule
->tun_id
!= fl
->flowi_tun_key
.tun_id
))
263 if (rule
->l3mdev
&& !l3mdev_fib_rule_match(rule
->fr_net
, fl
, arg
))
266 if (uid_lt(fl
->flowi_uid
, rule
->uid_range
.start
) ||
267 uid_gt(fl
->flowi_uid
, rule
->uid_range
.end
))
270 ret
= ops
->match(rule
, fl
, flags
);
272 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
275 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
276 int flags
, struct fib_lookup_arg
*arg
)
278 struct fib_rule
*rule
;
283 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
285 if (!fib_rule_match(rule
, ops
, fl
, flags
, arg
))
288 if (rule
->action
== FR_ACT_GOTO
) {
289 struct fib_rule
*target
;
291 target
= rcu_dereference(rule
->ctarget
);
292 if (target
== NULL
) {
298 } else if (rule
->action
== FR_ACT_NOP
)
301 err
= ops
->action(rule
, fl
, flags
, arg
);
303 if (!err
&& ops
->suppress
&& ops
->suppress(rule
, arg
))
306 if (err
!= -EAGAIN
) {
307 if ((arg
->flags
& FIB_LOOKUP_NOREF
) ||
308 likely(refcount_inc_not_zero(&rule
->refcnt
))) {
322 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
324 static int call_fib_rule_notifier(struct notifier_block
*nb
, struct net
*net
,
325 enum fib_event_type event_type
,
326 struct fib_rule
*rule
, int family
)
328 struct fib_rule_notifier_info info
= {
329 .info
.family
= family
,
333 return call_fib_notifier(nb
, net
, event_type
, &info
.info
);
336 static int call_fib_rule_notifiers(struct net
*net
,
337 enum fib_event_type event_type
,
338 struct fib_rule
*rule
,
339 struct fib_rules_ops
*ops
,
340 struct netlink_ext_ack
*extack
)
342 struct fib_rule_notifier_info info
= {
343 .info
.family
= ops
->family
,
344 .info
.extack
= extack
,
348 ops
->fib_rules_seq
++;
349 return call_fib_notifiers(net
, event_type
, &info
.info
);
352 /* Called with rcu_read_lock() */
353 int fib_rules_dump(struct net
*net
, struct notifier_block
*nb
, int family
)
355 struct fib_rules_ops
*ops
;
356 struct fib_rule
*rule
;
358 ops
= lookup_rules_ops(net
, family
);
360 return -EAFNOSUPPORT
;
361 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
)
362 call_fib_rule_notifier(nb
, net
, FIB_EVENT_RULE_ADD
, rule
,
368 EXPORT_SYMBOL_GPL(fib_rules_dump
);
370 unsigned int fib_rules_seq_read(struct net
*net
, int family
)
372 unsigned int fib_rules_seq
;
373 struct fib_rules_ops
*ops
;
377 ops
= lookup_rules_ops(net
, family
);
380 fib_rules_seq
= ops
->fib_rules_seq
;
383 return fib_rules_seq
;
385 EXPORT_SYMBOL_GPL(fib_rules_seq_read
);
387 static struct fib_rule
*rule_find(struct fib_rules_ops
*ops
,
388 struct fib_rule_hdr
*frh
,
390 struct fib_rule
*rule
,
395 list_for_each_entry(r
, &ops
->rules_list
, list
) {
396 if (rule
->action
&& r
->action
!= rule
->action
)
399 if (rule
->table
&& r
->table
!= rule
->table
)
402 if (user_priority
&& r
->pref
!= rule
->pref
)
405 if (rule
->iifname
[0] &&
406 memcmp(r
->iifname
, rule
->iifname
, IFNAMSIZ
))
409 if (rule
->oifname
[0] &&
410 memcmp(r
->oifname
, rule
->oifname
, IFNAMSIZ
))
413 if (rule
->mark
&& r
->mark
!= rule
->mark
)
416 if (rule
->suppress_ifgroup
!= -1 &&
417 r
->suppress_ifgroup
!= rule
->suppress_ifgroup
)
420 if (rule
->suppress_prefixlen
!= -1 &&
421 r
->suppress_prefixlen
!= rule
->suppress_prefixlen
)
424 if (rule
->mark_mask
&& r
->mark_mask
!= rule
->mark_mask
)
427 if (rule
->tun_id
&& r
->tun_id
!= rule
->tun_id
)
430 if (r
->fr_net
!= rule
->fr_net
)
433 if (rule
->l3mdev
&& r
->l3mdev
!= rule
->l3mdev
)
436 if (uid_range_set(&rule
->uid_range
) &&
437 (!uid_eq(r
->uid_range
.start
, rule
->uid_range
.start
) ||
438 !uid_eq(r
->uid_range
.end
, rule
->uid_range
.end
)))
441 if (rule
->ip_proto
&& r
->ip_proto
!= rule
->ip_proto
)
444 if (rule
->proto
&& r
->proto
!= rule
->proto
)
447 if (fib_rule_port_range_set(&rule
->sport_range
) &&
448 !fib_rule_port_range_compare(&r
->sport_range
,
452 if (fib_rule_port_range_set(&rule
->dport_range
) &&
453 !fib_rule_port_range_compare(&r
->dport_range
,
457 if (!ops
->compare(r
, frh
, tb
))
465 #ifdef CONFIG_NET_L3_MASTER_DEV
466 static int fib_nl2rule_l3mdev(struct nlattr
*nla
, struct fib_rule
*nlrule
,
467 struct netlink_ext_ack
*extack
)
469 nlrule
->l3mdev
= nla_get_u8(nla
);
470 if (nlrule
->l3mdev
!= 1) {
471 NL_SET_ERR_MSG(extack
, "Invalid l3mdev attribute");
478 static int fib_nl2rule_l3mdev(struct nlattr
*nla
, struct fib_rule
*nlrule
,
479 struct netlink_ext_ack
*extack
)
481 NL_SET_ERR_MSG(extack
, "l3mdev support is not enabled in kernel");
486 static int fib_nl2rule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
487 struct netlink_ext_ack
*extack
,
488 struct fib_rules_ops
*ops
,
490 struct fib_rule
**rule
,
493 struct net
*net
= sock_net(skb
->sk
);
494 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
495 struct fib_rule
*nlrule
= NULL
;
500 frh
->src_len
> (ops
->addr_size
* 8) ||
501 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
) {
502 NL_SET_ERR_MSG(extack
, "Invalid source address");
508 frh
->dst_len
> (ops
->addr_size
* 8) ||
509 nla_len(tb
[FRA_DST
]) != ops
->addr_size
) {
510 NL_SET_ERR_MSG(extack
, "Invalid dst address");
514 nlrule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
519 refcount_set(&nlrule
->refcnt
, 1);
520 nlrule
->fr_net
= net
;
522 if (tb
[FRA_PRIORITY
]) {
523 nlrule
->pref
= nla_get_u32(tb
[FRA_PRIORITY
]);
524 *user_priority
= true;
526 nlrule
->pref
= fib_default_rule_pref(ops
);
529 nlrule
->proto
= tb
[FRA_PROTOCOL
] ?
530 nla_get_u8(tb
[FRA_PROTOCOL
]) : RTPROT_UNSPEC
;
532 if (tb
[FRA_IIFNAME
]) {
533 struct net_device
*dev
;
535 nlrule
->iifindex
= -1;
536 nla_strlcpy(nlrule
->iifname
, tb
[FRA_IIFNAME
], IFNAMSIZ
);
537 dev
= __dev_get_by_name(net
, nlrule
->iifname
);
539 nlrule
->iifindex
= dev
->ifindex
;
542 if (tb
[FRA_OIFNAME
]) {
543 struct net_device
*dev
;
545 nlrule
->oifindex
= -1;
546 nla_strlcpy(nlrule
->oifname
, tb
[FRA_OIFNAME
], IFNAMSIZ
);
547 dev
= __dev_get_by_name(net
, nlrule
->oifname
);
549 nlrule
->oifindex
= dev
->ifindex
;
552 if (tb
[FRA_FWMARK
]) {
553 nlrule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
555 /* compatibility: if the mark value is non-zero all bits
556 * are compared unless a mask is explicitly specified.
558 nlrule
->mark_mask
= 0xFFFFFFFF;
562 nlrule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
565 nlrule
->tun_id
= nla_get_be64(tb
[FRA_TUN_ID
]);
568 if (tb
[FRA_L3MDEV
] &&
569 fib_nl2rule_l3mdev(tb
[FRA_L3MDEV
], nlrule
, extack
) < 0)
572 nlrule
->action
= frh
->action
;
573 nlrule
->flags
= frh
->flags
;
574 nlrule
->table
= frh_get_table(frh
, tb
);
575 if (tb
[FRA_SUPPRESS_PREFIXLEN
])
576 nlrule
->suppress_prefixlen
= nla_get_u32(tb
[FRA_SUPPRESS_PREFIXLEN
]);
578 nlrule
->suppress_prefixlen
= -1;
580 if (tb
[FRA_SUPPRESS_IFGROUP
])
581 nlrule
->suppress_ifgroup
= nla_get_u32(tb
[FRA_SUPPRESS_IFGROUP
]);
583 nlrule
->suppress_ifgroup
= -1;
586 if (nlrule
->action
!= FR_ACT_GOTO
) {
587 NL_SET_ERR_MSG(extack
, "Unexpected goto");
591 nlrule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
592 /* Backward jumps are prohibited to avoid endless loops */
593 if (nlrule
->target
<= nlrule
->pref
) {
594 NL_SET_ERR_MSG(extack
, "Backward goto not supported");
597 } else if (nlrule
->action
== FR_ACT_GOTO
) {
598 NL_SET_ERR_MSG(extack
, "Missing goto target for action goto");
602 if (nlrule
->l3mdev
&& nlrule
->table
) {
603 NL_SET_ERR_MSG(extack
, "l3mdev and table are mutually exclusive");
607 if (tb
[FRA_UID_RANGE
]) {
608 if (current_user_ns() != net
->user_ns
) {
610 NL_SET_ERR_MSG(extack
, "No permission to set uid");
614 nlrule
->uid_range
= nla_get_kuid_range(tb
);
616 if (!uid_range_set(&nlrule
->uid_range
) ||
617 !uid_lte(nlrule
->uid_range
.start
, nlrule
->uid_range
.end
)) {
618 NL_SET_ERR_MSG(extack
, "Invalid uid range");
622 nlrule
->uid_range
= fib_kuid_range_unset
;
625 if (tb
[FRA_IP_PROTO
])
626 nlrule
->ip_proto
= nla_get_u8(tb
[FRA_IP_PROTO
]);
628 if (tb
[FRA_SPORT_RANGE
]) {
629 err
= nla_get_port_range(tb
[FRA_SPORT_RANGE
],
630 &nlrule
->sport_range
);
632 NL_SET_ERR_MSG(extack
, "Invalid sport range");
637 if (tb
[FRA_DPORT_RANGE
]) {
638 err
= nla_get_port_range(tb
[FRA_DPORT_RANGE
],
639 &nlrule
->dport_range
);
641 NL_SET_ERR_MSG(extack
, "Invalid dport range");
656 static int rule_exists(struct fib_rules_ops
*ops
, struct fib_rule_hdr
*frh
,
657 struct nlattr
**tb
, struct fib_rule
*rule
)
661 list_for_each_entry(r
, &ops
->rules_list
, list
) {
662 if (r
->action
!= rule
->action
)
665 if (r
->table
!= rule
->table
)
668 if (r
->pref
!= rule
->pref
)
671 if (memcmp(r
->iifname
, rule
->iifname
, IFNAMSIZ
))
674 if (memcmp(r
->oifname
, rule
->oifname
, IFNAMSIZ
))
677 if (r
->mark
!= rule
->mark
)
680 if (r
->suppress_ifgroup
!= rule
->suppress_ifgroup
)
683 if (r
->suppress_prefixlen
!= rule
->suppress_prefixlen
)
686 if (r
->mark_mask
!= rule
->mark_mask
)
689 if (r
->tun_id
!= rule
->tun_id
)
692 if (r
->fr_net
!= rule
->fr_net
)
695 if (r
->l3mdev
!= rule
->l3mdev
)
698 if (!uid_eq(r
->uid_range
.start
, rule
->uid_range
.start
) ||
699 !uid_eq(r
->uid_range
.end
, rule
->uid_range
.end
))
702 if (r
->ip_proto
!= rule
->ip_proto
)
705 if (r
->proto
!= rule
->proto
)
708 if (!fib_rule_port_range_compare(&r
->sport_range
,
712 if (!fib_rule_port_range_compare(&r
->dport_range
,
716 if (!ops
->compare(r
, frh
, tb
))
723 int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
724 struct netlink_ext_ack
*extack
)
726 struct net
*net
= sock_net(skb
->sk
);
727 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
728 struct fib_rules_ops
*ops
= NULL
;
729 struct fib_rule
*rule
= NULL
, *r
, *last
= NULL
;
730 struct nlattr
*tb
[FRA_MAX
+ 1];
731 int err
= -EINVAL
, unresolved
= 0;
732 bool user_priority
= false;
734 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
735 NL_SET_ERR_MSG(extack
, "Invalid msg length");
739 ops
= lookup_rules_ops(net
, frh
->family
);
742 NL_SET_ERR_MSG(extack
, "Rule family not supported");
746 err
= nlmsg_parse_deprecated(nlh
, sizeof(*frh
), tb
, FRA_MAX
,
747 ops
->policy
, extack
);
749 NL_SET_ERR_MSG(extack
, "Error parsing msg");
753 err
= fib_nl2rule(skb
, nlh
, extack
, ops
, tb
, &rule
, &user_priority
);
757 if ((nlh
->nlmsg_flags
& NLM_F_EXCL
) &&
758 rule_exists(ops
, frh
, tb
, rule
)) {
763 err
= ops
->configure(rule
, skb
, frh
, tb
, extack
);
767 err
= call_fib_rule_notifiers(net
, FIB_EVENT_RULE_ADD
, rule
, ops
,
772 list_for_each_entry(r
, &ops
->rules_list
, list
) {
773 if (r
->pref
== rule
->target
) {
774 RCU_INIT_POINTER(rule
->ctarget
, r
);
779 if (rcu_dereference_protected(rule
->ctarget
, 1) == NULL
)
782 list_for_each_entry(r
, &ops
->rules_list
, list
) {
783 if (r
->pref
> rule
->pref
)
789 list_add_rcu(&rule
->list
, &last
->list
);
791 list_add_rcu(&rule
->list
, &ops
->rules_list
);
793 if (ops
->unresolved_rules
) {
795 * There are unresolved goto rules in the list, check if
796 * any of them are pointing to this new rule.
798 list_for_each_entry(r
, &ops
->rules_list
, list
) {
799 if (r
->action
== FR_ACT_GOTO
&&
800 r
->target
== rule
->pref
&&
801 rtnl_dereference(r
->ctarget
) == NULL
) {
802 rcu_assign_pointer(r
->ctarget
, rule
);
803 if (--ops
->unresolved_rules
== 0)
809 if (rule
->action
== FR_ACT_GOTO
)
810 ops
->nr_goto_rules
++;
813 ops
->unresolved_rules
++;
816 ip_tunnel_need_metadata();
818 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).portid
);
819 flush_route_cache(ops
);
829 EXPORT_SYMBOL_GPL(fib_nl_newrule
);
831 int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
832 struct netlink_ext_ack
*extack
)
834 struct net
*net
= sock_net(skb
->sk
);
835 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
836 struct fib_rules_ops
*ops
= NULL
;
837 struct fib_rule
*rule
= NULL
, *r
, *nlrule
= NULL
;
838 struct nlattr
*tb
[FRA_MAX
+1];
840 bool user_priority
= false;
842 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
843 NL_SET_ERR_MSG(extack
, "Invalid msg length");
847 ops
= lookup_rules_ops(net
, frh
->family
);
850 NL_SET_ERR_MSG(extack
, "Rule family not supported");
854 err
= nlmsg_parse_deprecated(nlh
, sizeof(*frh
), tb
, FRA_MAX
,
855 ops
->policy
, extack
);
857 NL_SET_ERR_MSG(extack
, "Error parsing msg");
861 err
= fib_nl2rule(skb
, nlh
, extack
, ops
, tb
, &nlrule
, &user_priority
);
865 rule
= rule_find(ops
, frh
, tb
, nlrule
, user_priority
);
871 if (rule
->flags
& FIB_RULE_PERMANENT
) {
877 err
= ops
->delete(rule
);
883 ip_tunnel_unneed_metadata();
885 list_del_rcu(&rule
->list
);
887 if (rule
->action
== FR_ACT_GOTO
) {
888 ops
->nr_goto_rules
--;
889 if (rtnl_dereference(rule
->ctarget
) == NULL
)
890 ops
->unresolved_rules
--;
894 * Check if this rule is a target to any of them. If so,
895 * adjust to the next one with the same preference or
896 * disable them. As this operation is eventually very
897 * expensive, it is only performed if goto rules, except
898 * current if it is goto rule, have actually been added.
900 if (ops
->nr_goto_rules
> 0) {
903 n
= list_next_entry(rule
, list
);
904 if (&n
->list
== &ops
->rules_list
|| n
->pref
!= rule
->pref
)
906 list_for_each_entry(r
, &ops
->rules_list
, list
) {
907 if (rtnl_dereference(r
->ctarget
) != rule
)
909 rcu_assign_pointer(r
->ctarget
, n
);
911 ops
->unresolved_rules
++;
915 call_fib_rule_notifiers(net
, FIB_EVENT_RULE_DEL
, rule
, ops
,
917 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
918 NETLINK_CB(skb
).portid
);
920 flush_route_cache(ops
);
930 EXPORT_SYMBOL_GPL(fib_nl_delrule
);
932 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
933 struct fib_rule
*rule
)
935 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
936 + nla_total_size(IFNAMSIZ
) /* FRA_IIFNAME */
937 + nla_total_size(IFNAMSIZ
) /* FRA_OIFNAME */
938 + nla_total_size(4) /* FRA_PRIORITY */
939 + nla_total_size(4) /* FRA_TABLE */
940 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
941 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
942 + nla_total_size(4) /* FRA_FWMARK */
943 + nla_total_size(4) /* FRA_FWMASK */
944 + nla_total_size_64bit(8) /* FRA_TUN_ID */
945 + nla_total_size(sizeof(struct fib_kuid_range
))
946 + nla_total_size(1) /* FRA_PROTOCOL */
947 + nla_total_size(1) /* FRA_IP_PROTO */
948 + nla_total_size(sizeof(struct fib_rule_port_range
)) /* FRA_SPORT_RANGE */
949 + nla_total_size(sizeof(struct fib_rule_port_range
)); /* FRA_DPORT_RANGE */
951 if (ops
->nlmsg_payload
)
952 payload
+= ops
->nlmsg_payload(rule
);
957 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
958 u32 pid
, u32 seq
, int type
, int flags
,
959 struct fib_rules_ops
*ops
)
961 struct nlmsghdr
*nlh
;
962 struct fib_rule_hdr
*frh
;
964 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
968 frh
= nlmsg_data(nlh
);
969 frh
->family
= ops
->family
;
970 frh
->table
= rule
->table
;
971 if (nla_put_u32(skb
, FRA_TABLE
, rule
->table
))
972 goto nla_put_failure
;
973 if (nla_put_u32(skb
, FRA_SUPPRESS_PREFIXLEN
, rule
->suppress_prefixlen
))
974 goto nla_put_failure
;
977 frh
->action
= rule
->action
;
978 frh
->flags
= rule
->flags
;
980 if (nla_put_u8(skb
, FRA_PROTOCOL
, rule
->proto
))
981 goto nla_put_failure
;
983 if (rule
->action
== FR_ACT_GOTO
&&
984 rcu_access_pointer(rule
->ctarget
) == NULL
)
985 frh
->flags
|= FIB_RULE_UNRESOLVED
;
987 if (rule
->iifname
[0]) {
988 if (nla_put_string(skb
, FRA_IIFNAME
, rule
->iifname
))
989 goto nla_put_failure
;
990 if (rule
->iifindex
== -1)
991 frh
->flags
|= FIB_RULE_IIF_DETACHED
;
994 if (rule
->oifname
[0]) {
995 if (nla_put_string(skb
, FRA_OIFNAME
, rule
->oifname
))
996 goto nla_put_failure
;
997 if (rule
->oifindex
== -1)
998 frh
->flags
|= FIB_RULE_OIF_DETACHED
;
1002 nla_put_u32(skb
, FRA_PRIORITY
, rule
->pref
)) ||
1004 nla_put_u32(skb
, FRA_FWMARK
, rule
->mark
)) ||
1005 ((rule
->mark_mask
|| rule
->mark
) &&
1006 nla_put_u32(skb
, FRA_FWMASK
, rule
->mark_mask
)) ||
1008 nla_put_u32(skb
, FRA_GOTO
, rule
->target
)) ||
1010 nla_put_be64(skb
, FRA_TUN_ID
, rule
->tun_id
, FRA_PAD
)) ||
1012 nla_put_u8(skb
, FRA_L3MDEV
, rule
->l3mdev
)) ||
1013 (uid_range_set(&rule
->uid_range
) &&
1014 nla_put_uid_range(skb
, &rule
->uid_range
)) ||
1015 (fib_rule_port_range_set(&rule
->sport_range
) &&
1016 nla_put_port_range(skb
, FRA_SPORT_RANGE
, &rule
->sport_range
)) ||
1017 (fib_rule_port_range_set(&rule
->dport_range
) &&
1018 nla_put_port_range(skb
, FRA_DPORT_RANGE
, &rule
->dport_range
)) ||
1019 (rule
->ip_proto
&& nla_put_u8(skb
, FRA_IP_PROTO
, rule
->ip_proto
)))
1020 goto nla_put_failure
;
1022 if (rule
->suppress_ifgroup
!= -1) {
1023 if (nla_put_u32(skb
, FRA_SUPPRESS_IFGROUP
, rule
->suppress_ifgroup
))
1024 goto nla_put_failure
;
1027 if (ops
->fill(rule
, skb
, frh
) < 0)
1028 goto nla_put_failure
;
1030 nlmsg_end(skb
, nlh
);
1034 nlmsg_cancel(skb
, nlh
);
1038 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
1039 struct fib_rules_ops
*ops
)
1042 struct fib_rule
*rule
;
1046 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
1047 if (idx
< cb
->args
[1])
1050 err
= fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).portid
,
1051 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
1065 static int fib_valid_dumprule_req(const struct nlmsghdr
*nlh
,
1066 struct netlink_ext_ack
*extack
)
1068 struct fib_rule_hdr
*frh
;
1070 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
))) {
1071 NL_SET_ERR_MSG(extack
, "Invalid header for fib rule dump request");
1075 frh
= nlmsg_data(nlh
);
1076 if (frh
->dst_len
|| frh
->src_len
|| frh
->tos
|| frh
->table
||
1077 frh
->res1
|| frh
->res2
|| frh
->action
|| frh
->flags
) {
1078 NL_SET_ERR_MSG(extack
,
1079 "Invalid values in header for fib rule dump request");
1083 if (nlmsg_attrlen(nlh
, sizeof(*frh
))) {
1084 NL_SET_ERR_MSG(extack
, "Invalid data after header in fib rule dump request");
1091 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1093 const struct nlmsghdr
*nlh
= cb
->nlh
;
1094 struct net
*net
= sock_net(skb
->sk
);
1095 struct fib_rules_ops
*ops
;
1096 int idx
= 0, family
;
1098 if (cb
->strict_check
) {
1099 int err
= fib_valid_dumprule_req(nlh
, cb
->extack
);
1105 family
= rtnl_msg_family(nlh
);
1106 if (family
!= AF_UNSPEC
) {
1107 /* Protocol specific dump request */
1108 ops
= lookup_rules_ops(net
, family
);
1110 return -EAFNOSUPPORT
;
1112 dump_rules(skb
, cb
, ops
);
1118 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
1119 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
1122 if (dump_rules(skb
, cb
, ops
) < 0)
1135 static void notify_rule_change(int event
, struct fib_rule
*rule
,
1136 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
1140 struct sk_buff
*skb
;
1144 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
1148 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
1150 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
1151 WARN_ON(err
== -EMSGSIZE
);
1156 rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
1160 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
1163 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
1165 struct fib_rule
*rule
;
1167 list_for_each_entry(rule
, rules
, list
) {
1168 if (rule
->iifindex
== -1 &&
1169 strcmp(dev
->name
, rule
->iifname
) == 0)
1170 rule
->iifindex
= dev
->ifindex
;
1171 if (rule
->oifindex
== -1 &&
1172 strcmp(dev
->name
, rule
->oifname
) == 0)
1173 rule
->oifindex
= dev
->ifindex
;
1177 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
1179 struct fib_rule
*rule
;
1181 list_for_each_entry(rule
, rules
, list
) {
1182 if (rule
->iifindex
== dev
->ifindex
)
1183 rule
->iifindex
= -1;
1184 if (rule
->oifindex
== dev
->ifindex
)
1185 rule
->oifindex
= -1;
1190 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
1193 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1194 struct net
*net
= dev_net(dev
);
1195 struct fib_rules_ops
*ops
;
1200 case NETDEV_REGISTER
:
1201 list_for_each_entry(ops
, &net
->rules_ops
, list
)
1202 attach_rules(&ops
->rules_list
, dev
);
1205 case NETDEV_CHANGENAME
:
1206 list_for_each_entry(ops
, &net
->rules_ops
, list
) {
1207 detach_rules(&ops
->rules_list
, dev
);
1208 attach_rules(&ops
->rules_list
, dev
);
1212 case NETDEV_UNREGISTER
:
1213 list_for_each_entry(ops
, &net
->rules_ops
, list
)
1214 detach_rules(&ops
->rules_list
, dev
);
1221 static struct notifier_block fib_rules_notifier
= {
1222 .notifier_call
= fib_rules_event
,
1225 static int __net_init
fib_rules_net_init(struct net
*net
)
1227 INIT_LIST_HEAD(&net
->rules_ops
);
1228 spin_lock_init(&net
->rules_mod_lock
);
1232 static void __net_exit
fib_rules_net_exit(struct net
*net
)
1234 WARN_ON_ONCE(!list_empty(&net
->rules_ops
));
1237 static struct pernet_operations fib_rules_net_ops
= {
1238 .init
= fib_rules_net_init
,
1239 .exit
= fib_rules_net_exit
,
1242 static int __init
fib_rules_init(void)
1245 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
, 0);
1246 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
, 0);
1247 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
, 0);
1249 err
= register_pernet_subsys(&fib_rules_net_ops
);
1253 err
= register_netdevice_notifier(&fib_rules_notifier
);
1255 goto fail_unregister
;
1260 unregister_pernet_subsys(&fib_rules_net_ops
);
1262 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
1263 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
1264 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
1268 subsys_initcall(fib_rules_init
);