hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/net/core/fib_notifier.c
....@@ -5,17 +5,22 @@
55 #include <linux/module.h>
66 #include <linux/init.h>
77 #include <net/net_namespace.h>
8
+#include <net/netns/generic.h>
89 #include <net/fib_notifier.h>
910
10
-static ATOMIC_NOTIFIER_HEAD(fib_chain);
11
+static unsigned int fib_notifier_net_id;
1112
12
-int call_fib_notifier(struct notifier_block *nb, struct net *net,
13
+struct fib_notifier_net {
14
+ struct list_head fib_notifier_ops;
15
+ struct atomic_notifier_head fib_chain;
16
+};
17
+
18
+int call_fib_notifier(struct notifier_block *nb,
1319 enum fib_event_type event_type,
1420 struct fib_notifier_info *info)
1521 {
1622 int err;
1723
18
- info->net = net;
1924 err = nb->notifier_call(nb, event_type, info);
2025 return notifier_to_errno(err);
2126 }
....@@ -24,115 +29,113 @@
2429 int call_fib_notifiers(struct net *net, enum fib_event_type event_type,
2530 struct fib_notifier_info *info)
2631 {
32
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
2733 int err;
2834
29
- info->net = net;
30
- err = atomic_notifier_call_chain(&fib_chain, event_type, info);
35
+ err = atomic_notifier_call_chain(&fn_net->fib_chain, event_type, info);
3136 return notifier_to_errno(err);
3237 }
3338 EXPORT_SYMBOL(call_fib_notifiers);
3439
35
-static unsigned int fib_seq_sum(void)
40
+static unsigned int fib_seq_sum(struct net *net)
3641 {
42
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
3743 struct fib_notifier_ops *ops;
3844 unsigned int fib_seq = 0;
39
- struct net *net;
4045
4146 rtnl_lock();
42
- down_read(&net_rwsem);
43
- for_each_net(net) {
44
- rcu_read_lock();
45
- list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
46
- if (!try_module_get(ops->owner))
47
- continue;
48
- fib_seq += ops->fib_seq_read(net);
49
- module_put(ops->owner);
50
- }
51
- rcu_read_unlock();
47
+ rcu_read_lock();
48
+ list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) {
49
+ if (!try_module_get(ops->owner))
50
+ continue;
51
+ fib_seq += ops->fib_seq_read(net);
52
+ module_put(ops->owner);
5253 }
53
- up_read(&net_rwsem);
54
+ rcu_read_unlock();
5455 rtnl_unlock();
5556
5657 return fib_seq;
5758 }
5859
59
-static int fib_net_dump(struct net *net, struct notifier_block *nb)
60
+static int fib_net_dump(struct net *net, struct notifier_block *nb,
61
+ struct netlink_ext_ack *extack)
6062 {
63
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
6164 struct fib_notifier_ops *ops;
65
+ int err = 0;
6266
63
- list_for_each_entry_rcu(ops, &net->fib_notifier_ops, list) {
64
- int err;
65
-
67
+ rcu_read_lock();
68
+ list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) {
6669 if (!try_module_get(ops->owner))
6770 continue;
68
- err = ops->fib_dump(net, nb);
71
+ err = ops->fib_dump(net, nb, extack);
6972 module_put(ops->owner);
7073 if (err)
71
- return err;
74
+ goto unlock;
7275 }
7376
74
- return 0;
77
+unlock:
78
+ rcu_read_unlock();
79
+
80
+ return err;
7581 }
7682
77
-static bool fib_dump_is_consistent(struct notifier_block *nb,
83
+static bool fib_dump_is_consistent(struct net *net, struct notifier_block *nb,
7884 void (*cb)(struct notifier_block *nb),
7985 unsigned int fib_seq)
8086 {
81
- atomic_notifier_chain_register(&fib_chain, nb);
82
- if (fib_seq == fib_seq_sum())
87
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
88
+
89
+ atomic_notifier_chain_register(&fn_net->fib_chain, nb);
90
+ if (fib_seq == fib_seq_sum(net))
8391 return true;
84
- atomic_notifier_chain_unregister(&fib_chain, nb);
92
+ atomic_notifier_chain_unregister(&fn_net->fib_chain, nb);
8593 if (cb)
8694 cb(nb);
8795 return false;
8896 }
8997
9098 #define FIB_DUMP_MAX_RETRIES 5
91
-int register_fib_notifier(struct notifier_block *nb,
92
- void (*cb)(struct notifier_block *nb))
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)
93102 {
94103 int retries = 0;
95104 int err;
96105
97106 do {
98
- unsigned int fib_seq = fib_seq_sum();
99
- struct net *net;
107
+ unsigned int fib_seq = fib_seq_sum(net);
100108
101
- rcu_read_lock();
102
- for_each_net_rcu(net) {
103
- err = fib_net_dump(net, nb);
104
- if (err)
105
- goto err_fib_net_dump;
106
- }
107
- rcu_read_unlock();
109
+ err = fib_net_dump(net, nb, extack);
110
+ if (err)
111
+ return err;
108112
109
- if (fib_dump_is_consistent(nb, cb, fib_seq))
113
+ if (fib_dump_is_consistent(net, nb, cb, fib_seq))
110114 return 0;
111115 } while (++retries < FIB_DUMP_MAX_RETRIES);
112116
113117 return -EBUSY;
114
-
115
-err_fib_net_dump:
116
- rcu_read_unlock();
117
- return err;
118118 }
119119 EXPORT_SYMBOL(register_fib_notifier);
120120
121
-int unregister_fib_notifier(struct notifier_block *nb)
121
+int unregister_fib_notifier(struct net *net, struct notifier_block *nb)
122122 {
123
- return atomic_notifier_chain_unregister(&fib_chain, nb);
123
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
124
+
125
+ return atomic_notifier_chain_unregister(&fn_net->fib_chain, nb);
124126 }
125127 EXPORT_SYMBOL(unregister_fib_notifier);
126128
127129 static int __fib_notifier_ops_register(struct fib_notifier_ops *ops,
128130 struct net *net)
129131 {
132
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
130133 struct fib_notifier_ops *o;
131134
132
- list_for_each_entry(o, &net->fib_notifier_ops, list)
135
+ list_for_each_entry(o, &fn_net->fib_notifier_ops, list)
133136 if (ops->family == o->family)
134137 return -EEXIST;
135
- list_add_tail_rcu(&ops->list, &net->fib_notifier_ops);
138
+ list_add_tail_rcu(&ops->list, &fn_net->fib_notifier_ops);
136139 return 0;
137140 }
138141
....@@ -167,18 +170,25 @@
167170
168171 static int __net_init fib_notifier_net_init(struct net *net)
169172 {
170
- INIT_LIST_HEAD(&net->fib_notifier_ops);
173
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
174
+
175
+ INIT_LIST_HEAD(&fn_net->fib_notifier_ops);
176
+ ATOMIC_INIT_NOTIFIER_HEAD(&fn_net->fib_chain);
171177 return 0;
172178 }
173179
174180 static void __net_exit fib_notifier_net_exit(struct net *net)
175181 {
176
- WARN_ON_ONCE(!list_empty(&net->fib_notifier_ops));
182
+ struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id);
183
+
184
+ WARN_ON_ONCE(!list_empty(&fn_net->fib_notifier_ops));
177185 }
178186
179187 static struct pernet_operations fib_notifier_net_ops = {
180188 .init = fib_notifier_net_init,
181189 .exit = fib_notifier_net_exit,
190
+ .id = &fib_notifier_net_id,
191
+ .size = sizeof(struct fib_notifier_net),
182192 };
183193
184194 static int __init fib_notifier_init(void)