net_namespace.c 10 KB

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