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> 10 11 static unsigned int fib_notifier_net_id; 12 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, 19 enum fib_event_type event_type, 20 struct fib_notifier_info *info) 21 { 22 int err; 23 24 err = nb->notifier_call(nb, event_type, info); 25 return notifier_to_errno(err); 26 } 27 EXPORT_SYMBOL(call_fib_notifier); 28 29 int call_fib_notifiers(struct net *net, enum fib_event_type event_type, 30 struct fib_notifier_info *info) 31 { 32 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 33 int err; 34 35 err = atomic_notifier_call_chain(&fn_net->fib_chain, event_type, info); 36 return notifier_to_errno(err); 37 } 38 EXPORT_SYMBOL(call_fib_notifiers); 39 40 static unsigned int fib_seq_sum(struct net *net) 41 { 42 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 43 struct fib_notifier_ops *ops; 44 unsigned int fib_seq = 0; 45 46 rcu_read_lock(); 47 list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) { 48 if (!try_module_get(ops->owner)) 49 continue; 50 fib_seq += ops->fib_seq_read(net); 51 module_put(ops->owner); 52 } 53 rcu_read_unlock(); 54 55 return fib_seq; 56 } 57 58 static int fib_net_dump(struct net *net, struct notifier_block *nb, 59 struct netlink_ext_ack *extack) 60 { 61 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 62 struct fib_notifier_ops *ops; 63 int err = 0; 64 65 rcu_read_lock(); 66 list_for_each_entry_rcu(ops, &fn_net->fib_notifier_ops, list) { 67 if (!try_module_get(ops->owner)) 68 continue; 69 err = ops->fib_dump(net, nb, extack); 70 module_put(ops->owner); 71 if (err) 72 goto unlock; 73 } 74 75 unlock: 76 rcu_read_unlock(); 77 78 return err; 79 } 80 81 static bool fib_dump_is_consistent(struct net *net, struct notifier_block *nb, 82 void (*cb)(struct notifier_block *nb), 83 unsigned int fib_seq) 84 { 85 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 86 87 atomic_notifier_chain_register(&fn_net->fib_chain, nb); 88 if (fib_seq == fib_seq_sum(net)) 89 return true; 90 atomic_notifier_chain_unregister(&fn_net->fib_chain, nb); 91 if (cb) 92 cb(nb); 93 return false; 94 } 95 96 #define FIB_DUMP_MAX_RETRIES 5 97 int register_fib_notifier(struct net *net, struct notifier_block *nb, 98 void (*cb)(struct notifier_block *nb), 99 struct netlink_ext_ack *extack) 100 { 101 int retries = 0; 102 int err; 103 104 do { 105 unsigned int fib_seq = fib_seq_sum(net); 106 107 err = fib_net_dump(net, nb, extack); 108 if (err) 109 return err; 110 111 if (fib_dump_is_consistent(net, nb, cb, fib_seq)) 112 return 0; 113 } while (++retries < FIB_DUMP_MAX_RETRIES); 114 115 return -EBUSY; 116 } 117 EXPORT_SYMBOL(register_fib_notifier); 118 119 int unregister_fib_notifier(struct net *net, struct notifier_block *nb) 120 { 121 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 122 123 return atomic_notifier_chain_unregister(&fn_net->fib_chain, nb); 124 } 125 EXPORT_SYMBOL(unregister_fib_notifier); 126 127 static int __fib_notifier_ops_register(struct fib_notifier_ops *ops, 128 struct net *net) 129 { 130 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 131 struct fib_notifier_ops *o; 132 133 list_for_each_entry(o, &fn_net->fib_notifier_ops, list) 134 if (ops->family == o->family) 135 return -EEXIST; 136 list_add_tail_rcu(&ops->list, &fn_net->fib_notifier_ops); 137 return 0; 138 } 139 140 struct fib_notifier_ops * 141 fib_notifier_ops_register(const struct fib_notifier_ops *tmpl, struct net *net) 142 { 143 struct fib_notifier_ops *ops; 144 int err; 145 146 ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL); 147 if (!ops) 148 return ERR_PTR(-ENOMEM); 149 150 err = __fib_notifier_ops_register(ops, net); 151 if (err) 152 goto err_register; 153 154 return ops; 155 156 err_register: 157 kfree(ops); 158 return ERR_PTR(err); 159 } 160 EXPORT_SYMBOL(fib_notifier_ops_register); 161 162 void fib_notifier_ops_unregister(struct fib_notifier_ops *ops) 163 { 164 list_del_rcu(&ops->list); 165 kfree_rcu(ops, rcu); 166 } 167 EXPORT_SYMBOL(fib_notifier_ops_unregister); 168 169 static int __net_init fib_notifier_net_init(struct net *net) 170 { 171 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 172 173 INIT_LIST_HEAD(&fn_net->fib_notifier_ops); 174 ATOMIC_INIT_NOTIFIER_HEAD(&fn_net->fib_chain); 175 return 0; 176 } 177 178 static void __net_exit fib_notifier_net_exit(struct net *net) 179 { 180 struct fib_notifier_net *fn_net = net_generic(net, fib_notifier_net_id); 181 182 WARN_ON_ONCE(!list_empty(&fn_net->fib_notifier_ops)); 183 } 184 185 static struct pernet_operations fib_notifier_net_ops = { 186 .init = fib_notifier_net_init, 187 .exit = fib_notifier_net_exit, 188 .id = &fib_notifier_net_id, 189 .size = sizeof(struct fib_notifier_net), 190 }; 191 192 static int __init fib_notifier_init(void) 193 { 194 return register_pernet_subsys(&fib_notifier_net_ops); 195 } 196 197 subsys_initcall(fib_notifier_init); 198