net_namespace.c 7.3 KB

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