net_namespace.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. goto out;
  93. }
  94. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  95. {
  96. struct net *new_net = NULL;
  97. int err;
  98. get_net(old_net);
  99. if (!(flags & CLONE_NEWNET))
  100. return old_net;
  101. #ifndef CONFIG_NET_NS
  102. return ERR_PTR(-EINVAL);
  103. #endif
  104. err = -ENOMEM;
  105. new_net = net_alloc();
  106. if (!new_net)
  107. goto out;
  108. mutex_lock(&net_mutex);
  109. err = setup_net(new_net);
  110. if (err)
  111. goto out_unlock;
  112. rtnl_lock();
  113. list_add_tail(&new_net->list, &net_namespace_list);
  114. rtnl_unlock();
  115. out_unlock:
  116. mutex_unlock(&net_mutex);
  117. out:
  118. put_net(old_net);
  119. if (err) {
  120. net_free(new_net);
  121. new_net = ERR_PTR(err);
  122. }
  123. return new_net;
  124. }
  125. static int __init net_ns_init(void)
  126. {
  127. int err;
  128. printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
  129. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  130. SMP_CACHE_BYTES,
  131. SLAB_PANIC, NULL);
  132. mutex_lock(&net_mutex);
  133. err = setup_net(&init_net);
  134. rtnl_lock();
  135. list_add_tail(&init_net.list, &net_namespace_list);
  136. rtnl_unlock();
  137. mutex_unlock(&net_mutex);
  138. if (err)
  139. panic("Could not setup the initial network namespace");
  140. return 0;
  141. }
  142. pure_initcall(net_ns_init);
  143. static int register_pernet_operations(struct list_head *list,
  144. struct pernet_operations *ops)
  145. {
  146. struct net *net, *undo_net;
  147. int error;
  148. error = 0;
  149. list_add_tail(&ops->list, list);
  150. for_each_net(net) {
  151. if (ops->init) {
  152. error = ops->init(net);
  153. if (error)
  154. goto out_undo;
  155. }
  156. }
  157. out:
  158. return error;
  159. out_undo:
  160. /* If I have an error cleanup all namespaces I initialized */
  161. list_del(&ops->list);
  162. for_each_net(undo_net) {
  163. if (undo_net == net)
  164. goto undone;
  165. if (ops->exit)
  166. ops->exit(undo_net);
  167. }
  168. undone:
  169. goto out;
  170. }
  171. static void unregister_pernet_operations(struct pernet_operations *ops)
  172. {
  173. struct net *net;
  174. list_del(&ops->list);
  175. for_each_net(net)
  176. if (ops->exit)
  177. ops->exit(net);
  178. }
  179. /**
  180. * register_pernet_subsys - register a network namespace subsystem
  181. * @ops: pernet operations structure for the subsystem
  182. *
  183. * Register a subsystem which has init and exit functions
  184. * that are called when network namespaces are created and
  185. * destroyed respectively.
  186. *
  187. * When registered all network namespace init functions are
  188. * called for every existing network namespace. Allowing kernel
  189. * modules to have a race free view of the set of network namespaces.
  190. *
  191. * When a new network namespace is created all of the init
  192. * methods are called in the order in which they were registered.
  193. *
  194. * When a network namespace is destroyed all of the exit methods
  195. * are called in the reverse of the order with which they were
  196. * registered.
  197. */
  198. int register_pernet_subsys(struct pernet_operations *ops)
  199. {
  200. int error;
  201. mutex_lock(&net_mutex);
  202. error = register_pernet_operations(first_device, ops);
  203. mutex_unlock(&net_mutex);
  204. return error;
  205. }
  206. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  207. /**
  208. * unregister_pernet_subsys - unregister a network namespace subsystem
  209. * @ops: pernet operations structure to manipulate
  210. *
  211. * Remove the pernet operations structure from the list to be
  212. * used when network namespaces are created or destoryed. In
  213. * addition run the exit method for all existing network
  214. * namespaces.
  215. */
  216. void unregister_pernet_subsys(struct pernet_operations *module)
  217. {
  218. mutex_lock(&net_mutex);
  219. unregister_pernet_operations(module);
  220. mutex_unlock(&net_mutex);
  221. }
  222. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  223. /**
  224. * register_pernet_device - register a network namespace device
  225. * @ops: pernet operations structure for the subsystem
  226. *
  227. * Register a device which has init and exit functions
  228. * that are called when network namespaces are created and
  229. * destroyed respectively.
  230. *
  231. * When registered all network namespace init functions are
  232. * called for every existing network namespace. Allowing kernel
  233. * modules to have a race free view of the set of network namespaces.
  234. *
  235. * When a new network namespace is created all of the init
  236. * methods are called in the order in which they were registered.
  237. *
  238. * When a network namespace is destroyed all of the exit methods
  239. * are called in the reverse of the order with which they were
  240. * registered.
  241. */
  242. int register_pernet_device(struct pernet_operations *ops)
  243. {
  244. int error;
  245. mutex_lock(&net_mutex);
  246. error = register_pernet_operations(&pernet_list, ops);
  247. if (!error && (first_device == &pernet_list))
  248. first_device = &ops->list;
  249. mutex_unlock(&net_mutex);
  250. return error;
  251. }
  252. EXPORT_SYMBOL_GPL(register_pernet_device);
  253. /**
  254. * unregister_pernet_device - unregister a network namespace netdevice
  255. * @ops: pernet operations structure to manipulate
  256. *
  257. * Remove the pernet operations structure from the list to be
  258. * used when network namespaces are created or destoryed. In
  259. * addition run the exit method for all existing network
  260. * namespaces.
  261. */
  262. void unregister_pernet_device(struct pernet_operations *ops)
  263. {
  264. mutex_lock(&net_mutex);
  265. if (&ops->list == first_device)
  266. first_device = first_device->next;
  267. unregister_pernet_operations(ops);
  268. mutex_unlock(&net_mutex);
  269. }
  270. EXPORT_SYMBOL_GPL(unregister_pernet_device);