net_namespace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 void net_generic_release(struct rcu_head *rcu)
  27. {
  28. struct net_generic *ng;
  29. ng = container_of(rcu, struct net_generic, rcu);
  30. kfree(ng);
  31. }
  32. static int net_assign_generic(struct net *net, int id, void *data)
  33. {
  34. struct net_generic *ng, *old_ng;
  35. BUG_ON(!mutex_is_locked(&net_mutex));
  36. BUG_ON(id == 0);
  37. old_ng = rcu_dereference_protected(net->gen,
  38. lockdep_is_held(&net_mutex));
  39. ng = old_ng;
  40. if (old_ng->len >= id)
  41. goto assign;
  42. ng = kzalloc(sizeof(struct net_generic) +
  43. id * sizeof(void *), GFP_KERNEL);
  44. if (ng == NULL)
  45. return -ENOMEM;
  46. /*
  47. * Some synchronisation notes:
  48. *
  49. * The net_generic explores the net->gen array inside rcu
  50. * read section. Besides once set the net->gen->ptr[x]
  51. * pointer never changes (see rules in netns/generic.h).
  52. *
  53. * That said, we simply duplicate this array and schedule
  54. * the old copy for kfree after a grace period.
  55. */
  56. ng->len = id;
  57. memcpy(&ng->ptr, &old_ng->ptr, old_ng->len * sizeof(void*));
  58. rcu_assign_pointer(net->gen, ng);
  59. call_rcu(&old_ng->rcu, net_generic_release);
  60. assign:
  61. ng->ptr[id - 1] = data;
  62. return 0;
  63. }
  64. static int ops_init(const struct pernet_operations *ops, struct net *net)
  65. {
  66. int err;
  67. if (ops->id && ops->size) {
  68. void *data = kzalloc(ops->size, GFP_KERNEL);
  69. if (!data)
  70. return -ENOMEM;
  71. err = net_assign_generic(net, *ops->id, data);
  72. if (err) {
  73. kfree(data);
  74. return err;
  75. }
  76. }
  77. if (ops->init)
  78. return ops->init(net);
  79. return 0;
  80. }
  81. static void ops_free(const struct pernet_operations *ops, struct net *net)
  82. {
  83. if (ops->id && ops->size) {
  84. int id = *ops->id;
  85. kfree(net_generic(net, id));
  86. }
  87. }
  88. static void ops_exit_list(const struct pernet_operations *ops,
  89. struct list_head *net_exit_list)
  90. {
  91. struct net *net;
  92. if (ops->exit) {
  93. list_for_each_entry(net, net_exit_list, exit_list)
  94. ops->exit(net);
  95. }
  96. if (ops->exit_batch)
  97. ops->exit_batch(net_exit_list);
  98. }
  99. static void ops_free_list(const struct pernet_operations *ops,
  100. struct list_head *net_exit_list)
  101. {
  102. struct net *net;
  103. if (ops->size && ops->id) {
  104. list_for_each_entry(net, net_exit_list, exit_list)
  105. ops_free(ops, net);
  106. }
  107. }
  108. /*
  109. * setup_net runs the initializers for the network namespace object.
  110. */
  111. static __net_init int setup_net(struct net *net)
  112. {
  113. /* Must be called with net_mutex held */
  114. const struct pernet_operations *ops, *saved_ops;
  115. int error = 0;
  116. LIST_HEAD(net_exit_list);
  117. atomic_set(&net->count, 1);
  118. #ifdef NETNS_REFCNT_DEBUG
  119. atomic_set(&net->use_count, 0);
  120. #endif
  121. list_for_each_entry(ops, &pernet_list, list) {
  122. error = ops_init(ops, net);
  123. if (error < 0)
  124. goto out_undo;
  125. }
  126. out:
  127. return error;
  128. out_undo:
  129. /* Walk through the list backwards calling the exit functions
  130. * for the pernet modules whose init functions did not fail.
  131. */
  132. list_add(&net->exit_list, &net_exit_list);
  133. saved_ops = ops;
  134. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  135. ops_exit_list(ops, &net_exit_list);
  136. ops = saved_ops;
  137. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  138. ops_free_list(ops, &net_exit_list);
  139. rcu_barrier();
  140. goto out;
  141. }
  142. static struct net_generic *net_alloc_generic(void)
  143. {
  144. struct net_generic *ng;
  145. size_t generic_size = sizeof(struct net_generic) +
  146. INITIAL_NET_GEN_PTRS * sizeof(void *);
  147. ng = kzalloc(generic_size, GFP_KERNEL);
  148. if (ng)
  149. ng->len = INITIAL_NET_GEN_PTRS;
  150. return ng;
  151. }
  152. #ifdef CONFIG_NET_NS
  153. static struct kmem_cache *net_cachep;
  154. static struct workqueue_struct *netns_wq;
  155. static struct net *net_alloc(void)
  156. {
  157. struct net *net = NULL;
  158. struct net_generic *ng;
  159. ng = net_alloc_generic();
  160. if (!ng)
  161. goto out;
  162. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  163. if (!net)
  164. goto out_free;
  165. rcu_assign_pointer(net->gen, ng);
  166. out:
  167. return net;
  168. out_free:
  169. kfree(ng);
  170. goto out;
  171. }
  172. static void net_free(struct net *net)
  173. {
  174. #ifdef NETNS_REFCNT_DEBUG
  175. if (unlikely(atomic_read(&net->use_count) != 0)) {
  176. printk(KERN_EMERG "network namespace not free! Usage: %d\n",
  177. atomic_read(&net->use_count));
  178. return;
  179. }
  180. #endif
  181. kfree(net->gen);
  182. kmem_cache_free(net_cachep, net);
  183. }
  184. static struct net *net_create(void)
  185. {
  186. struct net *net;
  187. int rv;
  188. net = net_alloc();
  189. if (!net)
  190. return ERR_PTR(-ENOMEM);
  191. mutex_lock(&net_mutex);
  192. rv = setup_net(net);
  193. if (rv == 0) {
  194. rtnl_lock();
  195. list_add_tail_rcu(&net->list, &net_namespace_list);
  196. rtnl_unlock();
  197. }
  198. mutex_unlock(&net_mutex);
  199. if (rv < 0) {
  200. net_free(net);
  201. return ERR_PTR(rv);
  202. }
  203. return net;
  204. }
  205. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  206. {
  207. if (!(flags & CLONE_NEWNET))
  208. return get_net(old_net);
  209. return net_create();
  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_free(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. net = ERR_PTR(-EINVAL);
  271. file = proc_ns_fget(fd);
  272. if (!file)
  273. goto out;
  274. ei = PROC_I(file->f_dentry->d_inode);
  275. if (ei->ns_ops != &netns_operations)
  276. goto out;
  277. net = get_net(ei->ns);
  278. out:
  279. if (file)
  280. fput(file);
  281. return net;
  282. }
  283. #else
  284. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  285. {
  286. if (flags & CLONE_NEWNET)
  287. return ERR_PTR(-EINVAL);
  288. return old_net;
  289. }
  290. struct net *get_net_ns_by_fd(int fd)
  291. {
  292. return ERR_PTR(-EINVAL);
  293. }
  294. #endif
  295. struct net *get_net_ns_by_pid(pid_t pid)
  296. {
  297. struct task_struct *tsk;
  298. struct net *net;
  299. /* Lookup the network namespace */
  300. net = ERR_PTR(-ESRCH);
  301. rcu_read_lock();
  302. tsk = find_task_by_vpid(pid);
  303. if (tsk) {
  304. struct nsproxy *nsproxy;
  305. nsproxy = task_nsproxy(tsk);
  306. if (nsproxy)
  307. net = get_net(nsproxy->net_ns);
  308. }
  309. rcu_read_unlock();
  310. return net;
  311. }
  312. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  313. static int __init net_ns_init(void)
  314. {
  315. struct net_generic *ng;
  316. #ifdef CONFIG_NET_NS
  317. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  318. SMP_CACHE_BYTES,
  319. SLAB_PANIC, NULL);
  320. /* Create workqueue for cleanup */
  321. netns_wq = create_singlethread_workqueue("netns");
  322. if (!netns_wq)
  323. panic("Could not create netns workq");
  324. #endif
  325. ng = net_alloc_generic();
  326. if (!ng)
  327. panic("Could not allocate generic netns");
  328. rcu_assign_pointer(init_net.gen, ng);
  329. mutex_lock(&net_mutex);
  330. if (setup_net(&init_net))
  331. panic("Could not setup the initial network namespace");
  332. rtnl_lock();
  333. list_add_tail_rcu(&init_net.list, &net_namespace_list);
  334. rtnl_unlock();
  335. mutex_unlock(&net_mutex);
  336. return 0;
  337. }
  338. pure_initcall(net_ns_init);
  339. #ifdef CONFIG_NET_NS
  340. static int __register_pernet_operations(struct list_head *list,
  341. struct pernet_operations *ops)
  342. {
  343. struct net *net;
  344. int error;
  345. LIST_HEAD(net_exit_list);
  346. list_add_tail(&ops->list, list);
  347. if (ops->init || (ops->id && ops->size)) {
  348. for_each_net(net) {
  349. error = ops_init(ops, net);
  350. if (error)
  351. goto out_undo;
  352. list_add_tail(&net->exit_list, &net_exit_list);
  353. }
  354. }
  355. return 0;
  356. out_undo:
  357. /* If I have an error cleanup all namespaces I initialized */
  358. list_del(&ops->list);
  359. ops_exit_list(ops, &net_exit_list);
  360. ops_free_list(ops, &net_exit_list);
  361. return error;
  362. }
  363. static void __unregister_pernet_operations(struct pernet_operations *ops)
  364. {
  365. struct net *net;
  366. LIST_HEAD(net_exit_list);
  367. list_del(&ops->list);
  368. for_each_net(net)
  369. list_add_tail(&net->exit_list, &net_exit_list);
  370. ops_exit_list(ops, &net_exit_list);
  371. ops_free_list(ops, &net_exit_list);
  372. }
  373. #else
  374. static int __register_pernet_operations(struct list_head *list,
  375. struct pernet_operations *ops)
  376. {
  377. int err = 0;
  378. err = ops_init(ops, &init_net);
  379. if (err)
  380. ops_free(ops, &init_net);
  381. return err;
  382. }
  383. static void __unregister_pernet_operations(struct pernet_operations *ops)
  384. {
  385. LIST_HEAD(net_exit_list);
  386. list_add(&init_net.exit_list, &net_exit_list);
  387. ops_exit_list(ops, &net_exit_list);
  388. ops_free_list(ops, &net_exit_list);
  389. }
  390. #endif /* CONFIG_NET_NS */
  391. static DEFINE_IDA(net_generic_ids);
  392. static int register_pernet_operations(struct list_head *list,
  393. struct pernet_operations *ops)
  394. {
  395. int error;
  396. if (ops->id) {
  397. again:
  398. error = ida_get_new_above(&net_generic_ids, 1, ops->id);
  399. if (error < 0) {
  400. if (error == -EAGAIN) {
  401. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  402. goto again;
  403. }
  404. return error;
  405. }
  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