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
11#include <asm/uaccess.h>
12
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/proc_fs.h>
16#include <linux/stat.h>
17#include <linux/slab.h>
18#include <linux/init.h>
19#include <linux/sched.h>
20#include <linux/module.h>
21#include <linux/bitops.h>
22#include <linux/mount.h>
23#include <linux/nsproxy.h>
24#include <net/net_namespace.h>
25#include <linux/seq_file.h>
26
27#include "internal.h"
28
29
30static struct net *get_proc_net(const struct inode *inode)
31{
32	return maybe_get_net(PDE_NET(PDE(inode)));
33}
34
35int seq_open_net(struct inode *ino, struct file *f,
36		 const struct seq_operations *ops, int size)
37{
38	struct net *net;
39	struct seq_net_private *p;
40
41	BUG_ON(size < sizeof(*p));
42
43	net = get_proc_net(ino);
44	if (net == NULL)
45		return -ENXIO;
46
47	p = __seq_open_private(f, ops, size);
48	if (p == NULL) {
49		put_net(net);
50		return -ENOMEM;
51	}
52#ifdef CONFIG_NET_NS
53	p->net = net;
54#endif
55	return 0;
56}
57EXPORT_SYMBOL_GPL(seq_open_net);
58
59int single_open_net(struct inode *inode, struct file *file,
60		int (*show)(struct seq_file *, void *))
61{
62	int err;
63	struct net *net;
64
65	err = -ENXIO;
66	net = get_proc_net(inode);
67	if (net == NULL)
68		goto err_net;
69
70	err = single_open(file, show, net);
71	if (err < 0)
72		goto err_open;
73
74	return 0;
75
76err_open:
77	put_net(net);
78err_net:
79	return err;
80}
81EXPORT_SYMBOL_GPL(single_open_net);
82
83int seq_release_net(struct inode *ino, struct file *f)
84{
85	struct seq_file *seq;
86
87	seq = f->private_data;
88
89	put_net(seq_file_net(seq));
90	seq_release_private(ino, f);
91	return 0;
92}
93EXPORT_SYMBOL_GPL(seq_release_net);
94
95int single_release_net(struct inode *ino, struct file *f)
96{
97	struct seq_file *seq = f->private_data;
98	put_net(seq->private);
99	return single_release(ino, f);
100}
101EXPORT_SYMBOL_GPL(single_release_net);
102
103static struct net *get_proc_task_net(struct inode *dir)
104{
105	struct task_struct *task;
106	struct nsproxy *ns;
107	struct net *net = NULL;
108
109	rcu_read_lock();
110	task = pid_task(proc_pid(dir), PIDTYPE_PID);
111	if (task != NULL) {
112		ns = task_nsproxy(task);
113		if (ns != NULL)
114			net = get_net(ns->net_ns);
115	}
116	rcu_read_unlock();
117
118	return net;
119}
120
121static struct dentry *proc_tgid_net_lookup(struct inode *dir,
122		struct dentry *dentry, struct nameidata *nd)
123{
124	struct dentry *de;
125	struct net *net;
126
127	de = ERR_PTR(-ENOENT);
128	net = get_proc_task_net(dir);
129	if (net != NULL) {
130		de = proc_lookup_de(net->proc_net, dir, dentry);
131		put_net(net);
132	}
133	return de;
134}
135
136static int proc_tgid_net_getattr(struct vfsmount *mnt, struct dentry *dentry,
137		struct kstat *stat)
138{
139	struct inode *inode = dentry->d_inode;
140	struct net *net;
141
142	net = get_proc_task_net(inode);
143
144	generic_fillattr(inode, stat);
145
146	if (net != NULL) {
147		stat->nlink = net->proc_net->nlink;
148		put_net(net);
149	}
150
151	return 0;
152}
153
154const struct inode_operations proc_net_inode_operations = {
155	.lookup		= proc_tgid_net_lookup,
156	.getattr	= proc_tgid_net_getattr,
157};
158
159static int proc_tgid_net_readdir(struct file *filp, void *dirent,
160		filldir_t filldir)
161{
162	int ret;
163	struct net *net;
164
165	ret = -EINVAL;
166	net = get_proc_task_net(filp->f_path.dentry->d_inode);
167	if (net != NULL) {
168		ret = proc_readdir_de(net->proc_net, filp, dirent, filldir);
169		put_net(net);
170	}
171	return ret;
172}
173
174const struct file_operations proc_net_operations = {
175	.llseek		= generic_file_llseek,
176	.read		= generic_read_dir,
177	.readdir	= proc_tgid_net_readdir,
178};
179
180
181struct proc_dir_entry *proc_net_fops_create(struct net *net,
182	const char *name, mode_t mode, const struct file_operations *fops)
183{
184	return proc_create(name, mode, net->proc_net, fops);
185}
186EXPORT_SYMBOL_GPL(proc_net_fops_create);
187
188void proc_net_remove(struct net *net, const char *name)
189{
190	remove_proc_entry(name, net->proc_net);
191}
192EXPORT_SYMBOL_GPL(proc_net_remove);
193
194static __net_init int proc_net_ns_init(struct net *net)
195{
196	struct proc_dir_entry *netd, *net_statd;
197	int err;
198
199	err = -ENOMEM;
200	netd = kzalloc(sizeof(*netd), GFP_KERNEL);
201	if (!netd)
202		goto out;
203
204	netd->data = net;
205	netd->nlink = 2;
206	netd->name = "net";
207	netd->namelen = 3;
208	netd->parent = &proc_root;
209
210	err = -EEXIST;
211	net_statd = proc_net_mkdir(net, "stat", netd);
212	if (!net_statd)
213		goto free_net;
214
215	net->proc_net = netd;
216	net->proc_net_stat = net_statd;
217	return 0;
218
219free_net:
220	kfree(netd);
221out:
222	return err;
223}
224
225static __net_exit void proc_net_ns_exit(struct net *net)
226{
227	remove_proc_entry("stat", net->proc_net);
228	kfree(net->proc_net);
229}
230
231static struct pernet_operations __net_initdata proc_net_ns_ops = {
232	.init = proc_net_ns_init,
233	.exit = proc_net_ns_exit,
234};
235
236int __init proc_net_init(void)
237{
238	proc_symlink("net", NULL, "self/net");
239
240	return register_pernet_subsys(&proc_net_ns_ops);
241}
242