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
)
17 return nb
->notifier_call(nb
, event_type
, info
);
19 EXPORT_SYMBOL(call_fib_notifier
);
21 int call_fib_notifiers(struct net
*net
, enum fib_event_type event_type
,
22 struct fib_notifier_info
*info
)
25 return atomic_notifier_call_chain(&fib_chain
, event_type
, info
);
27 EXPORT_SYMBOL(call_fib_notifiers
);
29 static unsigned int fib_seq_sum(void)
31 struct fib_notifier_ops
*ops
;
32 unsigned int fib_seq
= 0;
38 list_for_each_entry_rcu(ops
, &net
->fib_notifier_ops
, list
) {
39 if (!try_module_get(ops
->owner
))
41 fib_seq
+= ops
->fib_seq_read(net
);
42 module_put(ops
->owner
);
51 static int fib_net_dump(struct net
*net
, struct notifier_block
*nb
)
53 struct fib_notifier_ops
*ops
;
55 list_for_each_entry_rcu(ops
, &net
->fib_notifier_ops
, list
) {
58 if (!try_module_get(ops
->owner
))
60 err
= ops
->fib_dump(net
, nb
);
61 module_put(ops
->owner
);
69 static bool fib_dump_is_consistent(struct notifier_block
*nb
,
70 void (*cb
)(struct notifier_block
*nb
),
73 atomic_notifier_chain_register(&fib_chain
, nb
);
74 if (fib_seq
== fib_seq_sum())
76 atomic_notifier_chain_unregister(&fib_chain
, nb
);
82 #define FIB_DUMP_MAX_RETRIES 5
83 int register_fib_notifier(struct notifier_block
*nb
,
84 void (*cb
)(struct notifier_block
*nb
))
90 unsigned int fib_seq
= fib_seq_sum();
94 for_each_net_rcu(net
) {
95 err
= fib_net_dump(net
, nb
);
97 goto err_fib_net_dump
;
101 if (fib_dump_is_consistent(nb
, cb
, fib_seq
))
103 } while (++retries
< FIB_DUMP_MAX_RETRIES
);
111 EXPORT_SYMBOL(register_fib_notifier
);
113 int unregister_fib_notifier(struct notifier_block
*nb
)
115 return atomic_notifier_chain_unregister(&fib_chain
, nb
);
117 EXPORT_SYMBOL(unregister_fib_notifier
);
119 static int __fib_notifier_ops_register(struct fib_notifier_ops
*ops
,
122 struct fib_notifier_ops
*o
;
124 list_for_each_entry(o
, &net
->fib_notifier_ops
, list
)
125 if (ops
->family
== o
->family
)
127 list_add_tail_rcu(&ops
->list
, &net
->fib_notifier_ops
);
131 struct fib_notifier_ops
*
132 fib_notifier_ops_register(const struct fib_notifier_ops
*tmpl
, struct net
*net
)
134 struct fib_notifier_ops
*ops
;
137 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
139 return ERR_PTR(-ENOMEM
);
141 err
= __fib_notifier_ops_register(ops
, net
);
151 EXPORT_SYMBOL(fib_notifier_ops_register
);
153 void fib_notifier_ops_unregister(struct fib_notifier_ops
*ops
)
155 list_del_rcu(&ops
->list
);
158 EXPORT_SYMBOL(fib_notifier_ops_unregister
);
160 static int __net_init
fib_notifier_net_init(struct net
*net
)
162 INIT_LIST_HEAD(&net
->fib_notifier_ops
);
166 static void __net_exit
fib_notifier_net_exit(struct net
*net
)
168 WARN_ON_ONCE(!list_empty(&net
->fib_notifier_ops
));
171 static struct pernet_operations fib_notifier_net_ops
= {
172 .init
= fib_notifier_net_init
,
173 .exit
= fib_notifier_net_exit
,
176 static int __init
fib_notifier_init(void)
178 return register_pernet_subsys(&fib_notifier_net_ops
);
181 subsys_initcall(fib_notifier_init
);