net_namespace.c 14 KB

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