net_namespace.c 28 KB

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