net_namespace.c 27 KB

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