net_namespace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/workqueue.h>
  3. #include <linux/rtnetlink.h>
  4. #include <linux/cache.h>
  5. #include <linux/slab.h>
  6. #include <linux/list.h>
  7. #include <linux/delay.h>
  8. #include <linux/sched.h>
  9. #include <linux/idr.h>
  10. #include <linux/rculist.h>
  11. #include <linux/nsproxy.h>
  12. #include <linux/proc_fs.h>
  13. #include <linux/file.h>
  14. #include <linux/export.h>
  15. #include <net/net_namespace.h>
  16. #include <net/netns/generic.h>
  17. /*
  18. * Our network namespace constructor/destructor lists
  19. */
  20. static LIST_HEAD(pernet_list);
  21. static struct list_head *first_device = &pernet_list;
  22. static DEFINE_MUTEX(net_mutex);
  23. LIST_HEAD(net_namespace_list);
  24. EXPORT_SYMBOL_GPL(net_namespace_list);
  25. struct net init_net = {
  26. .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
  27. };
  28. EXPORT_SYMBOL(init_net);
  29. #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
  30. static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
  31. static struct net_generic *net_alloc_generic(void)
  32. {
  33. struct net_generic *ng;
  34. size_t generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
  35. ng = kzalloc(generic_size, GFP_KERNEL);
  36. if (ng)
  37. ng->len = max_gen_ptrs;
  38. return ng;
  39. }
  40. static int net_assign_generic(struct net *net, int id, void *data)
  41. {
  42. struct net_generic *ng, *old_ng;
  43. BUG_ON(!mutex_is_locked(&net_mutex));
  44. BUG_ON(id == 0);
  45. old_ng = rcu_dereference_protected(net->gen,
  46. lockdep_is_held(&net_mutex));
  47. ng = old_ng;
  48. if (old_ng->len >= id)
  49. goto assign;
  50. ng = net_alloc_generic();
  51. if (ng == NULL)
  52. return -ENOMEM;
  53. /*
  54. * Some synchronisation notes:
  55. *
  56. * The net_generic explores the net->gen array inside rcu
  57. * read section. Besides once set the net->gen->ptr[x]
  58. * pointer never changes (see rules in netns/generic.h).
  59. *
  60. * That said, we simply duplicate this array and schedule
  61. * the old copy for kfree after a grace period.
  62. */
  63. memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
  64. rcu_assign_pointer(net->gen, ng);
  65. kfree_rcu(old_ng, rcu);
  66. assign:
  67. ng->ptr[id - 1] = data;
  68. return 0;
  69. }
  70. static int ops_init(const struct pernet_operations *ops, struct net *net)
  71. {
  72. int err = -ENOMEM;
  73. void *data = NULL;
  74. if (ops->id && ops->size) {
  75. data = kzalloc(ops->size, GFP_KERNEL);
  76. if (!data)
  77. goto out;
  78. err = net_assign_generic(net, *ops->id, data);
  79. if (err)
  80. goto cleanup;
  81. }
  82. err = 0;
  83. if (ops->init)
  84. err = ops->init(net);
  85. if (!err)
  86. return 0;
  87. cleanup:
  88. kfree(data);
  89. out:
  90. return err;
  91. }
  92. static void ops_free(const struct pernet_operations *ops, struct net *net)
  93. {
  94. if (ops->id && ops->size) {
  95. int id = *ops->id;
  96. kfree(net_generic(net, id));
  97. }
  98. }
  99. static void ops_exit_list(const struct pernet_operations *ops,
  100. struct list_head *net_exit_list)
  101. {
  102. struct net *net;
  103. if (ops->exit) {
  104. list_for_each_entry(net, net_exit_list, exit_list)
  105. ops->exit(net);
  106. }
  107. if (ops->exit_batch)
  108. ops->exit_batch(net_exit_list);
  109. }
  110. static void ops_free_list(const struct pernet_operations *ops,
  111. struct list_head *net_exit_list)
  112. {
  113. struct net *net;
  114. if (ops->size && ops->id) {
  115. list_for_each_entry(net, net_exit_list, exit_list)
  116. ops_free(ops, net);
  117. }
  118. }
  119. /*
  120. * setup_net runs the initializers for the network namespace object.
  121. */
  122. static __net_init int setup_net(struct net *net)
  123. {
  124. /* Must be called with net_mutex held */
  125. const struct pernet_operations *ops, *saved_ops;
  126. int error = 0;
  127. LIST_HEAD(net_exit_list);
  128. atomic_set(&net->count, 1);
  129. atomic_set(&net->passive, 1);
  130. net->dev_base_seq = 1;
  131. #ifdef NETNS_REFCNT_DEBUG
  132. atomic_set(&net->use_count, 0);
  133. #endif
  134. list_for_each_entry(ops, &pernet_list, list) {
  135. error = ops_init(ops, net);
  136. if (error < 0)
  137. goto out_undo;
  138. }
  139. out:
  140. return error;
  141. out_undo:
  142. /* Walk through the list backwards calling the exit functions
  143. * for the pernet modules whose init functions did not fail.
  144. */
  145. list_add(&net->exit_list, &net_exit_list);
  146. saved_ops = ops;
  147. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  148. ops_exit_list(ops, &net_exit_list);
  149. ops = saved_ops;
  150. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  151. ops_free_list(ops, &net_exit_list);
  152. rcu_barrier();
  153. goto out;
  154. }
  155. #ifdef CONFIG_NET_NS
  156. static struct kmem_cache *net_cachep;
  157. static struct workqueue_struct *netns_wq;
  158. static struct net *net_alloc(void)
  159. {
  160. struct net *net = NULL;
  161. struct net_generic *ng;
  162. ng = net_alloc_generic();
  163. if (!ng)
  164. goto out;
  165. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  166. if (!net)
  167. goto out_free;
  168. rcu_assign_pointer(net->gen, ng);
  169. out:
  170. return net;
  171. out_free:
  172. kfree(ng);
  173. goto out;
  174. }
  175. static void net_free(struct net *net)
  176. {
  177. #ifdef NETNS_REFCNT_DEBUG
  178. if (unlikely(atomic_read(&net->use_count) != 0)) {
  179. pr_emerg("network namespace not free! Usage: %d\n",
  180. atomic_read(&net->use_count));
  181. return;
  182. }
  183. #endif
  184. kfree(net->gen);
  185. kmem_cache_free(net_cachep, net);
  186. }
  187. void net_drop_ns(void *p)
  188. {
  189. struct net *ns = p;
  190. if (ns && atomic_dec_and_test(&ns->passive))
  191. net_free(ns);
  192. }
  193. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  194. {
  195. struct net *net;
  196. int rv;
  197. if (!(flags & CLONE_NEWNET))
  198. return get_net(old_net);
  199. net = net_alloc();
  200. if (!net)
  201. return ERR_PTR(-ENOMEM);
  202. mutex_lock(&net_mutex);
  203. rv = setup_net(net);
  204. if (rv == 0) {
  205. rtnl_lock();
  206. list_add_tail_rcu(&net->list, &net_namespace_list);
  207. rtnl_unlock();
  208. }
  209. mutex_unlock(&net_mutex);
  210. if (rv < 0) {
  211. net_drop_ns(net);
  212. return ERR_PTR(rv);
  213. }
  214. return net;
  215. }
  216. static DEFINE_SPINLOCK(cleanup_list_lock);
  217. static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
  218. static void cleanup_net(struct work_struct *work)
  219. {
  220. const struct pernet_operations *ops;
  221. struct net *net, *tmp;
  222. LIST_HEAD(net_kill_list);
  223. LIST_HEAD(net_exit_list);
  224. /* Atomically snapshot the list of namespaces to cleanup */
  225. spin_lock_irq(&cleanup_list_lock);
  226. list_replace_init(&cleanup_list, &net_kill_list);
  227. spin_unlock_irq(&cleanup_list_lock);
  228. mutex_lock(&net_mutex);
  229. /* Don't let anyone else find us. */
  230. rtnl_lock();
  231. list_for_each_entry(net, &net_kill_list, cleanup_list) {
  232. list_del_rcu(&net->list);
  233. list_add_tail(&net->exit_list, &net_exit_list);
  234. }
  235. rtnl_unlock();
  236. /*
  237. * Another CPU might be rcu-iterating the list, wait for it.
  238. * This needs to be before calling the exit() notifiers, so
  239. * the rcu_barrier() below isn't sufficient alone.
  240. */
  241. synchronize_rcu();
  242. /* Run all of the network namespace exit methods */
  243. list_for_each_entry_reverse(ops, &pernet_list, list)
  244. ops_exit_list(ops, &net_exit_list);
  245. /* Free the net generic variables */
  246. list_for_each_entry_reverse(ops, &pernet_list, list)
  247. ops_free_list(ops, &net_exit_list);
  248. mutex_unlock(&net_mutex);
  249. /* Ensure there are no outstanding rcu callbacks using this
  250. * network namespace.
  251. */
  252. rcu_barrier();
  253. /* Finally it is safe to free my network namespace structure */
  254. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  255. list_del_init(&net->exit_list);
  256. net_drop_ns(net);
  257. }
  258. }
  259. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  260. void __put_net(struct net *net)
  261. {
  262. /* Cleanup the network namespace in process context */
  263. unsigned long flags;
  264. spin_lock_irqsave(&cleanup_list_lock, flags);
  265. list_add(&net->cleanup_list, &cleanup_list);
  266. spin_unlock_irqrestore(&cleanup_list_lock, flags);
  267. queue_work(netns_wq, &net_cleanup_work);
  268. }
  269. EXPORT_SYMBOL_GPL(__put_net);
  270. struct net *get_net_ns_by_fd(int fd)
  271. {
  272. struct proc_inode *ei;
  273. struct file *file;
  274. struct net *net;
  275. file = proc_ns_fget(fd);
  276. if (IS_ERR(file))
  277. return ERR_CAST(file);
  278. ei = PROC_I(file->f_dentry->d_inode);
  279. if (ei->ns_ops == &netns_operations)
  280. net = get_net(ei->ns);
  281. else
  282. net = ERR_PTR(-EINVAL);
  283. fput(file);
  284. return net;
  285. }
  286. #else
  287. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  288. {
  289. if (flags & CLONE_NEWNET)
  290. return ERR_PTR(-EINVAL);
  291. return old_net;
  292. }
  293. struct net *get_net_ns_by_fd(int fd)
  294. {
  295. return ERR_PTR(-EINVAL);
  296. }
  297. #endif
  298. struct net *get_net_ns_by_pid(pid_t pid)
  299. {
  300. struct task_struct *tsk;
  301. struct net *net;
  302. /* Lookup the network namespace */
  303. net = ERR_PTR(-ESRCH);
  304. rcu_read_lock();
  305. tsk = find_task_by_vpid(pid);
  306. if (tsk) {
  307. struct nsproxy *nsproxy;
  308. nsproxy = task_nsproxy(tsk);
  309. if (nsproxy)
  310. net = get_net(nsproxy->net_ns);
  311. }
  312. rcu_read_unlock();
  313. return net;
  314. }
  315. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  316. static int __init net_ns_init(void)
  317. {
  318. struct net_generic *ng;
  319. #ifdef CONFIG_NET_NS
  320. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  321. SMP_CACHE_BYTES,
  322. SLAB_PANIC, NULL);
  323. /* Create workqueue for cleanup */
  324. netns_wq = create_singlethread_workqueue("netns");
  325. if (!netns_wq)
  326. panic("Could not create netns workq");
  327. #endif
  328. ng = net_alloc_generic();
  329. if (!ng)
  330. panic("Could not allocate generic netns");
  331. rcu_assign_pointer(init_net.gen, ng);
  332. mutex_lock(&net_mutex);
  333. if (setup_net(&init_net))
  334. panic("Could not setup the initial network namespace");
  335. rtnl_lock();
  336. list_add_tail_rcu(&init_net.list, &net_namespace_list);
  337. rtnl_unlock();
  338. mutex_unlock(&net_mutex);
  339. return 0;
  340. }
  341. pure_initcall(net_ns_init);
  342. #ifdef CONFIG_NET_NS
  343. static int __register_pernet_operations(struct list_head *list,
  344. struct pernet_operations *ops)
  345. {
  346. struct net *net;
  347. int error;
  348. LIST_HEAD(net_exit_list);
  349. list_add_tail(&ops->list, list);
  350. if (ops->init || (ops->id && ops->size)) {
  351. for_each_net(net) {
  352. error = ops_init(ops, net);
  353. if (error)
  354. goto out_undo;
  355. list_add_tail(&net->exit_list, &net_exit_list);
  356. }
  357. }
  358. return 0;
  359. out_undo:
  360. /* If I have an error cleanup all namespaces I initialized */
  361. list_del(&ops->list);
  362. ops_exit_list(ops, &net_exit_list);
  363. ops_free_list(ops, &net_exit_list);
  364. return error;
  365. }
  366. static void __unregister_pernet_operations(struct pernet_operations *ops)
  367. {
  368. struct net *net;
  369. LIST_HEAD(net_exit_list);
  370. list_del(&ops->list);
  371. for_each_net(net)
  372. list_add_tail(&net->exit_list, &net_exit_list);
  373. ops_exit_list(ops, &net_exit_list);
  374. ops_free_list(ops, &net_exit_list);
  375. }
  376. #else
  377. static int __register_pernet_operations(struct list_head *list,
  378. struct pernet_operations *ops)
  379. {
  380. return ops_init(ops, &init_net);
  381. }
  382. static void __unregister_pernet_operations(struct pernet_operations *ops)
  383. {
  384. LIST_HEAD(net_exit_list);
  385. list_add(&init_net.exit_list, &net_exit_list);
  386. ops_exit_list(ops, &net_exit_list);
  387. ops_free_list(ops, &net_exit_list);
  388. }
  389. #endif /* CONFIG_NET_NS */
  390. static DEFINE_IDA(net_generic_ids);
  391. static int register_pernet_operations(struct list_head *list,
  392. struct pernet_operations *ops)
  393. {
  394. int error;
  395. if (ops->id) {
  396. again:
  397. error = ida_get_new_above(&net_generic_ids, 1, ops->id);
  398. if (error < 0) {
  399. if (error == -EAGAIN) {
  400. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  401. goto again;
  402. }
  403. return error;
  404. }
  405. max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
  406. }
  407. error = __register_pernet_operations(list, ops);
  408. if (error) {
  409. rcu_barrier();
  410. if (ops->id)
  411. ida_remove(&net_generic_ids, *ops->id);
  412. }
  413. return error;
  414. }
  415. static void unregister_pernet_operations(struct pernet_operations *ops)
  416. {
  417. __unregister_pernet_operations(ops);
  418. rcu_barrier();
  419. if (ops->id)
  420. ida_remove(&net_generic_ids, *ops->id);
  421. }
  422. /**
  423. * register_pernet_subsys - register a network namespace subsystem
  424. * @ops: pernet operations structure for the subsystem
  425. *
  426. * Register a subsystem which has init and exit functions
  427. * that are called when network namespaces are created and
  428. * destroyed respectively.
  429. *
  430. * When registered all network namespace init functions are
  431. * called for every existing network namespace. Allowing kernel
  432. * modules to have a race free view of the set of network namespaces.
  433. *
  434. * When a new network namespace is created all of the init
  435. * methods are called in the order in which they were registered.
  436. *
  437. * When a network namespace is destroyed all of the exit methods
  438. * are called in the reverse of the order with which they were
  439. * registered.
  440. */
  441. int register_pernet_subsys(struct pernet_operations *ops)
  442. {
  443. int error;
  444. mutex_lock(&net_mutex);
  445. error = register_pernet_operations(first_device, ops);
  446. mutex_unlock(&net_mutex);
  447. return error;
  448. }
  449. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  450. /**
  451. * unregister_pernet_subsys - unregister a network namespace subsystem
  452. * @ops: pernet operations structure to manipulate
  453. *
  454. * Remove the pernet operations structure from the list to be
  455. * used when network namespaces are created or destroyed. In
  456. * addition run the exit method for all existing network
  457. * namespaces.
  458. */
  459. void unregister_pernet_subsys(struct pernet_operations *ops)
  460. {
  461. mutex_lock(&net_mutex);
  462. unregister_pernet_operations(ops);
  463. mutex_unlock(&net_mutex);
  464. }
  465. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  466. /**
  467. * register_pernet_device - register a network namespace device
  468. * @ops: pernet operations structure for the subsystem
  469. *
  470. * Register a device which has init and exit functions
  471. * that are called when network namespaces are created and
  472. * destroyed respectively.
  473. *
  474. * When registered all network namespace init functions are
  475. * called for every existing network namespace. Allowing kernel
  476. * modules to have a race free view of the set of network namespaces.
  477. *
  478. * When a new network namespace is created all of the init
  479. * methods are called in the order in which they were registered.
  480. *
  481. * When a network namespace is destroyed all of the exit methods
  482. * are called in the reverse of the order with which they were
  483. * registered.
  484. */
  485. int register_pernet_device(struct pernet_operations *ops)
  486. {
  487. int error;
  488. mutex_lock(&net_mutex);
  489. error = register_pernet_operations(&pernet_list, ops);
  490. if (!error && (first_device == &pernet_list))
  491. first_device = &ops->list;
  492. mutex_unlock(&net_mutex);
  493. return error;
  494. }
  495. EXPORT_SYMBOL_GPL(register_pernet_device);
  496. /**
  497. * unregister_pernet_device - unregister a network namespace netdevice
  498. * @ops: pernet operations structure to manipulate
  499. *
  500. * Remove the pernet operations structure from the list to be
  501. * used when network namespaces are created or destroyed. In
  502. * addition run the exit method for all existing network
  503. * namespaces.
  504. */
  505. void unregister_pernet_device(struct pernet_operations *ops)
  506. {
  507. mutex_lock(&net_mutex);
  508. if (&ops->list == first_device)
  509. first_device = first_device->next;
  510. unregister_pernet_operations(ops);
  511. mutex_unlock(&net_mutex);
  512. }
  513. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  514. #ifdef CONFIG_NET_NS
  515. static void *netns_get(struct task_struct *task)
  516. {
  517. struct net *net = NULL;
  518. struct nsproxy *nsproxy;
  519. rcu_read_lock();
  520. nsproxy = task_nsproxy(task);
  521. if (nsproxy)
  522. net = get_net(nsproxy->net_ns);
  523. rcu_read_unlock();
  524. return net;
  525. }
  526. static void netns_put(void *ns)
  527. {
  528. put_net(ns);
  529. }
  530. static int netns_install(struct nsproxy *nsproxy, void *ns)
  531. {
  532. put_net(nsproxy->net_ns);
  533. nsproxy->net_ns = get_net(ns);
  534. return 0;
  535. }
  536. const struct proc_ns_operations netns_operations = {
  537. .name = "net",
  538. .type = CLONE_NEWNET,
  539. .get = netns_get,
  540. .put = netns_put,
  541. .install = netns_install,
  542. };
  543. #endif