net_namespace.c 14 KB

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