| .. | .. |
|---|
| 5 | 5 | #include <linux/module.h> |
|---|
| 6 | 6 | #include <linux/init.h> |
|---|
| 7 | 7 | #include <net/net_namespace.h> |
|---|
| 8 | +#include <net/netns/generic.h> |
|---|
| 8 | 9 | #include <net/fib_notifier.h> |
|---|
| 9 | 10 | |
|---|
| 10 | | -static ATOMIC_NOTIFIER_HEAD(fib_chain); |
|---|
| 11 | +static unsigned int fib_notifier_net_id; |
|---|
| 11 | 12 | |
|---|
| 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, |
|---|
| 13 | 19 | enum fib_event_type event_type, |
|---|
| 14 | 20 | struct fib_notifier_info *info) |
|---|
| 15 | 21 | { |
|---|
| 16 | 22 | int err; |
|---|
| 17 | 23 | |
|---|
| 18 | | - info->net = net; |
|---|
| 19 | 24 | err = nb->notifier_call(nb, event_type, info); |
|---|
| 20 | 25 | return notifier_to_errno(err); |
|---|
| 21 | 26 | } |
|---|
| .. | .. |
|---|
| 24 | 29 | int call_fib_notifiers(struct net *net, enum fib_event_type event_type, |
|---|
| 25 | 30 | struct fib_notifier_info *info) |
|---|
| 26 | 31 | { |
|---|
| 32 | + struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); |
|---|
| 27 | 33 | int err; |
|---|
| 28 | 34 | |
|---|
| 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); |
|---|
| 31 | 36 | return notifier_to_errno(err); |
|---|
| 32 | 37 | } |
|---|
| 33 | 38 | EXPORT_SYMBOL(call_fib_notifiers); |
|---|
| 34 | 39 | |
|---|
| 35 | | -static unsigned int fib_seq_sum(void) |
|---|
| 40 | +static unsigned int fib_seq_sum(struct net *net) |
|---|
| 36 | 41 | { |
|---|
| 42 | + struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); |
|---|
| 37 | 43 | struct fib_notifier_ops *ops; |
|---|
| 38 | 44 | unsigned int fib_seq = 0; |
|---|
| 39 | | - struct net *net; |
|---|
| 40 | 45 | |
|---|
| 41 | 46 | 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); |
|---|
| 52 | 53 | } |
|---|
| 53 | | - up_read(&net_rwsem); |
|---|
| 54 | + rcu_read_unlock(); |
|---|
| 54 | 55 | rtnl_unlock(); |
|---|
| 55 | 56 | |
|---|
| 56 | 57 | return fib_seq; |
|---|
| 57 | 58 | } |
|---|
| 58 | 59 | |
|---|
| 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) |
|---|
| 60 | 62 | { |
|---|
| 63 | + struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); |
|---|
| 61 | 64 | struct fib_notifier_ops *ops; |
|---|
| 65 | + int err = 0; |
|---|
| 62 | 66 | |
|---|
| 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) { |
|---|
| 66 | 69 | if (!try_module_get(ops->owner)) |
|---|
| 67 | 70 | continue; |
|---|
| 68 | | - err = ops->fib_dump(net, nb); |
|---|
| 71 | + err = ops->fib_dump(net, nb, extack); |
|---|
| 69 | 72 | module_put(ops->owner); |
|---|
| 70 | 73 | if (err) |
|---|
| 71 | | - return err; |
|---|
| 74 | + goto unlock; |
|---|
| 72 | 75 | } |
|---|
| 73 | 76 | |
|---|
| 74 | | - return 0; |
|---|
| 77 | +unlock: |
|---|
| 78 | + rcu_read_unlock(); |
|---|
| 79 | + |
|---|
| 80 | + return err; |
|---|
| 75 | 81 | } |
|---|
| 76 | 82 | |
|---|
| 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, |
|---|
| 78 | 84 | void (*cb)(struct notifier_block *nb), |
|---|
| 79 | 85 | unsigned int fib_seq) |
|---|
| 80 | 86 | { |
|---|
| 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)) |
|---|
| 83 | 91 | return true; |
|---|
| 84 | | - atomic_notifier_chain_unregister(&fib_chain, nb); |
|---|
| 92 | + atomic_notifier_chain_unregister(&fn_net->fib_chain, nb); |
|---|
| 85 | 93 | if (cb) |
|---|
| 86 | 94 | cb(nb); |
|---|
| 87 | 95 | return false; |
|---|
| 88 | 96 | } |
|---|
| 89 | 97 | |
|---|
| 90 | 98 | #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) |
|---|
| 93 | 102 | { |
|---|
| 94 | 103 | int retries = 0; |
|---|
| 95 | 104 | int err; |
|---|
| 96 | 105 | |
|---|
| 97 | 106 | do { |
|---|
| 98 | | - unsigned int fib_seq = fib_seq_sum(); |
|---|
| 99 | | - struct net *net; |
|---|
| 107 | + unsigned int fib_seq = fib_seq_sum(net); |
|---|
| 100 | 108 | |
|---|
| 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; |
|---|
| 108 | 112 | |
|---|
| 109 | | - if (fib_dump_is_consistent(nb, cb, fib_seq)) |
|---|
| 113 | + if (fib_dump_is_consistent(net, nb, cb, fib_seq)) |
|---|
| 110 | 114 | return 0; |
|---|
| 111 | 115 | } while (++retries < FIB_DUMP_MAX_RETRIES); |
|---|
| 112 | 116 | |
|---|
| 113 | 117 | return -EBUSY; |
|---|
| 114 | | - |
|---|
| 115 | | -err_fib_net_dump: |
|---|
| 116 | | - rcu_read_unlock(); |
|---|
| 117 | | - return err; |
|---|
| 118 | 118 | } |
|---|
| 119 | 119 | EXPORT_SYMBOL(register_fib_notifier); |
|---|
| 120 | 120 | |
|---|
| 121 | | -int unregister_fib_notifier(struct notifier_block *nb) |
|---|
| 121 | +int unregister_fib_notifier(struct net *net, struct notifier_block *nb) |
|---|
| 122 | 122 | { |
|---|
| 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); |
|---|
| 124 | 126 | } |
|---|
| 125 | 127 | EXPORT_SYMBOL(unregister_fib_notifier); |
|---|
| 126 | 128 | |
|---|
| 127 | 129 | static int __fib_notifier_ops_register(struct fib_notifier_ops *ops, |
|---|
| 128 | 130 | struct net *net) |
|---|
| 129 | 131 | { |
|---|
| 132 | + struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); |
|---|
| 130 | 133 | struct fib_notifier_ops *o; |
|---|
| 131 | 134 | |
|---|
| 132 | | - list_for_each_entry(o, &net->fib_notifier_ops, list) |
|---|
| 135 | + list_for_each_entry(o, &fn_net->fib_notifier_ops, list) |
|---|
| 133 | 136 | if (ops->family == o->family) |
|---|
| 134 | 137 | 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); |
|---|
| 136 | 139 | return 0; |
|---|
| 137 | 140 | } |
|---|
| 138 | 141 | |
|---|
| .. | .. |
|---|
| 167 | 170 | |
|---|
| 168 | 171 | static int __net_init fib_notifier_net_init(struct net *net) |
|---|
| 169 | 172 | { |
|---|
| 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); |
|---|
| 171 | 177 | return 0; |
|---|
| 172 | 178 | } |
|---|
| 173 | 179 | |
|---|
| 174 | 180 | static void __net_exit fib_notifier_net_exit(struct net *net) |
|---|
| 175 | 181 | { |
|---|
| 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)); |
|---|
| 177 | 185 | } |
|---|
| 178 | 186 | |
|---|
| 179 | 187 | static struct pernet_operations fib_notifier_net_ops = { |
|---|
| 180 | 188 | .init = fib_notifier_net_init, |
|---|
| 181 | 189 | .exit = fib_notifier_net_exit, |
|---|
| 190 | + .id = &fib_notifier_net_id, |
|---|
| 191 | + .size = sizeof(struct fib_notifier_net), |
|---|
| 182 | 192 | }; |
|---|
| 183 | 193 | |
|---|
| 184 | 194 | static int __init fib_notifier_init(void) |
|---|