2 * net/core/fib_rules.c Generic Routing Rules
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.
8 * Authors: Thomas Graf <tgraf@suug.ch>
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>
18 #include <net/fib_rules.h>
19 #include <net/ip_tunnels.h>
21 static const struct fib_kuid_range fib_kuid_range_unset
= {
26 bool fib_rule_matchall(const struct fib_rule
*rule
)
28 if (rule
->iifindex
|| rule
->oifindex
|| rule
->mark
|| rule
->tun_id
||
31 if (rule
->suppress_ifgroup
!= -1 || rule
->suppress_prefixlen
!= -1)
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
))
38 EXPORT_SYMBOL_GPL(fib_rule_matchall
);
40 int fib_default_rule_add(struct fib_rules_ops
*ops
,
41 u32 pref
, u32 table
, u32 flags
)
45 r
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
49 refcount_set(&r
->refcnt
, 1);
50 r
->action
= FR_ACT_TO_TBL
;
54 r
->fr_net
= ops
->fro_net
;
55 r
->uid_range
= fib_kuid_range_unset
;
57 r
->suppress_prefixlen
= -1;
58 r
->suppress_ifgroup
= -1;
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
);
65 EXPORT_SYMBOL(fib_default_rule_add
);
67 static u32
fib_default_rule_pref(struct fib_rules_ops
*ops
)
69 struct list_head
*pos
;
70 struct fib_rule
*rule
;
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
);
77 return rule
->pref
- 1;
84 static void notify_rule_change(int event
, struct fib_rule
*rule
,
85 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
88 static struct fib_rules_ops
*lookup_rules_ops(struct net
*net
, int family
)
90 struct fib_rules_ops
*ops
;
93 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
94 if (ops
->family
== family
) {
95 if (!try_module_get(ops
->owner
))
106 static void rules_ops_put(struct fib_rules_ops
*ops
)
109 module_put(ops
->owner
);
112 static void flush_route_cache(struct fib_rules_ops
*ops
)
114 if (ops
->flush_cache
)
115 ops
->flush_cache(ops
);
118 static int __fib_rules_register(struct fib_rules_ops
*ops
)
121 struct fib_rules_ops
*o
;
126 if (ops
->rule_size
< sizeof(struct fib_rule
))
129 if (ops
->match
== NULL
|| ops
->configure
== NULL
||
130 ops
->compare
== NULL
|| ops
->fill
== NULL
||
134 spin_lock(&net
->rules_mod_lock
);
135 list_for_each_entry(o
, &net
->rules_ops
, list
)
136 if (ops
->family
== o
->family
)
139 list_add_tail_rcu(&ops
->list
, &net
->rules_ops
);
142 spin_unlock(&net
->rules_mod_lock
);
147 struct fib_rules_ops
*
148 fib_rules_register(const struct fib_rules_ops
*tmpl
, struct net
*net
)
150 struct fib_rules_ops
*ops
;
153 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
155 return ERR_PTR(-ENOMEM
);
157 INIT_LIST_HEAD(&ops
->rules_list
);
160 err
= __fib_rules_register(ops
);
168 EXPORT_SYMBOL_GPL(fib_rules_register
);
170 static void fib_rules_cleanup_ops(struct fib_rules_ops
*ops
)
172 struct fib_rule
*rule
, *tmp
;
174 list_for_each_entry_safe(rule
, tmp
, &ops
->rules_list
, list
) {
175 list_del_rcu(&rule
->list
);
182 void fib_rules_unregister(struct fib_rules_ops
*ops
)
184 struct net
*net
= ops
->fro_net
;
186 spin_lock(&net
->rules_mod_lock
);
187 list_del_rcu(&ops
->list
);
188 spin_unlock(&net
->rules_mod_lock
);
190 fib_rules_cleanup_ops(ops
);
193 EXPORT_SYMBOL_GPL(fib_rules_unregister
);
195 static int uid_range_set(struct fib_kuid_range
*range
)
197 return uid_valid(range
->start
) && uid_valid(range
->end
);
200 static struct fib_kuid_range
nla_get_kuid_range(struct nlattr
**tb
)
202 struct fib_rule_uid_range
*in
;
203 struct fib_kuid_range out
;
205 in
= (struct fib_rule_uid_range
*)nla_data(tb
[FRA_UID_RANGE
]);
207 out
.start
= make_kuid(current_user_ns(), in
->start
);
208 out
.end
= make_kuid(current_user_ns(), in
->end
);
213 static int nla_put_uid_range(struct sk_buff
*skb
, struct fib_kuid_range
*range
)
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
)
220 return nla_put(skb
, FRA_UID_RANGE
, sizeof(out
), &out
);
223 static int fib_rule_match(struct fib_rule
*rule
, struct fib_rules_ops
*ops
,
224 struct flowi
*fl
, int flags
,
225 struct fib_lookup_arg
*arg
)
229 if (rule
->iifindex
&& (rule
->iifindex
!= fl
->flowi_iif
))
232 if (rule
->oifindex
&& (rule
->oifindex
!= fl
->flowi_oif
))
235 if ((rule
->mark
^ fl
->flowi_mark
) & rule
->mark_mask
)
238 if (rule
->tun_id
&& (rule
->tun_id
!= fl
->flowi_tun_key
.tun_id
))
241 if (rule
->l3mdev
&& !l3mdev_fib_rule_match(rule
->fr_net
, fl
, arg
))
244 if (uid_lt(fl
->flowi_uid
, rule
->uid_range
.start
) ||
245 uid_gt(fl
->flowi_uid
, rule
->uid_range
.end
))
248 ret
= ops
->match(rule
, fl
, flags
);
250 return (rule
->flags
& FIB_RULE_INVERT
) ? !ret
: ret
;
253 int fib_rules_lookup(struct fib_rules_ops
*ops
, struct flowi
*fl
,
254 int flags
, struct fib_lookup_arg
*arg
)
256 struct fib_rule
*rule
;
261 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
263 if (!fib_rule_match(rule
, ops
, fl
, flags
, arg
))
266 if (rule
->action
== FR_ACT_GOTO
) {
267 struct fib_rule
*target
;
269 target
= rcu_dereference(rule
->ctarget
);
270 if (target
== NULL
) {
276 } else if (rule
->action
== FR_ACT_NOP
)
279 err
= ops
->action(rule
, fl
, flags
, arg
);
281 if (!err
&& ops
->suppress
&& ops
->suppress(rule
, arg
))
284 if (err
!= -EAGAIN
) {
285 if ((arg
->flags
& FIB_LOOKUP_NOREF
) ||
286 likely(refcount_inc_not_zero(&rule
->refcnt
))) {
300 EXPORT_SYMBOL_GPL(fib_rules_lookup
);
302 static int validate_rulemsg(struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
303 struct fib_rules_ops
*ops
)
308 if (tb
[FRA_SRC
] == NULL
||
309 frh
->src_len
> (ops
->addr_size
* 8) ||
310 nla_len(tb
[FRA_SRC
]) != ops
->addr_size
)
314 if (tb
[FRA_DST
] == NULL
||
315 frh
->dst_len
> (ops
->addr_size
* 8) ||
316 nla_len(tb
[FRA_DST
]) != ops
->addr_size
)
324 static int rule_exists(struct fib_rules_ops
*ops
, struct fib_rule_hdr
*frh
,
325 struct nlattr
**tb
, struct fib_rule
*rule
)
329 list_for_each_entry(r
, &ops
->rules_list
, list
) {
330 if (r
->action
!= rule
->action
)
333 if (r
->table
!= rule
->table
)
336 if (r
->pref
!= rule
->pref
)
339 if (memcmp(r
->iifname
, rule
->iifname
, IFNAMSIZ
))
342 if (memcmp(r
->oifname
, rule
->oifname
, IFNAMSIZ
))
345 if (r
->mark
!= rule
->mark
)
348 if (r
->mark_mask
!= rule
->mark_mask
)
351 if (r
->tun_id
!= rule
->tun_id
)
354 if (r
->fr_net
!= rule
->fr_net
)
357 if (r
->l3mdev
!= rule
->l3mdev
)
360 if (!uid_eq(r
->uid_range
.start
, rule
->uid_range
.start
) ||
361 !uid_eq(r
->uid_range
.end
, rule
->uid_range
.end
))
364 if (!ops
->compare(r
, frh
, tb
))
371 int fib_nl_newrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
372 struct netlink_ext_ack
*extack
)
374 struct net
*net
= sock_net(skb
->sk
);
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];
379 int err
= -EINVAL
, unresolved
= 0;
381 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
384 ops
= lookup_rules_ops(net
, frh
->family
);
390 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
, extack
);
394 err
= validate_rulemsg(frh
, tb
, ops
);
398 rule
= kzalloc(ops
->rule_size
, GFP_KERNEL
);
403 refcount_set(&rule
->refcnt
, 1);
406 rule
->pref
= tb
[FRA_PRIORITY
] ? nla_get_u32(tb
[FRA_PRIORITY
])
407 : fib_default_rule_pref(ops
);
409 if (tb
[FRA_IIFNAME
]) {
410 struct net_device
*dev
;
413 nla_strlcpy(rule
->iifname
, tb
[FRA_IIFNAME
], IFNAMSIZ
);
414 dev
= __dev_get_by_name(net
, rule
->iifname
);
416 rule
->iifindex
= dev
->ifindex
;
419 if (tb
[FRA_OIFNAME
]) {
420 struct net_device
*dev
;
423 nla_strlcpy(rule
->oifname
, tb
[FRA_OIFNAME
], IFNAMSIZ
);
424 dev
= __dev_get_by_name(net
, rule
->oifname
);
426 rule
->oifindex
= dev
->ifindex
;
429 if (tb
[FRA_FWMARK
]) {
430 rule
->mark
= nla_get_u32(tb
[FRA_FWMARK
]);
432 /* compatibility: if the mark value is non-zero all bits
433 * are compared unless a mask is explicitly specified.
435 rule
->mark_mask
= 0xFFFFFFFF;
439 rule
->mark_mask
= nla_get_u32(tb
[FRA_FWMASK
]);
442 rule
->tun_id
= nla_get_be64(tb
[FRA_TUN_ID
]);
445 if (tb
[FRA_L3MDEV
]) {
446 #ifdef CONFIG_NET_L3_MASTER_DEV
447 rule
->l3mdev
= nla_get_u8(tb
[FRA_L3MDEV
]);
448 if (rule
->l3mdev
!= 1)
453 rule
->action
= frh
->action
;
454 rule
->flags
= frh
->flags
;
455 rule
->table
= frh_get_table(frh
, tb
);
456 if (tb
[FRA_SUPPRESS_PREFIXLEN
])
457 rule
->suppress_prefixlen
= nla_get_u32(tb
[FRA_SUPPRESS_PREFIXLEN
]);
459 rule
->suppress_prefixlen
= -1;
461 if (tb
[FRA_SUPPRESS_IFGROUP
])
462 rule
->suppress_ifgroup
= nla_get_u32(tb
[FRA_SUPPRESS_IFGROUP
]);
464 rule
->suppress_ifgroup
= -1;
467 if (rule
->action
!= FR_ACT_GOTO
)
470 rule
->target
= nla_get_u32(tb
[FRA_GOTO
]);
471 /* Backward jumps are prohibited to avoid endless loops */
472 if (rule
->target
<= rule
->pref
)
475 list_for_each_entry(r
, &ops
->rules_list
, list
) {
476 if (r
->pref
== rule
->target
) {
477 RCU_INIT_POINTER(rule
->ctarget
, r
);
482 if (rcu_dereference_protected(rule
->ctarget
, 1) == NULL
)
484 } else if (rule
->action
== FR_ACT_GOTO
)
487 if (rule
->l3mdev
&& rule
->table
)
490 if (tb
[FRA_UID_RANGE
]) {
491 if (current_user_ns() != net
->user_ns
) {
496 rule
->uid_range
= nla_get_kuid_range(tb
);
498 if (!uid_range_set(&rule
->uid_range
) ||
499 !uid_lte(rule
->uid_range
.start
, rule
->uid_range
.end
))
502 rule
->uid_range
= fib_kuid_range_unset
;
505 if ((nlh
->nlmsg_flags
& NLM_F_EXCL
) &&
506 rule_exists(ops
, frh
, tb
, rule
)) {
511 err
= ops
->configure(rule
, skb
, frh
, tb
);
515 list_for_each_entry(r
, &ops
->rules_list
, list
) {
516 if (r
->pref
> rule
->pref
)
522 list_add_rcu(&rule
->list
, &last
->list
);
524 list_add_rcu(&rule
->list
, &ops
->rules_list
);
526 if (ops
->unresolved_rules
) {
528 * There are unresolved goto rules in the list, check if
529 * any of them are pointing to this new rule.
531 list_for_each_entry(r
, &ops
->rules_list
, list
) {
532 if (r
->action
== FR_ACT_GOTO
&&
533 r
->target
== rule
->pref
&&
534 rtnl_dereference(r
->ctarget
) == NULL
) {
535 rcu_assign_pointer(r
->ctarget
, rule
);
536 if (--ops
->unresolved_rules
== 0)
542 if (rule
->action
== FR_ACT_GOTO
)
543 ops
->nr_goto_rules
++;
546 ops
->unresolved_rules
++;
549 ip_tunnel_need_metadata();
551 notify_rule_change(RTM_NEWRULE
, rule
, ops
, nlh
, NETLINK_CB(skb
).portid
);
552 flush_route_cache(ops
);
562 EXPORT_SYMBOL_GPL(fib_nl_newrule
);
564 int fib_nl_delrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
565 struct netlink_ext_ack
*extack
)
567 struct net
*net
= sock_net(skb
->sk
);
568 struct fib_rule_hdr
*frh
= nlmsg_data(nlh
);
569 struct fib_rules_ops
*ops
= NULL
;
570 struct fib_rule
*rule
, *r
;
571 struct nlattr
*tb
[FRA_MAX
+1];
572 struct fib_kuid_range range
;
575 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*frh
)))
578 ops
= lookup_rules_ops(net
, frh
->family
);
584 err
= nlmsg_parse(nlh
, sizeof(*frh
), tb
, FRA_MAX
, ops
->policy
, extack
);
588 err
= validate_rulemsg(frh
, tb
, ops
);
592 if (tb
[FRA_UID_RANGE
]) {
593 range
= nla_get_kuid_range(tb
);
594 if (!uid_range_set(&range
)) {
599 range
= fib_kuid_range_unset
;
602 list_for_each_entry(rule
, &ops
->rules_list
, list
) {
603 if (frh
->action
&& (frh
->action
!= rule
->action
))
606 if (frh_get_table(frh
, tb
) &&
607 (frh_get_table(frh
, tb
) != rule
->table
))
610 if (tb
[FRA_PRIORITY
] &&
611 (rule
->pref
!= nla_get_u32(tb
[FRA_PRIORITY
])))
614 if (tb
[FRA_IIFNAME
] &&
615 nla_strcmp(tb
[FRA_IIFNAME
], rule
->iifname
))
618 if (tb
[FRA_OIFNAME
] &&
619 nla_strcmp(tb
[FRA_OIFNAME
], rule
->oifname
))
622 if (tb
[FRA_FWMARK
] &&
623 (rule
->mark
!= nla_get_u32(tb
[FRA_FWMARK
])))
626 if (tb
[FRA_FWMASK
] &&
627 (rule
->mark_mask
!= nla_get_u32(tb
[FRA_FWMASK
])))
630 if (tb
[FRA_TUN_ID
] &&
631 (rule
->tun_id
!= nla_get_be64(tb
[FRA_TUN_ID
])))
634 if (tb
[FRA_L3MDEV
] &&
635 (rule
->l3mdev
!= nla_get_u8(tb
[FRA_L3MDEV
])))
638 if (uid_range_set(&range
) &&
639 (!uid_eq(rule
->uid_range
.start
, range
.start
) ||
640 !uid_eq(rule
->uid_range
.end
, range
.end
)))
643 if (!ops
->compare(rule
, frh
, tb
))
646 if (rule
->flags
& FIB_RULE_PERMANENT
) {
652 err
= ops
->delete(rule
);
658 ip_tunnel_unneed_metadata();
660 list_del_rcu(&rule
->list
);
662 if (rule
->action
== FR_ACT_GOTO
) {
663 ops
->nr_goto_rules
--;
664 if (rtnl_dereference(rule
->ctarget
) == NULL
)
665 ops
->unresolved_rules
--;
669 * Check if this rule is a target to any of them. If so,
670 * adjust to the next one with the same preference or
671 * disable them. As this operation is eventually very
672 * expensive, it is only performed if goto rules, except
673 * current if it is goto rule, have actually been added.
675 if (ops
->nr_goto_rules
> 0) {
678 n
= list_next_entry(rule
, list
);
679 if (&n
->list
== &ops
->rules_list
|| n
->pref
!= rule
->pref
)
681 list_for_each_entry(r
, &ops
->rules_list
, list
) {
682 if (rtnl_dereference(r
->ctarget
) != rule
)
684 rcu_assign_pointer(r
->ctarget
, n
);
686 ops
->unresolved_rules
++;
690 notify_rule_change(RTM_DELRULE
, rule
, ops
, nlh
,
691 NETLINK_CB(skb
).portid
);
693 flush_route_cache(ops
);
703 EXPORT_SYMBOL_GPL(fib_nl_delrule
);
705 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops
*ops
,
706 struct fib_rule
*rule
)
708 size_t payload
= NLMSG_ALIGN(sizeof(struct fib_rule_hdr
))
709 + nla_total_size(IFNAMSIZ
) /* FRA_IIFNAME */
710 + nla_total_size(IFNAMSIZ
) /* FRA_OIFNAME */
711 + nla_total_size(4) /* FRA_PRIORITY */
712 + nla_total_size(4) /* FRA_TABLE */
713 + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */
714 + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */
715 + nla_total_size(4) /* FRA_FWMARK */
716 + nla_total_size(4) /* FRA_FWMASK */
717 + nla_total_size_64bit(8) /* FRA_TUN_ID */
718 + nla_total_size(sizeof(struct fib_kuid_range
));
720 if (ops
->nlmsg_payload
)
721 payload
+= ops
->nlmsg_payload(rule
);
726 static int fib_nl_fill_rule(struct sk_buff
*skb
, struct fib_rule
*rule
,
727 u32 pid
, u32 seq
, int type
, int flags
,
728 struct fib_rules_ops
*ops
)
730 struct nlmsghdr
*nlh
;
731 struct fib_rule_hdr
*frh
;
733 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*frh
), flags
);
737 frh
= nlmsg_data(nlh
);
738 frh
->family
= ops
->family
;
739 frh
->table
= rule
->table
;
740 if (nla_put_u32(skb
, FRA_TABLE
, rule
->table
))
741 goto nla_put_failure
;
742 if (nla_put_u32(skb
, FRA_SUPPRESS_PREFIXLEN
, rule
->suppress_prefixlen
))
743 goto nla_put_failure
;
746 frh
->action
= rule
->action
;
747 frh
->flags
= rule
->flags
;
749 if (rule
->action
== FR_ACT_GOTO
&&
750 rcu_access_pointer(rule
->ctarget
) == NULL
)
751 frh
->flags
|= FIB_RULE_UNRESOLVED
;
753 if (rule
->iifname
[0]) {
754 if (nla_put_string(skb
, FRA_IIFNAME
, rule
->iifname
))
755 goto nla_put_failure
;
756 if (rule
->iifindex
== -1)
757 frh
->flags
|= FIB_RULE_IIF_DETACHED
;
760 if (rule
->oifname
[0]) {
761 if (nla_put_string(skb
, FRA_OIFNAME
, rule
->oifname
))
762 goto nla_put_failure
;
763 if (rule
->oifindex
== -1)
764 frh
->flags
|= FIB_RULE_OIF_DETACHED
;
768 nla_put_u32(skb
, FRA_PRIORITY
, rule
->pref
)) ||
770 nla_put_u32(skb
, FRA_FWMARK
, rule
->mark
)) ||
771 ((rule
->mark_mask
|| rule
->mark
) &&
772 nla_put_u32(skb
, FRA_FWMASK
, rule
->mark_mask
)) ||
774 nla_put_u32(skb
, FRA_GOTO
, rule
->target
)) ||
776 nla_put_be64(skb
, FRA_TUN_ID
, rule
->tun_id
, FRA_PAD
)) ||
778 nla_put_u8(skb
, FRA_L3MDEV
, rule
->l3mdev
)) ||
779 (uid_range_set(&rule
->uid_range
) &&
780 nla_put_uid_range(skb
, &rule
->uid_range
)))
781 goto nla_put_failure
;
783 if (rule
->suppress_ifgroup
!= -1) {
784 if (nla_put_u32(skb
, FRA_SUPPRESS_IFGROUP
, rule
->suppress_ifgroup
))
785 goto nla_put_failure
;
788 if (ops
->fill(rule
, skb
, frh
) < 0)
789 goto nla_put_failure
;
795 nlmsg_cancel(skb
, nlh
);
799 static int dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
,
800 struct fib_rules_ops
*ops
)
803 struct fib_rule
*rule
;
807 list_for_each_entry_rcu(rule
, &ops
->rules_list
, list
) {
808 if (idx
< cb
->args
[1])
811 err
= fib_nl_fill_rule(skb
, rule
, NETLINK_CB(cb
->skb
).portid
,
812 cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
,
826 static int fib_nl_dumprule(struct sk_buff
*skb
, struct netlink_callback
*cb
)
828 struct net
*net
= sock_net(skb
->sk
);
829 struct fib_rules_ops
*ops
;
832 family
= rtnl_msg_family(cb
->nlh
);
833 if (family
!= AF_UNSPEC
) {
834 /* Protocol specific dump request */
835 ops
= lookup_rules_ops(net
, family
);
837 return -EAFNOSUPPORT
;
839 dump_rules(skb
, cb
, ops
);
845 list_for_each_entry_rcu(ops
, &net
->rules_ops
, list
) {
846 if (idx
< cb
->args
[0] || !try_module_get(ops
->owner
))
849 if (dump_rules(skb
, cb
, ops
) < 0)
862 static void notify_rule_change(int event
, struct fib_rule
*rule
,
863 struct fib_rules_ops
*ops
, struct nlmsghdr
*nlh
,
871 skb
= nlmsg_new(fib_rule_nlmsg_size(ops
, rule
), GFP_KERNEL
);
875 err
= fib_nl_fill_rule(skb
, rule
, pid
, nlh
->nlmsg_seq
, event
, 0, ops
);
877 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
878 WARN_ON(err
== -EMSGSIZE
);
883 rtnl_notify(skb
, net
, pid
, ops
->nlgroup
, nlh
, GFP_KERNEL
);
887 rtnl_set_sk_err(net
, ops
->nlgroup
, err
);
890 static void attach_rules(struct list_head
*rules
, struct net_device
*dev
)
892 struct fib_rule
*rule
;
894 list_for_each_entry(rule
, rules
, list
) {
895 if (rule
->iifindex
== -1 &&
896 strcmp(dev
->name
, rule
->iifname
) == 0)
897 rule
->iifindex
= dev
->ifindex
;
898 if (rule
->oifindex
== -1 &&
899 strcmp(dev
->name
, rule
->oifname
) == 0)
900 rule
->oifindex
= dev
->ifindex
;
904 static void detach_rules(struct list_head
*rules
, struct net_device
*dev
)
906 struct fib_rule
*rule
;
908 list_for_each_entry(rule
, rules
, list
) {
909 if (rule
->iifindex
== dev
->ifindex
)
911 if (rule
->oifindex
== dev
->ifindex
)
917 static int fib_rules_event(struct notifier_block
*this, unsigned long event
,
920 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
921 struct net
*net
= dev_net(dev
);
922 struct fib_rules_ops
*ops
;
927 case NETDEV_REGISTER
:
928 list_for_each_entry(ops
, &net
->rules_ops
, list
)
929 attach_rules(&ops
->rules_list
, dev
);
932 case NETDEV_CHANGENAME
:
933 list_for_each_entry(ops
, &net
->rules_ops
, list
) {
934 detach_rules(&ops
->rules_list
, dev
);
935 attach_rules(&ops
->rules_list
, dev
);
939 case NETDEV_UNREGISTER
:
940 list_for_each_entry(ops
, &net
->rules_ops
, list
)
941 detach_rules(&ops
->rules_list
, dev
);
948 static struct notifier_block fib_rules_notifier
= {
949 .notifier_call
= fib_rules_event
,
952 static int __net_init
fib_rules_net_init(struct net
*net
)
954 INIT_LIST_HEAD(&net
->rules_ops
);
955 spin_lock_init(&net
->rules_mod_lock
);
959 static struct pernet_operations fib_rules_net_ops
= {
960 .init
= fib_rules_net_init
,
963 static int __init
fib_rules_init(void)
966 rtnl_register(PF_UNSPEC
, RTM_NEWRULE
, fib_nl_newrule
, NULL
, NULL
);
967 rtnl_register(PF_UNSPEC
, RTM_DELRULE
, fib_nl_delrule
, NULL
, NULL
);
968 rtnl_register(PF_UNSPEC
, RTM_GETRULE
, NULL
, fib_nl_dumprule
, NULL
);
970 err
= register_pernet_subsys(&fib_rules_net_ops
);
974 err
= register_netdevice_notifier(&fib_rules_notifier
);
976 goto fail_unregister
;
981 unregister_pernet_subsys(&fib_rules_net_ops
);
983 rtnl_unregister(PF_UNSPEC
, RTM_NEWRULE
);
984 rtnl_unregister(PF_UNSPEC
, RTM_DELRULE
);
985 rtnl_unregister(PF_UNSPEC
, RTM_GETRULE
);
989 subsys_initcall(fib_rules_init
);