net_namespace.c 11 KB

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