net_namespace.c 27 KB

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