proc_net.c 4.3 KB

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