1 // SPDX-License-Identifier: GPL-2.0-only
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);
64 netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL);
69 static void seq_file_net_put_net(struct seq_file *seq)
72 struct seq_net_private *priv = seq->private;
74 put_net_track(priv->net, &priv->ns_tracker);
80 static int seq_release_net(struct inode *ino, struct file *f)
82 struct seq_file *seq = f->private_data;
84 seq_file_net_put_net(seq);
85 seq_release_private(ino, f);
89 static const struct proc_ops proc_net_seq_ops = {
90 .proc_open = seq_open_net,
91 .proc_read = seq_read,
92 .proc_write = proc_simple_write,
93 .proc_lseek = seq_lseek,
94 .proc_release = seq_release_net,
97 int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux)
100 struct seq_net_private *p = priv_data;
102 p->net = get_net_track(current->nsproxy->net_ns, &p->ns_tracker,
108 void bpf_iter_fini_seq_net(void *priv_data)
111 struct seq_net_private *p = priv_data;
113 put_net_track(p->net, &p->ns_tracker);
117 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
118 struct proc_dir_entry *parent, const struct seq_operations *ops,
119 unsigned int state_size, void *data)
121 struct proc_dir_entry *p;
123 p = proc_create_reg(name, mode, &parent, data);
127 p->proc_ops = &proc_net_seq_ops;
129 p->state_size = state_size;
130 return proc_register(parent, p);
132 EXPORT_SYMBOL_GPL(proc_create_net_data);
135 * proc_create_net_data_write - Create a writable net_ns-specific proc file
136 * @name: The name of the file.
137 * @mode: The file's access mode.
138 * @parent: The parent directory in which to create.
139 * @ops: The seq_file ops with which to read the file.
140 * @write: The write method with which to 'modify' the file.
141 * @data: Data for retrieval by pde_data().
143 * Create a network namespaced proc file in the @parent directory with the
144 * specified @name and @mode that allows reading of a file that displays a
145 * series of elements and also provides for the file accepting writes that have
146 * some arbitrary effect.
148 * The functions in the @ops table are used to iterate over items to be
149 * presented and extract the readable content using the seq_file interface.
151 * The @write function is called with the data copied into a kernel space
152 * scratch buffer and has a NUL appended for convenience. The buffer may be
153 * modified by the @write function. @write should return 0 on success.
155 * The @data value is accessible from the @show and @write functions by calling
156 * pde_data() on the file inode. The network namespace must be accessed by
157 * calling seq_file_net() on the seq_file struct.
159 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
160 struct proc_dir_entry *parent,
161 const struct seq_operations *ops,
163 unsigned int state_size, void *data)
165 struct proc_dir_entry *p;
167 p = proc_create_reg(name, mode, &parent, data);
171 p->proc_ops = &proc_net_seq_ops;
173 p->state_size = state_size;
175 return proc_register(parent, p);
177 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
179 static int single_open_net(struct inode *inode, struct file *file)
181 struct proc_dir_entry *de = PDE(inode);
185 net = get_proc_net(inode);
189 err = single_open(file, de->single_show, net);
195 static int single_release_net(struct inode *ino, struct file *f)
197 struct seq_file *seq = f->private_data;
198 put_net(seq->private);
199 return single_release(ino, f);
202 static const struct proc_ops proc_net_single_ops = {
203 .proc_open = single_open_net,
204 .proc_read = seq_read,
205 .proc_write = proc_simple_write,
206 .proc_lseek = seq_lseek,
207 .proc_release = single_release_net,
210 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
211 struct proc_dir_entry *parent,
212 int (*show)(struct seq_file *, void *), void *data)
214 struct proc_dir_entry *p;
216 p = proc_create_reg(name, mode, &parent, data);
220 p->proc_ops = &proc_net_single_ops;
221 p->single_show = show;
222 return proc_register(parent, p);
224 EXPORT_SYMBOL_GPL(proc_create_net_single);
227 * proc_create_net_single_write - Create a writable net_ns-specific proc file
228 * @name: The name of the file.
229 * @mode: The file's access mode.
230 * @parent: The parent directory in which to create.
231 * @show: The seqfile show method with which to read the file.
232 * @write: The write method with which to 'modify' the file.
233 * @data: Data for retrieval by pde_data().
235 * Create a network-namespaced proc file in the @parent directory with the
236 * specified @name and @mode that allows reading of a file that displays a
237 * single element rather than a series and also provides for the file accepting
238 * writes that have some arbitrary effect.
240 * The @show function is called to extract the readable content via the
241 * seq_file interface.
243 * The @write function is called with the data copied into a kernel space
244 * scratch buffer and has a NUL appended for convenience. The buffer may be
245 * modified by the @write function. @write should return 0 on success.
247 * The @data value is accessible from the @show and @write functions by calling
248 * pde_data() on the file inode. The network namespace must be accessed by
249 * calling seq_file_single_net() on the seq_file struct.
251 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
252 struct proc_dir_entry *parent,
253 int (*show)(struct seq_file *, void *),
257 struct proc_dir_entry *p;
259 p = proc_create_reg(name, mode, &parent, data);
263 p->proc_ops = &proc_net_single_ops;
264 p->single_show = show;
266 return proc_register(parent, p);
268 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
270 static struct net *get_proc_task_net(struct inode *dir)
272 struct task_struct *task;
274 struct net *net = NULL;
277 task = pid_task(proc_pid(dir), PIDTYPE_PID);
282 net = get_net(ns->net_ns);
290 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
291 struct dentry *dentry, unsigned int flags)
296 de = ERR_PTR(-ENOENT);
297 net = get_proc_task_net(dir);
299 de = proc_lookup_de(dir, dentry, net->proc_net);
305 static int proc_tgid_net_getattr(struct user_namespace *mnt_userns,
306 const struct path *path, struct kstat *stat,
307 u32 request_mask, unsigned int query_flags)
309 struct inode *inode = d_inode(path->dentry);
312 net = get_proc_task_net(inode);
314 generic_fillattr(&init_user_ns, inode, stat);
317 stat->nlink = net->proc_net->nlink;
324 const struct inode_operations proc_net_inode_operations = {
325 .lookup = proc_tgid_net_lookup,
326 .getattr = proc_tgid_net_getattr,
329 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
335 net = get_proc_task_net(file_inode(file));
337 ret = proc_readdir_de(file, ctx, net->proc_net);
343 const struct file_operations proc_net_operations = {
344 .llseek = generic_file_llseek,
345 .read = generic_read_dir,
346 .iterate_shared = proc_tgid_net_readdir,
349 static __net_init int proc_net_ns_init(struct net *net)
351 struct proc_dir_entry *netd, *net_statd;
357 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
361 netd->subdir = RB_ROOT;
365 netd->parent = &proc_root;
366 netd->name = netd->inline_name;
367 memcpy(netd->name, "net", 4);
369 uid = make_kuid(net->user_ns, 0);
373 gid = make_kgid(net->user_ns, 0);
377 proc_set_user(netd, uid, gid);
380 net_statd = proc_net_mkdir(net, "stat", netd);
384 net->proc_net = netd;
385 net->proc_net_stat = net_statd;
394 static __net_exit void proc_net_ns_exit(struct net *net)
396 remove_proc_entry("stat", net->proc_net);
397 pde_free(net->proc_net);
400 static struct pernet_operations __net_initdata proc_net_ns_ops = {
401 .init = proc_net_ns_init,
402 .exit = proc_net_ns_exit,
405 int __init proc_net_init(void)
407 proc_symlink("net", NULL, "self/net");
409 return register_pernet_subsys(&proc_net_ns_ops);