net_namespace.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  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. get_random_bytes(&net->hash_mix, sizeof(u32));
  257. net->dev_base_seq = 1;
  258. net->user_ns = user_ns;
  259. idr_init(&net->netns_ids);
  260. spin_lock_init(&net->nsid_lock);
  261. mutex_init(&net->ipv4.ra_mutex);
  262. list_for_each_entry(ops, &pernet_list, list) {
  263. error = ops_init(ops, net);
  264. if (error < 0)
  265. goto out_undo;
  266. }
  267. down_write(&net_rwsem);
  268. list_add_tail_rcu(&net->list, &net_namespace_list);
  269. up_write(&net_rwsem);
  270. out:
  271. return error;
  272. out_undo:
  273. /* Walk through the list backwards calling the exit functions
  274. * for the pernet modules whose init functions did not fail.
  275. */
  276. list_add(&net->exit_list, &net_exit_list);
  277. saved_ops = ops;
  278. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  279. ops_exit_list(ops, &net_exit_list);
  280. ops = saved_ops;
  281. list_for_each_entry_continue_reverse(ops, &pernet_list, list)
  282. ops_free_list(ops, &net_exit_list);
  283. rcu_barrier();
  284. goto out;
  285. }
  286. static int __net_init net_defaults_init_net(struct net *net)
  287. {
  288. net->core.sysctl_somaxconn = SOMAXCONN;
  289. return 0;
  290. }
  291. static struct pernet_operations net_defaults_ops = {
  292. .init = net_defaults_init_net,
  293. };
  294. static __init int net_defaults_init(void)
  295. {
  296. if (register_pernet_subsys(&net_defaults_ops))
  297. panic("Cannot initialize net default settings");
  298. return 0;
  299. }
  300. core_initcall(net_defaults_init);
  301. #ifdef CONFIG_NET_NS
  302. static struct ucounts *inc_net_namespaces(struct user_namespace *ns)
  303. {
  304. return inc_ucount(ns, current_euid(), UCOUNT_NET_NAMESPACES);
  305. }
  306. static void dec_net_namespaces(struct ucounts *ucounts)
  307. {
  308. dec_ucount(ucounts, UCOUNT_NET_NAMESPACES);
  309. }
  310. static struct kmem_cache *net_cachep __ro_after_init;
  311. static struct workqueue_struct *netns_wq;
  312. static struct net *net_alloc(void)
  313. {
  314. struct net *net = NULL;
  315. struct net_generic *ng;
  316. ng = net_alloc_generic();
  317. if (!ng)
  318. goto out;
  319. net = kmem_cache_zalloc(net_cachep, GFP_KERNEL);
  320. if (!net)
  321. goto out_free;
  322. rcu_assign_pointer(net->gen, ng);
  323. out:
  324. return net;
  325. out_free:
  326. kfree(ng);
  327. goto out;
  328. }
  329. static void net_free(struct net *net)
  330. {
  331. kfree(rcu_access_pointer(net->gen));
  332. kmem_cache_free(net_cachep, net);
  333. }
  334. void net_drop_ns(void *p)
  335. {
  336. struct net *ns = p;
  337. if (ns && refcount_dec_and_test(&ns->passive))
  338. net_free(ns);
  339. }
  340. struct net *copy_net_ns(unsigned long flags,
  341. struct user_namespace *user_ns, struct net *old_net)
  342. {
  343. struct ucounts *ucounts;
  344. struct net *net;
  345. int rv;
  346. if (!(flags & CLONE_NEWNET))
  347. return get_net(old_net);
  348. ucounts = inc_net_namespaces(user_ns);
  349. if (!ucounts)
  350. return ERR_PTR(-ENOSPC);
  351. net = net_alloc();
  352. if (!net) {
  353. rv = -ENOMEM;
  354. goto dec_ucounts;
  355. }
  356. refcount_set(&net->passive, 1);
  357. net->ucounts = ucounts;
  358. get_user_ns(user_ns);
  359. rv = down_read_killable(&pernet_ops_rwsem);
  360. if (rv < 0)
  361. goto put_userns;
  362. rv = setup_net(net, user_ns);
  363. up_read(&pernet_ops_rwsem);
  364. if (rv < 0) {
  365. put_userns:
  366. put_user_ns(user_ns);
  367. net_drop_ns(net);
  368. dec_ucounts:
  369. dec_net_namespaces(ucounts);
  370. return ERR_PTR(rv);
  371. }
  372. return net;
  373. }
  374. /**
  375. * net_ns_get_ownership - get sysfs ownership data for @net
  376. * @net: network namespace in question (can be NULL)
  377. * @uid: kernel user ID for sysfs objects
  378. * @gid: kernel group ID for sysfs objects
  379. *
  380. * Returns the uid/gid pair of root in the user namespace associated with the
  381. * given network namespace.
  382. */
  383. void net_ns_get_ownership(const struct net *net, kuid_t *uid, kgid_t *gid)
  384. {
  385. if (net) {
  386. kuid_t ns_root_uid = make_kuid(net->user_ns, 0);
  387. kgid_t ns_root_gid = make_kgid(net->user_ns, 0);
  388. if (uid_valid(ns_root_uid))
  389. *uid = ns_root_uid;
  390. if (gid_valid(ns_root_gid))
  391. *gid = ns_root_gid;
  392. } else {
  393. *uid = GLOBAL_ROOT_UID;
  394. *gid = GLOBAL_ROOT_GID;
  395. }
  396. }
  397. EXPORT_SYMBOL_GPL(net_ns_get_ownership);
  398. static void unhash_nsid(struct net *net, struct net *last)
  399. {
  400. struct net *tmp;
  401. /* This function is only called from cleanup_net() work,
  402. * and this work is the only process, that may delete
  403. * a net from net_namespace_list. So, when the below
  404. * is executing, the list may only grow. Thus, we do not
  405. * use for_each_net_rcu() or net_rwsem.
  406. */
  407. for_each_net(tmp) {
  408. int id;
  409. spin_lock_bh(&tmp->nsid_lock);
  410. id = __peernet2id(tmp, net);
  411. if (id >= 0)
  412. idr_remove(&tmp->netns_ids, id);
  413. spin_unlock_bh(&tmp->nsid_lock);
  414. if (id >= 0)
  415. rtnl_net_notifyid(tmp, RTM_DELNSID, id);
  416. if (tmp == last)
  417. break;
  418. }
  419. spin_lock_bh(&net->nsid_lock);
  420. idr_destroy(&net->netns_ids);
  421. spin_unlock_bh(&net->nsid_lock);
  422. }
  423. static LLIST_HEAD(cleanup_list);
  424. static void cleanup_net(struct work_struct *work)
  425. {
  426. const struct pernet_operations *ops;
  427. struct net *net, *tmp, *last;
  428. struct llist_node *net_kill_list;
  429. LIST_HEAD(net_exit_list);
  430. /* Atomically snapshot the list of namespaces to cleanup */
  431. net_kill_list = llist_del_all(&cleanup_list);
  432. down_read(&pernet_ops_rwsem);
  433. /* Don't let anyone else find us. */
  434. down_write(&net_rwsem);
  435. llist_for_each_entry(net, net_kill_list, cleanup_list)
  436. list_del_rcu(&net->list);
  437. /* Cache last net. After we unlock rtnl, no one new net
  438. * added to net_namespace_list can assign nsid pointer
  439. * to a net from net_kill_list (see peernet2id_alloc()).
  440. * So, we skip them in unhash_nsid().
  441. *
  442. * Note, that unhash_nsid() does not delete nsid links
  443. * between net_kill_list's nets, as they've already
  444. * deleted from net_namespace_list. But, this would be
  445. * useless anyway, as netns_ids are destroyed there.
  446. */
  447. last = list_last_entry(&net_namespace_list, struct net, list);
  448. up_write(&net_rwsem);
  449. llist_for_each_entry(net, net_kill_list, cleanup_list) {
  450. unhash_nsid(net, last);
  451. list_add_tail(&net->exit_list, &net_exit_list);
  452. }
  453. /*
  454. * Another CPU might be rcu-iterating the list, wait for it.
  455. * This needs to be before calling the exit() notifiers, so
  456. * the rcu_barrier() below isn't sufficient alone.
  457. */
  458. synchronize_rcu();
  459. /* Run all of the network namespace exit methods */
  460. list_for_each_entry_reverse(ops, &pernet_list, list)
  461. ops_exit_list(ops, &net_exit_list);
  462. /* Free the net generic variables */
  463. list_for_each_entry_reverse(ops, &pernet_list, list)
  464. ops_free_list(ops, &net_exit_list);
  465. up_read(&pernet_ops_rwsem);
  466. /* Ensure there are no outstanding rcu callbacks using this
  467. * network namespace.
  468. */
  469. rcu_barrier();
  470. /* Finally it is safe to free my network namespace structure */
  471. list_for_each_entry_safe(net, tmp, &net_exit_list, exit_list) {
  472. list_del_init(&net->exit_list);
  473. dec_net_namespaces(net->ucounts);
  474. put_user_ns(net->user_ns);
  475. net_drop_ns(net);
  476. }
  477. }
  478. /**
  479. * net_ns_barrier - wait until concurrent net_cleanup_work is done
  480. *
  481. * cleanup_net runs from work queue and will first remove namespaces
  482. * from the global list, then run net exit functions.
  483. *
  484. * Call this in module exit path to make sure that all netns
  485. * ->exit ops have been invoked before the function is removed.
  486. */
  487. void net_ns_barrier(void)
  488. {
  489. down_write(&pernet_ops_rwsem);
  490. up_write(&pernet_ops_rwsem);
  491. }
  492. EXPORT_SYMBOL(net_ns_barrier);
  493. static DECLARE_WORK(net_cleanup_work, cleanup_net);
  494. void __put_net(struct net *net)
  495. {
  496. /* Cleanup the network namespace in process context */
  497. if (llist_add(&net->cleanup_list, &cleanup_list))
  498. queue_work(netns_wq, &net_cleanup_work);
  499. }
  500. EXPORT_SYMBOL_GPL(__put_net);
  501. struct net *get_net_ns_by_fd(int fd)
  502. {
  503. struct file *file;
  504. struct ns_common *ns;
  505. struct net *net;
  506. file = proc_ns_fget(fd);
  507. if (IS_ERR(file))
  508. return ERR_CAST(file);
  509. ns = get_proc_ns(file_inode(file));
  510. if (ns->ops == &netns_operations)
  511. net = get_net(container_of(ns, struct net, ns));
  512. else
  513. net = ERR_PTR(-EINVAL);
  514. fput(file);
  515. return net;
  516. }
  517. #else
  518. struct net *get_net_ns_by_fd(int fd)
  519. {
  520. return ERR_PTR(-EINVAL);
  521. }
  522. #endif
  523. EXPORT_SYMBOL_GPL(get_net_ns_by_fd);
  524. struct net *get_net_ns_by_pid(pid_t pid)
  525. {
  526. struct task_struct *tsk;
  527. struct net *net;
  528. /* Lookup the network namespace */
  529. net = ERR_PTR(-ESRCH);
  530. rcu_read_lock();
  531. tsk = find_task_by_vpid(pid);
  532. if (tsk) {
  533. struct nsproxy *nsproxy;
  534. task_lock(tsk);
  535. nsproxy = tsk->nsproxy;
  536. if (nsproxy)
  537. net = get_net(nsproxy->net_ns);
  538. task_unlock(tsk);
  539. }
  540. rcu_read_unlock();
  541. return net;
  542. }
  543. EXPORT_SYMBOL_GPL(get_net_ns_by_pid);
  544. static __net_init int net_ns_net_init(struct net *net)
  545. {
  546. #ifdef CONFIG_NET_NS
  547. net->ns.ops = &netns_operations;
  548. #endif
  549. return ns_alloc_inum(&net->ns);
  550. }
  551. static __net_exit void net_ns_net_exit(struct net *net)
  552. {
  553. ns_free_inum(&net->ns);
  554. }
  555. static struct pernet_operations __net_initdata net_ns_ops = {
  556. .init = net_ns_net_init,
  557. .exit = net_ns_net_exit,
  558. };
  559. static const struct nla_policy rtnl_net_policy[NETNSA_MAX + 1] = {
  560. [NETNSA_NONE] = { .type = NLA_UNSPEC },
  561. [NETNSA_NSID] = { .type = NLA_S32 },
  562. [NETNSA_PID] = { .type = NLA_U32 },
  563. [NETNSA_FD] = { .type = NLA_U32 },
  564. };
  565. static int rtnl_net_newid(struct sk_buff *skb, struct nlmsghdr *nlh,
  566. struct netlink_ext_ack *extack)
  567. {
  568. struct net *net = sock_net(skb->sk);
  569. struct nlattr *tb[NETNSA_MAX + 1];
  570. struct nlattr *nla;
  571. struct net *peer;
  572. int nsid, err;
  573. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  574. rtnl_net_policy, extack);
  575. if (err < 0)
  576. return err;
  577. if (!tb[NETNSA_NSID]) {
  578. NL_SET_ERR_MSG(extack, "nsid is missing");
  579. return -EINVAL;
  580. }
  581. nsid = nla_get_s32(tb[NETNSA_NSID]);
  582. if (tb[NETNSA_PID]) {
  583. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  584. nla = tb[NETNSA_PID];
  585. } else if (tb[NETNSA_FD]) {
  586. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  587. nla = tb[NETNSA_FD];
  588. } else {
  589. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  590. return -EINVAL;
  591. }
  592. if (IS_ERR(peer)) {
  593. NL_SET_BAD_ATTR(extack, nla);
  594. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  595. return PTR_ERR(peer);
  596. }
  597. spin_lock_bh(&net->nsid_lock);
  598. if (__peernet2id(net, peer) >= 0) {
  599. spin_unlock_bh(&net->nsid_lock);
  600. err = -EEXIST;
  601. NL_SET_BAD_ATTR(extack, nla);
  602. NL_SET_ERR_MSG(extack,
  603. "Peer netns already has a nsid assigned");
  604. goto out;
  605. }
  606. err = alloc_netid(net, peer, nsid);
  607. spin_unlock_bh(&net->nsid_lock);
  608. if (err >= 0) {
  609. rtnl_net_notifyid(net, RTM_NEWNSID, err);
  610. err = 0;
  611. } else if (err == -ENOSPC && nsid >= 0) {
  612. err = -EEXIST;
  613. NL_SET_BAD_ATTR(extack, tb[NETNSA_NSID]);
  614. NL_SET_ERR_MSG(extack, "The specified nsid is already used");
  615. }
  616. out:
  617. put_net(peer);
  618. return err;
  619. }
  620. static int rtnl_net_get_size(void)
  621. {
  622. return NLMSG_ALIGN(sizeof(struct rtgenmsg))
  623. + nla_total_size(sizeof(s32)) /* NETNSA_NSID */
  624. ;
  625. }
  626. static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
  627. int cmd, struct net *net, int nsid)
  628. {
  629. struct nlmsghdr *nlh;
  630. struct rtgenmsg *rth;
  631. nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rth), flags);
  632. if (!nlh)
  633. return -EMSGSIZE;
  634. rth = nlmsg_data(nlh);
  635. rth->rtgen_family = AF_UNSPEC;
  636. if (nla_put_s32(skb, NETNSA_NSID, nsid))
  637. goto nla_put_failure;
  638. nlmsg_end(skb, nlh);
  639. return 0;
  640. nla_put_failure:
  641. nlmsg_cancel(skb, nlh);
  642. return -EMSGSIZE;
  643. }
  644. static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh,
  645. struct netlink_ext_ack *extack)
  646. {
  647. struct net *net = sock_net(skb->sk);
  648. struct nlattr *tb[NETNSA_MAX + 1];
  649. struct nlattr *nla;
  650. struct sk_buff *msg;
  651. struct net *peer;
  652. int err, id;
  653. err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
  654. rtnl_net_policy, extack);
  655. if (err < 0)
  656. return err;
  657. if (tb[NETNSA_PID]) {
  658. peer = get_net_ns_by_pid(nla_get_u32(tb[NETNSA_PID]));
  659. nla = tb[NETNSA_PID];
  660. } else if (tb[NETNSA_FD]) {
  661. peer = get_net_ns_by_fd(nla_get_u32(tb[NETNSA_FD]));
  662. nla = tb[NETNSA_FD];
  663. } else {
  664. NL_SET_ERR_MSG(extack, "Peer netns reference is missing");
  665. return -EINVAL;
  666. }
  667. if (IS_ERR(peer)) {
  668. NL_SET_BAD_ATTR(extack, nla);
  669. NL_SET_ERR_MSG(extack, "Peer netns reference is invalid");
  670. return PTR_ERR(peer);
  671. }
  672. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  673. if (!msg) {
  674. err = -ENOMEM;
  675. goto out;
  676. }
  677. id = peernet2id(net, peer);
  678. err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  679. RTM_NEWNSID, net, id);
  680. if (err < 0)
  681. goto err_out;
  682. err = rtnl_unicast(msg, net, NETLINK_CB(skb).portid);
  683. goto out;
  684. err_out:
  685. nlmsg_free(msg);
  686. out:
  687. put_net(peer);
  688. return err;
  689. }
  690. struct rtnl_net_dump_cb {
  691. struct net *net;
  692. struct sk_buff *skb;
  693. struct netlink_callback *cb;
  694. int idx;
  695. int s_idx;
  696. };
  697. static int rtnl_net_dumpid_one(int id, void *peer, void *data)
  698. {
  699. struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
  700. int ret;
  701. if (net_cb->idx < net_cb->s_idx)
  702. goto cont;
  703. ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
  704. net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
  705. RTM_NEWNSID, net_cb->net, id);
  706. if (ret < 0)
  707. return ret;
  708. cont:
  709. net_cb->idx++;
  710. return 0;
  711. }
  712. static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
  713. {
  714. struct net *net = sock_net(skb->sk);
  715. struct rtnl_net_dump_cb net_cb = {
  716. .net = net,
  717. .skb = skb,
  718. .cb = cb,
  719. .idx = 0,
  720. .s_idx = cb->args[0],
  721. };
  722. spin_lock_bh(&net->nsid_lock);
  723. idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
  724. spin_unlock_bh(&net->nsid_lock);
  725. cb->args[0] = net_cb.idx;
  726. return skb->len;
  727. }
  728. static void rtnl_net_notifyid(struct net *net, int cmd, int id)
  729. {
  730. struct sk_buff *msg;
  731. int err = -ENOMEM;
  732. msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
  733. if (!msg)
  734. goto out;
  735. err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, id);
  736. if (err < 0)
  737. goto err_out;
  738. rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
  739. return;
  740. err_out:
  741. nlmsg_free(msg);
  742. out:
  743. rtnl_set_sk_err(net, RTNLGRP_NSID, err);
  744. }
  745. static int __init net_ns_init(void)
  746. {
  747. struct net_generic *ng;
  748. #ifdef CONFIG_NET_NS
  749. net_cachep = kmem_cache_create("net_namespace", sizeof(struct net),
  750. SMP_CACHE_BYTES,
  751. SLAB_PANIC|SLAB_ACCOUNT, NULL);
  752. /* Create workqueue for cleanup */
  753. netns_wq = create_singlethread_workqueue("netns");
  754. if (!netns_wq)
  755. panic("Could not create netns workq");
  756. #endif
  757. ng = net_alloc_generic();
  758. if (!ng)
  759. panic("Could not allocate generic netns");
  760. rcu_assign_pointer(init_net.gen, ng);
  761. down_write(&pernet_ops_rwsem);
  762. if (setup_net(&init_net, &init_user_ns))
  763. panic("Could not setup the initial network namespace");
  764. init_net_initialized = true;
  765. up_write(&pernet_ops_rwsem);
  766. register_pernet_subsys(&net_ns_ops);
  767. rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL,
  768. RTNL_FLAG_DOIT_UNLOCKED);
  769. rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
  770. RTNL_FLAG_DOIT_UNLOCKED);
  771. return 0;
  772. }
  773. pure_initcall(net_ns_init);
  774. #ifdef CONFIG_NET_NS
  775. static int __register_pernet_operations(struct list_head *list,
  776. struct pernet_operations *ops)
  777. {
  778. struct net *net;
  779. int error;
  780. LIST_HEAD(net_exit_list);
  781. list_add_tail(&ops->list, list);
  782. if (ops->init || (ops->id && ops->size)) {
  783. /* We held write locked pernet_ops_rwsem, and parallel
  784. * setup_net() and cleanup_net() are not possible.
  785. */
  786. for_each_net(net) {
  787. error = ops_init(ops, net);
  788. if (error)
  789. goto out_undo;
  790. list_add_tail(&net->exit_list, &net_exit_list);
  791. }
  792. }
  793. return 0;
  794. out_undo:
  795. /* If I have an error cleanup all namespaces I initialized */
  796. list_del(&ops->list);
  797. ops_exit_list(ops, &net_exit_list);
  798. ops_free_list(ops, &net_exit_list);
  799. return error;
  800. }
  801. static void __unregister_pernet_operations(struct pernet_operations *ops)
  802. {
  803. struct net *net;
  804. LIST_HEAD(net_exit_list);
  805. list_del(&ops->list);
  806. /* See comment in __register_pernet_operations() */
  807. for_each_net(net)
  808. list_add_tail(&net->exit_list, &net_exit_list);
  809. ops_exit_list(ops, &net_exit_list);
  810. ops_free_list(ops, &net_exit_list);
  811. }
  812. #else
  813. static int __register_pernet_operations(struct list_head *list,
  814. struct pernet_operations *ops)
  815. {
  816. if (!init_net_initialized) {
  817. list_add_tail(&ops->list, list);
  818. return 0;
  819. }
  820. return ops_init(ops, &init_net);
  821. }
  822. static void __unregister_pernet_operations(struct pernet_operations *ops)
  823. {
  824. if (!init_net_initialized) {
  825. list_del(&ops->list);
  826. } else {
  827. LIST_HEAD(net_exit_list);
  828. list_add(&init_net.exit_list, &net_exit_list);
  829. ops_exit_list(ops, &net_exit_list);
  830. ops_free_list(ops, &net_exit_list);
  831. }
  832. }
  833. #endif /* CONFIG_NET_NS */
  834. static DEFINE_IDA(net_generic_ids);
  835. static int register_pernet_operations(struct list_head *list,
  836. struct pernet_operations *ops)
  837. {
  838. int error;
  839. if (ops->id) {
  840. error = ida_alloc_min(&net_generic_ids, MIN_PERNET_OPS_ID,
  841. GFP_KERNEL);
  842. if (error < 0)
  843. return error;
  844. *ops->id = error;
  845. max_gen_ptrs = max(max_gen_ptrs, *ops->id + 1);
  846. }
  847. error = __register_pernet_operations(list, ops);
  848. if (error) {
  849. rcu_barrier();
  850. if (ops->id)
  851. ida_free(&net_generic_ids, *ops->id);
  852. }
  853. return error;
  854. }
  855. static void unregister_pernet_operations(struct pernet_operations *ops)
  856. {
  857. __unregister_pernet_operations(ops);
  858. rcu_barrier();
  859. if (ops->id)
  860. ida_free(&net_generic_ids, *ops->id);
  861. }
  862. /**
  863. * register_pernet_subsys - register a network namespace subsystem
  864. * @ops: pernet operations structure for the subsystem
  865. *
  866. * Register a subsystem which has init and exit functions
  867. * that are called when network namespaces are created and
  868. * destroyed respectively.
  869. *
  870. * When registered all network namespace init functions are
  871. * called for every existing network namespace. Allowing kernel
  872. * modules to have a race free view of the set of network namespaces.
  873. *
  874. * When a new network namespace is created all of the init
  875. * methods are called in the order in which they were registered.
  876. *
  877. * When a network namespace is destroyed all of the exit methods
  878. * are called in the reverse of the order with which they were
  879. * registered.
  880. */
  881. int register_pernet_subsys(struct pernet_operations *ops)
  882. {
  883. int error;
  884. down_write(&pernet_ops_rwsem);
  885. error = register_pernet_operations(first_device, ops);
  886. up_write(&pernet_ops_rwsem);
  887. return error;
  888. }
  889. EXPORT_SYMBOL_GPL(register_pernet_subsys);
  890. /**
  891. * unregister_pernet_subsys - unregister a network namespace subsystem
  892. * @ops: pernet operations structure to manipulate
  893. *
  894. * Remove the pernet operations structure from the list to be
  895. * used when network namespaces are created or destroyed. In
  896. * addition run the exit method for all existing network
  897. * namespaces.
  898. */
  899. void unregister_pernet_subsys(struct pernet_operations *ops)
  900. {
  901. down_write(&pernet_ops_rwsem);
  902. unregister_pernet_operations(ops);
  903. up_write(&pernet_ops_rwsem);
  904. }
  905. EXPORT_SYMBOL_GPL(unregister_pernet_subsys);
  906. /**
  907. * register_pernet_device - register a network namespace device
  908. * @ops: pernet operations structure for the subsystem
  909. *
  910. * Register a device which has init and exit functions
  911. * that are called when network namespaces are created and
  912. * destroyed respectively.
  913. *
  914. * When registered all network namespace init functions are
  915. * called for every existing network namespace. Allowing kernel
  916. * modules to have a race free view of the set of network namespaces.
  917. *
  918. * When a new network namespace is created all of the init
  919. * methods are called in the order in which they were registered.
  920. *
  921. * When a network namespace is destroyed all of the exit methods
  922. * are called in the reverse of the order with which they were
  923. * registered.
  924. */
  925. int register_pernet_device(struct pernet_operations *ops)
  926. {
  927. int error;
  928. down_write(&pernet_ops_rwsem);
  929. error = register_pernet_operations(&pernet_list, ops);
  930. if (!error && (first_device == &pernet_list))
  931. first_device = &ops->list;
  932. up_write(&pernet_ops_rwsem);
  933. return error;
  934. }
  935. EXPORT_SYMBOL_GPL(register_pernet_device);
  936. /**
  937. * unregister_pernet_device - unregister a network namespace netdevice
  938. * @ops: pernet operations structure to manipulate
  939. *
  940. * Remove the pernet operations structure from the list to be
  941. * used when network namespaces are created or destroyed. In
  942. * addition run the exit method for all existing network
  943. * namespaces.
  944. */
  945. void unregister_pernet_device(struct pernet_operations *ops)
  946. {
  947. down_write(&pernet_ops_rwsem);
  948. if (&ops->list == first_device)
  949. first_device = first_device->next;
  950. unregister_pernet_operations(ops);
  951. up_write(&pernet_ops_rwsem);
  952. }
  953. EXPORT_SYMBOL_GPL(unregister_pernet_device);
  954. #ifdef CONFIG_NET_NS
  955. static struct ns_common *netns_get(struct task_struct *task)
  956. {
  957. struct net *net = NULL;
  958. struct nsproxy *nsproxy;
  959. task_lock(task);
  960. nsproxy = task->nsproxy;
  961. if (nsproxy)
  962. net = get_net(nsproxy->net_ns);
  963. task_unlock(task);
  964. return net ? &net->ns : NULL;
  965. }
  966. static inline struct net *to_net_ns(struct ns_common *ns)
  967. {
  968. return container_of(ns, struct net, ns);
  969. }
  970. static void netns_put(struct ns_common *ns)
  971. {
  972. put_net(to_net_ns(ns));
  973. }
  974. static int netns_install(struct nsproxy *nsproxy, struct ns_common *ns)
  975. {
  976. struct net *net = to_net_ns(ns);
  977. if (!ns_capable(net->user_ns, CAP_SYS_ADMIN) ||
  978. !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
  979. return -EPERM;
  980. put_net(nsproxy->net_ns);
  981. nsproxy->net_ns = get_net(net);
  982. return 0;
  983. }
  984. static struct user_namespace *netns_owner(struct ns_common *ns)
  985. {
  986. return to_net_ns(ns)->user_ns;
  987. }
  988. const struct proc_ns_operations netns_operations = {
  989. .name = "net",
  990. .type = CLONE_NEWNET,
  991. .get = netns_get,
  992. .put = netns_put,
  993. .install = netns_install,
  994. .owner = netns_owner,
  995. };
  996. #endif