1 // SPDX-License-Identifier: GPL-2.0-only
7 * Author: Eric Biederman <ebiederm@xmission.com>
9 * proc net directory handling functions
12 #include <linux/uaccess.h>
14 #include <linux/errno.h>
15 #include <linux/time.h>
16 #include <linux/proc_fs.h>
17 #include <linux/stat.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/sched.h>
21 #include <linux/sched/task.h>
22 #include <linux/module.h>
23 #include <linux/bitops.h>
24 #include <linux/mount.h>
25 #include <linux/nsproxy.h>
26 #include <linux/uidgid.h>
27 #include <net/net_namespace.h>
28 #include <linux/seq_file.h>
32 static inline struct net *PDE_NET(struct proc_dir_entry *pde)
34 return pde->parent->data;
37 static struct net *get_proc_net(const struct inode *inode)
39 return maybe_get_net(PDE_NET(PDE(inode)));
42 static int seq_open_net(struct inode *inode, struct file *file)
44 unsigned int state_size = PDE(inode)->state_size;
45 struct seq_net_private *p;
48 WARN_ON_ONCE(state_size < sizeof(*p));
50 if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
53 net = get_proc_net(inode);
57 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
68 static int seq_release_net(struct inode *ino, struct file *f)
70 struct seq_file *seq = f->private_data;
72 put_net(seq_file_net(seq));
73 seq_release_private(ino, f);
77 static const struct file_operations proc_net_seq_fops = {
80 .write = proc_simple_write,
82 .release = seq_release_net,
85 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
86 struct proc_dir_entry *parent, const struct seq_operations *ops,
87 unsigned int state_size, void *data)
89 struct proc_dir_entry *p;
91 p = proc_create_reg(name, mode, &parent, data);
95 p->proc_fops = &proc_net_seq_fops;
97 p->state_size = state_size;
98 return proc_register(parent, p);
100 EXPORT_SYMBOL_GPL(proc_create_net_data);
103 * proc_create_net_data_write - Create a writable net_ns-specific proc file
104 * @name: The name of the file.
105 * @mode: The file's access mode.
106 * @parent: The parent directory in which to create.
107 * @ops: The seq_file ops with which to read the file.
108 * @write: The write method which which to 'modify' the file.
109 * @data: Data for retrieval by PDE_DATA().
111 * Create a network namespaced proc file in the @parent directory with the
112 * specified @name and @mode that allows reading of a file that displays a
113 * series of elements and also provides for the file accepting writes that have
114 * some arbitrary effect.
116 * The functions in the @ops table are used to iterate over items to be
117 * presented and extract the readable content using the seq_file interface.
119 * The @write function is called with the data copied into a kernel space
120 * scratch buffer and has a NUL appended for convenience. The buffer may be
121 * modified by the @write function. @write should return 0 on success.
123 * The @data value is accessible from the @show and @write functions by calling
124 * PDE_DATA() on the file inode. The network namespace must be accessed by
125 * calling seq_file_net() on the seq_file struct.
127 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
128 struct proc_dir_entry *parent,
129 const struct seq_operations *ops,
131 unsigned int state_size, void *data)
133 struct proc_dir_entry *p;
135 p = proc_create_reg(name, mode, &parent, data);
139 p->proc_fops = &proc_net_seq_fops;
141 p->state_size = state_size;
143 return proc_register(parent, p);
145 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
147 static int single_open_net(struct inode *inode, struct file *file)
149 struct proc_dir_entry *de = PDE(inode);
153 net = get_proc_net(inode);
157 err = single_open(file, de->single_show, net);
163 static int single_release_net(struct inode *ino, struct file *f)
165 struct seq_file *seq = f->private_data;
166 put_net(seq->private);
167 return single_release(ino, f);
170 static const struct file_operations proc_net_single_fops = {
171 .open = single_open_net,
173 .write = proc_simple_write,
175 .release = single_release_net,
178 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
179 struct proc_dir_entry *parent,
180 int (*show)(struct seq_file *, void *), void *data)
182 struct proc_dir_entry *p;
184 p = proc_create_reg(name, mode, &parent, data);
188 p->proc_fops = &proc_net_single_fops;
189 p->single_show = show;
190 return proc_register(parent, p);
192 EXPORT_SYMBOL_GPL(proc_create_net_single);
195 * proc_create_net_single_write - Create a writable net_ns-specific proc file
196 * @name: The name of the file.
197 * @mode: The file's access mode.
198 * @parent: The parent directory in which to create.
199 * @show: The seqfile show method with which to read the file.
200 * @write: The write method which which to 'modify' the file.
201 * @data: Data for retrieval by PDE_DATA().
203 * Create a network-namespaced proc file in the @parent directory with the
204 * specified @name and @mode that allows reading of a file that displays a
205 * single element rather than a series and also provides for the file accepting
206 * writes that have some arbitrary effect.
208 * The @show function is called to extract the readable content via the
209 * seq_file interface.
211 * The @write function is called with the data copied into a kernel space
212 * scratch buffer and has a NUL appended for convenience. The buffer may be
213 * modified by the @write function. @write should return 0 on success.
215 * The @data value is accessible from the @show and @write functions by calling
216 * PDE_DATA() on the file inode. The network namespace must be accessed by
217 * calling seq_file_single_net() on the seq_file struct.
219 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
220 struct proc_dir_entry *parent,
221 int (*show)(struct seq_file *, void *),
225 struct proc_dir_entry *p;
227 p = proc_create_reg(name, mode, &parent, data);
231 p->proc_fops = &proc_net_single_fops;
232 p->single_show = show;
234 return proc_register(parent, p);
236 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
238 static struct net *get_proc_task_net(struct inode *dir)
240 struct task_struct *task;
242 struct net *net = NULL;
245 task = pid_task(proc_pid(dir), PIDTYPE_PID);
250 net = get_net(ns->net_ns);
258 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
259 struct dentry *dentry, unsigned int flags)
264 de = ERR_PTR(-ENOENT);
265 net = get_proc_task_net(dir);
267 de = proc_lookup_de(dir, dentry, net->proc_net);
273 static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
274 u32 request_mask, unsigned int query_flags)
276 struct inode *inode = d_inode(path->dentry);
279 net = get_proc_task_net(inode);
281 generic_fillattr(inode, stat);
284 stat->nlink = net->proc_net->nlink;
291 const struct inode_operations proc_net_inode_operations = {
292 .lookup = proc_tgid_net_lookup,
293 .getattr = proc_tgid_net_getattr,
296 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
302 net = get_proc_task_net(file_inode(file));
304 ret = proc_readdir_de(file, ctx, net->proc_net);
310 const struct file_operations proc_net_operations = {
311 .llseek = generic_file_llseek,
312 .read = generic_read_dir,
313 .iterate_shared = proc_tgid_net_readdir,
316 static __net_init int proc_net_ns_init(struct net *net)
318 struct proc_dir_entry *netd, *net_statd;
324 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
328 netd->subdir = RB_ROOT;
332 netd->parent = &proc_root;
333 netd->name = netd->inline_name;
334 memcpy(netd->name, "net", 4);
336 uid = make_kuid(net->user_ns, 0);
340 gid = make_kgid(net->user_ns, 0);
344 proc_set_user(netd, uid, gid);
346 /* Seed dentry revalidation for /proc/${pid}/net */
347 pde_force_lookup(netd);
350 net_statd = proc_net_mkdir(net, "stat", netd);
354 net->proc_net = netd;
355 net->proc_net_stat = net_statd;
364 static __net_exit void proc_net_ns_exit(struct net *net)
366 remove_proc_entry("stat", net->proc_net);
367 pde_free(net->proc_net);
370 static struct pernet_operations __net_initdata proc_net_ns_ops = {
371 .init = proc_net_ns_init,
372 .exit = proc_net_ns_exit,
375 int __init proc_net_init(void)
377 proc_symlink("net", NULL, "self/net");
379 return register_pernet_subsys(&proc_net_ns_ops);