net_namespace.c 15 KB

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