net_namespace.c 7.9 KB

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