net_namespace.c 13 KB

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