net_namespace.c 21 KB

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