net_namespace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. int register_pernet_gen_subsys(int *id, struct pernet_operations *ops)
  274. {
  275. int rv;
  276. mutex_lock(&net_mutex);
  277. again:
  278. rv = ida_get_new_above(&net_generic_ids, 1, id);
  279. if (rv < 0) {
  280. if (rv == -EAGAIN) {
  281. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  282. goto again;
  283. }
  284. goto out;
  285. }
  286. rv = register_pernet_operations(first_device, ops);
  287. if (rv < 0)
  288. ida_remove(&net_generic_ids, *id);
  289. mutex_unlock(&net_mutex);
  290. out:
  291. return rv;
  292. }
  293. EXPORT_SYMBOL_GPL(register_pernet_gen_subsys);
  294. void unregister_pernet_gen_subsys(int id, struct pernet_operations *ops)
  295. {
  296. mutex_lock(&net_mutex);
  297. unregister_pernet_operations(ops);
  298. ida_remove(&net_generic_ids, id);
  299. mutex_unlock(&net_mutex);
  300. }
  301. EXPORT_SYMBOL_GPL(unregister_pernet_gen_subsys);
  302. /**
  303. * register_pernet_device - register a network namespace device
  304. * @ops: pernet operations structure for the subsystem
  305. *
  306. * Register a device which has init and exit functions
  307. * that are called when network namespaces are created and
  308. * destroyed respectively.
  309. *
  310. * When registered all network namespace init functions are
  311. * called for every existing network namespace. Allowing kernel
  312. * modules to have a race free view of the set of network namespaces.
  313. *
  314. * When a new network namespace is created all of the init
  315. * methods are called in the order in which they were registered.
  316. *
  317. * When a network namespace is destroyed all of the exit methods
  318. * are called in the reverse of the order with which they were
  319. * registered.
  320. */
  321. int register_pernet_device(struct pernet_operations *ops)
  322. {
  323. int error;
  324. mutex_lock(&net_mutex);
  325. error = register_pernet_operations(&pernet_list, ops);
  326. if (!error && (first_device == &pernet_list))
  327. first_device = &ops->list;
  328. mutex_unlock(&net_mutex);
  329. return error;
  330. }
  331. EXPORT_SYMBOL_GPL(register_pernet_device);
  332. int register_pernet_gen_device(int *id, struct pernet_operations *ops)
  333. {
  334. int error;
  335. mutex_lock(&net_mutex);
  336. again:
  337. error = ida_get_new_above(&net_generic_ids, 1, id);
  338. if (error) {
  339. if (error == -EAGAIN) {
  340. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  341. goto again;
  342. }
  343. goto out;
  344. }
  345. error = register_pernet_operations(&pernet_list, ops);
  346. if (error)
  347. ida_remove(&net_generic_ids, *id);
  348. else if (first_device == &pernet_list)
  349. first_device = &ops->list;
  350. out:
  351. mutex_unlock(&net_mutex);
  352. return error;
  353. }
  354. EXPORT_SYMBOL_GPL(register_pernet_gen_device);
  355. /**
  356. * unregister_pernet_device - unregister a network namespace netdevice
  357. * @ops: pernet operations structure to manipulate
  358. *
  359. * Remove the pernet operations structure from the list to be
  360. * used when network namespaces are created or destroyed. In
  361. * addition run the exit method for all existing network
  362. * namespaces.
  363. */
  364. void unregister_pernet_device(struct pernet_operations *ops)
  365. {
  366. mutex_lock(&net_mutex);
  367. if (&ops->list == first_device)
  368. first_device = first_device->next;
  369. unregister_pernet_operations(ops);
  370. mutex_unlock(&net_mutex);
  371. }
  372. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  373. void unregister_pernet_gen_device(int id, struct pernet_operations *ops)
  374. {
  375. mutex_lock(&net_mutex);
  376. if (&ops->list == first_device)
  377. first_device = first_device->next;
  378. unregister_pernet_operations(ops);
  379. ida_remove(&net_generic_ids, id);
  380. mutex_unlock(&net_mutex);
  381. }
  382. EXPORT_SYMBOL_GPL(unregister_pernet_gen_device);
  383. static void net_generic_release(struct rcu_head *rcu)
  384. {
  385. struct net_generic *ng;
  386. ng = container_of(rcu, struct net_generic, rcu);
  387. kfree(ng);
  388. }
  389. int net_assign_generic(struct net *net, int id, void *data)
  390. {
  391. struct net_generic *ng, *old_ng;
  392. BUG_ON(!mutex_is_locked(&net_mutex));
  393. BUG_ON(id == 0);
  394. ng = old_ng = net->gen;
  395. if (old_ng->len >= id)
  396. goto assign;
  397. ng = kzalloc(sizeof(struct net_generic) +
  398. id * sizeof(void *), GFP_KERNEL);
  399. if (ng == NULL)
  400. return -ENOMEM;
  401. /*
  402. * Some synchronisation notes:
  403. *
  404. * The net_generic explores the net->gen array inside rcu
  405. * read section. Besides once set the net->gen->ptr[x]
  406. * pointer never changes (see rules in netns/generic.h).
  407. *
  408. * That said, we simply duplicate this array and schedule
  409. * the old copy for kfree after a grace period.
  410. */
  411. ng->len = id;
  412. INIT_RCU_HEAD(&ng->rcu);
  413. memcpy(&ng->ptr, &old_ng->ptr, old_ng->len);
  414. rcu_assign_pointer(net->gen, ng);
  415. call_rcu(&old_ng->rcu, net_generic_release);
  416. assign:
  417. ng->ptr[id - 1] = data;
  418. return 0;
  419. }
  420. EXPORT_SYMBOL_GPL(net_assign_generic);