net_namespace.c 10 KB

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