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