proc_net.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 <linux/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/sched/task.h>
  19. #include <linux/module.h>
  20. #include <linux/bitops.h>
  21. #include <linux/mount.h>
  22. #include <linux/nsproxy.h>
  23. #include <linux/uidgid.h>
  24. #include <net/net_namespace.h>
  25. #include <linux/seq_file.h>
  26. #include "internal.h"
  27. static inline struct net *PDE_NET(struct proc_dir_entry *pde)
  28. {
  29. return pde->parent->data;
  30. }
  31. static struct net *get_proc_net(const struct inode *inode)
  32. {
  33. return maybe_get_net(PDE_NET(PDE(inode)));
  34. }
  35. static int seq_open_net(struct inode *inode, struct file *file)
  36. {
  37. unsigned int state_size = PDE(inode)->state_size;
  38. struct seq_net_private *p;
  39. struct net *net;
  40. WARN_ON_ONCE(state_size < sizeof(*p));
  41. if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
  42. return -EACCES;
  43. net = get_proc_net(inode);
  44. if (!net)
  45. return -ENXIO;
  46. p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
  47. if (!p) {
  48. put_net(net);
  49. return -ENOMEM;
  50. }
  51. #ifdef CONFIG_NET_NS
  52. p->net = net;
  53. #endif
  54. return 0;
  55. }
  56. static int seq_release_net(struct inode *ino, struct file *f)
  57. {
  58. struct seq_file *seq = f->private_data;
  59. put_net(seq_file_net(seq));
  60. seq_release_private(ino, f);
  61. return 0;
  62. }
  63. static const struct file_operations proc_net_seq_fops = {
  64. .open = seq_open_net,
  65. .read = seq_read,
  66. .write = proc_simple_write,
  67. .llseek = seq_lseek,
  68. .release = seq_release_net,
  69. };
  70. struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
  71. struct proc_dir_entry *parent, const struct seq_operations *ops,
  72. unsigned int state_size, void *data)
  73. {
  74. struct proc_dir_entry *p;
  75. p = proc_create_reg(name, mode, &parent, data);
  76. if (!p)
  77. return NULL;
  78. pde_force_lookup(p);
  79. p->proc_fops = &proc_net_seq_fops;
  80. p->seq_ops = ops;
  81. p->state_size = state_size;
  82. return proc_register(parent, p);
  83. }
  84. EXPORT_SYMBOL_GPL(proc_create_net_data);
  85. /**
  86. * proc_create_net_data_write - Create a writable net_ns-specific proc file
  87. * @name: The name of the file.
  88. * @mode: The file's access mode.
  89. * @parent: The parent directory in which to create.
  90. * @ops: The seq_file ops with which to read the file.
  91. * @write: The write method which which to 'modify' the file.
  92. * @data: Data for retrieval by PDE_DATA().
  93. *
  94. * Create a network namespaced proc file in the @parent directory with the
  95. * specified @name and @mode that allows reading of a file that displays a
  96. * series of elements and also provides for the file accepting writes that have
  97. * some arbitrary effect.
  98. *
  99. * The functions in the @ops table are used to iterate over items to be
  100. * presented and extract the readable content using the seq_file interface.
  101. *
  102. * The @write function is called with the data copied into a kernel space
  103. * scratch buffer and has a NUL appended for convenience. The buffer may be
  104. * modified by the @write function. @write should return 0 on success.
  105. *
  106. * The @data value is accessible from the @show and @write functions by calling
  107. * PDE_DATA() on the file inode. The network namespace must be accessed by
  108. * calling seq_file_net() on the seq_file struct.
  109. */
  110. struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
  111. struct proc_dir_entry *parent,
  112. const struct seq_operations *ops,
  113. proc_write_t write,
  114. unsigned int state_size, void *data)
  115. {
  116. struct proc_dir_entry *p;
  117. p = proc_create_reg(name, mode, &parent, data);
  118. if (!p)
  119. return NULL;
  120. pde_force_lookup(p);
  121. p->proc_fops = &proc_net_seq_fops;
  122. p->seq_ops = ops;
  123. p->state_size = state_size;
  124. p->write = write;
  125. return proc_register(parent, p);
  126. }
  127. EXPORT_SYMBOL_GPL(proc_create_net_data_write);
  128. static int single_open_net(struct inode *inode, struct file *file)
  129. {
  130. struct proc_dir_entry *de = PDE(inode);
  131. struct net *net;
  132. int err;
  133. net = get_proc_net(inode);
  134. if (!net)
  135. return -ENXIO;
  136. err = single_open(file, de->single_show, net);
  137. if (err)
  138. put_net(net);
  139. return err;
  140. }
  141. static int single_release_net(struct inode *ino, struct file *f)
  142. {
  143. struct seq_file *seq = f->private_data;
  144. put_net(seq->private);
  145. return single_release(ino, f);
  146. }
  147. static const struct file_operations proc_net_single_fops = {
  148. .open = single_open_net,
  149. .read = seq_read,
  150. .write = proc_simple_write,
  151. .llseek = seq_lseek,
  152. .release = single_release_net,
  153. };
  154. struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
  155. struct proc_dir_entry *parent,
  156. int (*show)(struct seq_file *, void *), void *data)
  157. {
  158. struct proc_dir_entry *p;
  159. p = proc_create_reg(name, mode, &parent, data);
  160. if (!p)
  161. return NULL;
  162. pde_force_lookup(p);
  163. p->proc_fops = &proc_net_single_fops;
  164. p->single_show = show;
  165. return proc_register(parent, p);
  166. }
  167. EXPORT_SYMBOL_GPL(proc_create_net_single);
  168. /**
  169. * proc_create_net_single_write - Create a writable net_ns-specific proc file
  170. * @name: The name of the file.
  171. * @mode: The file's access mode.
  172. * @parent: The parent directory in which to create.
  173. * @show: The seqfile show method with which to read the file.
  174. * @write: The write method which which to 'modify' the file.
  175. * @data: Data for retrieval by PDE_DATA().
  176. *
  177. * Create a network-namespaced proc file in the @parent directory with the
  178. * specified @name and @mode that allows reading of a file that displays a
  179. * single element rather than a series and also provides for the file accepting
  180. * writes that have some arbitrary effect.
  181. *
  182. * The @show function is called to extract the readable content via the
  183. * seq_file interface.
  184. *
  185. * The @write function is called with the data copied into a kernel space
  186. * scratch buffer and has a NUL appended for convenience. The buffer may be
  187. * modified by the @write function. @write should return 0 on success.
  188. *
  189. * The @data value is accessible from the @show and @write functions by calling
  190. * PDE_DATA() on the file inode. The network namespace must be accessed by
  191. * calling seq_file_single_net() on the seq_file struct.
  192. */
  193. struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
  194. struct proc_dir_entry *parent,
  195. int (*show)(struct seq_file *, void *),
  196. proc_write_t write,
  197. void *data)
  198. {
  199. struct proc_dir_entry *p;
  200. p = proc_create_reg(name, mode, &parent, data);
  201. if (!p)
  202. return NULL;
  203. pde_force_lookup(p);
  204. p->proc_fops = &proc_net_single_fops;
  205. p->single_show = show;
  206. p->write = write;
  207. return proc_register(parent, p);
  208. }
  209. EXPORT_SYMBOL_GPL(proc_create_net_single_write);
  210. static struct net *get_proc_task_net(struct inode *dir)
  211. {
  212. struct task_struct *task;
  213. struct nsproxy *ns;
  214. struct net *net = NULL;
  215. rcu_read_lock();
  216. task = pid_task(proc_pid(dir), PIDTYPE_PID);
  217. if (task != NULL) {
  218. task_lock(task);
  219. ns = task->nsproxy;
  220. if (ns != NULL)
  221. net = get_net(ns->net_ns);
  222. task_unlock(task);
  223. }
  224. rcu_read_unlock();
  225. return net;
  226. }
  227. static struct dentry *proc_tgid_net_lookup(struct inode *dir,
  228. struct dentry *dentry, unsigned int flags)
  229. {
  230. struct dentry *de;
  231. struct net *net;
  232. de = ERR_PTR(-ENOENT);
  233. net = get_proc_task_net(dir);
  234. if (net != NULL) {
  235. de = proc_lookup_de(dir, dentry, net->proc_net);
  236. put_net(net);
  237. }
  238. return de;
  239. }
  240. static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
  241. u32 request_mask, unsigned int query_flags)
  242. {
  243. struct inode *inode = d_inode(path->dentry);
  244. struct net *net;
  245. net = get_proc_task_net(inode);
  246. generic_fillattr(inode, stat);
  247. if (net != NULL) {
  248. stat->nlink = net->proc_net->nlink;
  249. put_net(net);
  250. }
  251. return 0;
  252. }
  253. const struct inode_operations proc_net_inode_operations = {
  254. .lookup = proc_tgid_net_lookup,
  255. .getattr = proc_tgid_net_getattr,
  256. };
  257. static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
  258. {
  259. int ret;
  260. struct net *net;
  261. ret = -EINVAL;
  262. net = get_proc_task_net(file_inode(file));
  263. if (net != NULL) {
  264. ret = proc_readdir_de(file, ctx, net->proc_net);
  265. put_net(net);
  266. }
  267. return ret;
  268. }
  269. const struct file_operations proc_net_operations = {
  270. .llseek = generic_file_llseek,
  271. .read = generic_read_dir,
  272. .iterate_shared = proc_tgid_net_readdir,
  273. };
  274. static __net_init int proc_net_ns_init(struct net *net)
  275. {
  276. struct proc_dir_entry *netd, *net_statd;
  277. kuid_t uid;
  278. kgid_t gid;
  279. int err;
  280. err = -ENOMEM;
  281. netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
  282. if (!netd)
  283. goto out;
  284. netd->subdir = RB_ROOT;
  285. netd->data = net;
  286. netd->nlink = 2;
  287. netd->namelen = 3;
  288. netd->parent = &proc_root;
  289. netd->name = netd->inline_name;
  290. memcpy(netd->name, "net", 4);
  291. uid = make_kuid(net->user_ns, 0);
  292. if (!uid_valid(uid))
  293. uid = netd->uid;
  294. gid = make_kgid(net->user_ns, 0);
  295. if (!gid_valid(gid))
  296. gid = netd->gid;
  297. proc_set_user(netd, uid, gid);
  298. err = -EEXIST;
  299. net_statd = proc_net_mkdir(net, "stat", netd);
  300. if (!net_statd)
  301. goto free_net;
  302. net->proc_net = netd;
  303. net->proc_net_stat = net_statd;
  304. return 0;
  305. free_net:
  306. pde_free(netd);
  307. out:
  308. return err;
  309. }
  310. static __net_exit void proc_net_ns_exit(struct net *net)
  311. {
  312. remove_proc_entry("stat", net->proc_net);
  313. pde_free(net->proc_net);
  314. }
  315. static struct pernet_operations __net_initdata proc_net_ns_ops = {
  316. .init = proc_net_ns_init,
  317. .exit = proc_net_ns_exit,
  318. };
  319. int __init proc_net_init(void)
  320. {
  321. proc_symlink("net", NULL, "self/net");
  322. return register_pernet_subsys(&proc_net_ns_ops);
  323. }