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;
37 list_for_each_entry(ops
, &net
->fib_notifier_ops
, list
) {
38 if (!try_module_get(ops
->owner
))
40 fib_seq
+= ops
->fib_seq_read(net
);
41 module_put(ops
->owner
);
49 static int fib_net_dump(struct net
*net
, struct notifier_block
*nb
)
51 struct fib_notifier_ops
*ops
;
53 list_for_each_entry_rcu(ops
, &net
->fib_notifier_ops
, list
) {
56 if (!try_module_get(ops
->owner
))
58 err
= ops
->fib_dump(net
, nb
);
59 module_put(ops
->owner
);
67 static bool fib_dump_is_consistent(struct notifier_block
*nb
,
68 void (*cb
)(struct notifier_block
*nb
),
71 atomic_notifier_chain_register(&fib_chain
, nb
);
72 if (fib_seq
== fib_seq_sum())
74 atomic_notifier_chain_unregister(&fib_chain
, nb
);
80 #define FIB_DUMP_MAX_RETRIES 5
81 int register_fib_notifier(struct notifier_block
*nb
,
82 void (*cb
)(struct notifier_block
*nb
))
88 unsigned int fib_seq
= fib_seq_sum();
92 for_each_net_rcu(net
) {
93 err
= fib_net_dump(net
, nb
);
95 goto err_fib_net_dump
;
99 if (fib_dump_is_consistent(nb
, cb
, fib_seq
))
101 } while (++retries
< FIB_DUMP_MAX_RETRIES
);
109 EXPORT_SYMBOL(register_fib_notifier
);
111 int unregister_fib_notifier(struct notifier_block
*nb
)
113 return atomic_notifier_chain_unregister(&fib_chain
, nb
);
115 EXPORT_SYMBOL(unregister_fib_notifier
);
117 static int __fib_notifier_ops_register(struct fib_notifier_ops
*ops
,
120 struct fib_notifier_ops
*o
;
122 list_for_each_entry(o
, &net
->fib_notifier_ops
, list
)
123 if (ops
->family
== o
->family
)
125 list_add_tail_rcu(&ops
->list
, &net
->fib_notifier_ops
);
129 struct fib_notifier_ops
*
130 fib_notifier_ops_register(const struct fib_notifier_ops
*tmpl
, struct net
*net
)
132 struct fib_notifier_ops
*ops
;
135 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
137 return ERR_PTR(-ENOMEM
);
139 err
= __fib_notifier_ops_register(ops
, net
);
149 EXPORT_SYMBOL(fib_notifier_ops_register
);
151 void fib_notifier_ops_unregister(struct fib_notifier_ops
*ops
)
153 list_del_rcu(&ops
->list
);
156 EXPORT_SYMBOL(fib_notifier_ops_unregister
);
158 static int __net_init
fib_notifier_net_init(struct net
*net
)
160 INIT_LIST_HEAD(&net
->fib_notifier_ops
);
164 static struct pernet_operations fib_notifier_net_ops
= {
165 .init
= fib_notifier_net_init
,
168 static int __init
fib_notifier_init(void)
170 return register_pernet_subsys(&fib_notifier_net_ops
);
173 subsys_initcall(fib_notifier_init
);