net_namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. int err;
  165. #ifdef CONFIG_NET_NS
  166. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  167. SMP_CACHE_BYTES,
  168. SLAB_PANIC, NULL);
  169. /* Create workqueue for cleanup */
  170. netns_wq = create_singlethread_workqueue("netns");
  171. if (!netns_wq)
  172. panic("Could not create netns workq");
  173. #endif
  174. ng = net_alloc_generic();
  175. if (!ng)
  176. panic("Could not allocate generic netns");
  177. rcu_assign_pointer(init_net.gen, ng);
  178. mutex_lock(&net_mutex);
  179. err = setup_net(&init_net);
  180. rtnl_lock();
  181. list_add_tail(&init_net.list, &net_namespace_list);
  182. rtnl_unlock();
  183. mutex_unlock(&net_mutex);
  184. if (err)
  185. panic("Could not setup the initial network namespace");
  186. return 0;
  187. }
  188. pure_initcall(net_ns_init);
  189. #ifdef CONFIG_NET_NS
  190. static int register_pernet_operations(struct list_head *list,
  191. struct pernet_operations *ops)
  192. {
  193. struct net *net, *undo_net;
  194. int error;
  195. list_add_tail(&ops->list, list);
  196. if (ops->init) {
  197. for_each_net(net) {
  198. error = ops->init(net);
  199. if (error)
  200. goto out_undo;
  201. }
  202. }
  203. return 0;
  204. out_undo:
  205. /* If I have an error cleanup all namespaces I initialized */
  206. list_del(&ops->list);
  207. if (ops->exit) {
  208. for_each_net(undo_net) {
  209. if (undo_net == net)
  210. goto undone;
  211. ops->exit(undo_net);
  212. }
  213. }
  214. undone:
  215. return error;
  216. }
  217. static void unregister_pernet_operations(struct pernet_operations *ops)
  218. {
  219. struct net *net;
  220. list_del(&ops->list);
  221. if (ops->exit)
  222. for_each_net(net)
  223. ops->exit(net);
  224. }
  225. #else
  226. static int register_pernet_operations(struct list_head *list,
  227. struct pernet_operations *ops)
  228. {
  229. if (ops->init == NULL)
  230. return 0;
  231. return ops->init(&init_net);
  232. }
  233. static void unregister_pernet_operations(struct pernet_operations *ops)
  234. {
  235. if (ops->exit)
  236. ops->exit(&init_net);
  237. }
  238. #endif
  239. static DEFINE_IDA(net_generic_ids);
  240. /**
  241. * register_pernet_subsys - register a network namespace subsystem
  242. * @ops: pernet operations structure for the subsystem
  243. *
  244. * Register a subsystem which has init and exit functions
  245. * that are called when network namespaces are created and
  246. * destroyed respectively.
  247. *
  248. * When registered all network namespace init functions are
  249. * called for every existing network namespace. Allowing kernel
  250. * modules to have a race free view of the set of network namespaces.
  251. *
  252. * When a new network namespace is created all of the init
  253. * methods are called in the order in which they were registered.
  254. *
  255. * When a network namespace is destroyed all of the exit methods
  256. * are called in the reverse of the order with which they were
  257. * registered.
  258. */
  259. int register_pernet_subsys(struct pernet_operations *ops)
  260. {
  261. int error;
  262. mutex_lock(&net_mutex);
  263. error = register_pernet_operations(first_device, ops);
  264. mutex_unlock(&net_mutex);
  265. return error;
  266. }
  267. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  268. /**
  269. * unregister_pernet_subsys - unregister a network namespace subsystem
  270. * @ops: pernet operations structure to manipulate
  271. *
  272. * Remove the pernet operations structure from the list to be
  273. * used when network namespaces are created or destroyed. In
  274. * addition run the exit method for all existing network
  275. * namespaces.
  276. */
  277. void unregister_pernet_subsys(struct pernet_operations *module)
  278. {
  279. mutex_lock(&net_mutex);
  280. unregister_pernet_operations(module);
  281. mutex_unlock(&net_mutex);
  282. }
  283. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  284. int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
  285. {
  286. int rv;
  287. mutex_lock(&net_mutex);
  288. again:
  289. rv = ida_get_new_above(&net_generic_ids, 1, id);
  290. if (rv < 0) {
  291. if (rv == -EAGAIN) {
  292. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  293. goto again;
  294. }
  295. goto out;
  296. }
  297. rv = register_pernet_operations(first_device, ops);
  298. if (rv < 0)
  299. ida_remove(&net_generic_ids, *id);
  300. out:
  301. mutex_unlock(&net_mutex);
  302. return rv;
  303. }
  304. EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
  305. void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
  306. {
  307. mutex_lock(&net_mutex);
  308. unregister_pernet_operations(ops);
  309. ida_remove(&net_generic_ids, id);
  310. mutex_unlock(&net_mutex);
  311. }
  312. EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
  313. /**
  314. * register_pernet_device - register a network namespace device
  315. * @ops: pernet operations structure for the subsystem
  316. *
  317. * Register a device which has init and exit functions
  318. * that are called when network namespaces are created and
  319. * destroyed respectively.
  320. *
  321. * When registered all network namespace init functions are
  322. * called for every existing network namespace. Allowing kernel
  323. * modules to have a race free view of the set of network namespaces.
  324. *
  325. * When a new network namespace is created all of the init
  326. * methods are called in the order in which they were registered.
  327. *
  328. * When a network namespace is destroyed all of the exit methods
  329. * are called in the reverse of the order with which they were
  330. * registered.
  331. */
  332. int register_pernet_device(struct pernet_operations *ops)
  333. {
  334. int error;
  335. mutex_lock(&net_mutex);
  336. error = register_pernet_operations(&pernet_list, ops);
  337. if (!error && (first_device == &pernet_list))
  338. first_device = &ops->list;
  339. mutex_unlock(&net_mutex);
  340. return error;
  341. }
  342. EXPORT_SYMBOL_GPL(register_pernet_device);
  343. int register_pernet_gen_device(int *id, struct pernet_operations *ops)
  344. {
  345. int error;
  346. mutex_lock(&net_mutex);
  347. again:
  348. error = ida_get_new_above(&net_generic_ids, 1, id);
  349. if (error) {
  350. if (error == -EAGAIN) {
  351. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  352. goto again;
  353. }
  354. goto out;
  355. }
  356. error = register_pernet_operations(&pernet_list, ops);
  357. if (error)
  358. ida_remove(&net_generic_ids, *id);
  359. else if (first_device == &pernet_list)
  360. first_device = &ops->list;
  361. out:
  362. mutex_unlock(&net_mutex);
  363. return error;
  364. }
  365. EXPORT_SYMBOL_GPL(register_pernet_gen_device);
  366. /**
  367. * unregister_pernet_device - unregister a network namespace netdevice
  368. * @ops: pernet operations structure to manipulate
  369. *
  370. * Remove the pernet operations structure from the list to be
  371. * used when network namespaces are created or destroyed. In
  372. * addition run the exit method for all existing network
  373. * namespaces.
  374. */
  375. void unregister_pernet_device(struct pernet_operations *ops)
  376. {
  377. mutex_lock(&net_mutex);
  378. if (&ops->list == first_device)
  379. first_device = first_device->next;
  380. unregister_pernet_operations(ops);
  381. mutex_unlock(&net_mutex);
  382. }
  383. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  384. void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
  385. {
  386. mutex_lock(&net_mutex);
  387. if (&ops->list == first_device)
  388. first_device = first_device->next;
  389. unregister_pernet_operations(ops);
  390. ida_remove(&net_generic_ids, id);
  391. mutex_unlock(&net_mutex);
  392. }
  393. EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
  394. static void net_generic_release(struct rcu_head *rcu)
  395. {
  396. struct net_generic *ng;
  397. ng = container_of(rcu, struct net_generic, rcu);
  398. kfree(ng);
  399. }
  400. int net_assign_generic(struct net *net, int id, void *data)
  401. {
  402. struct net_generic *ng, *old_ng;
  403. BUG_ON(!mutex_is_locked(&net_mutex));
  404. BUG_ON(id == 0);
  405. ng = old_ng = net->gen;
  406. if (old_ng->len >= id)
  407. goto assign;
  408. ng = kzalloc(sizeof(struct net_generic) +
  409. id * sizeof(void *), GFP_KERNEL);
  410. if (ng == NULL)
  411. return -ENOMEM;
  412. /*
  413. * Some synchronisation notes:
  414. *
  415. * The net_generic explores the net->gen array inside rcu
  416. * read section. Besides once set the net->gen->ptr[x]
  417. * pointer never changes (see rules in netns/generic.h).
  418. *
  419. * That said, we simply duplicate this array and schedule
  420. * the old copy for kfree after a grace period.
  421. */
  422. ng->len = id;
  423. memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
  424. rcu_assign_pointer(net->gen, ng);
  425. call_rcu(&old_ng->rcu, net_generic_release);
  426. assign:
  427. ng->ptr[id - 1] = data;
  428. return 0;
  429. }
  430. EXPORT_SYMBOL_GPL(net_assign_generic);