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