net_namespace.c 23 KB

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