net_namespace.c 7.0 KB

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