net_namespace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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. 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(rcu_access_pointer(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. struct 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 ns_common *ns;
  282. struct net *net;
  283. file = proc_ns_fget(fd);
  284. if (IS_ERR(file))
  285. return ERR_CAST(file);
  286. ei = get_proc_ns(file_inode(file));
  287. ns = ei->ns;
  288. if (ns->ops == &netns_operations)
  289. net = get_net(container_of(ns, struct net, ns));
  290. else
  291. net = ERR_PTR(-EINVAL);
  292. fput(file);
  293. return net;
  294. }
  295. #else
  296. struct net *get_net_ns_by_fd(int fd)
  297. {
  298. return ERR_PTR(-EINVAL);
  299. }
  300. #endif
  301. struct net *get_net_ns_by_pid(pid_t pid)
  302. {
  303. struct task_struct *tsk;
  304. struct net *net;
  305. /* Lookup the network namespace */
  306. net = ERR_PTR(-ESRCH);
  307. rcu_read_lock();
  308. tsk = find_task_by_vpid(pid);
  309. if (tsk) {
  310. struct nsproxy *nsproxy;
  311. task_lock(tsk);
  312. nsproxy = tsk->nsproxy;
  313. if (nsproxy)
  314. net = get_net(nsproxy->net_ns);
  315. task_unlock(tsk);
  316. }
  317. rcu_read_unlock();
  318. return net;
  319. }
  320. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  321. static __net_init int net_ns_net_init(struct net *net)
  322. {
  323. #ifdef CONFIG_NET_NS
  324. net->ns.ops = &netns_operations;
  325. #endif
  326. return ns_alloc_inum(&net->ns);
  327. }
  328. static __net_exit void net_ns_net_exit(struct net *net)
  329. {
  330. ns_free_inum(&net->ns);
  331. }
  332. static struct pernet_operations __net_initdata net_ns_ops = {
  333. .init = net_ns_net_init,
  334. .exit = net_ns_net_exit,
  335. };
  336. static int __init net_ns_init(void)
  337. {
  338. struct net_generic *ng;
  339. #ifdef CONFIG_NET_NS
  340. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  341. SMP_CACHE_BYTES,
  342. SLAB_PANIC, NULL);
  343. /* Create workqueue for cleanup */
  344. netns_wq = create_singlethread_workqueue("netns");
  345. if (!netns_wq)
  346. panic("Could not create netns workq");
  347. #endif
  348. ng = net_alloc_generic();
  349. if (!ng)
  350. panic("Could not allocate generic netns");
  351. rcu_assign_pointer(init_net.gen, ng);
  352. mutex_lock(&net_mutex);
  353. if (setup_net(&init_net, &init_user_ns))
  354. panic("Could not setup the initial network namespace");
  355. rtnl_lock();
  356. list_add_tail_rcu(&init_net.list, &net_namespace_list);
  357. rtnl_unlock();
  358. mutex_unlock(&net_mutex);
  359. register_pernet_subsys(&net_ns_ops);
  360. return 0;
  361. }
  362. pure_initcall(net_ns_init);
  363. #ifdef CONFIG_NET_NS
  364. static int __register_pernet_operations(struct list_head *list,
  365. struct pernet_operations *ops)
  366. {
  367. struct net *net;
  368. int error;
  369. LIST_HEAD(net_exit_list);
  370. list_add_tail(&ops->list, list);
  371. if (ops->init || (ops->id && ops->size)) {
  372. for_each_net(net) {
  373. error = ops_init(ops, net);
  374. if (error)
  375. goto out_undo;
  376. list_add_tail(&net->exit_list, &net_exit_list);
  377. }
  378. }
  379. return 0;
  380. out_undo:
  381. /* If I have an error cleanup all namespaces I initialized */
  382. list_del(&ops->list);
  383. ops_exit_list(ops, &net_exit_list);
  384. ops_free_list(ops, &net_exit_list);
  385. return error;
  386. }
  387. static void __unregister_pernet_operations(struct pernet_operations *ops)
  388. {
  389. struct net *net;
  390. LIST_HEAD(net_exit_list);
  391. list_del(&ops->list);
  392. for_each_net(net)
  393. list_add_tail(&net->exit_list, &net_exit_list);
  394. ops_exit_list(ops, &net_exit_list);
  395. ops_free_list(ops, &net_exit_list);
  396. }
  397. #else
  398. static int __register_pernet_operations(struct list_head *list,
  399. struct pernet_operations *ops)
  400. {
  401. return ops_init(ops, &init_net);
  402. }
  403. static void __unregister_pernet_operations(struct pernet_operations *ops)
  404. {
  405. LIST_HEAD(net_exit_list);
  406. list_add(&init_net.exit_list, &net_exit_list);
  407. ops_exit_list(ops, &net_exit_list);
  408. ops_free_list(ops, &net_exit_list);
  409. }
  410. #endif /* CONFIG_NET_NS */
  411. static DEFINE_IDA(net_generic_ids);
  412. static int register_pernet_operations(struct list_head *list,
  413. struct pernet_operations *ops)
  414. {
  415. int error;
  416. if (ops->id) {
  417. again:
  418. error = ida_get_new_above(&net_generic_ids, 1, ops->id);
  419. if (error < 0) {
  420. if (error == -EAGAIN) {
  421. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  422. goto again;
  423. }
  424. return error;
  425. }
  426. max_gen_ptrs = max_t(unsigned int, max_gen_ptrs, *ops->id);
  427. }
  428. error = __register_pernet_operations(list, ops);
  429. if (error) {
  430. rcu_barrier();
  431. if (ops->id)
  432. ida_remove(&net_generic_ids, *ops->id);
  433. }
  434. return error;
  435. }
  436. static void unregister_pernet_operations(struct pernet_operations *ops)
  437. {
  438. __unregister_pernet_operations(ops);
  439. rcu_barrier();
  440. if (ops->id)
  441. ida_remove(&net_generic_ids, *ops->id);
  442. }
  443. /**
  444. * register_pernet_subsys - register a network namespace subsystem
  445. * @ops: pernet operations structure for the subsystem
  446. *
  447. * Register a subsystem which has init and exit functions
  448. * that are called when network namespaces are created and
  449. * destroyed respectively.
  450. *
  451. * When registered all network namespace init functions are
  452. * called for every existing network namespace. Allowing kernel
  453. * modules to have a race free view of the set of network namespaces.
  454. *
  455. * When a new network namespace is created all of the init
  456. * methods are called in the order in which they were registered.
  457. *
  458. * When a network namespace is destroyed all of the exit methods
  459. * are called in the reverse of the order with which they were
  460. * registered.
  461. */
  462. int register_pernet_subsys(struct pernet_operations *ops)
  463. {
  464. int error;
  465. mutex_lock(&net_mutex);
  466. error = register_pernet_operations(first_device, ops);
  467. mutex_unlock(&net_mutex);
  468. return error;
  469. }
  470. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  471. /**
  472. * unregister_pernet_subsys - unregister a network namespace subsystem
  473. * @ops: pernet operations structure to manipulate
  474. *
  475. * Remove the pernet operations structure from the list to be
  476. * used when network namespaces are created or destroyed. In
  477. * addition run the exit method for all existing network
  478. * namespaces.
  479. */
  480. void unregister_pernet_subsys(struct pernet_operations *ops)
  481. {
  482. mutex_lock(&net_mutex);
  483. unregister_pernet_operations(ops);
  484. mutex_unlock(&net_mutex);
  485. }
  486. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  487. /**
  488. * register_pernet_device - register a network namespace device
  489. * @ops: pernet operations structure for the subsystem
  490. *
  491. * Register a device which has init and exit functions
  492. * that are called when network namespaces are created and
  493. * destroyed respectively.
  494. *
  495. * When registered all network namespace init functions are
  496. * called for every existing network namespace. Allowing kernel
  497. * modules to have a race free view of the set of network namespaces.
  498. *
  499. * When a new network namespace is created all of the init
  500. * methods are called in the order in which they were registered.
  501. *
  502. * When a network namespace is destroyed all of the exit methods
  503. * are called in the reverse of the order with which they were
  504. * registered.
  505. */
  506. int register_pernet_device(struct pernet_operations *ops)
  507. {
  508. int error;
  509. mutex_lock(&net_mutex);
  510. error = register_pernet_operations(&pernet_list, ops);
  511. if (!error && (first_device == &pernet_list))
  512. first_device = &ops->list;
  513. mutex_unlock(&net_mutex);
  514. return error;
  515. }
  516. EXPORT_SYMBOL_GPL(register_pernet_device);
  517. /**
  518. * unregister_pernet_device - unregister a network namespace netdevice
  519. * @ops: pernet operations structure to manipulate
  520. *
  521. * Remove the pernet operations structure from the list to be
  522. * used when network namespaces are created or destroyed. In
  523. * addition run the exit method for all existing network
  524. * namespaces.
  525. */
  526. void unregister_pernet_device(struct pernet_operations *ops)
  527. {
  528. mutex_lock(&net_mutex);
  529. if (&ops->list == first_device)
  530. first_device = first_device->next;
  531. unregister_pernet_operations(ops);
  532. mutex_unlock(&net_mutex);
  533. }
  534. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  535. #ifdef CONFIG_NET_NS
  536. static struct ns_common *netns_get(struct task_struct *task)
  537. {
  538. struct net *net = NULL;
  539. struct nsproxy *nsproxy;
  540. task_lock(task);
  541. nsproxy = task->nsproxy;
  542. if (nsproxy)
  543. net = get_net(nsproxy->net_ns);
  544. task_unlock(task);
  545. return net ? &net->ns : NULL;
  546. }
  547. static inline struct net *to_net_ns(struct ns_common *ns)
  548. {
  549. return container_of(ns, struct net, ns);
  550. }
  551. static void netns_put(struct ns_common *ns)
  552. {
  553. put_net(to_net_ns(ns));
  554. }
  555. static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  556. {
  557. struct net *net = to_net_ns(ns);
  558. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  559. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  560. return -EPERM;
  561. put_net(nsproxy->net_ns);
  562. nsproxy->net_ns = get_net(net);
  563. return 0;
  564. }
  565. const struct proc_ns_operations netns_operations = {
  566. .name = "net",
  567. .type = CLONE_NEWNET,
  568. .get = netns_get,
  569. .put = netns_put,
  570. .install = netns_install,
  571. };
  572. #endif