net_namespace.c 26 KB

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