net_namespace.c 15 KB

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