net_namespace.c 13 KB

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