net_namespace.c 11 KB

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