net_namespace.c 11 KB

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