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 proc_net_d_revalidate(struct dentry *dentry, unsigned int flags)
46 static const struct dentry_operations proc_net_dentry_ops = {
47 .d_revalidate = proc_net_d_revalidate,
48 .d_delete = always_delete_dentry,
51 static void pde_force_lookup(struct proc_dir_entry *pde)
53 /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */
54 pde->proc_dops = &proc_net_dentry_ops;
57 static int seq_open_net(struct inode *inode, struct file *file)
59 unsigned int state_size = PDE(inode)->state_size;
60 struct seq_net_private *p;
63 WARN_ON_ONCE(state_size < sizeof(*p));
65 if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
68 net = get_proc_net(inode);
72 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
83 static int seq_release_net(struct inode *ino, struct file *f)
85 struct seq_file *seq = f->private_data;
87 put_net(seq_file_net(seq));
88 seq_release_private(ino, f);
92 static const struct file_operations proc_net_seq_fops = {
95 .write = proc_simple_write,
97 .release = seq_release_net,
100 struct proc_dir_entry *proc_create_net_data(const char *name, umode_t mode,
101 struct proc_dir_entry *parent, const struct seq_operations *ops,
102 unsigned int state_size, void *data)
104 struct proc_dir_entry *p;
106 p = proc_create_reg(name, mode, &parent, data);
110 p->proc_fops = &proc_net_seq_fops;
112 p->state_size = state_size;
113 return proc_register(parent, p);
115 EXPORT_SYMBOL_GPL(proc_create_net_data);
118 * proc_create_net_data_write - Create a writable net_ns-specific proc file
119 * @name: The name of the file.
120 * @mode: The file's access mode.
121 * @parent: The parent directory in which to create.
122 * @ops: The seq_file ops with which to read the file.
123 * @write: The write method which which to 'modify' the file.
124 * @data: Data for retrieval by PDE_DATA().
126 * Create a network namespaced proc file in the @parent directory with the
127 * specified @name and @mode that allows reading of a file that displays a
128 * series of elements and also provides for the file accepting writes that have
129 * some arbitrary effect.
131 * The functions in the @ops table are used to iterate over items to be
132 * presented and extract the readable content using the seq_file interface.
134 * The @write function is called with the data copied into a kernel space
135 * scratch buffer and has a NUL appended for convenience. The buffer may be
136 * modified by the @write function. @write should return 0 on success.
138 * The @data value is accessible from the @show and @write functions by calling
139 * PDE_DATA() on the file inode. The network namespace must be accessed by
140 * calling seq_file_net() on the seq_file struct.
142 struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
143 struct proc_dir_entry *parent,
144 const struct seq_operations *ops,
146 unsigned int state_size, void *data)
148 struct proc_dir_entry *p;
150 p = proc_create_reg(name, mode, &parent, data);
154 p->proc_fops = &proc_net_seq_fops;
156 p->state_size = state_size;
158 return proc_register(parent, p);
160 EXPORT_SYMBOL_GPL(proc_create_net_data_write);
162 static int single_open_net(struct inode *inode, struct file *file)
164 struct proc_dir_entry *de = PDE(inode);
168 net = get_proc_net(inode);
172 err = single_open(file, de->single_show, net);
178 static int single_release_net(struct inode *ino, struct file *f)
180 struct seq_file *seq = f->private_data;
181 put_net(seq->private);
182 return single_release(ino, f);
185 static const struct file_operations proc_net_single_fops = {
186 .open = single_open_net,
188 .write = proc_simple_write,
190 .release = single_release_net,
193 struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
194 struct proc_dir_entry *parent,
195 int (*show)(struct seq_file *, void *), void *data)
197 struct proc_dir_entry *p;
199 p = proc_create_reg(name, mode, &parent, data);
203 p->proc_fops = &proc_net_single_fops;
204 p->single_show = show;
205 return proc_register(parent, p);
207 EXPORT_SYMBOL_GPL(proc_create_net_single);
210 * proc_create_net_single_write - Create a writable net_ns-specific proc file
211 * @name: The name of the file.
212 * @mode: The file's access mode.
213 * @parent: The parent directory in which to create.
214 * @show: The seqfile show method with which to read the file.
215 * @write: The write method which which to 'modify' the file.
216 * @data: Data for retrieval by PDE_DATA().
218 * Create a network-namespaced proc file in the @parent directory with the
219 * specified @name and @mode that allows reading of a file that displays a
220 * single element rather than a series and also provides for the file accepting
221 * writes that have some arbitrary effect.
223 * The @show function is called to extract the readable content via the
224 * seq_file interface.
226 * The @write function is called with the data copied into a kernel space
227 * scratch buffer and has a NUL appended for convenience. The buffer may be
228 * modified by the @write function. @write should return 0 on success.
230 * The @data value is accessible from the @show and @write functions by calling
231 * PDE_DATA() on the file inode. The network namespace must be accessed by
232 * calling seq_file_single_net() on the seq_file struct.
234 struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
235 struct proc_dir_entry *parent,
236 int (*show)(struct seq_file *, void *),
240 struct proc_dir_entry *p;
242 p = proc_create_reg(name, mode, &parent, data);
246 p->proc_fops = &proc_net_single_fops;
247 p->single_show = show;
249 return proc_register(parent, p);
251 EXPORT_SYMBOL_GPL(proc_create_net_single_write);
253 static struct net *get_proc_task_net(struct inode *dir)
255 struct task_struct *task;
257 struct net *net = NULL;
260 task = pid_task(proc_pid(dir), PIDTYPE_PID);
265 net = get_net(ns->net_ns);
273 static struct dentry *proc_tgid_net_lookup(struct inode *dir,
274 struct dentry *dentry, unsigned int flags)
279 de = ERR_PTR(-ENOENT);
280 net = get_proc_task_net(dir);
282 de = proc_lookup_de(dir, dentry, net->proc_net);
288 static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
289 u32 request_mask, unsigned int query_flags)
291 struct inode *inode = d_inode(path->dentry);
294 net = get_proc_task_net(inode);
296 generic_fillattr(inode, stat);
299 stat->nlink = net->proc_net->nlink;
306 const struct inode_operations proc_net_inode_operations = {
307 .lookup = proc_tgid_net_lookup,
308 .getattr = proc_tgid_net_getattr,
311 static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
317 net = get_proc_task_net(file_inode(file));
319 ret = proc_readdir_de(file, ctx, net->proc_net);
325 const struct file_operations proc_net_operations = {
326 .llseek = generic_file_llseek,
327 .read = generic_read_dir,
328 .iterate_shared = proc_tgid_net_readdir,
331 static __net_init int proc_net_ns_init(struct net *net)
333 struct proc_dir_entry *netd, *net_statd;
339 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
343 netd->subdir = RB_ROOT;
347 netd->parent = &proc_root;
348 netd->name = netd->inline_name;
349 memcpy(netd->name, "net", 4);
351 uid = make_kuid(net->user_ns, 0);
355 gid = make_kgid(net->user_ns, 0);
359 proc_set_user(netd, uid, gid);
362 net_statd = proc_net_mkdir(net, "stat", netd);
366 net->proc_net = netd;
367 net->proc_net_stat = net_statd;
376 static __net_exit void proc_net_ns_exit(struct net *net)
378 remove_proc_entry("stat", net->proc_net);
379 pde_free(net->proc_net);
382 static struct pernet_operations __net_initdata proc_net_ns_ops = {
383 .init = proc_net_ns_init,
384 .exit = proc_net_ns_exit,
387 int __init proc_net_init(void)
389 proc_symlink("net", NULL, "self/net");
391 return register_pernet_subsys(&proc_net_ns_ops);