net_namespace.c 15 KB

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