net_namespace.c 14 KB

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