net_namespace.c 20 KB

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