net_namespace.c 7.4 KB

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