net_namespace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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->f_dentry->d_inode);
  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 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, &init_user_ns))
  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