net_namespace.c 28 KB

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