proc_net.c 9.2 KB

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