net_namespace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <linux/idr.h>
  9. #include <net/net_namespace.h>
  10. #include <net/netns/generic.h>
  11. /*
  12. * Our network namespace constructor/destructor lists
  13. */
  14. static LIST_HEAD(pernet_list);
  15. static struct list_head *first_device = &pernet_list;
  16. static DEFINE_MUTEX(net_mutex);
  17. LIST_HEAD(net_namespace_list);
  18. struct net init_net;
  19. EXPORT_SYMBOL(init_net);
  20. #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
  21. /*
  22. * setup_net runs the initializers for the network namespace object.
  23. */
  24. static __net_init int setup_net(struct net *net)
  25. {
  26. /* Must be called with net_mutex held */
  27. struct pernet_operations *ops;
  28. int error;
  29. struct net_generic *ng;
  30. atomic_set(&net->count, 1);
  31. #ifdef NETNS_REFCNT_DEBUG
  32. atomic_set(&net->use_count, 0);
  33. #endif
  34. error = -ENOMEM;
  35. ng = kzalloc(sizeof(struct net_generic) +
  36. INITIAL_NET_GEN_PTRS * sizeof(void *), GFP_KERNEL);
  37. if (ng == NULL)
  38. goto out;
  39. ng->len = INITIAL_NET_GEN_PTRS;
  40. INIT_RCU_HEAD(&ng->rcu);
  41. rcu_assign_pointer(net->gen, ng);
  42. error = 0;
  43. list_for_each_entry(ops, &pernet_list, list) {
  44. if (ops->init) {
  45. error = ops->init(net);
  46. if (error < 0)
  47. goto out_undo;
  48. }
  49. }
  50. out:
  51. return error;
  52. out_undo:
  53. /* Walk through the list backwards calling the exit functions
  54. * for the pernet modules whose init functions did not fail.
  55. */
  56. list_for_each_entry_continue_reverse(ops, &pernet_list, list) {
  57. if (ops->exit)
  58. ops->exit(net);
  59. }
  60. rcu_barrier();
  61. kfree(ng);
  62. goto out;
  63. }
  64. #ifdef CONFIG_NET_NS
  65. static struct kmem_cache *net_cachep;
  66. static struct workqueue_struct *netns_wq;
  67. static struct net *net_alloc(void)
  68. {
  69. return kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  70. }
  71. static void net_free(struct net *net)
  72. {
  73. if (!net)
  74. return;
  75. #ifdef NETNS_REFCNT_DEBUG
  76. if (unlikely(atomic_read(&net->use_count) != 0)) {
  77. printk(KERN_EMERG "network namespace not free! Usage: %d\n",
  78. atomic_read(&net->use_count));
  79. return;
  80. }
  81. #endif
  82. kmem_cache_free(net_cachep, net);
  83. }
  84. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  85. {
  86. struct net *new_net = NULL;
  87. int err;
  88. get_net(old_net);
  89. if (!(flags & CLONE_NEWNET))
  90. return old_net;
  91. err = -ENOMEM;
  92. new_net = net_alloc();
  93. if (!new_net)
  94. goto out;
  95. mutex_lock(&net_mutex);
  96. err = setup_net(new_net);
  97. if (err)
  98. goto out_unlock;
  99. rtnl_lock();
  100. list_add_tail(&new_net->list, &net_namespace_list);
  101. rtnl_unlock();
  102. out_unlock:
  103. mutex_unlock(&net_mutex);
  104. out:
  105. put_net(old_net);
  106. if (err) {
  107. net_free(new_net);
  108. new_net = ERR_PTR(err);
  109. }
  110. return new_net;
  111. }
  112. static void cleanup_net(struct work_struct *work)
  113. {
  114. struct pernet_operations *ops;
  115. struct net *net;
  116. net = container_of(work, struct net, work);
  117. mutex_lock(&net_mutex);
  118. /* Don't let anyone else find us. */
  119. rtnl_lock();
  120. list_del(&net->list);
  121. rtnl_unlock();
  122. /* Run all of the network namespace exit methods */
  123. list_for_each_entry_reverse(ops, &pernet_list, list) {
  124. if (ops->exit)
  125. ops->exit(net);
  126. }
  127. mutex_unlock(&net_mutex);
  128. /* Ensure there are no outstanding rcu callbacks using this
  129. * network namespace.
  130. */
  131. rcu_barrier();
  132. /* Finally it is safe to free my network namespace structure */
  133. net_free(net);
  134. }
  135. void __put_net(struct net *net)
  136. {
  137. /* Cleanup the network namespace in process context */
  138. INIT_WORK(&net->work, cleanup_net);
  139. queue_work(netns_wq, &net->work);
  140. }
  141. EXPORT_SYMBOL_GPL(__put_net);
  142. #else
  143. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  144. {
  145. if (flags & CLONE_NEWNET)
  146. return ERR_PTR(-EINVAL);
  147. return old_net;
  148. }
  149. #endif
  150. static int __init net_ns_init(void)
  151. {
  152. int err;
  153. printk(KERN_INFO "net_namespace: %zd bytes\n", sizeof(struct net));
  154. #ifdef CONFIG_NET_NS
  155. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  156. SMP_CACHE_BYTES,
  157. SLAB_PANIC, NULL);
  158. /* Create workqueue for cleanup */
  159. netns_wq = create_singlethread_workqueue("netns");
  160. if (!netns_wq)
  161. panic("Could not create netns workq");
  162. #endif
  163. mutex_lock(&net_mutex);
  164. err = setup_net(&init_net);
  165. rtnl_lock();
  166. list_add_tail(&init_net.list, &net_namespace_list);
  167. rtnl_unlock();
  168. mutex_unlock(&net_mutex);
  169. if (err)
  170. panic("Could not setup the initial network namespace");
  171. return 0;
  172. }
  173. pure_initcall(net_ns_init);
  174. #ifdef CONFIG_NET_NS
  175. static int register_pernet_operations(struct list_head *list,
  176. struct pernet_operations *ops)
  177. {
  178. struct net *net, *undo_net;
  179. int error;
  180. list_add_tail(&ops->list, list);
  181. if (ops->init) {
  182. for_each_net(net) {
  183. error = ops->init(net);
  184. if (error)
  185. goto out_undo;
  186. }
  187. }
  188. return 0;
  189. out_undo:
  190. /* If I have an error cleanup all namespaces I initialized */
  191. list_del(&ops->list);
  192. if (ops->exit) {
  193. for_each_net(undo_net) {
  194. if (undo_net == net)
  195. goto undone;
  196. ops->exit(undo_net);
  197. }
  198. }
  199. undone:
  200. return error;
  201. }
  202. static void unregister_pernet_operations(struct pernet_operations *ops)
  203. {
  204. struct net *net;
  205. list_del(&ops->list);
  206. if (ops->exit)
  207. for_each_net(net)
  208. ops->exit(net);
  209. }
  210. #else
  211. static int register_pernet_operations(struct list_head *list,
  212. struct pernet_operations *ops)
  213. {
  214. if (ops->init == NULL)
  215. return 0;
  216. return ops->init(&init_net);
  217. }
  218. static void unregister_pernet_operations(struct pernet_operations *ops)
  219. {
  220. if (ops->exit)
  221. ops->exit(&init_net);
  222. }
  223. #endif
  224. static DEFINE_IDA(net_generic_ids);
  225. /**
  226. * register_pernet_subsys - register a network namespace subsystem
  227. * @ops: pernet operations structure for the subsystem
  228. *
  229. * Register a subsystem which has init and exit functions
  230. * that are called when network namespaces are created and
  231. * destroyed respectively.
  232. *
  233. * When registered all network namespace init functions are
  234. * called for every existing network namespace. Allowing kernel
  235. * modules to have a race free view of the set of network namespaces.
  236. *
  237. * When a new network namespace is created all of the init
  238. * methods are called in the order in which they were registered.
  239. *
  240. * When a network namespace is destroyed all of the exit methods
  241. * are called in the reverse of the order with which they were
  242. * registered.
  243. */
  244. int register_pernet_subsys(struct pernet_operations *ops)
  245. {
  246. int error;
  247. mutex_lock(&net_mutex);
  248. error = register_pernet_operations(first_device, ops);
  249. mutex_unlock(&net_mutex);
  250. return error;
  251. }
  252. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  253. /**
  254. * unregister_pernet_subsys - unregister a network namespace subsystem
  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 destroyed. In
  259. * addition run the exit method for all existing network
  260. * namespaces.
  261. */
  262. void unregister_pernet_subsys(struct pernet_operations *module)
  263. {
  264. mutex_lock(&net_mutex);
  265. unregister_pernet_operations(module);
  266. mutex_unlock(&net_mutex);
  267. }
  268. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  269. /**
  270. * register_pernet_device - register a network namespace device
  271. * @ops: pernet operations structure for the subsystem
  272. *
  273. * Register a device which has init and exit functions
  274. * that are called when network namespaces are created and
  275. * destroyed respectively.
  276. *
  277. * When registered all network namespace init functions are
  278. * called for every existing network namespace. Allowing kernel
  279. * modules to have a race free view of the set of network namespaces.
  280. *
  281. * When a new network namespace is created all of the init
  282. * methods are called in the order in which they were registered.
  283. *
  284. * When a network namespace is destroyed all of the exit methods
  285. * are called in the reverse of the order with which they were
  286. * registered.
  287. */
  288. int register_pernet_device(struct pernet_operations *ops)
  289. {
  290. int error;
  291. mutex_lock(&net_mutex);
  292. error = register_pernet_operations(&pernet_list, ops);
  293. if (!error && (first_device == &pernet_list))
  294. first_device = &ops->list;
  295. mutex_unlock(&net_mutex);
  296. return error;
  297. }
  298. EXPORT_SYMBOL_GPL(register_pernet_device);
  299. int register_pernet_gen_device(int *id, struct pernet_operations *ops)
  300. {
  301. int error;
  302. mutex_lock(&net_mutex);
  303. again:
  304. error = ida_get_new_above(&net_generic_ids, 1, id);
  305. if (error) {
  306. if (error == -EAGAIN) {
  307. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  308. goto again;
  309. }
  310. goto out;
  311. }
  312. error = register_pernet_operations(&pernet_list, ops);
  313. if (error)
  314. ida_remove(&net_generic_ids, *id);
  315. else if (first_device == &pernet_list)
  316. first_device = &ops->list;
  317. out:
  318. mutex_unlock(&net_mutex);
  319. return error;
  320. }
  321. EXPORT_SYMBOL_GPL(register_pernet_gen_device);
  322. /**
  323. * unregister_pernet_device - unregister a network namespace netdevice
  324. * @ops: pernet operations structure to manipulate
  325. *
  326. * Remove the pernet operations structure from the list to be
  327. * used when network namespaces are created or destroyed. In
  328. * addition run the exit method for all existing network
  329. * namespaces.
  330. */
  331. void unregister_pernet_device(struct pernet_operations *ops)
  332. {
  333. mutex_lock(&net_mutex);
  334. if (&ops->list == first_device)
  335. first_device = first_device->next;
  336. unregister_pernet_operations(ops);
  337. mutex_unlock(&net_mutex);
  338. }
  339. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  340. void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
  341. {
  342. mutex_lock(&net_mutex);
  343. if (&ops->list == first_device)
  344. first_device = first_device->next;
  345. unregister_pernet_operations(ops);
  346. ida_remove(&net_generic_ids, id);
  347. mutex_unlock(&net_mutex);
  348. }
  349. EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
  350. static void net_generic_release(struct rcu_head *rcu)
  351. {
  352. struct net_generic *ng;
  353. ng = container_of(rcu, struct net_generic, rcu);
  354. kfree(ng);
  355. }
  356. int net_assign_generic(struct net *net, int id, void *data)
  357. {
  358. struct net_generic *ng, *old_ng;
  359. BUG_ON(!mutex_is_locked(&net_mutex));
  360. BUG_ON(id == 0);
  361. ng = old_ng = net->gen;
  362. if (old_ng->len >= id)
  363. goto assign;
  364. ng = kzalloc(sizeof(struct net_generic) +
  365. id * sizeof(void *), GFP_KERNEL);
  366. if (ng == NULL)
  367. return -ENOMEM;
  368. /*
  369. * Some synchronisation notes:
  370. *
  371. * The net_generic explores the net->gen array inside rcu
  372. * read section. Besides once set the net->gen->ptr[x]
  373. * pointer never changes (see rules in netns/generic.h).
  374. *
  375. * That said, we simply duplicate this array and schedule
  376. * the old copy for kfree after a grace period.
  377. */
  378. ng->len = id;
  379. INIT_RCU_HEAD(&ng->rcu);
  380. memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
  381. rcu_assign_pointer(net->gen, ng);
  382. call_rcu(&old_ng->rcu, net_generic_release);
  383. assign:
  384. ng->ptr[id - 1] = data;
  385. return 0;
  386. }
  387. EXPORT_SYMBOL_GPL(net_assign_generic);