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
;
17 static ATOMIC_NOTIFIER_HEAD(fib_chain
);
19 int call_fib_notifier(struct notifier_block
*nb
, struct net
*net
,
20 enum fib_event_type event_type
,
21 struct fib_notifier_info
*info
)
26 err
= nb
->notifier_call(nb
, event_type
, info
);
27 return notifier_to_errno(err
);
29 EXPORT_SYMBOL(call_fib_notifier
);
31 int call_fib_notifiers(struct net
*net
, enum fib_event_type event_type
,
32 struct fib_notifier_info
*info
)
37 err
= atomic_notifier_call_chain(&fib_chain
, event_type
, info
);
38 return notifier_to_errno(err
);
40 EXPORT_SYMBOL(call_fib_notifiers
);
42 static unsigned int fib_seq_sum(void)
44 struct fib_notifier_net
*fn_net
;
45 struct fib_notifier_ops
*ops
;
46 unsigned int fib_seq
= 0;
50 down_read(&net_rwsem
);
52 fn_net
= net_generic(net
, fib_notifier_net_id
);
54 list_for_each_entry_rcu(ops
, &fn_net
->fib_notifier_ops
, list
) {
55 if (!try_module_get(ops
->owner
))
57 fib_seq
+= ops
->fib_seq_read(net
);
58 module_put(ops
->owner
);
68 static int fib_net_dump(struct net
*net
, struct notifier_block
*nb
)
70 struct fib_notifier_net
*fn_net
= net_generic(net
, fib_notifier_net_id
);
71 struct fib_notifier_ops
*ops
;
73 list_for_each_entry_rcu(ops
, &fn_net
->fib_notifier_ops
, list
) {
76 if (!try_module_get(ops
->owner
))
78 err
= ops
->fib_dump(net
, nb
);
79 module_put(ops
->owner
);
87 static bool fib_dump_is_consistent(struct notifier_block
*nb
,
88 void (*cb
)(struct notifier_block
*nb
),
91 atomic_notifier_chain_register(&fib_chain
, nb
);
92 if (fib_seq
== fib_seq_sum())
94 atomic_notifier_chain_unregister(&fib_chain
, nb
);
100 #define FIB_DUMP_MAX_RETRIES 5
101 int register_fib_notifier(struct notifier_block
*nb
,
102 void (*cb
)(struct notifier_block
*nb
))
108 unsigned int fib_seq
= fib_seq_sum();
112 for_each_net_rcu(net
) {
113 err
= fib_net_dump(net
, nb
);
115 goto err_fib_net_dump
;
119 if (fib_dump_is_consistent(nb
, cb
, fib_seq
))
121 } while (++retries
< FIB_DUMP_MAX_RETRIES
);
129 EXPORT_SYMBOL(register_fib_notifier
);
131 int unregister_fib_notifier(struct notifier_block
*nb
)
133 return atomic_notifier_chain_unregister(&fib_chain
, nb
);
135 EXPORT_SYMBOL(unregister_fib_notifier
);
137 static int __fib_notifier_ops_register(struct fib_notifier_ops
*ops
,
140 struct fib_notifier_net
*fn_net
= net_generic(net
, fib_notifier_net_id
);
141 struct fib_notifier_ops
*o
;
143 list_for_each_entry(o
, &fn_net
->fib_notifier_ops
, list
)
144 if (ops
->family
== o
->family
)
146 list_add_tail_rcu(&ops
->list
, &fn_net
->fib_notifier_ops
);
150 struct fib_notifier_ops
*
151 fib_notifier_ops_register(const struct fib_notifier_ops
*tmpl
, struct net
*net
)
153 struct fib_notifier_ops
*ops
;
156 ops
= kmemdup(tmpl
, sizeof(*ops
), GFP_KERNEL
);
158 return ERR_PTR(-ENOMEM
);
160 err
= __fib_notifier_ops_register(ops
, net
);
170 EXPORT_SYMBOL(fib_notifier_ops_register
);
172 void fib_notifier_ops_unregister(struct fib_notifier_ops
*ops
)
174 list_del_rcu(&ops
->list
);
177 EXPORT_SYMBOL(fib_notifier_ops_unregister
);
179 static int __net_init
fib_notifier_net_init(struct net
*net
)
181 struct fib_notifier_net
*fn_net
= net_generic(net
, fib_notifier_net_id
);
183 INIT_LIST_HEAD(&fn_net
->fib_notifier_ops
);
187 static void __net_exit
fib_notifier_net_exit(struct net
*net
)
189 struct fib_notifier_net
*fn_net
= net_generic(net
, fib_notifier_net_id
);
191 WARN_ON_ONCE(!list_empty(&fn_net
->fib_notifier_ops
));
194 static struct pernet_operations fib_notifier_net_ops
= {
195 .init
= fib_notifier_net_init
,
196 .exit
= fib_notifier_net_exit
,
197 .id
= &fib_notifier_net_id
,
198 .size
= sizeof(struct fib_notifier_net
),
201 static int __init
fib_notifier_init(void)
203 return register_pernet_subsys(&fib_notifier_net_ops
);
206 subsys_initcall(fib_notifier_init
);