proc_net.c 4.3 KB

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