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/netns/generic.h>
9 #include <net/fib_notifier.h>
11 static unsigned int fib_notifier_net_id;
13 struct fib_notifier_net {
14 struct list_head fib_notifier_ops;
15 struct atomic_notifier_head fib_chain;
18 int call_fib_notifier(struct notifier_block *nb,
19 enum fib_event_type event_type,
20 struct fib_notifier_info *info)
24 err = nb->notifier_call(nb, event_type, info);
25 return notifier_to_errno(err);
27 EXPORT_SYMBOL(call_fib_notifier);
29 int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
30 struct fib_notifier_info *info)
32 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
35 err = atomic_notifier_call_chain(&fn_net->fib_chain, event_type, info);
36 return notifier_to_errno(err);
38 EXPORT_SYMBOL(call_fib_notifiers);
40 static unsigned int fib_seq_sum(struct net *net)
42 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
43 struct fib_notifier_ops *ops;
44 unsigned int fib_seq = 0;
48 list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) {
49 if (!try_module_get(ops->owner))
51 fib_seq += ops->fib_seq_read(net);
52 module_put(ops->owner);
60 static int fib_net_dump(struct net *net, struct notifier_block *nb,
61 struct netlink_ext_ack *extack)
63 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
64 struct fib_notifier_ops *ops;
68 list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) {
69 if (!try_module_get(ops->owner))
71 err = ops->fib_dump(net, nb, extack);
72 module_put(ops->owner);
83 static bool fib_dump_is_consistent(struct net *net, struct notifier_block *nb,
84 void (*cb)(struct notifier_block *nb),
87 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
89 atomic_notifier_chain_register(&fn_net->fib_chain, nb);
90 if (fib_seq == fib_seq_sum(net))
92 atomic_notifier_chain_unregister(&fn_net->fib_chain, nb);
98 #define FIB_DUMP_MAX_RETRIES 5
99 int register_fib_notifier(struct net *net, struct notifier_block *nb,
100 void (*cb)(struct notifier_block *nb),
101 struct netlink_ext_ack *extack)
107 unsigned int fib_seq = fib_seq_sum(net);
109 err = fib_net_dump(net, nb, extack);
113 if (fib_dump_is_consistent(net, nb, cb, fib_seq))
115 } while (++retries < FIB_DUMP_MAX_RETRIES);
119 EXPORT_SYMBOL(register_fib_notifier);
121 int unregister_fib_notifier(struct net *net, struct notifier_block *nb)
123 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
125 return atomic_notifier_chain_unregister(&fn_net->fib_chain, nb);
127 EXPORT_SYMBOL(unregister_fib_notifier);
129 static int __fib_notifier_ops_register(struct fib_notifier_ops *ops,
132 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
133 struct fib_notifier_ops *o;
135 list_for_each_entry(o, &fn_net->fib_notifier_ops, list)
136 if (ops->family == o->family)
138 list_add_tail_rcu(&ops->list, &fn_net->fib_notifier_ops);
142 struct fib_notifier_ops *
143 fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net)
145 struct fib_notifier_ops *ops;
148 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL);
150 return ERR_PTR(-ENOMEM);
152 err = __fib_notifier_ops_register(ops, net);
162 EXPORT_SYMBOL(fib_notifier_ops_register);
164 void fib_notifier_ops_unregister(struct fib_notifier_ops *ops)
166 list_del_rcu(&ops->list);
169 EXPORT_SYMBOL(fib_notifier_ops_unregister);
171 static int __net_init fib_notifier_net_init(struct net *net)
173 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
175 INIT_LIST_HEAD(&fn_net->fib_notifier_ops);
176 ATOMIC_INIT_NOTIFIER_HEAD(&fn_net->fib_chain);
180 static void __net_exit fib_notifier_net_exit(struct net *net)
182 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
184 WARN_ON_ONCE(!list_empty(&fn_net->fib_notifier_ops));
187 static struct pernet_operations fib_notifier_net_ops = {
188 .init = fib_notifier_net_init,
189 .exit = fib_notifier_net_exit,
190 .id = &fib_notifier_net_id,
191 .size = sizeof(struct fib_notifier_net),
194 static int __init fib_notifier_init(void)
196 return register_pernet_subsys(&fib_notifier_net_ops);
199 subsys_initcall(fib_notifier_init);