net_namespace.c 27 KB

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