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 if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
52 net = get_proc_net(inode);
56 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
67 static int seq_release_net(struct inode *ino, struct file *f)
69 struct seq_file *seq = f->private_data;
71 put_net(seq_file_net(seq));
72 seq_release_private(ino, f);
76 static const struct file_operations proc_net_seq_fops = {
79 .write = proc_simple_write,
81 .release = seq_release_net,
84 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
85 struct proc_dir_entry *parent, const struct seq_operations *ops,
86 unsigned int state_size, void *data)
88 struct proc_dir_entry *p;
90 p = proc_create_reg(name, mode, &parent, data);
94 p->proc_fops = &proc_net_seq_fops;
96 p->state_size = state_size;
97 return proc_register(parent, p);
99 EXPORT_SYMBOL_GPL(proc_create_net_data);
102 * proc_create_net_data_write - Create a writable net_ns-specific proc file
103 * @name: The name of the file.
104 * @mode: The file's access mode.
105 * @parent: The parent directory in which to create.
106 * @ops: The seq_file ops with which to read the file.
107 * @write: The write method which which to 'modify' the file.
108 * @data: Data for retrieval by PDE_DATA().
110 * Create a network namespaced proc file in the @parent directory with the
111 * specified @name and @mode that allows reading of a file that displays a
112 * series of elements and also provides for the file accepting writes that have
113 * some arbitrary effect.
115 * The functions in the @ops table are used to iterate over items to be
116 * presented and extract the readable content using the seq_file interface.
118 * The @write function is called with the data copied into a kernel space
119 * scratch buffer and has a NUL appended for convenience. The buffer may be
120 * modified by the @write function. @write should return 0 on success.
122 * The @data value is accessible from the @show and @write functions by calling
123 * PDE_DATA() on the file inode. The network namespace must be accessed by
124 * calling seq_file_net() on the seq_file struct.
126 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
127 struct proc_dir_entry *parent,
128 const struct seq_operations *ops,
130 unsigned int state_size, void *data)
132 struct proc_dir_entry *p;
134 p = proc_create_reg(name, mode, &parent, data);
138 p->proc_fops = &proc_net_seq_fops;
140 p->state_size = state_size;
142 return proc_register(parent, p);
144 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
146 static int single_open_net(struct inode *inode, struct file *file)
148 struct proc_dir_entry *de = PDE(inode);
152 net = get_proc_net(inode);
156 err = single_open(file, de->single_show, net);
162 static int single_release_net(struct inode *ino, struct file *f)
164 struct seq_file *seq = f->private_data;
165 put_net(seq->private);
166 return single_release(ino, f);
169 static const struct file_operations proc_net_single_fops = {
170 .open = single_open_net,
172 .write = proc_simple_write,
174 .release = single_release_net,
177 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
178 struct proc_dir_entry *parent,
179 int (*show)(struct seq_file *, void *), void *data)
181 struct proc_dir_entry *p;
183 p = proc_create_reg(name, mode, &parent, data);
187 p->proc_fops = &proc_net_single_fops;
188 p->single_show = show;
189 return proc_register(parent, p);
191 EXPORT_SYMBOL_GPL(proc_create_net_single);
194 * proc_create_net_single_write - Create a writable net_ns-specific proc file
195 * @name: The name of the file.
196 * @mode: The file's access mode.
197 * @parent: The parent directory in which to create.
198 * @show: The seqfile show method with which to read the file.
199 * @write: The write method which which to 'modify' the file.
200 * @data: Data for retrieval by PDE_DATA().
202 * Create a network-namespaced proc file in the @parent directory with the
203 * specified @name and @mode that allows reading of a file that displays a
204 * single element rather than a series and also provides for the file accepting
205 * writes that have some arbitrary effect.
207 * The @show function is called to extract the readable content via the
208 * seq_file interface.
210 * The @write function is called with the data copied into a kernel space
211 * scratch buffer and has a NUL appended for convenience. The buffer may be
212 * modified by the @write function. @write should return 0 on success.
214 * The @data value is accessible from the @show and @write functions by calling
215 * PDE_DATA() on the file inode. The network namespace must be accessed by
216 * calling seq_file_single_net() on the seq_file struct.
218 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
219 struct proc_dir_entry *parent,
220 int (*show)(struct seq_file *, void *),
224 struct proc_dir_entry *p;
226 p = proc_create_reg(name, mode, &parent, data);
230 p->proc_fops = &proc_net_single_fops;
231 p->single_show = show;
233 return proc_register(parent, p);
235 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
237 static struct net *get_proc_task_net(struct inode *dir)
239 struct task_struct *task;
241 struct net *net = NULL;
244 task = pid_task(proc_pid(dir), PIDTYPE_PID);
249 net = get_net(ns->net_ns);
257 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
258 struct dentry *dentry, unsigned int flags)
263 de = ERR_PTR(-ENOENT);
264 net = get_proc_task_net(dir);
266 de = proc_lookup_de(dir, dentry, net->proc_net);
272 static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
273 u32 request_mask, unsigned int query_flags)
275 struct inode *inode = d_inode(path->dentry);
278 net = get_proc_task_net(inode);
280 generic_fillattr(inode, stat);
283 stat->nlink = net->proc_net->nlink;
290 const struct inode_operations proc_net_inode_operations = {
291 .lookup = proc_tgid_net_lookup,
292 .getattr = proc_tgid_net_getattr,
295 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
301 net = get_proc_task_net(file_inode(file));
303 ret = proc_readdir_de(file, ctx, net->proc_net);
309 const struct file_operations proc_net_operations = {
310 .llseek = generic_file_llseek,
311 .read = generic_read_dir,
312 .iterate_shared = proc_tgid_net_readdir,
315 static __net_init int proc_net_ns_init(struct net *net)
317 struct proc_dir_entry *netd, *net_statd;
323 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
327 netd->subdir = RB_ROOT;
331 netd->parent = &proc_root;
332 netd->name = netd->inline_name;
333 memcpy(netd->name, "net", 4);
335 uid = make_kuid(net->user_ns, 0);
339 gid = make_kgid(net->user_ns, 0);
343 proc_set_user(netd, uid, gid);
346 net_statd = proc_net_mkdir(net, "stat", netd);
350 net->proc_net = netd;
351 net->proc_net_stat = net_statd;
360 static __net_exit void proc_net_ns_exit(struct net *net)
362 remove_proc_entry("stat", net->proc_net);
363 pde_free(net->proc_net);
366 static struct pernet_operations __net_initdata proc_net_ns_ops = {
367 .init = proc_net_ns_init,
368 .exit = proc_net_ns_exit,
371 int __init proc_net_init(void)
373 proc_symlink("net", NULL, "self/net");
375 return register_pernet_subsys(&proc_net_ns_ops);