proc_net.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/module.h>
  19. #include <linux/bitops.h>
  20. #include <linux/mount.h>
  21. #include <linux/nsproxy.h>
  22. #include <net/net_namespace.h>
  23. #include <linux/seq_file.h>
  24. #include "internal.h"
  25. static inline struct net *PDE_NET(struct proc_dir_entry *pde)
  26. {
  27. return pde->parent->data;
  28. }
  29. static struct net *get_proc_net(const struct inode *inode)
  30. {
  31. return maybe_get_net(PDE_NET(PDE(inode)));
  32. }
  33. int seq_open_net(struct inode *ino, struct file *f,
  34. const struct seq_operations *ops, int size)
  35. {
  36. struct net *net;
  37. struct seq_net_private *p;
  38. BUG_ON(size < sizeof(*p));
  39. net = get_proc_net(ino);
  40. if (net == NULL)
  41. return -ENXIO;
  42. p = __seq_open_private(f, ops, size);
  43. if (p == NULL) {
  44. put_net(net);
  45. return -ENOMEM;
  46. }
  47. #ifdef CONFIG_NET_NS
  48. p->net = net;
  49. #endif
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(seq_open_net);
  53. int single_open_net(struct inode *inode, struct file *file,
  54. int (*show)(struct seq_file *, void *))
  55. {
  56. int err;
  57. struct net *net;
  58. err = -ENXIO;
  59. net = get_proc_net(inode);
  60. if (net == NULL)
  61. goto err_net;
  62. err = single_open(file, show, net);
  63. if (err < 0)
  64. goto err_open;
  65. return 0;
  66. err_open:
  67. put_net(net);
  68. err_net:
  69. return err;
  70. }
  71. EXPORT_SYMBOL_GPL(single_open_net);
  72. int seq_release_net(struct inode *ino, struct file *f)
  73. {
  74. struct seq_file *seq;
  75. seq = f->private_data;
  76. put_net(seq_file_net(seq));
  77. seq_release_private(ino, f);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL_GPL(seq_release_net);
  81. int single_release_net(struct inode *ino, struct file *f)
  82. {
  83. struct seq_file *seq = f->private_data;
  84. put_net(seq->private);
  85. return single_release(ino, f);
  86. }
  87. EXPORT_SYMBOL_GPL(single_release_net);
  88. static struct net *get_proc_task_net(struct inode *dir)
  89. {
  90. struct task_struct *task;
  91. struct nsproxy *ns;
  92. struct net *net = NULL;
  93. rcu_read_lock();
  94. task = pid_task(proc_pid(dir), PIDTYPE_PID);
  95. if (task != NULL) {
  96. task_lock(task);
  97. ns = task->nsproxy;
  98. if (ns != NULL)
  99. net = get_net(ns->net_ns);
  100. task_unlock(task);
  101. }
  102. rcu_read_unlock();
  103. return net;
  104. }
  105. static struct dentry *proc_tgid_net_lookup(struct inode *dir,
  106. struct dentry *dentry, unsigned int flags)
  107. {
  108. struct dentry *de;
  109. struct net *net;
  110. de = ERR_PTR(-ENOENT);
  111. net = get_proc_task_net(dir);
  112. if (net != NULL) {
  113. de = proc_lookup_de(net->proc_net, dir, dentry);
  114. put_net(net);
  115. }
  116. return de;
  117. }
  118. static int proc_tgid_net_getattr(struct vfsmount *mnt, struct dentry *dentry,
  119. struct kstat *stat)
  120. {
  121. struct inode *inode = d_inode(dentry);
  122. struct net *net;
  123. net = get_proc_task_net(inode);
  124. generic_fillattr(inode, stat);
  125. if (net != NULL) {
  126. stat->nlink = net->proc_net->nlink;
  127. put_net(net);
  128. }
  129. return 0;
  130. }
  131. const struct inode_operations proc_net_inode_operations = {
  132. .lookup = proc_tgid_net_lookup,
  133. .getattr = proc_tgid_net_getattr,
  134. };
  135. static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
  136. {
  137. int ret;
  138. struct net *net;
  139. ret = -EINVAL;
  140. net = get_proc_task_net(file_inode(file));
  141. if (net != NULL) {
  142. ret = proc_readdir_de(net->proc_net, file, ctx);
  143. put_net(net);
  144. }
  145. return ret;
  146. }
  147. const struct file_operations proc_net_operations = {
  148. .llseek = generic_file_llseek,
  149. .read = generic_read_dir,
  150. .iterate = proc_tgid_net_readdir,
  151. };
  152. static __net_init int proc_net_ns_init(struct net *net)
  153. {
  154. struct proc_dir_entry *netd, *net_statd;
  155. int err;
  156. err = -ENOMEM;
  157. netd = kzalloc(sizeof(*netd) + 4, GFP_KERNEL);
  158. if (!netd)
  159. goto out;
  160. netd->subdir = RB_ROOT;
  161. netd->data = net;
  162. netd->nlink = 2;
  163. netd->namelen = 3;
  164. netd->parent = &proc_root;
  165. memcpy(netd->name, "net", 4);
  166. err = -EEXIST;
  167. net_statd = proc_net_mkdir(net, "stat", netd);
  168. if (!net_statd)
  169. goto free_net;
  170. net->proc_net = netd;
  171. net->proc_net_stat = net_statd;
  172. return 0;
  173. free_net:
  174. kfree(netd);
  175. out:
  176. return err;
  177. }
  178. static __net_exit void proc_net_ns_exit(struct net *net)
  179. {
  180. remove_proc_entry("stat", net->proc_net);
  181. kfree(net->proc_net);
  182. }
  183. static struct pernet_operations __net_initdata proc_net_ns_ops = {
  184. .init = proc_net_ns_init,
  185. .exit = proc_net_ns_exit,
  186. };
  187. int __init proc_net_init(void)
  188. {
  189. proc_symlink("net", NULL, "self/net");
  190. return register_pernet_subsys(&proc_net_ns_ops);
  191. }