proc_net.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_fops_create(struct net *net,
  25. const char *name, mode_t mode, const struct file_operations *fops)
  26. {
  27. struct proc_dir_entry *res;
  28. res = create_proc_entry(name, mode, net->proc_net);
  29. if (res)
  30. res->proc_fops = fops;
  31. return res;
  32. }
  33. EXPORT_SYMBOL_GPL(proc_net_fops_create);
  34. void proc_net_remove(struct net *net, const char *name)
  35. {
  36. remove_proc_entry(name, net->proc_net);
  37. }
  38. EXPORT_SYMBOL_GPL(proc_net_remove);
  39. struct net *get_proc_net(const struct inode *inode)
  40. {
  41. return maybe_get_net(PDE_NET(PDE(inode)));
  42. }
  43. EXPORT_SYMBOL_GPL(get_proc_net);
  44. static struct proc_dir_entry *proc_net_shadow;
  45. static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
  46. struct proc_dir_entry *de)
  47. {
  48. struct dentry *shadow = NULL;
  49. struct inode *inode;
  50. if (!de)
  51. goto out;
  52. de_get(de);
  53. inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
  54. if (!inode)
  55. goto out_de_put;
  56. shadow = d_alloc_name(parent, de->name);
  57. if (!shadow)
  58. goto out_iput;
  59. shadow->d_op = parent->d_op; /* proc_dentry_operations */
  60. d_instantiate(shadow, inode);
  61. out:
  62. return shadow;
  63. out_iput:
  64. iput(inode);
  65. out_de_put:
  66. de_put(de);
  67. goto out;
  68. }
  69. static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
  70. {
  71. struct net *net = current->nsproxy->net_ns;
  72. struct dentry *shadow;
  73. shadow = proc_net_shadow_dentry(parent, net->proc_net);
  74. if (!shadow)
  75. return ERR_PTR(-ENOENT);
  76. dput(nd->dentry);
  77. /* My dentry count is 1 and that should be enough as the
  78. * shadow dentry is thrown away immediately.
  79. */
  80. nd->dentry = shadow;
  81. return NULL;
  82. }
  83. static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
  84. struct nameidata *nd)
  85. {
  86. struct net *net = current->nsproxy->net_ns;
  87. struct dentry *shadow;
  88. shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
  89. if (!shadow)
  90. return ERR_PTR(-ENOENT);
  91. dput(nd->dentry);
  92. nd->dentry = shadow;
  93. return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
  94. }
  95. static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
  96. {
  97. struct net *net = current->nsproxy->net_ns;
  98. struct dentry *shadow;
  99. int ret;
  100. shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
  101. if (!shadow)
  102. return -ENOENT;
  103. ret = shadow->d_inode->i_op->setattr(shadow, iattr);
  104. dput(shadow);
  105. return ret;
  106. }
  107. static const struct file_operations proc_net_dir_operations = {
  108. .read = generic_read_dir,
  109. };
  110. static struct inode_operations proc_net_dir_inode_operations = {
  111. .follow_link = proc_net_follow_link,
  112. .lookup = proc_net_lookup,
  113. .setattr = proc_net_setattr,
  114. };
  115. static __net_init int proc_net_ns_init(struct net *net)
  116. {
  117. struct proc_dir_entry *root, *netd, *net_statd;
  118. int err;
  119. err = -ENOMEM;
  120. root = kzalloc(sizeof(*root), GFP_KERNEL);
  121. if (!root)
  122. goto out;
  123. err = -EEXIST;
  124. netd = proc_mkdir("net", root);
  125. if (!netd)
  126. goto free_root;
  127. err = -EEXIST;
  128. net_statd = proc_mkdir("stat", netd);
  129. if (!net_statd)
  130. goto free_net;
  131. root->data = net;
  132. netd->data = net;
  133. net_statd->data = net;
  134. net->proc_net_root = root;
  135. net->proc_net = netd;
  136. net->proc_net_stat = net_statd;
  137. err = 0;
  138. out:
  139. return err;
  140. free_net:
  141. remove_proc_entry("net", root);
  142. free_root:
  143. kfree(root);
  144. goto out;
  145. }
  146. static __net_exit void proc_net_ns_exit(struct net *net)
  147. {
  148. remove_proc_entry("stat", net->proc_net);
  149. remove_proc_entry("net", net->proc_net_root);
  150. kfree(net->proc_net_root);
  151. }
  152. static struct pernet_operations proc_net_ns_ops = {
  153. .init = proc_net_ns_init,
  154. .exit = proc_net_ns_exit,
  155. };
  156. int __init proc_net_init(void)
  157. {
  158. proc_net_shadow = proc_mkdir("net", NULL);
  159. proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
  160. proc_net_shadow->proc_fops = &proc_net_dir_operations;
  161. return register_pernet_subsys(&proc_net_ns_ops);
  162. }