net_namespace.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. struct net init_net;
  17. EXPORT_SYMBOL(init_net);
  18. /*
  19. * setup_net runs the initializers for the network namespace object.
  20. */
  21. static __net_init int setup_net(struct net *net)
  22. {
  23. /* Must be called with net_mutex held */
  24. struct pernet_operations *ops;
  25. int error;
  26. atomic_set(&net->count, 1);
  27. atomic_set(&net->use_count, 0);
  28. error = 0;
  29. list_for_each_entry(ops, &pernet_list, list) {
  30. if (ops->init) {
  31. error = ops->init(net);
  32. if (error < 0)
  33. goto out_undo;
  34. }
  35. }
  36. out:
  37. return error;
  38. out_undo:
  39. /* Walk through the list backwards calling the exit functions
  40. * for the pernet modules whose init functions did not fail.
  41. */
  42. list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
  43. if (ops->exit)
  44. ops->exit(net);
  45. }
  46. rcu_barrier();
  47. goto out;
  48. }
  49. #ifdef CONFIG_NET_NS
  50. static struct kmem_cache *net_cachep;
  51. static struct net *net_alloc(void)
  52. {
  53. return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  54. }
  55. static void net_free(struct net *net)
  56. {
  57. if (!net)
  58. return;
  59. if (unlikely(atomic_read(&net->use_count) != 0)) {
  60. printk(KERN_EMERG "network namespace not free! Usage: %d\n",
  61. atomic_read(&net->use_count));
  62. return;
  63. }
  64. kmem_cache_free(net_cachep, net);
  65. }
  66. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  67. {
  68. struct net *new_net = NULL;
  69. int err;
  70. get_net(old_net);
  71. if (!(flags & CLONE_NEWNET))
  72. return old_net;
  73. err = -ENOMEM;
  74. new_net = net_alloc();
  75. if (!new_net)
  76. goto out;
  77. mutex_lock(&net_mutex);
  78. err = setup_net(new_net);
  79. if (err)
  80. goto out_unlock;
  81. rtnl_lock();
  82. list_add_tail(&new_net->list, &net_namespace_list);
  83. rtnl_unlock();
  84. out_unlock:
  85. mutex_unlock(&net_mutex);
  86. out:
  87. put_net(old_net);
  88. if (err) {
  89. net_free(new_net);
  90. new_net = ERR_PTR(err);
  91. }
  92. return new_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. #ifdef CONFIG_NET_NS
  137. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  138. SMP_CACHE_BYTES,
  139. SLAB_PANIC, NULL);
  140. #endif
  141. mutex_lock(&net_mutex);
  142. err = setup_net(&init_net);
  143. rtnl_lock();
  144. list_add_tail(&init_net.list, &net_namespace_list);
  145. rtnl_unlock();
  146. mutex_unlock(&net_mutex);
  147. if (err)
  148. panic("Could not setup the initial network namespace");
  149. return 0;
  150. }
  151. pure_initcall(net_ns_init);
  152. #ifdef CONFIG_NET_NS
  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. list_add_tail(&ops->list, list);
  159. if (ops->init) {
  160. for_each_net(net) {
  161. error = ops->init(net);
  162. if (error)
  163. goto out_undo;
  164. }
  165. }
  166. return 0;
  167. out_undo:
  168. /* If I have an error cleanup all namespaces I initialized */
  169. list_del(&ops->list);
  170. if (ops->exit) {
  171. for_each_net(undo_net) {
  172. if (undo_net == net)
  173. goto undone;
  174. ops->exit(undo_net);
  175. }
  176. }
  177. undone:
  178. return error;
  179. }
  180. static void unregister_pernet_operations(struct pernet_operations *ops)
  181. {
  182. struct net *net;
  183. list_del(&ops->list);
  184. if (ops->exit)
  185. for_each_net(net)
  186. ops->exit(net);
  187. }
  188. #else
  189. static int register_pernet_operations(struct list_head *list,
  190. struct pernet_operations *ops)
  191. {
  192. if (ops->init == NULL)
  193. return 0;
  194. return ops->init(&init_net);
  195. }
  196. static void unregister_pernet_operations(struct pernet_operations *ops)
  197. {
  198. if (ops->exit)
  199. ops->exit(&init_net);
  200. }
  201. #endif
  202. /**
  203. * register_pernet_subsys - register a network namespace subsystem
  204. * @ops: pernet operations structure for the subsystem
  205. *
  206. * Register a subsystem which has init and exit functions
  207. * that are called when network namespaces are created and
  208. * destroyed respectively.
  209. *
  210. * When registered all network namespace init functions are
  211. * called for every existing network namespace. Allowing kernel
  212. * modules to have a race free view of the set of network namespaces.
  213. *
  214. * When a new network namespace is created all of the init
  215. * methods are called in the order in which they were registered.
  216. *
  217. * When a network namespace is destroyed all of the exit methods
  218. * are called in the reverse of the order with which they were
  219. * registered.
  220. */
  221. int register_pernet_subsys(struct pernet_operations *ops)
  222. {
  223. int error;
  224. mutex_lock(&net_mutex);
  225. error = register_pernet_operations(first_device, ops);
  226. mutex_unlock(&net_mutex);
  227. return error;
  228. }
  229. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  230. /**
  231. * unregister_pernet_subsys - unregister a network namespace subsystem
  232. * @ops: pernet operations structure to manipulate
  233. *
  234. * Remove the pernet operations structure from the list to be
  235. * used when network namespaces are created or destoryed. In
  236. * addition run the exit method for all existing network
  237. * namespaces.
  238. */
  239. void unregister_pernet_subsys(struct pernet_operations *module)
  240. {
  241. mutex_lock(&net_mutex);
  242. unregister_pernet_operations(module);
  243. mutex_unlock(&net_mutex);
  244. }
  245. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  246. /**
  247. * register_pernet_device - register a network namespace device
  248. * @ops: pernet operations structure for the subsystem
  249. *
  250. * Register a device which has init and exit functions
  251. * that are called when network namespaces are created and
  252. * destroyed respectively.
  253. *
  254. * When registered all network namespace init functions are
  255. * called for every existing network namespace. Allowing kernel
  256. * modules to have a race free view of the set of network namespaces.
  257. *
  258. * When a new network namespace is created all of the init
  259. * methods are called in the order in which they were registered.
  260. *
  261. * When a network namespace is destroyed all of the exit methods
  262. * are called in the reverse of the order with which they were
  263. * registered.
  264. */
  265. int register_pernet_device(struct pernet_operations *ops)
  266. {
  267. int error;
  268. mutex_lock(&net_mutex);
  269. error = register_pernet_operations(&pernet_list, ops);
  270. if (!error && (first_device == &pernet_list))
  271. first_device = &ops->list;
  272. mutex_unlock(&net_mutex);
  273. return error;
  274. }
  275. EXPORT_SYMBOL_GPL(register_pernet_device);
  276. /**
  277. * unregister_pernet_device - unregister a network namespace netdevice
  278. * @ops: pernet operations structure to manipulate
  279. *
  280. * Remove the pernet operations structure from the list to be
  281. * used when network namespaces are created or destoryed. In
  282. * addition run the exit method for all existing network
  283. * namespaces.
  284. */
  285. void unregister_pernet_device(struct pernet_operations *ops)
  286. {
  287. mutex_lock(&net_mutex);
  288. if (&ops->list == first_device)
  289. first_device = first_device->next;
  290. unregister_pernet_operations(ops);
  291. mutex_unlock(&net_mutex);
  292. }
  293. EXPORT_SYMBOL_GPL(unregister_pernet_device);