net_namespace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  183. {
  184. struct net *net;
  185. int rv;
  186. if (!(flags & CLONE_NEWNET))
  187. return get_net(old_net);
  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. static DEFINE_SPINLOCK(cleanup_list_lock);
  206. static LIST_HEAD(cleanup_list); /* Must hold cleanup_list_lock to touch */
  207. static void cleanup_net(struct work_struct *work)
  208. {
  209. const struct pernet_operations *ops;
  210. struct net *net, *tmp;
  211. LIST_HEAD(net_kill_list);
  212. LIST_HEAD(net_exit_list);
  213. /* Atomically snapshot the list of namespaces to cleanup */
  214. spin_lock_irq(&cleanup_list_lock);
  215. list_replace_init(&cleanup_list, &net_kill_list);
  216. spin_unlock_irq(&cleanup_list_lock);
  217. mutex_lock(&net_mutex);
  218. /* Don't let anyone else find us. */
  219. rtnl_lock();
  220. list_for_each_entry(net, &net_kill_list, cleanup_list) {
  221. list_del_rcu(&net->list);
  222. list_add_tail(&net->exit_list, &net_exit_list);
  223. }
  224. rtnl_unlock();
  225. /*
  226. * Another CPU might be rcu-iterating the list, wait for it.
  227. * This needs to be before calling the exit() notifiers, so
  228. * the rcu_barrier() below isn't sufficient alone.
  229. */
  230. synchronize_rcu();
  231. /* Run all of the network namespace exit methods */
  232. list_for_each_entry_reverse(ops, &pernet_list, list)
  233. ops_exit_list(ops, &net_exit_list);
  234. /* Free the net generic variables */
  235. list_for_each_entry_reverse(ops, &pernet_list, list)
  236. ops_free_list(ops, &net_exit_list);
  237. mutex_unlock(&net_mutex);
  238. /* Ensure there are no outstanding rcu callbacks using this
  239. * network namespace.
  240. */
  241. rcu_barrier();
  242. /* Finally it is safe to free my network namespace structure */
  243. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  244. list_del_init(&net->exit_list);
  245. net_free(net);
  246. }
  247. }
  248. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  249. void __put_net(struct net *net)
  250. {
  251. /* Cleanup the network namespace in process context */
  252. unsigned long flags;
  253. spin_lock_irqsave(&cleanup_list_lock, flags);
  254. list_add(&net->cleanup_list, &cleanup_list);
  255. spin_unlock_irqrestore(&cleanup_list_lock, flags);
  256. queue_work(netns_wq, &net_cleanup_work);
  257. }
  258. EXPORT_SYMBOL_GPL(__put_net);
  259. #else
  260. struct net *copy_net_ns(unsigned long flags, struct net *old_net)
  261. {
  262. if (flags & CLONE_NEWNET)
  263. return ERR_PTR(-EINVAL);
  264. return old_net;
  265. }
  266. #endif
  267. struct net *get_net_ns_by_pid(pid_t pid)
  268. {
  269. struct task_struct *tsk;
  270. struct net *net;
  271. /* Lookup the network namespace */
  272. net = ERR_PTR(-ESRCH);
  273. rcu_read_lock();
  274. tsk = find_task_by_vpid(pid);
  275. if (tsk) {
  276. struct nsproxy *nsproxy;
  277. nsproxy = task_nsproxy(tsk);
  278. if (nsproxy)
  279. net = get_net(nsproxy->net_ns);
  280. }
  281. rcu_read_unlock();
  282. return net;
  283. }
  284. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  285. static int __init net_ns_init(void)
  286. {
  287. struct net_generic *ng;
  288. #ifdef CONFIG_NET_NS
  289. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  290. SMP_CACHE_BYTES,
  291. SLAB_PANIC, NULL);
  292. /* Create workqueue for cleanup */
  293. netns_wq = create_singlethread_workqueue("netns");
  294. if (!netns_wq)
  295. panic("Could not create netns workq");
  296. #endif
  297. ng = net_alloc_generic();
  298. if (!ng)
  299. panic("Could not allocate generic netns");
  300. rcu_assign_pointer(init_net.gen, ng);
  301. mutex_lock(&net_mutex);
  302. if (setup_net(&init_net))
  303. panic("Could not setup the initial network namespace");
  304. rtnl_lock();
  305. list_add_tail_rcu(&init_net.list, &net_namespace_list);
  306. rtnl_unlock();
  307. mutex_unlock(&net_mutex);
  308. return 0;
  309. }
  310. pure_initcall(net_ns_init);
  311. #ifdef CONFIG_NET_NS
  312. static int __register_pernet_operations(struct list_head *list,
  313. struct pernet_operations *ops)
  314. {
  315. struct net *net;
  316. int error;
  317. LIST_HEAD(net_exit_list);
  318. list_add_tail(&ops->list, list);
  319. if (ops->init || (ops->id && ops->size)) {
  320. for_each_net(net) {
  321. error = ops_init(ops, net);
  322. if (error)
  323. goto out_undo;
  324. list_add_tail(&net->exit_list, &net_exit_list);
  325. }
  326. }
  327. return 0;
  328. out_undo:
  329. /* If I have an error cleanup all namespaces I initialized */
  330. list_del(&ops->list);
  331. ops_exit_list(ops, &net_exit_list);
  332. ops_free_list(ops, &net_exit_list);
  333. return error;
  334. }
  335. static void __unregister_pernet_operations(struct pernet_operations *ops)
  336. {
  337. struct net *net;
  338. LIST_HEAD(net_exit_list);
  339. list_del(&ops->list);
  340. for_each_net(net)
  341. list_add_tail(&net->exit_list, &net_exit_list);
  342. ops_exit_list(ops, &net_exit_list);
  343. ops_free_list(ops, &net_exit_list);
  344. }
  345. #else
  346. static int __register_pernet_operations(struct list_head *list,
  347. struct pernet_operations *ops)
  348. {
  349. int err = 0;
  350. err = ops_init(ops, &init_net);
  351. if (err)
  352. ops_free(ops, &init_net);
  353. return err;
  354. }
  355. static void __unregister_pernet_operations(struct pernet_operations *ops)
  356. {
  357. LIST_HEAD(net_exit_list);
  358. list_add(&init_net.exit_list, &net_exit_list);
  359. ops_exit_list(ops, &net_exit_list);
  360. ops_free_list(ops, &net_exit_list);
  361. }
  362. #endif /* CONFIG_NET_NS */
  363. static DEFINE_IDA(net_generic_ids);
  364. static int register_pernet_operations(struct list_head *list,
  365. struct pernet_operations *ops)
  366. {
  367. int error;
  368. if (ops->id) {
  369. again:
  370. error = ida_get_new_above(&net_generic_ids, 1, ops->id);
  371. if (error < 0) {
  372. if (error == -EAGAIN) {
  373. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  374. goto again;
  375. }
  376. return error;
  377. }
  378. }
  379. error = __register_pernet_operations(list, ops);
  380. if (error) {
  381. rcu_barrier();
  382. if (ops->id)
  383. ida_remove(&net_generic_ids, *ops->id);
  384. }
  385. return error;
  386. }
  387. static void unregister_pernet_operations(struct pernet_operations *ops)
  388. {
  389. __unregister_pernet_operations(ops);
  390. rcu_barrier();
  391. if (ops->id)
  392. ida_remove(&net_generic_ids, *ops->id);
  393. }
  394. /**
  395. * register_pernet_subsys - register a network namespace subsystem
  396. * @ops: pernet operations structure for the subsystem
  397. *
  398. * Register a subsystem which has init and exit functions
  399. * that are called when network namespaces are created and
  400. * destroyed respectively.
  401. *
  402. * When registered all network namespace init functions are
  403. * called for every existing network namespace. Allowing kernel
  404. * modules to have a race free view of the set of network namespaces.
  405. *
  406. * When a new network namespace is created all of the init
  407. * methods are called in the order in which they were registered.
  408. *
  409. * When a network namespace is destroyed all of the exit methods
  410. * are called in the reverse of the order with which they were
  411. * registered.
  412. */
  413. int register_pernet_subsys(struct pernet_operations *ops)
  414. {
  415. int error;
  416. mutex_lock(&net_mutex);
  417. error = register_pernet_operations(first_device, ops);
  418. mutex_unlock(&net_mutex);
  419. return error;
  420. }
  421. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  422. /**
  423. * unregister_pernet_subsys - unregister a network namespace subsystem
  424. * @ops: pernet operations structure to manipulate
  425. *
  426. * Remove the pernet operations structure from the list to be
  427. * used when network namespaces are created or destroyed. In
  428. * addition run the exit method for all existing network
  429. * namespaces.
  430. */
  431. void unregister_pernet_subsys(struct pernet_operations *ops)
  432. {
  433. mutex_lock(&net_mutex);
  434. unregister_pernet_operations(ops);
  435. mutex_unlock(&net_mutex);
  436. }
  437. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  438. /**
  439. * register_pernet_device - register a network namespace device
  440. * @ops: pernet operations structure for the subsystem
  441. *
  442. * Register a device which has init and exit functions
  443. * that are called when network namespaces are created and
  444. * destroyed respectively.
  445. *
  446. * When registered all network namespace init functions are
  447. * called for every existing network namespace. Allowing kernel
  448. * modules to have a race free view of the set of network namespaces.
  449. *
  450. * When a new network namespace is created all of the init
  451. * methods are called in the order in which they were registered.
  452. *
  453. * When a network namespace is destroyed all of the exit methods
  454. * are called in the reverse of the order with which they were
  455. * registered.
  456. */
  457. int register_pernet_device(struct pernet_operations *ops)
  458. {
  459. int error;
  460. mutex_lock(&net_mutex);
  461. error = register_pernet_operations(&pernet_list, ops);
  462. if (!error && (first_device == &pernet_list))
  463. first_device = &ops->list;
  464. mutex_unlock(&net_mutex);
  465. return error;
  466. }
  467. EXPORT_SYMBOL_GPL(register_pernet_device);
  468. /**
  469. * unregister_pernet_device - unregister a network namespace netdevice
  470. * @ops: pernet operations structure to manipulate
  471. *
  472. * Remove the pernet operations structure from the list to be
  473. * used when network namespaces are created or destroyed. In
  474. * addition run the exit method for all existing network
  475. * namespaces.
  476. */
  477. void unregister_pernet_device(struct pernet_operations *ops)
  478. {
  479. mutex_lock(&net_mutex);
  480. if (&ops->list == first_device)
  481. first_device = first_device->next;
  482. unregister_pernet_operations(ops);
  483. mutex_unlock(&net_mutex);
  484. }
  485. EXPORT_SYMBOL_GPL(unregister_pernet_device);