net_namespace.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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>
  7. #include <linux/sched.h>
  8. #include <net/net_namespace.h>
  9. /*
  10. * Our network namespace constructor/destructor lists
  11. */
  12. static LIST_HEAD(pernet_list);
  13. static struct list_head *first_device = &pernet_list;
  14. static DEFINE_MUTEX(net_mutex);
  15. static DEFINE_MUTEX(net_list_mutex);
  16. LIST_HEAD(net_namespace_list);
  17. static struct kmem_cache *net_cachep;
  18. struct net init_net;
  19. EXPORT_SYMBOL_GPL(init_net);
  20. void net_lock(void)
  21. {
  22. mutex_lock(&net_list_mutex);
  23. }
  24. void net_unlock(void)
  25. {
  26. mutex_unlock(&net_list_mutex);
  27. }
  28. static struct net *net_alloc(void)
  29. {
  30. return kmem_cache_alloc(net_cachep, GFP_KERNEL);
  31. }
  32. static void net_free(struct net *net)
  33. {
  34. if (!net)
  35. return;
  36. if (unlikely(atomic_read(&net->use_count) != 0)) {
  37. printk(KERN_EMERG "network namespace not free! Usage: %d\n",
  38. atomic_read(&net->use_count));
  39. return;
  40. }
  41. kmem_cache_free(net_cachep, net);
  42. }
  43. static void cleanup_net(struct work_struct *work)
  44. {
  45. struct pernet_operations *ops;
  46. struct net *net;
  47. net = container_of(work, struct net, work);
  48. mutex_lock(&net_mutex);
  49. /* Don't let anyone else find us. */
  50. net_lock();
  51. list_del(&net->list);
  52. net_unlock();
  53. /* Run all of the network namespace exit methods */
  54. list_for_each_entry_reverse(ops, &pernet_list, list) {
  55. if (ops->exit)
  56. ops->exit(net);
  57. }
  58. mutex_unlock(&net_mutex);
  59. /* Ensure there are no outstanding rcu callbacks using this
  60. * network namespace.
  61. */
  62. rcu_barrier();
  63. /* Finally it is safe to free my network namespace structure */
  64. net_free(net);
  65. }
  66. void __put_net(struct net *net)
  67. {
  68. /* Cleanup the network namespace in process context */
  69. INIT_WORK(&net->work, cleanup_net);
  70. schedule_work(&net->work);
  71. }
  72. EXPORT_SYMBOL_GPL(__put_net);
  73. /*
  74. * setup_net runs the initializers for the network namespace object.
  75. */
  76. static int setup_net(struct net *net)
  77. {
  78. /* Must be called with net_mutex held */
  79. struct pernet_operations *ops;
  80. int error;
  81. memset(net, 0, sizeof(struct net));
  82. atomic_set(&net->count, 1);
  83. atomic_set(&net->use_count, 0);
  84. error = 0;
  85. list_for_each_entry(ops, &pernet_list, list) {
  86. if (ops->init) {
  87. error = ops->init(net);
  88. if (error < 0)
  89. goto out_undo;
  90. }
  91. }
  92. out:
  93. return error;
  94. out_undo:
  95. /* Walk through the list backwards calling the exit functions
  96. * for the pernet modules whose init functions did not fail.
  97. */
  98. list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
  99. if (ops->exit)
  100. ops->exit(net);
  101. }
  102. goto out;
  103. }
  104. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  105. {
  106. struct net *new_net = NULL;
  107. int err;
  108. get_net(old_net);
  109. if (!(flags & CLONE_NEWNET))
  110. return old_net;
  111. #ifndef CONFIG_NET_NS
  112. return ERR_PTR(-EINVAL);
  113. #endif
  114. err = -ENOMEM;
  115. new_net = net_alloc();
  116. if (!new_net)
  117. goto out;
  118. mutex_lock(&net_mutex);
  119. err = setup_net(new_net);
  120. if (err)
  121. goto out_unlock;
  122. net_lock();
  123. list_add_tail(&new_net->list, &net_namespace_list);
  124. net_unlock();
  125. out_unlock:
  126. mutex_unlock(&net_mutex);
  127. out:
  128. put_net(old_net);
  129. if (err) {
  130. net_free(new_net);
  131. new_net = ERR_PTR(err);
  132. }
  133. return new_net;
  134. }
  135. static int __init net_ns_init(void)
  136. {
  137. int err;
  138. printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
  139. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  140. SMP_CACHE_BYTES,
  141. SLAB_PANIC, NULL);
  142. mutex_lock(&net_mutex);
  143. err = setup_net(&init_net);
  144. net_lock();
  145. list_add_tail(&init_net.list, &net_namespace_list);
  146. net_unlock();
  147. mutex_unlock(&net_mutex);
  148. if (err)
  149. panic("Could not setup the initial network namespace");
  150. return 0;
  151. }
  152. pure_initcall(net_ns_init);
  153. static int register_pernet_operations(struct list_head *list,
  154. struct pernet_operations *ops)
  155. {
  156. struct net *net, *undo_net;
  157. int error;
  158. error = 0;
  159. list_add_tail(&ops->list, list);
  160. for_each_net(net) {
  161. if (ops->init) {
  162. error = ops->init(net);
  163. if (error)
  164. goto out_undo;
  165. }
  166. }
  167. out:
  168. return error;
  169. out_undo:
  170. /* If I have an error cleanup all namespaces I initialized */
  171. list_del(&ops->list);
  172. for_each_net(undo_net) {
  173. if (undo_net == net)
  174. goto undone;
  175. if (ops->exit)
  176. ops->exit(undo_net);
  177. }
  178. undone:
  179. goto out;
  180. }
  181. static void unregister_pernet_operations(struct pernet_operations *ops)
  182. {
  183. struct net *net;
  184. list_del(&ops->list);
  185. for_each_net(net)
  186. if (ops->exit)
  187. ops->exit(net);
  188. }
  189. /**
  190. * register_pernet_subsys - register a network namespace subsystem
  191. * @ops: pernet operations structure for the subsystem
  192. *
  193. * Register a subsystem which has init and exit functions
  194. * that are called when network namespaces are created and
  195. * destroyed respectively.
  196. *
  197. * When registered all network namespace init functions are
  198. * called for every existing network namespace. Allowing kernel
  199. * modules to have a race free view of the set of network namespaces.
  200. *
  201. * When a new network namespace is created all of the init
  202. * methods are called in the order in which they were registered.
  203. *
  204. * When a network namespace is destroyed all of the exit methods
  205. * are called in the reverse of the order with which they were
  206. * registered.
  207. */
  208. int register_pernet_subsys(struct pernet_operations *ops)
  209. {
  210. int error;
  211. mutex_lock(&net_mutex);
  212. error = register_pernet_operations(first_device, ops);
  213. mutex_unlock(&net_mutex);
  214. return error;
  215. }
  216. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  217. /**
  218. * unregister_pernet_subsys - unregister a network namespace subsystem
  219. * @ops: pernet operations structure to manipulate
  220. *
  221. * Remove the pernet operations structure from the list to be
  222. * used when network namespaces are created or destoryed. In
  223. * addition run the exit method for all existing network
  224. * namespaces.
  225. */
  226. void unregister_pernet_subsys(struct pernet_operations *module)
  227. {
  228. mutex_lock(&net_mutex);
  229. unregister_pernet_operations(module);
  230. mutex_unlock(&net_mutex);
  231. }
  232. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  233. /**
  234. * register_pernet_device - register a network namespace device
  235. * @ops: pernet operations structure for the subsystem
  236. *
  237. * Register a device which has init and exit functions
  238. * that are called when network namespaces are created and
  239. * destroyed respectively.
  240. *
  241. * When registered all network namespace init functions are
  242. * called for every existing network namespace. Allowing kernel
  243. * modules to have a race free view of the set of network namespaces.
  244. *
  245. * When a new network namespace is created all of the init
  246. * methods are called in the order in which they were registered.
  247. *
  248. * When a network namespace is destroyed all of the exit methods
  249. * are called in the reverse of the order with which they were
  250. * registered.
  251. */
  252. int register_pernet_device(struct pernet_operations *ops)
  253. {
  254. int error;
  255. mutex_lock(&net_mutex);
  256. error = register_pernet_operations(&pernet_list, ops);
  257. if (!error && (first_device == &pernet_list))
  258. first_device = &ops->list;
  259. mutex_unlock(&net_mutex);
  260. return error;
  261. }
  262. EXPORT_SYMBOL_GPL(register_pernet_device);
  263. /**
  264. * unregister_pernet_device - unregister a network namespace netdevice
  265. * @ops: pernet operations structure to manipulate
  266. *
  267. * Remove the pernet operations structure from the list to be
  268. * used when network namespaces are created or destoryed. In
  269. * addition run the exit method for all existing network
  270. * namespaces.
  271. */
  272. void unregister_pernet_device(struct pernet_operations *ops)
  273. {
  274. mutex_lock(&net_mutex);
  275. if (&ops->list == first_device)
  276. first_device = first_device->next;
  277. unregister_pernet_operations(ops);
  278. mutex_unlock(&net_mutex);
  279. }
  280. EXPORT_SYMBOL_GPL(unregister_pernet_device);