net_namespace.c 11 KB

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