proc_net.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * linux/fs/proc/net.c
  3. *
  4. * Copyright (C) 2007
  5. *
  6. * Author: Eric Biederman <ebiederm@xmission.com>
  7. *
  8. * proc net directory handling functions
  9. */
  10. #include <asm/uaccess.h>
  11. #include <linux/errno.h>
  12. #include <linux/time.h>
  13. #include <linux/proc_fs.h>
  14. #include <linux/stat.h>
  15. #include <linux/init.h>
  16. #include <linux/sched.h>
  17. #include <linux/module.h>
  18. #include <linux/bitops.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/mount.h>
  21. #include <linux/nsproxy.h>
  22. #include <net/net_namespace.h>
  23. #include "internal.h"
  24. struct proc_dir_entry *proc_net_create(struct net *net,
  25. const char *name, mode_t mode, get_info_t *get_info)
  26. {
  27. return create_proc_info_entry(name,mode, net->proc_net, get_info);
  28. }
  29. EXPORT_SYMBOL_GPL(proc_net_create);
  30. struct proc_dir_entry *proc_net_fops_create(struct net *net,
  31. const char *name, mode_t mode, const struct file_operations *fops)
  32. {
  33. struct proc_dir_entry *res;
  34. res = create_proc_entry(name, mode, net->proc_net);
  35. if (res)
  36. res->proc_fops = fops;
  37. return res;
  38. }
  39. EXPORT_SYMBOL_GPL(proc_net_fops_create);
  40. void proc_net_remove(struct net *net, const char *name)
  41. {
  42. remove_proc_entry(name, net->proc_net);
  43. }
  44. EXPORT_SYMBOL_GPL(proc_net_remove);
  45. struct net *get_proc_net(const struct inode *inode)
  46. {
  47. return maybe_get_net(PDE_NET(PDE(inode)));
  48. }
  49. EXPORT_SYMBOL_GPL(get_proc_net);
  50. static struct proc_dir_entry *proc_net_shadow;
  51. static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
  52. struct proc_dir_entry *de)
  53. {
  54. struct dentry *shadow = NULL;
  55. struct inode *inode;
  56. if (!de)
  57. goto out;
  58. de_get(de);
  59. inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
  60. if (!inode)
  61. goto out_de_put;
  62. shadow = d_alloc_name(parent, de->name);
  63. if (!shadow)
  64. goto out_iput;
  65. shadow->d_op = parent->d_op; /* proc_dentry_operations */
  66. d_instantiate(shadow, inode);
  67. out:
  68. return shadow;
  69. out_iput:
  70. iput(inode);
  71. out_de_put:
  72. de_put(de);
  73. goto out;
  74. }
  75. static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
  76. {
  77. struct net *net = current->nsproxy->net_ns;
  78. struct dentry *shadow;
  79. shadow = proc_net_shadow_dentry(parent, net->proc_net);
  80. if (!shadow)
  81. return ERR_PTR(-ENOENT);
  82. dput(nd->dentry);
  83. /* My dentry count is 1 and that should be enough as the
  84. * shadow dentry is thrown away immediately.
  85. */
  86. nd->dentry = shadow;
  87. return NULL;
  88. }
  89. static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
  90. struct nameidata *nd)
  91. {
  92. struct net *net = current->nsproxy->net_ns;
  93. struct dentry *shadow;
  94. shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
  95. if (!shadow)
  96. return ERR_PTR(-ENOENT);
  97. dput(nd->dentry);
  98. nd->dentry = shadow;
  99. return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
  100. }
  101. static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
  102. {
  103. struct net *net = current->nsproxy->net_ns;
  104. struct dentry *shadow;
  105. int ret;
  106. shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
  107. if (!shadow)
  108. return -ENOENT;
  109. ret = shadow->d_inode->i_op->setattr(shadow, iattr);
  110. dput(shadow);
  111. return ret;
  112. }
  113. static const struct file_operations proc_net_dir_operations = {
  114. .read = generic_read_dir,
  115. };
  116. static struct inode_operations proc_net_dir_inode_operations = {
  117. .follow_link = proc_net_follow_link,
  118. .lookup = proc_net_lookup,
  119. .setattr = proc_net_setattr,
  120. };
  121. static int proc_net_ns_init(struct net *net)
  122. {
  123. struct proc_dir_entry *root, *netd, *net_statd;
  124. int err;
  125. err = -ENOMEM;
  126. root = kzalloc(sizeof(*root), GFP_KERNEL);
  127. if (!root)
  128. goto out;
  129. err = -EEXIST;
  130. netd = proc_mkdir("net", root);
  131. if (!netd)
  132. goto free_root;
  133. err = -EEXIST;
  134. net_statd = proc_mkdir("stat", netd);
  135. if (!net_statd)
  136. goto free_net;
  137. root->data = net;
  138. netd->data = net;
  139. net_statd->data = net;
  140. net->proc_net_root = root;
  141. net->proc_net = netd;
  142. net->proc_net_stat = net_statd;
  143. err = 0;
  144. out:
  145. return err;
  146. free_net:
  147. remove_proc_entry("net", root);
  148. free_root:
  149. kfree(root);
  150. goto out;
  151. }
  152. static void proc_net_ns_exit(struct net *net)
  153. {
  154. remove_proc_entry("stat", net->proc_net);
  155. remove_proc_entry("net", net->proc_net_root);
  156. kfree(net->proc_net_root);
  157. }
  158. struct pernet_operations proc_net_ns_ops = {
  159. .init = proc_net_ns_init,
  160. .exit = proc_net_ns_exit,
  161. };
  162. int proc_net_init(void)
  163. {
  164. proc_net_shadow = proc_mkdir("net", NULL);
  165. proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
  166. proc_net_shadow->proc_fops = &proc_net_dir_operations;
  167. return register_pernet_subsys(&proc_net_ns_ops);
  168. }