1 #include <linux/rtnetlink.h>
2 #include <linux/notifier.h>
3 #include <linux/rcupdate.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <net/net_namespace.h>
8 #include <net/fib_notifier.h>
10 static ATOMIC_NOTIFIER_HEAD(fib_chain);
12 int call_fib_notifier(struct notifier_block *nb, struct net *net,
13 enum fib_event_type event_type,
14 struct fib_notifier_info *info)
19 err = nb->notifier_call(nb, event_type, info);
20 return notifier_to_errno(err);
22 EXPORT_SYMBOL(call_fib_notifier);
24 int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
25 struct fib_notifier_info *info)
30 err = atomic_notifier_call_chain(&fib_chain, event_type, info);
31 return notifier_to_errno(err);
33 EXPORT_SYMBOL(call_fib_notifiers);
35 static unsigned int fib_seq_sum(void)
37 struct fib_notifier_ops *ops;
38 unsigned int fib_seq = 0;
42 down_read(&net_rwsem);
45 list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
46 if (!try_module_get(ops->owner))
48 fib_seq += ops->fib_seq_read(net);
49 module_put(ops->owner);
59 static int fib_net_dump(struct net *net, struct notifier_block *nb)
61 struct fib_notifier_ops *ops;
63 list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
66 if (!try_module_get(ops->owner))
68 err = ops->fib_dump(net, nb);
69 module_put(ops->owner);
77 static bool fib_dump_is_consistent(struct notifier_block *nb,
78 void (*cb)(struct notifier_block *nb),
81 atomic_notifier_chain_register(&fib_chain, nb);
82 if (fib_seq == fib_seq_sum())
84 atomic_notifier_chain_unregister(&fib_chain, nb);
90 #define FIB_DUMP_MAX_RETRIES 5
91 int register_fib_notifier(struct notifier_block *nb,
92 void (*cb)(struct notifier_block *nb))
98 unsigned int fib_seq = fib_seq_sum();
102 for_each_net_rcu(net) {
103 err = fib_net_dump(net, nb);
105 goto err_fib_net_dump;
109 if (fib_dump_is_consistent(nb, cb, fib_seq))
111 } while (++retries < FIB_DUMP_MAX_RETRIES);
119 EXPORT_SYMBOL(register_fib_notifier);
121 int unregister_fib_notifier(struct notifier_block *nb)
123 return atomic_notifier_chain_unregister(&fib_chain, nb);
125 EXPORT_SYMBOL(unregister_fib_notifier);
127 static int __fib_notifier_ops_register(struct fib_notifier_ops *ops,
130 struct fib_notifier_ops *o;
132 list_for_each_entry(o, &net->fib_notifier_ops, list)
133 if (ops->family == o->family)
135 list_add_tail_rcu(&ops->list, &net->fib_notifier_ops);
139 struct fib_notifier_ops *
140 fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net)
142 struct fib_notifier_ops *ops;
145 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
149 err = __fib_notifier_ops_register(ops, net);
159 EXPORT_SYMBOL(fib_notifier_ops_register);
161 void fib_notifier_ops_unregister(struct fib_notifier_ops *ops)
163 list_del_rcu(&ops->list);
166 EXPORT_SYMBOL(fib_notifier_ops_unregister);
168 static int __net_init fib_notifier_net_init(struct net *net)
170 INIT_LIST_HEAD(&net->fib_notifier_ops);
174 static void __net_exit fib_notifier_net_exit(struct net *net)
176 WARN_ON_ONCE(!list_empty(&net->fib_notifier_ops));
179 static struct pernet_operations fib_notifier_net_ops = {
180 .init = fib_notifier_net_init,
181 .exit = fib_notifier_net_exit,
184 static int __init fib_notifier_init(void)
186 return register_pernet_subsys(&fib_notifier_net_ops);
189 subsys_initcall(fib_notifier_init);