proc_net.c 4.6 KB

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