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