net_namespace.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <linux/workqueue.h>
  3. #include <linux/rtnetlink.h>
  4. #include <linux/cache.h>
  5. #include <linux/slab.h>
  6. #include <linux/list.h>
  7. #include <linux/delay.h>
  8. #include <linux/sched.h>
  9. #include <linux/idr.h>
  10. #include <linux/rculist.h>
  11. #include <linux/nsproxy.h>
  12. #include <linux/fs.h>
  13. #include <linux/proc_ns.h>
  14. #include <linux/file.h>
  15. #include <linux/export.h>
  16. #include <linux/user_namespace.h>
  17. #include <linux/net_namespace.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/uidgid.h>
  20. #include <net/sock.h>
  21. #include <net/netlink.h>
  22. #include <net/net_namespace.h>
  23. #include <net/netns/generic.h>
  24. /*
  25. * Our network namespace constructor/destructor lists
  26. */
  27. static LIST_HEAD(pernet_list);
  28. static struct list_head *first_device = &pernet_list;
  29. LIST_HEAD(net_namespace_list);
  30. EXPORT_SYMBOL_GPL(net_namespace_list);
  31. /* Protects net_namespace_list. Nests iside rtnl_lock() */
  32. DECLARE_RWSEM(net_rwsem);
  33. EXPORT_SYMBOL_GPL(net_rwsem);
  34. struct net init_net = {
  35. .count = REFCOUNT_INIT(1),
  36. .dev_base_head = LIST_HEAD_INIT(init_net.dev_base_head),
  37. };
  38. EXPORT_SYMBOL(init_net);
  39. static bool init_net_initialized;
  40. /*
  41. * pernet_ops_rwsem: protects: pernet_list, net_generic_ids,
  42. * init_net_initialized and first_device pointer.
  43. * This is internal net namespace object. Please, don't use it
  44. * outside.
  45. */
  46. DECLARE_RWSEM(pernet_ops_rwsem);
  47. EXPORT_SYMBOL_GPL(pernet_ops_rwsem);
  48. #define MIN_PERNET_OPS_ID \
  49. ((sizeof(struct net_generic) + sizeof(void *) - 1) / sizeof(void *))
  50. #define INITIAL_NET_GEN_PTRS 13 /* +1 for len +2 for rcu_head */
  51. static unsigned int max_gen_ptrs = INITIAL_NET_GEN_PTRS;
  52. static struct net_generic *net_alloc_generic(void)
  53. {
  54. struct net_generic *ng;
  55. unsigned int generic_size = offsetof(struct net_generic, ptr[max_gen_ptrs]);
  56. ng = kzalloc(generic_size, GFP_KERNEL);
  57. if (ng)
  58. ng->s.len = max_gen_ptrs;
  59. return ng;
  60. }
  61. static int net_assign_generic(struct net *net, unsigned int id, void *data)
  62. {
  63. struct net_generic *ng, *old_ng;
  64. BUG_ON(id < MIN_PERNET_OPS_ID);
  65. old_ng = rcu_dereference_protected(net->gen,
  66. lockdep_is_held(&pernet_ops_rwsem));
  67. if (old_ng->s.len > id) {
  68. old_ng->ptr[id] = data;
  69. return 0;
  70. }
  71. ng = net_alloc_generic();
  72. if (ng == NULL)
  73. return -ENOMEM;
  74. /*
  75. * Some synchronisation notes:
  76. *
  77. * The net_generic explores the net->gen array inside rcu
  78. * read section. Besides once set the net->gen->ptr[x]
  79. * pointer never changes (see rules in netns/generic.h).
  80. *
  81. * That said, we simply duplicate this array and schedule
  82. * the old copy for kfree after a grace period.
  83. */
  84. memcpy(&ng->ptr[MIN_PERNET_OPS_ID], &old_ng->ptr[MIN_PERNET_OPS_ID],
  85. (old_ng->s.len - MIN_PERNET_OPS_ID) * sizeof(void *));
  86. ng->ptr[id] = data;
  87. rcu_assign_pointer(net->gen, ng);
  88. kfree_rcu(old_ng, s.rcu);
  89. return 0;
  90. }
  91. static int ops_init(const struct pernet_operations *ops, struct net *net)
  92. {
  93. int err = -ENOMEM;
  94. void *data = NULL;
  95. if (ops->id && ops->size) {
  96. data = kzalloc(ops->size, GFP_KERNEL);
  97. if (!data)
  98. goto out;
  99. err = net_assign_generic(net, *ops->id, data);
  100. if (err)
  101. goto cleanup;
  102. }
  103. err = 0;
  104. if (ops->init)
  105. err = ops->init(net);
  106. if (!err)
  107. return 0;
  108. cleanup:
  109. kfree(data);
  110. out:
  111. return err;
  112. }
  113. static void ops_free(const struct pernet_operations *ops, struct net *net)
  114. {
  115. if (ops->id && ops->size) {
  116. kfree(net_generic(net, *ops->id));
  117. }
  118. }
  119. static void ops_exit_list(const struct pernet_operations *ops,
  120. struct list_head *net_exit_list)
  121. {
  122. struct net *net;
  123. if (ops->exit) {
  124. list_for_each_entry(net, net_exit_list, exit_list)
  125. ops->exit(net);
  126. }
  127. if (ops->exit_batch)
  128. ops->exit_batch(net_exit_list);
  129. }
  130. static void ops_free_list(const struct pernet_operations *ops,
  131. struct list_head *net_exit_list)
  132. {
  133. struct net *net;
  134. if (ops->size && ops->id) {
  135. list_for_each_entry(net, net_exit_list, exit_list)
  136. ops_free(ops, net);
  137. }
  138. }
  139. /* should be called with nsid_lock held */
  140. static int alloc_netid(struct net *net, struct net *peer, int reqid)
  141. {
  142. int min = 0, max = 0;
  143. if (reqid >= 0) {
  144. min = reqid;
  145. max = reqid + 1;
  146. }
  147. return idr_alloc(&net->netns_ids, peer, min, max, GFP_ATOMIC);
  148. }
  149. /* This function is used by idr_for_each(). If net is equal to peer, the
  150. * function returns the id so that idr_for_each() stops. Because we cannot
  151. * returns the id 0 (idr_for_each() will not stop), we return the magic value
  152. * NET_ID_ZERO (-1) for it.
  153. */
  154. #define NET_ID_ZERO -1
  155. static int net_eq_idr(int id, void *net, void *peer)
  156. {
  157. if (net_eq(net, peer))
  158. return id ? : NET_ID_ZERO;
  159. return 0;
  160. }
  161. /* Should be called with nsid_lock held. If a new id is assigned, the bool alloc
  162. * is set to true, thus the caller knows that the new id must be notified via
  163. * rtnl.
  164. */
  165. static int __peernet2id_alloc(struct net *net, struct net *peer, bool *alloc)
  166. {
  167. int id = idr_for_each(&net->netns_ids, net_eq_idr, peer);
  168. bool alloc_it = *alloc;
  169. *alloc = false;
  170. /* Magic value for id 0. */
  171. if (id == NET_ID_ZERO)
  172. return 0;
  173. if (id > 0)
  174. return id;
  175. if (alloc_it) {
  176. id = alloc_netid(net, peer, -1);
  177. *alloc = true;
  178. return id >= 0 ? id : NETNSA_NSID_NOT_ASSIGNED;
  179. }
  180. return NETNSA_NSID_NOT_ASSIGNED;
  181. }
  182. /* should be called with nsid_lock held */
  183. static int __peernet2id(struct net *net, struct net *peer)
  184. {
  185. bool no = false;
  186. return __peernet2id_alloc(net, peer, &no);
  187. }
  188. static void rtnl_net_notifyid(struct net *net, int cmd, int id);
  189. /* This function returns the id of a peer netns. If no id is assigned, one will
  190. * be allocated and returned.
  191. */
  192. int peernet2id_alloc(struct net *net, struct net *peer)
  193. {
  194. bool alloc = false, alive = false;
  195. int id;
  196. if (refcount_read(&net->count) == 0)
  197. return NETNSA_NSID_NOT_ASSIGNED;
  198. spin_lock_bh(&net->nsid_lock);
  199. /*
  200. * When peer is obtained from RCU lists, we may race with
  201. * its cleanup. Check whether it's alive, and this guarantees
  202. * we never hash a peer back to net->netns_ids, after it has
  203. * just been idr_remove()'d from there in cleanup_net().
  204. */
  205. if (maybe_get_net(peer))
  206. alive = alloc = true;
  207. id = __peernet2id_alloc(net, peer, &alloc);
  208. spin_unlock_bh(&net->nsid_lock);
  209. if (alloc && id >= 0)
  210. rtnl_net_notifyid(net, RTM_NEWNSID, id);
  211. if (alive)
  212. put_net(peer);
  213. return id;
  214. }
  215. EXPORT_SYMBOL_GPL(peernet2id_alloc);
  216. /* This function returns, if assigned, the id of a peer netns. */
  217. int peernet2id(struct net *net, struct net *peer)
  218. {
  219. int id;
  220. spin_lock_bh(&net->nsid_lock);
  221. id = __peernet2id(net, peer);
  222. spin_unlock_bh(&net->nsid_lock);
  223. return id;
  224. }
  225. EXPORT_SYMBOL(peernet2id);
  226. /* This function returns true is the peer netns has an id assigned into the
  227. * current netns.
  228. */
  229. bool peernet_has_id(struct net *net, struct net *peer)
  230. {
  231. return peernet2id(net, peer) >= 0;
  232. }
  233. struct net *get_net_ns_by_id(struct net *net, int id)
  234. {
  235. struct net *peer;
  236. if (id < 0)
  237. return NULL;
  238. rcu_read_lock();
  239. peer = idr_find(&net->netns_ids, id);
  240. if (peer)
  241. peer = maybe_get_net(peer);
  242. rcu_read_unlock();
  243. return peer;
  244. }
  245. /*
  246. * setup_net runs the initializers for the network namespace object.
  247. */
  248. static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
  249. {
  250. /* Must be called with pernet_ops_rwsem held */
  251. const struct pernet_operations *ops, *saved_ops;
  252. int error = 0;
  253. LIST_HEAD(net_exit_list);
  254. refcount_set(&net->count, 1);
  255. refcount_set(&net->passive, 1);
  256. net->dev_base_seq = 1;
  257. net->user_ns = user_ns;
  258. idr_init(&net->netns_ids);
  259. spin_lock_init(&net->nsid_lock);
  260. mutex_init(&net->ipv4.ra_mutex);
  261. list_for_each_entry(ops, &pernet_list, list) {
  262. error = ops_init(ops, net);
  263. if (error < 0)
  264. goto out_undo;
  265. }
  266. down_write(&net_rwsem);
  267. list_add_tail_rcu(&net->list, &net_namespace_list);
  268. up_write(&net_rwsem);
  269. out:
  270. return error;
  271. out_undo:
  272. /* Walk through the list backwards calling the exit functions
  273. * for the pernet modules whose init functions did not fail.
  274. */
  275. list_add(&net->exit_list, &net_exit_list);
  276. saved_ops = ops;
  277. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  278. ops_exit_list(ops, &net_exit_list);
  279. ops = saved_ops;
  280. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  281. ops_free_list(ops, &net_exit_list);
  282. rcu_barrier();
  283. goto out;
  284. }
  285. static int __net_init net_defaults_init_net(struct net *net)
  286. {
  287. net->core.sysctl_somaxconn = SOMAXCONN;
  288. return 0;
  289. }
  290. static struct pernet_operations net_defaults_ops = {
  291. .init = net_defaults_init_net,
  292. };
  293. static __init int net_defaults_init(void)
  294. {
  295. if (register_pernet_subsys(&net_defaults_ops))
  296. panic("Cannot initialize net default settings");
  297. return 0;
  298. }
  299. core_initcall(net_defaults_init);
  300. #ifdef CONFIG_NET_NS
  301. static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
  302. {
  303. return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
  304. }
  305. static void dec_net_namespaces(struct ucounts *ucounts)
  306. {
  307. dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
  308. }
  309. static struct kmem_cache *net_cachep __ro_after_init;
  310. static struct workqueue_struct *netns_wq;
  311. static struct net *net_alloc(void)
  312. {
  313. struct net *net = NULL;
  314. struct net_generic *ng;
  315. ng = net_alloc_generic();
  316. if (!ng)
  317. goto out;
  318. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  319. if (!net)
  320. goto out_free;
  321. rcu_assign_pointer(net->gen, ng);
  322. out:
  323. return net;
  324. out_free:
  325. kfree(ng);
  326. goto out;
  327. }
  328. static void net_free(struct net *net)
  329. {
  330. kfree(rcu_access_pointer(net->gen));
  331. kmem_cache_free(net_cachep, net);
  332. }
  333. void net_drop_ns(void *p)
  334. {
  335. struct net *ns = p;
  336. if (ns && refcount_dec_and_test(&ns->passive))
  337. net_free(ns);
  338. }
  339. struct net *copy_net_ns(unsigned long flags,
  340. struct user_namespace *user_ns, struct net *old_net)
  341. {
  342. struct ucounts *ucounts;
  343. struct net *net;
  344. int rv;
  345. if (!(flags & CLONE_NEWNET))
  346. return get_net(old_net);
  347. ucounts = inc_net_namespaces(user_ns);
  348. if (!ucounts)
  349. return ERR_PTR(-ENOSPC);
  350. net = net_alloc();
  351. if (!net) {
  352. rv = -ENOMEM;
  353. goto dec_ucounts;
  354. }
  355. refcount_set(&net->passive, 1);
  356. net->ucounts = ucounts;
  357. get_user_ns(user_ns);
  358. rv = down_read_killable(&pernet_ops_rwsem);
  359. if (rv < 0)
  360. goto put_userns;
  361. rv = setup_net(net, user_ns);
  362. up_read(&pernet_ops_rwsem);
  363. if (rv < 0) {
  364. put_userns:
  365. put_user_ns(user_ns);
  366. net_drop_ns(net);
  367. dec_ucounts:
  368. dec_net_namespaces(ucounts);
  369. return ERR_PTR(rv);
  370. }
  371. return net;
  372. }
  373. /**
  374. * net_ns_get_ownership - get sysfs ownership data for @net
  375. * @net: network namespace in question (can be NULL)
  376. * @uid: kernel user ID for sysfs objects
  377. * @gid: kernel group ID for sysfs objects
  378. *
  379. * Returns the uid/gid pair of root in the user namespace associated with the
  380. * given network namespace.
  381. */
  382. void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
  383. {
  384. if (net) {
  385. kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
  386. kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
  387. if (uid_valid(ns_root_uid))
  388. *uid = ns_root_uid;
  389. if (gid_valid(ns_root_gid))
  390. *gid = ns_root_gid;
  391. } else {
  392. *uid = GLOBAL_ROOT_UID;
  393. *gid = GLOBAL_ROOT_GID;
  394. }
  395. }
  396. EXPORT_SYMBOL_GPL(net_ns_get_ownership);
  397. static void unhash_nsid(struct net *net, struct net *last)
  398. {
  399. struct net *tmp;
  400. /* This function is only called from cleanup_net() work,
  401. * and this work is the only process, that may delete
  402. * a net from net_namespace_list. So, when the below
  403. * is executing, the list may only grow. Thus, we do not
  404. * use for_each_net_rcu() or net_rwsem.
  405. */
  406. for_each_net(tmp) {
  407. int id;
  408. spin_lock_bh(&tmp->nsid_lock);
  409. id = __peernet2id(tmp, net);
  410. if (id >= 0)
  411. idr_remove(&tmp->netns_ids, id);
  412. spin_unlock_bh(&tmp->nsid_lock);
  413. if (id >= 0)
  414. rtnl_net_notifyid(tmp, RTM_DELNSID, id);
  415. if (tmp == last)
  416. break;
  417. }
  418. spin_lock_bh(&net->nsid_lock);
  419. idr_destroy(&net->netns_ids);
  420. spin_unlock_bh(&net->nsid_lock);
  421. }
  422. static LLIST_HEAD(cleanup_list);
  423. static void cleanup_net(struct work_struct *work)
  424. {
  425. const struct pernet_operations *ops;
  426. struct net *net, *tmp, *last;
  427. struct llist_node *net_kill_list;
  428. LIST_HEAD(net_exit_list);
  429. /* Atomically snapshot the list of namespaces to cleanup */
  430. net_kill_list = llist_del_all(&cleanup_list);
  431. down_read(&pernet_ops_rwsem);
  432. /* Don't let anyone else find us. */
  433. down_write(&net_rwsem);
  434. llist_for_each_entry(net, net_kill_list, cleanup_list)
  435. list_del_rcu(&net->list);
  436. /* Cache last net. After we unlock rtnl, no one new net
  437. * added to net_namespace_list can assign nsid pointer
  438. * to a net from net_kill_list (see peernet2id_alloc()).
  439. * So, we skip them in unhash_nsid().
  440. *
  441. * Note, that unhash_nsid() does not delete nsid links
  442. * between net_kill_list's nets, as they've already
  443. * deleted from net_namespace_list. But, this would be
  444. * useless anyway, as netns_ids are destroyed there.
  445. */
  446. last = list_last_entry(&net_namespace_list, struct net, list);
  447. up_write(&net_rwsem);
  448. llist_for_each_entry(net, net_kill_list, cleanup_list) {
  449. unhash_nsid(net, last);
  450. list_add_tail(&net->exit_list, &net_exit_list);
  451. }
  452. /*
  453. * Another CPU might be rcu-iterating the list, wait for it.
  454. * This needs to be before calling the exit() notifiers, so
  455. * the rcu_barrier() below isn't sufficient alone.
  456. */
  457. synchronize_rcu();
  458. /* Run all of the network namespace exit methods */
  459. list_for_each_entry_reverse(ops, &pernet_list, list)
  460. ops_exit_list(ops, &net_exit_list);
  461. /* Free the net generic variables */
  462. list_for_each_entry_reverse(ops, &pernet_list, list)
  463. ops_free_list(ops, &net_exit_list);
  464. up_read(&pernet_ops_rwsem);
  465. /* Ensure there are no outstanding rcu callbacks using this
  466. * network namespace.
  467. */
  468. rcu_barrier();
  469. /* Finally it is safe to free my network namespace structure */
  470. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  471. list_del_init(&net->exit_list);
  472. dec_net_namespaces(net->ucounts);
  473. put_user_ns(net->user_ns);
  474. net_drop_ns(net);
  475. }
  476. }
  477. /**
  478. * net_ns_barrier - wait until concurrent net_cleanup_work is done
  479. *
  480. * cleanup_net runs from work queue and will first remove namespaces
  481. * from the global list, then run net exit functions.
  482. *
  483. * Call this in module exit path to make sure that all netns
  484. * ->exit ops have been invoked before the function is removed.
  485. */
  486. void net_ns_barrier(void)
  487. {
  488. down_write(&pernet_ops_rwsem);
  489. up_write(&pernet_ops_rwsem);
  490. }
  491. EXPORT_SYMBOL(net_ns_barrier);
  492. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  493. void __put_net(struct net *net)
  494. {
  495. /* Cleanup the network namespace in process context */
  496. if (llist_add(&net->cleanup_list, &cleanup_list))
  497. queue_work(netns_wq, &net_cleanup_work);
  498. }
  499. EXPORT_SYMBOL_GPL(__put_net);
  500. struct net *get_net_ns_by_fd(int fd)
  501. {
  502. struct file *file;
  503. struct ns_common *ns;
  504. struct net *net;
  505. file = proc_ns_fget(fd);
  506. if (IS_ERR(file))
  507. return ERR_CAST(file);
  508. ns = get_proc_ns(file_inode(file));
  509. if (ns->ops == &netns_operations)
  510. net = get_net(container_of(ns, struct net, ns));
  511. else
  512. net = ERR_PTR(-EINVAL);
  513. fput(file);
  514. return net;
  515. }
  516. #else
  517. struct net *get_net_ns_by_fd(int fd)
  518. {
  519. return ERR_PTR(-EINVAL);
  520. }
  521. #endif
  522. EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
  523. struct net *get_net_ns_by_pid(pid_t pid)
  524. {
  525. struct task_struct *tsk;
  526. struct net *net;
  527. /* Lookup the network namespace */
  528. net = ERR_PTR(-ESRCH);
  529. rcu_read_lock();
  530. tsk = find_task_by_vpid(pid);
  531. if (tsk) {
  532. struct nsproxy *nsproxy;
  533. task_lock(tsk);
  534. nsproxy = tsk->nsproxy;
  535. if (nsproxy)
  536. net = get_net(nsproxy->net_ns);
  537. task_unlock(tsk);
  538. }
  539. rcu_read_unlock();
  540. return net;
  541. }
  542. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  543. static __net_init int net_ns_net_init(struct net *net)
  544. {
  545. #ifdef CONFIG_NET_NS
  546. net->ns.ops = &netns_operations;
  547. #endif
  548. return ns_alloc_inum(&net->ns);
  549. }
  550. static __net_exit void net_ns_net_exit(struct net *net)
  551. {
  552. ns_free_inum(&net->ns);
  553. }
  554. static struct pernet_operations __net_initdata net_ns_ops = {
  555. .init = net_ns_net_init,
  556. .exit = net_ns_net_exit,
  557. };
  558. static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
  559. [NETNSA_NONE] = { .type = NLA_UNSPEC },
  560. [NETNSA_NSID] = { .type = NLA_S32 },
  561. [NETNSA_PID] = { .type = NLA_U32 },
  562. [NETNSA_FD] = { .type = NLA_U32 },
  563. };
  564. static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
  565. struct netlink_ext_ack *extack)
  566. {
  567. struct net *net = sock_net(skb->sk);
  568. struct nlattr *tb[NETNSA_MAX + 1];
  569. struct nlattr *nla;
  570. struct net *peer;
  571. int nsid, err;
  572. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  573. rtnl_net_policy, extack);
  574. if (err < 0)
  575. return err;
  576. if (!tb[NETNSA_NSID]) {
  577. NL_SET_ERR_MSG(extack, "nsid is missing");
  578. return -EINVAL;
  579. }
  580. nsid = nla_get_s32(tb[NETNSA_NSID]);
  581. if (tb[NETNSA_PID]) {
  582. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  583. nla = tb[NETNSA_PID];
  584. } else if (tb[NETNSA_FD]) {
  585. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  586. nla = tb[NETNSA_FD];
  587. } else {
  588. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  589. return -EINVAL;
  590. }
  591. if (IS_ERR(peer)) {
  592. NL_SET_BAD_ATTR(extack, nla);
  593. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  594. return PTR_ERR(peer);
  595. }
  596. spin_lock_bh(&net->nsid_lock);
  597. if (__peernet2id(net, peer) >= 0) {
  598. spin_unlock_bh(&net->nsid_lock);
  599. err = -EEXIST;
  600. NL_SET_BAD_ATTR(extack, nla);
  601. NL_SET_ERR_MSG(extack,
  602. "Peer netns already has a nsid assigned");
  603. goto out;
  604. }
  605. err = alloc_netid(net, peer, nsid);
  606. spin_unlock_bh(&net->nsid_lock);
  607. if (err >= 0) {
  608. rtnl_net_notifyid(net, RTM_NEWNSID, err);
  609. err = 0;
  610. } else if (err == -ENOSPC && nsid >= 0) {
  611. err = -EEXIST;
  612. NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
  613. NL_SET_ERR_MSG(extack, "The specified nsid is already used");
  614. }
  615. out:
  616. put_net(peer);
  617. return err;
  618. }
  619. static int rtnl_net_get_size(void)
  620. {
  621. return NLMSG_ALIGN(sizeof(struct rtgenmsg))
  622. + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
  623. ;
  624. }
  625. static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  626. int cmd, struct net *net, int nsid)
  627. {
  628. struct nlmsghdr *nlh;
  629. struct rtgenmsg *rth;
  630. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
  631. if (!nlh)
  632. return -EMSGSIZE;
  633. rth = nlmsg_data(nlh);
  634. rth->rtgen_family = AF_UNSPEC;
  635. if (nla_put_s32(skb, NETNSA_NSID, nsid))
  636. goto nla_put_failure;
  637. nlmsg_end(skb, nlh);
  638. return 0;
  639. nla_put_failure:
  640. nlmsg_cancel(skb, nlh);
  641. return -EMSGSIZE;
  642. }
  643. static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
  644. struct netlink_ext_ack *extack)
  645. {
  646. struct net *net = sock_net(skb->sk);
  647. struct nlattr *tb[NETNSA_MAX + 1];
  648. struct nlattr *nla;
  649. struct sk_buff *msg;
  650. struct net *peer;
  651. int err, id;
  652. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  653. rtnl_net_policy, extack);
  654. if (err < 0)
  655. return err;
  656. if (tb[NETNSA_PID]) {
  657. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  658. nla = tb[NETNSA_PID];
  659. } else if (tb[NETNSA_FD]) {
  660. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  661. nla = tb[NETNSA_FD];
  662. } else {
  663. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  664. return -EINVAL;
  665. }
  666. if (IS_ERR(peer)) {
  667. NL_SET_BAD_ATTR(extack, nla);
  668. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  669. return PTR_ERR(peer);
  670. }
  671. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  672. if (!msg) {
  673. err = -ENOMEM;
  674. goto out;
  675. }
  676. id = peernet2id(net, peer);
  677. err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  678. RTM_NEWNSID, net, id);
  679. if (err < 0)
  680. goto err_out;
  681. err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
  682. goto out;
  683. err_out:
  684. nlmsg_free(msg);
  685. out:
  686. put_net(peer);
  687. return err;
  688. }
  689. struct rtnl_net_dump_cb {
  690. struct net *net;
  691. struct sk_buff *skb;
  692. struct netlink_callback *cb;
  693. int idx;
  694. int s_idx;
  695. };
  696. static int rtnl_net_dumpid_one(int id, void *peer, void *data)
  697. {
  698. struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
  699. int ret;
  700. if (net_cb->idx < net_cb->s_idx)
  701. goto cont;
  702. ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
  703. net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  704. RTM_NEWNSID, net_cb->net, id);
  705. if (ret < 0)
  706. return ret;
  707. cont:
  708. net_cb->idx++;
  709. return 0;
  710. }
  711. static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
  712. {
  713. struct net *net = sock_net(skb->sk);
  714. struct rtnl_net_dump_cb net_cb = {
  715. .net = net,
  716. .skb = skb,
  717. .cb = cb,
  718. .idx = 0,
  719. .s_idx = cb->args[0],
  720. };
  721. spin_lock_bh(&net->nsid_lock);
  722. idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
  723. spin_unlock_bh(&net->nsid_lock);
  724. cb->args[0] = net_cb.idx;
  725. return skb->len;
  726. }
  727. static void rtnl_net_notifyid(struct net *net, int cmd, int id)
  728. {
  729. struct sk_buff *msg;
  730. int err = -ENOMEM;
  731. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  732. if (!msg)
  733. goto out;
  734. err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
  735. if (err < 0)
  736. goto err_out;
  737. rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
  738. return;
  739. err_out:
  740. nlmsg_free(msg);
  741. out:
  742. rtnl_set_sk_err(net, RTNLGRP_NSID, err);
  743. }
  744. static int __init net_ns_init(void)
  745. {
  746. struct net_generic *ng;
  747. #ifdef CONFIG_NET_NS
  748. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  749. SMP_CACHE_BYTES,
  750. SLAB_PANIC|SLAB_ACCOUNT, NULL);
  751. /* Create workqueue for cleanup */
  752. netns_wq = create_singlethread_workqueue("netns");
  753. if (!netns_wq)
  754. panic("Could not create netns workq");
  755. #endif
  756. ng = net_alloc_generic();
  757. if (!ng)
  758. panic("Could not allocate generic netns");
  759. rcu_assign_pointer(init_net.gen, ng);
  760. down_write(&pernet_ops_rwsem);
  761. if (setup_net(&init_net, &init_user_ns))
  762. panic("Could not setup the initial network namespace");
  763. init_net_initialized = true;
  764. up_write(&pernet_ops_rwsem);
  765. register_pernet_subsys(&net_ns_ops);
  766. rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
  767. RTNL_FLAG_DOIT_UNLOCKED);
  768. rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
  769. RTNL_FLAG_DOIT_UNLOCKED);
  770. return 0;
  771. }
  772. pure_initcall(net_ns_init);
  773. #ifdef CONFIG_NET_NS
  774. static int __register_pernet_operations(struct list_head *list,
  775. struct pernet_operations *ops)
  776. {
  777. struct net *net;
  778. int error;
  779. LIST_HEAD(net_exit_list);
  780. list_add_tail(&ops->list, list);
  781. if (ops->init || (ops->id && ops->size)) {
  782. /* We held write locked pernet_ops_rwsem, and parallel
  783. * setup_net() and cleanup_net() are not possible.
  784. */
  785. for_each_net(net) {
  786. error = ops_init(ops, net);
  787. if (error)
  788. goto out_undo;
  789. list_add_tail(&net->exit_list, &net_exit_list);
  790. }
  791. }
  792. return 0;
  793. out_undo:
  794. /* If I have an error cleanup all namespaces I initialized */
  795. list_del(&ops->list);
  796. ops_exit_list(ops, &net_exit_list);
  797. ops_free_list(ops, &net_exit_list);
  798. return error;
  799. }
  800. static void __unregister_pernet_operations(struct pernet_operations *ops)
  801. {
  802. struct net *net;
  803. LIST_HEAD(net_exit_list);
  804. list_del(&ops->list);
  805. /* See comment in __register_pernet_operations() */
  806. for_each_net(net)
  807. list_add_tail(&net->exit_list, &net_exit_list);
  808. ops_exit_list(ops, &net_exit_list);
  809. ops_free_list(ops, &net_exit_list);
  810. }
  811. #else
  812. static int __register_pernet_operations(struct list_head *list,
  813. struct pernet_operations *ops)
  814. {
  815. if (!init_net_initialized) {
  816. list_add_tail(&ops->list, list);
  817. return 0;
  818. }
  819. return ops_init(ops, &init_net);
  820. }
  821. static void __unregister_pernet_operations(struct pernet_operations *ops)
  822. {
  823. if (!init_net_initialized) {
  824. list_del(&ops->list);
  825. } else {
  826. LIST_HEAD(net_exit_list);
  827. list_add(&init_net.exit_list, &net_exit_list);
  828. ops_exit_list(ops, &net_exit_list);
  829. ops_free_list(ops, &net_exit_list);
  830. }
  831. }
  832. #endif /* CONFIG_NET_NS */
  833. static DEFINE_IDA(net_generic_ids);
  834. static int register_pernet_operations(struct list_head *list,
  835. struct pernet_operations *ops)
  836. {
  837. int error;
  838. if (ops->id) {
  839. again:
  840. error = ida_get_new_above(&net_generic_ids, MIN_PERNET_OPS_ID, ops->id);
  841. if (error < 0) {
  842. if (error == -EAGAIN) {
  843. ida_pre_get(&net_generic_ids, GFP_KERNEL);
  844. goto again;
  845. }
  846. return error;
  847. }
  848. max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
  849. }
  850. error = __register_pernet_operations(list, ops);
  851. if (error) {
  852. rcu_barrier();
  853. if (ops->id)
  854. ida_remove(&net_generic_ids, *ops->id);
  855. }
  856. return error;
  857. }
  858. static void unregister_pernet_operations(struct pernet_operations *ops)
  859. {
  860. __unregister_pernet_operations(ops);
  861. rcu_barrier();
  862. if (ops->id)
  863. ida_remove(&net_generic_ids, *ops->id);
  864. }
  865. /**
  866. * register_pernet_subsys - register a network namespace subsystem
  867. * @ops: pernet operations structure for the subsystem
  868. *
  869. * Register a subsystem which has init and exit functions
  870. * that are called when network namespaces are created and
  871. * destroyed respectively.
  872. *
  873. * When registered all network namespace init functions are
  874. * called for every existing network namespace. Allowing kernel
  875. * modules to have a race free view of the set of network namespaces.
  876. *
  877. * When a new network namespace is created all of the init
  878. * methods are called in the order in which they were registered.
  879. *
  880. * When a network namespace is destroyed all of the exit methods
  881. * are called in the reverse of the order with which they were
  882. * registered.
  883. */
  884. int register_pernet_subsys(struct pernet_operations *ops)
  885. {
  886. int error;
  887. down_write(&pernet_ops_rwsem);
  888. error = register_pernet_operations(first_device, ops);
  889. up_write(&pernet_ops_rwsem);
  890. return error;
  891. }
  892. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  893. /**
  894. * unregister_pernet_subsys - unregister a network namespace subsystem
  895. * @ops: pernet operations structure to manipulate
  896. *
  897. * Remove the pernet operations structure from the list to be
  898. * used when network namespaces are created or destroyed. In
  899. * addition run the exit method for all existing network
  900. * namespaces.
  901. */
  902. void unregister_pernet_subsys(struct pernet_operations *ops)
  903. {
  904. down_write(&pernet_ops_rwsem);
  905. unregister_pernet_operations(ops);
  906. up_write(&pernet_ops_rwsem);
  907. }
  908. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  909. /**
  910. * register_pernet_device - register a network namespace device
  911. * @ops: pernet operations structure for the subsystem
  912. *
  913. * Register a device which has init and exit functions
  914. * that are called when network namespaces are created and
  915. * destroyed respectively.
  916. *
  917. * When registered all network namespace init functions are
  918. * called for every existing network namespace. Allowing kernel
  919. * modules to have a race free view of the set of network namespaces.
  920. *
  921. * When a new network namespace is created all of the init
  922. * methods are called in the order in which they were registered.
  923. *
  924. * When a network namespace is destroyed all of the exit methods
  925. * are called in the reverse of the order with which they were
  926. * registered.
  927. */
  928. int register_pernet_device(struct pernet_operations *ops)
  929. {
  930. int error;
  931. down_write(&pernet_ops_rwsem);
  932. error = register_pernet_operations(&pernet_list, ops);
  933. if (!error && (first_device == &pernet_list))
  934. first_device = &ops->list;
  935. up_write(&pernet_ops_rwsem);
  936. return error;
  937. }
  938. EXPORT_SYMBOL_GPL(register_pernet_device);
  939. /**
  940. * unregister_pernet_device - unregister a network namespace netdevice
  941. * @ops: pernet operations structure to manipulate
  942. *
  943. * Remove the pernet operations structure from the list to be
  944. * used when network namespaces are created or destroyed. In
  945. * addition run the exit method for all existing network
  946. * namespaces.
  947. */
  948. void unregister_pernet_device(struct pernet_operations *ops)
  949. {
  950. down_write(&pernet_ops_rwsem);
  951. if (&ops->list == first_device)
  952. first_device = first_device->next;
  953. unregister_pernet_operations(ops);
  954. up_write(&pernet_ops_rwsem);
  955. }
  956. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  957. #ifdef CONFIG_NET_NS
  958. static struct ns_common *netns_get(struct task_struct *task)
  959. {
  960. struct net *net = NULL;
  961. struct nsproxy *nsproxy;
  962. task_lock(task);
  963. nsproxy = task->nsproxy;
  964. if (nsproxy)
  965. net = get_net(nsproxy->net_ns);
  966. task_unlock(task);
  967. return net ? &net->ns : NULL;
  968. }
  969. static inline struct net *to_net_ns(struct ns_common *ns)
  970. {
  971. return container_of(ns, struct net, ns);
  972. }
  973. static void netns_put(struct ns_common *ns)
  974. {
  975. put_net(to_net_ns(ns));
  976. }
  977. static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  978. {
  979. struct net *net = to_net_ns(ns);
  980. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  981. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  982. return -EPERM;
  983. put_net(nsproxy->net_ns);
  984. nsproxy->net_ns = get_net(net);
  985. return 0;
  986. }
  987. static struct user_namespace *netns_owner(struct ns_common *ns)
  988. {
  989. return to_net_ns(ns)->user_ns;
  990. }
  991. const struct proc_ns_operations netns_operations = {
  992. .name = "net",
  993. .type = CLONE_NEWNET,
  994. .get = netns_get,
  995. .put = netns_put,
  996. .install = netns_install,
  997. .owner = netns_owner,
  998. };
  999. #endif