net_namespace.c 24 KB

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