net_namespace.c 14 KB

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