Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 1 | #include <linux/workqueue.h> |
| 2 | #include <linux/rtnetlink.h> |
| 3 | #include <linux/cache.h> |
| 4 | #include <linux/slab.h> |
| 5 | #include <linux/list.h> |
| 6 | #include <linux/delay.h> |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 7 | #include <linux/sched.h> |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 8 | #include <linux/idr.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 9 | #include <net/net_namespace.h> |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 10 | #include <net/netns/generic.h> |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Our network namespace constructor/destructor lists |
| 14 | */ |
| 15 | |
| 16 | static LIST_HEAD(pernet_list); |
| 17 | static struct list_head *first_device = &pernet_list; |
| 18 | static DEFINE_MUTEX(net_mutex); |
| 19 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 20 | LIST_HEAD(net_namespace_list); |
Alexey Dobriyan | b76a461 | 2008-10-08 11:35:06 +0200 | [diff] [blame] | 21 | EXPORT_SYMBOL_GPL(net_namespace_list); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 22 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 23 | struct net init_net; |
Denis V. Lunev | ff4b950 | 2008-01-22 22:05:33 -0800 | [diff] [blame] | 24 | EXPORT_SYMBOL(init_net); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 25 | |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 26 | #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */ |
| 27 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 28 | /* |
| 29 | * setup_net runs the initializers for the network namespace object. |
| 30 | */ |
Pavel Emelyanov | 1a2ee93 | 2007-11-01 00:45:59 -0700 | [diff] [blame] | 31 | static __net_init int setup_net(struct net *net) |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 32 | { |
| 33 | /* Must be called with net_mutex held */ |
| 34 | struct pernet_operations *ops; |
| 35 | int error; |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 36 | struct net_generic *ng; |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 37 | |
| 38 | atomic_set(&net->count, 1); |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 39 | #ifdef NETNS_REFCNT_DEBUG |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 40 | atomic_set(&net->use_count, 0); |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 41 | #endif |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 42 | |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 43 | error = -ENOMEM; |
| 44 | ng = kzalloc(sizeof(struct net_generic) + |
| 45 | INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL); |
| 46 | if (ng == NULL) |
| 47 | goto out; |
| 48 | |
| 49 | ng->len = INITIAL_NET_GEN_PTRS; |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 50 | rcu_assign_pointer(net->gen, ng); |
| 51 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 52 | error = 0; |
| 53 | list_for_each_entry(ops, &pernet_list, list) { |
| 54 | if (ops->init) { |
| 55 | error = ops->init(net); |
| 56 | if (error < 0) |
| 57 | goto out_undo; |
| 58 | } |
| 59 | } |
| 60 | out: |
| 61 | return error; |
| 62 | |
| 63 | out_undo: |
| 64 | /* Walk through the list backwards calling the exit functions |
| 65 | * for the pernet modules whose init functions did not fail. |
| 66 | */ |
| 67 | list_for_each_entry_continue_reverse(ops, &pernet_list, list) { |
| 68 | if (ops->exit) |
| 69 | ops->exit(net); |
| 70 | } |
| 71 | |
| 72 | rcu_barrier(); |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 73 | kfree(ng); |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 74 | goto out; |
| 75 | } |
| 76 | |
| 77 | #ifdef CONFIG_NET_NS |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 78 | static struct kmem_cache *net_cachep; |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 79 | static struct workqueue_struct *netns_wq; |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 80 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 81 | static struct net *net_alloc(void) |
| 82 | { |
Pavel Emelyanov | 32f0c4c | 2007-10-09 13:02:17 -0700 | [diff] [blame] | 83 | return kmem_cache_zalloc(net_cachep, GFP_KERNEL); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 84 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 85 | |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 86 | static void net_free(struct net *net) |
| 87 | { |
| 88 | if (!net) |
| 89 | return; |
| 90 | |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 91 | #ifdef NETNS_REFCNT_DEBUG |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 92 | if (unlikely(atomic_read(&net->use_count) != 0)) { |
| 93 | printk(KERN_EMERG "network namespace not free! Usage: %d\n", |
| 94 | atomic_read(&net->use_count)); |
| 95 | return; |
| 96 | } |
Denis V. Lunev | 5d1e446 | 2008-04-16 01:58:04 -0700 | [diff] [blame] | 97 | #endif |
Alexey Dobriyan | 4ef079c | 2008-10-14 22:54:48 -0700 | [diff] [blame] | 98 | kfree(net->gen); |
Johann Felix Soden | 45a19b0 | 2007-11-07 01:30:30 -0800 | [diff] [blame] | 99 | kmem_cache_free(net_cachep, net); |
| 100 | } |
| 101 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 102 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) |
| 103 | { |
| 104 | struct net *new_net = NULL; |
| 105 | int err; |
| 106 | |
| 107 | get_net(old_net); |
| 108 | |
| 109 | if (!(flags & CLONE_NEWNET)) |
| 110 | return old_net; |
| 111 | |
| 112 | err = -ENOMEM; |
| 113 | new_net = net_alloc(); |
| 114 | if (!new_net) |
| 115 | goto out; |
| 116 | |
| 117 | mutex_lock(&net_mutex); |
| 118 | err = setup_net(new_net); |
| 119 | if (err) |
| 120 | goto out_unlock; |
| 121 | |
| 122 | rtnl_lock(); |
| 123 | list_add_tail(&new_net->list, &net_namespace_list); |
| 124 | rtnl_unlock(); |
| 125 | |
| 126 | |
| 127 | out_unlock: |
| 128 | mutex_unlock(&net_mutex); |
| 129 | out: |
| 130 | put_net(old_net); |
| 131 | if (err) { |
| 132 | net_free(new_net); |
| 133 | new_net = ERR_PTR(err); |
| 134 | } |
| 135 | return new_net; |
| 136 | } |
| 137 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 138 | static void cleanup_net(struct work_struct *work) |
| 139 | { |
| 140 | struct pernet_operations *ops; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 141 | struct net *net; |
| 142 | |
Eric W. Biederman | b9f75f4 | 2008-06-20 22:16:51 -0700 | [diff] [blame] | 143 | /* Be very certain incoming network packets will not find us */ |
| 144 | rcu_barrier(); |
| 145 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 146 | net = container_of(work, struct net, work); |
| 147 | |
| 148 | mutex_lock(&net_mutex); |
| 149 | |
| 150 | /* Don't let anyone else find us. */ |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 151 | rtnl_lock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 152 | list_del(&net->list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 153 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 154 | |
| 155 | /* Run all of the network namespace exit methods */ |
Pavel Emelyanov | 768f3591 | 2007-09-18 13:20:41 -0700 | [diff] [blame] | 156 | list_for_each_entry_reverse(ops, &pernet_list, list) { |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 157 | if (ops->exit) |
| 158 | ops->exit(net); |
| 159 | } |
| 160 | |
| 161 | mutex_unlock(&net_mutex); |
| 162 | |
| 163 | /* Ensure there are no outstanding rcu callbacks using this |
| 164 | * network namespace. |
| 165 | */ |
| 166 | rcu_barrier(); |
| 167 | |
| 168 | /* Finally it is safe to free my network namespace structure */ |
| 169 | net_free(net); |
| 170 | } |
| 171 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 172 | void __put_net(struct net *net) |
| 173 | { |
| 174 | /* Cleanup the network namespace in process context */ |
| 175 | INIT_WORK(&net->work, cleanup_net); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 176 | queue_work(netns_wq, &net->work); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 177 | } |
| 178 | EXPORT_SYMBOL_GPL(__put_net); |
| 179 | |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 180 | #else |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 181 | struct net *copy_net_ns(unsigned long flags, struct net *old_net) |
| 182 | { |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 183 | if (flags & CLONE_NEWNET) |
| 184 | return ERR_PTR(-EINVAL); |
| 185 | return old_net; |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 186 | } |
Pavel Emelyanov | 6a1a3b9 | 2007-11-01 00:44:50 -0700 | [diff] [blame] | 187 | #endif |
Eric W. Biederman | 9dd776b | 2007-09-26 22:04:26 -0700 | [diff] [blame] | 188 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 189 | static int __init net_ns_init(void) |
| 190 | { |
| 191 | int err; |
| 192 | |
| 193 | printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net)); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 194 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 195 | net_cachep = kmem_cache_create("net_namespace", sizeof(struct net), |
| 196 | SMP_CACHE_BYTES, |
| 197 | SLAB_PANIC, NULL); |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 198 | |
| 199 | /* Create workqueue for cleanup */ |
| 200 | netns_wq = create_singlethread_workqueue("netns"); |
| 201 | if (!netns_wq) |
| 202 | panic("Could not create netns workq"); |
Pavel Emelyanov | d57a921 | 2007-11-01 00:46:50 -0700 | [diff] [blame] | 203 | #endif |
Benjamin Thery | 3ef1355 | 2007-11-19 23:18:16 -0800 | [diff] [blame] | 204 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 205 | mutex_lock(&net_mutex); |
| 206 | err = setup_net(&init_net); |
| 207 | |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 208 | rtnl_lock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 209 | list_add_tail(&init_net.list, &net_namespace_list); |
Eric W. Biederman | f4618d3 | 2007-09-26 22:40:08 -0700 | [diff] [blame] | 210 | rtnl_unlock(); |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 211 | |
| 212 | mutex_unlock(&net_mutex); |
| 213 | if (err) |
| 214 | panic("Could not setup the initial network namespace"); |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | pure_initcall(net_ns_init); |
| 220 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_NET_NS |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 222 | static int register_pernet_operations(struct list_head *list, |
| 223 | struct pernet_operations *ops) |
| 224 | { |
| 225 | struct net *net, *undo_net; |
| 226 | int error; |
| 227 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 228 | list_add_tail(&ops->list, list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 229 | if (ops->init) { |
| 230 | for_each_net(net) { |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 231 | error = ops->init(net); |
| 232 | if (error) |
| 233 | goto out_undo; |
| 234 | } |
| 235 | } |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 236 | return 0; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 237 | |
| 238 | out_undo: |
| 239 | /* If I have an error cleanup all namespaces I initialized */ |
| 240 | list_del(&ops->list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 241 | if (ops->exit) { |
| 242 | for_each_net(undo_net) { |
| 243 | if (undo_net == net) |
| 244 | goto undone; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 245 | ops->exit(undo_net); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 246 | } |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 247 | } |
| 248 | undone: |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 249 | return error; |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 253 | { |
| 254 | struct net *net; |
| 255 | |
| 256 | list_del(&ops->list); |
Pavel Emelyanov | 1dba323 | 2007-11-01 00:42:43 -0700 | [diff] [blame] | 257 | if (ops->exit) |
| 258 | for_each_net(net) |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 259 | ops->exit(net); |
| 260 | } |
| 261 | |
Denis V. Lunev | ed160e8 | 2007-11-13 03:23:21 -0800 | [diff] [blame] | 262 | #else |
| 263 | |
| 264 | static int register_pernet_operations(struct list_head *list, |
| 265 | struct pernet_operations *ops) |
| 266 | { |
| 267 | if (ops->init == NULL) |
| 268 | return 0; |
| 269 | return ops->init(&init_net); |
| 270 | } |
| 271 | |
| 272 | static void unregister_pernet_operations(struct pernet_operations *ops) |
| 273 | { |
| 274 | if (ops->exit) |
| 275 | ops->exit(&init_net); |
| 276 | } |
| 277 | #endif |
| 278 | |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 279 | static DEFINE_IDA(net_generic_ids); |
| 280 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 281 | /** |
| 282 | * register_pernet_subsys - register a network namespace subsystem |
| 283 | * @ops: pernet operations structure for the subsystem |
| 284 | * |
| 285 | * Register a subsystem which has init and exit functions |
| 286 | * that are called when network namespaces are created and |
| 287 | * destroyed respectively. |
| 288 | * |
| 289 | * When registered all network namespace init functions are |
| 290 | * called for every existing network namespace. Allowing kernel |
| 291 | * modules to have a race free view of the set of network namespaces. |
| 292 | * |
| 293 | * When a new network namespace is created all of the init |
| 294 | * methods are called in the order in which they were registered. |
| 295 | * |
| 296 | * When a network namespace is destroyed all of the exit methods |
| 297 | * are called in the reverse of the order with which they were |
| 298 | * registered. |
| 299 | */ |
| 300 | int register_pernet_subsys(struct pernet_operations *ops) |
| 301 | { |
| 302 | int error; |
| 303 | mutex_lock(&net_mutex); |
| 304 | error = register_pernet_operations(first_device, ops); |
| 305 | mutex_unlock(&net_mutex); |
| 306 | return error; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(register_pernet_subsys); |
| 309 | |
| 310 | /** |
| 311 | * unregister_pernet_subsys - unregister a network namespace subsystem |
| 312 | * @ops: pernet operations structure to manipulate |
| 313 | * |
| 314 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 315 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 316 | * addition run the exit method for all existing network |
| 317 | * namespaces. |
| 318 | */ |
| 319 | void unregister_pernet_subsys(struct pernet_operations *module) |
| 320 | { |
| 321 | mutex_lock(&net_mutex); |
| 322 | unregister_pernet_operations(module); |
| 323 | mutex_unlock(&net_mutex); |
| 324 | } |
| 325 | EXPORT_SYMBOL_GPL(unregister_pernet_subsys); |
| 326 | |
Alexey Dobriyan | 485ac57 | 2008-10-30 23:55:16 -0700 | [diff] [blame] | 327 | int register_pernet_gen_subsys(int *id, struct pernet_operations *ops) |
| 328 | { |
| 329 | int rv; |
| 330 | |
| 331 | mutex_lock(&net_mutex); |
| 332 | again: |
| 333 | rv = ida_get_new_above(&net_generic_ids, 1, id); |
| 334 | if (rv < 0) { |
| 335 | if (rv == -EAGAIN) { |
| 336 | ida_pre_get(&net_generic_ids, GFP_KERNEL); |
| 337 | goto again; |
| 338 | } |
| 339 | goto out; |
| 340 | } |
| 341 | rv = register_pernet_operations(first_device, ops); |
| 342 | if (rv < 0) |
| 343 | ida_remove(&net_generic_ids, *id); |
| 344 | mutex_unlock(&net_mutex); |
| 345 | out: |
| 346 | return rv; |
| 347 | } |
| 348 | EXPORT_SYMBOL_GPL(register_pernet_gen_subsys); |
| 349 | |
| 350 | void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops) |
| 351 | { |
| 352 | mutex_lock(&net_mutex); |
| 353 | unregister_pernet_operations(ops); |
| 354 | ida_remove(&net_generic_ids, id); |
| 355 | mutex_unlock(&net_mutex); |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys); |
| 358 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 359 | /** |
| 360 | * register_pernet_device - register a network namespace device |
| 361 | * @ops: pernet operations structure for the subsystem |
| 362 | * |
| 363 | * Register a device which has init and exit functions |
| 364 | * that are called when network namespaces are created and |
| 365 | * destroyed respectively. |
| 366 | * |
| 367 | * When registered all network namespace init functions are |
| 368 | * called for every existing network namespace. Allowing kernel |
| 369 | * modules to have a race free view of the set of network namespaces. |
| 370 | * |
| 371 | * When a new network namespace is created all of the init |
| 372 | * methods are called in the order in which they were registered. |
| 373 | * |
| 374 | * When a network namespace is destroyed all of the exit methods |
| 375 | * are called in the reverse of the order with which they were |
| 376 | * registered. |
| 377 | */ |
| 378 | int register_pernet_device(struct pernet_operations *ops) |
| 379 | { |
| 380 | int error; |
| 381 | mutex_lock(&net_mutex); |
| 382 | error = register_pernet_operations(&pernet_list, ops); |
| 383 | if (!error && (first_device == &pernet_list)) |
| 384 | first_device = &ops->list; |
| 385 | mutex_unlock(&net_mutex); |
| 386 | return error; |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(register_pernet_device); |
| 389 | |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 390 | int register_pernet_gen_device(int *id, struct pernet_operations *ops) |
| 391 | { |
| 392 | int error; |
| 393 | mutex_lock(&net_mutex); |
| 394 | again: |
| 395 | error = ida_get_new_above(&net_generic_ids, 1, id); |
| 396 | if (error) { |
| 397 | if (error == -EAGAIN) { |
| 398 | ida_pre_get(&net_generic_ids, GFP_KERNEL); |
| 399 | goto again; |
| 400 | } |
| 401 | goto out; |
| 402 | } |
| 403 | error = register_pernet_operations(&pernet_list, ops); |
| 404 | if (error) |
| 405 | ida_remove(&net_generic_ids, *id); |
| 406 | else if (first_device == &pernet_list) |
| 407 | first_device = &ops->list; |
| 408 | out: |
| 409 | mutex_unlock(&net_mutex); |
| 410 | return error; |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(register_pernet_gen_device); |
| 413 | |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 414 | /** |
| 415 | * unregister_pernet_device - unregister a network namespace netdevice |
| 416 | * @ops: pernet operations structure to manipulate |
| 417 | * |
| 418 | * Remove the pernet operations structure from the list to be |
Oliver Pinter | 53379e5 | 2008-02-03 17:56:48 +0200 | [diff] [blame] | 419 | * used when network namespaces are created or destroyed. In |
Eric W. Biederman | 5f256be | 2007-09-12 11:50:50 +0200 | [diff] [blame] | 420 | * addition run the exit method for all existing network |
| 421 | * namespaces. |
| 422 | */ |
| 423 | void unregister_pernet_device(struct pernet_operations *ops) |
| 424 | { |
| 425 | mutex_lock(&net_mutex); |
| 426 | if (&ops->list == first_device) |
| 427 | first_device = first_device->next; |
| 428 | unregister_pernet_operations(ops); |
| 429 | mutex_unlock(&net_mutex); |
| 430 | } |
| 431 | EXPORT_SYMBOL_GPL(unregister_pernet_device); |
Pavel Emelyanov | c93cf61 | 2008-04-15 00:35:23 -0700 | [diff] [blame] | 432 | |
| 433 | void unregister_pernet_gen_device(int id, struct pernet_operations *ops) |
| 434 | { |
| 435 | mutex_lock(&net_mutex); |
| 436 | if (&ops->list == first_device) |
| 437 | first_device = first_device->next; |
| 438 | unregister_pernet_operations(ops); |
| 439 | ida_remove(&net_generic_ids, id); |
| 440 | mutex_unlock(&net_mutex); |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(unregister_pernet_gen_device); |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 443 | |
| 444 | static void net_generic_release(struct rcu_head *rcu) |
| 445 | { |
| 446 | struct net_generic *ng; |
| 447 | |
| 448 | ng = container_of(rcu, struct net_generic, rcu); |
| 449 | kfree(ng); |
| 450 | } |
| 451 | |
| 452 | int net_assign_generic(struct net *net, int id, void *data) |
| 453 | { |
| 454 | struct net_generic *ng, *old_ng; |
| 455 | |
| 456 | BUG_ON(!mutex_is_locked(&net_mutex)); |
| 457 | BUG_ON(id == 0); |
| 458 | |
| 459 | ng = old_ng = net->gen; |
| 460 | if (old_ng->len >= id) |
| 461 | goto assign; |
| 462 | |
| 463 | ng = kzalloc(sizeof(struct net_generic) + |
| 464 | id * sizeof(void *), GFP_KERNEL); |
| 465 | if (ng == NULL) |
| 466 | return -ENOMEM; |
| 467 | |
| 468 | /* |
| 469 | * Some synchronisation notes: |
| 470 | * |
| 471 | * The net_generic explores the net->gen array inside rcu |
| 472 | * read section. Besides once set the net->gen->ptr[x] |
| 473 | * pointer never changes (see rules in netns/generic.h). |
| 474 | * |
| 475 | * That said, we simply duplicate this array and schedule |
| 476 | * the old copy for kfree after a grace period. |
| 477 | */ |
| 478 | |
| 479 | ng->len = id; |
Pavel Emelyanov | dec827d | 2008-04-15 00:36:08 -0700 | [diff] [blame] | 480 | memcpy(&ng->ptr, &old_ng->ptr, old_ng->len); |
| 481 | |
| 482 | rcu_assign_pointer(net->gen, ng); |
| 483 | call_rcu(&old_ng->rcu, net_generic_release); |
| 484 | assign: |
| 485 | ng->ptr[id - 1] = data; |
| 486 | return 0; |
| 487 | } |
| 488 | EXPORT_SYMBOL_GPL(net_assign_generic); |