proc_net.c 4.2 KB

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