6 * Author: Eric Biederman <ebiederm@xmission.com>
8 * proc net directory handling functions
11 #include <linux/uaccess.h>
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/sched/task.h>
21 #include <linux/module.h>
22 #include <linux/bitops.h>
23 #include <linux/mount.h>
24 #include <linux/nsproxy.h>
25 #include <linux/uidgid.h>
26 #include <net/net_namespace.h>
27 #include <linux/seq_file.h>
31 static inline struct net *PDE_NET(struct proc_dir_entry *pde)
33 return pde->parent->data;
36 static struct net *get_proc_net(const struct inode *inode)
38 return maybe_get_net(PDE_NET(PDE(inode)));
41 static int seq_open_net(struct inode *inode, struct file *file)
43 unsigned int state_size = PDE(inode)->state_size;
44 struct seq_net_private *p;
47 WARN_ON_ONCE(state_size < sizeof(*p));
49 net = get_proc_net(inode);
53 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
64 static int seq_release_net(struct inode *ino, struct file *f)
66 struct seq_file *seq = f->private_data;
68 put_net(seq_file_net(seq));
69 seq_release_private(ino, f);
73 static const struct file_operations proc_net_seq_fops = {
77 .release = seq_release_net,
80 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
81 struct proc_dir_entry *parent, const struct seq_operations *ops,
82 unsigned int state_size, void *data)
84 struct proc_dir_entry *p;
86 p = proc_create_reg(name, mode, &parent, data);
89 p->proc_fops = &proc_net_seq_fops;
91 p->state_size = state_size;
92 return proc_register(parent, p);
94 EXPORT_SYMBOL_GPL(proc_create_net_data);
96 static int single_open_net(struct inode *inode, struct file *file)
98 struct proc_dir_entry *de = PDE(inode);
102 net = get_proc_net(inode);
106 err = single_open(file, de->single_show, net);
112 static int single_release_net(struct inode *ino, struct file *f)
114 struct seq_file *seq = f->private_data;
115 put_net(seq->private);
116 return single_release(ino, f);
119 static const struct file_operations proc_net_single_fops = {
120 .open = single_open_net,
123 .release = single_release_net,
126 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
127 struct proc_dir_entry *parent,
128 int (*show)(struct seq_file *, void *), void *data)
130 struct proc_dir_entry *p;
132 p = proc_create_reg(name, mode, &parent, data);
135 p->proc_fops = &proc_net_single_fops;
136 p->single_show = show;
137 return proc_register(parent, p);
139 EXPORT_SYMBOL_GPL(proc_create_net_single);
141 static struct net *get_proc_task_net(struct inode *dir)
143 struct task_struct *task;
145 struct net *net = NULL;
148 task = pid_task(proc_pid(dir), PIDTYPE_PID);
153 net = get_net(ns->net_ns);
161 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
162 struct dentry *dentry, unsigned int flags)
167 de = ERR_PTR(-ENOENT);
168 net = get_proc_task_net(dir);
170 de = proc_lookup_de(dir, dentry, net->proc_net);
176 static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
177 u32 request_mask, unsigned int query_flags)
179 struct inode *inode = d_inode(path->dentry);
182 net = get_proc_task_net(inode);
184 generic_fillattr(inode, stat);
187 stat->nlink = net->proc_net->nlink;
194 const struct inode_operations proc_net_inode_operations = {
195 .lookup = proc_tgid_net_lookup,
196 .getattr = proc_tgid_net_getattr,
199 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
205 net = get_proc_task_net(file_inode(file));
207 ret = proc_readdir_de(file, ctx, net->proc_net);
213 const struct file_operations proc_net_operations = {
214 .llseek = generic_file_llseek,
215 .read = generic_read_dir,
216 .iterate_shared = proc_tgid_net_readdir,
219 static __net_init int proc_net_ns_init(struct net *net)
221 struct proc_dir_entry *netd, *net_statd;
227 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
231 netd->subdir = RB_ROOT;
235 netd->parent = &proc_root;
236 netd->name = netd->inline_name;
237 memcpy(netd->name, "net", 4);
239 uid = make_kuid(net->user_ns, 0);
243 gid = make_kgid(net->user_ns, 0);
247 proc_set_user(netd, uid, gid);
250 net_statd = proc_net_mkdir(net, "stat", netd);
254 net->proc_net = netd;
255 net->proc_net_stat = net_statd;
264 static __net_exit void proc_net_ns_exit(struct net *net)
266 remove_proc_entry("stat", net->proc_net);
267 pde_free(net->proc_net);
270 static struct pernet_operations __net_initdata proc_net_ns_ops = {
271 .init = proc_net_ns_init,
272 .exit = proc_net_ns_exit,
275 int __init proc_net_init(void)
277 proc_symlink("net", NULL, "self/net");
279 return register_pernet_subsys(&proc_net_ns_ops);