net_namespace.c 11 KB

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