net_namespace.c 26 KB

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