net_namespace.c 14 KB

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