blob: d5e0fcb3439e91b8c722b7193c0d8a59e2ecbe98 [file] [log] [blame]
David S. Miller3c12afe2007-09-12 14:18:18 +02001/*
2 * linux/fs/proc/net.c
3 *
4 * Copyright (C) 2007
5 *
6 * Author: Eric Biederman <ebiederm@xmission.com>
7 *
8 * proc net directory handling functions
9 */
10
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080011#include <linux/uaccess.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020012
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/proc_fs.h>
16#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020018#include <linux/init.h>
19#include <linux/sched.h>
Ingo Molnarf719ff9b2017-02-06 10:57:33 +010020#include <linux/sched/task.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020021#include <linux/module.h>
22#include <linux/bitops.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020023#include <linux/mount.h>
24#include <linux/nsproxy.h>
Dmitry Torokhovc1104862016-08-10 14:36:01 -070025#include <linux/uidgid.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020026#include <net/net_namespace.h>
Denis V. Luneve372c412007-11-19 22:31:54 -080027#include <linux/seq_file.h>
David S. Miller3c12afe2007-09-12 14:18:18 +020028
29#include "internal.h"
30
David Howells4abfd022013-04-12 02:09:03 +010031static inline struct net *PDE_NET(struct proc_dir_entry *pde)
32{
33 return pde->parent->data;
34}
David S. Miller3c12afe2007-09-12 14:18:18 +020035
Adrian Bunk8086cd42008-07-22 14:19:19 -070036static struct net *get_proc_net(const struct inode *inode)
37{
38 return maybe_get_net(PDE_NET(PDE(inode)));
39}
40
Christoph Hellwigc3506372018-04-10 19:42:55 +020041static int seq_open_net(struct inode *inode, struct file *file)
Denis V. Luneve372c412007-11-19 22:31:54 -080042{
Christoph Hellwigc3506372018-04-10 19:42:55 +020043 unsigned int state_size = PDE(inode)->state_size;
Denis V. Luneve372c412007-11-19 22:31:54 -080044 struct seq_net_private *p;
Christoph Hellwigc3506372018-04-10 19:42:55 +020045 struct net *net;
Denis V. Luneve372c412007-11-19 22:31:54 -080046
Christoph Hellwigc3506372018-04-10 19:42:55 +020047 WARN_ON_ONCE(state_size < sizeof(*p));
Denis V. Luneve372c412007-11-19 22:31:54 -080048
David Howells564def72018-05-18 11:46:15 +010049 if (file->f_mode & FMODE_WRITE && !PDE(inode)->write)
50 return -EACCES;
51
Christoph Hellwigc3506372018-04-10 19:42:55 +020052 net = get_proc_net(inode);
53 if (!net)
Denis V. Luneve372c412007-11-19 22:31:54 -080054 return -ENXIO;
55
Christoph Hellwigc3506372018-04-10 19:42:55 +020056 p = __seq_open_private(file, PDE(inode)->seq_ops, state_size);
57 if (!p) {
Denis V. Luneve372c412007-11-19 22:31:54 -080058 put_net(net);
59 return -ENOMEM;
60 }
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +090061#ifdef CONFIG_NET_NS
Denis V. Luneve372c412007-11-19 22:31:54 -080062 p->net = net;
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +090063#endif
Denis V. Luneve372c412007-11-19 22:31:54 -080064 return 0;
65}
Christoph Hellwigc3506372018-04-10 19:42:55 +020066
67static int seq_release_net(struct inode *ino, struct file *f)
68{
69 struct seq_file *seq = f->private_data;
70
71 put_net(seq_file_net(seq));
72 seq_release_private(ino, f);
73 return 0;
74}
75
76static const struct file_operations proc_net_seq_fops = {
77 .open = seq_open_net,
78 .read = seq_read,
David Howells564def72018-05-18 11:46:15 +010079 .write = proc_simple_write,
Christoph Hellwigc3506372018-04-10 19:42:55 +020080 .llseek = seq_lseek,
81 .release = seq_release_net,
82};
83
84struct 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)
87{
88 struct proc_dir_entry *p;
89
90 p = proc_create_reg(name, mode, &parent, data);
91 if (!p)
92 return NULL;
93 p->proc_fops = &proc_net_seq_fops;
94 p->seq_ops = ops;
95 p->state_size = state_size;
96 return proc_register(parent, p);
97}
98EXPORT_SYMBOL_GPL(proc_create_net_data);
Denis V. Luneve372c412007-11-19 22:31:54 -080099
David Howells564def72018-05-18 11:46:15 +0100100/**
101 * proc_create_net_data_write - Create a writable net_ns-specific proc file
102 * @name: The name of the file.
103 * @mode: The file's access mode.
104 * @parent: The parent directory in which to create.
105 * @ops: The seq_file ops with which to read the file.
106 * @write: The write method which which to 'modify' the file.
107 * @data: Data for retrieval by PDE_DATA().
108 *
109 * Create a network namespaced proc file in the @parent directory with the
110 * specified @name and @mode that allows reading of a file that displays a
111 * series of elements and also provides for the file accepting writes that have
112 * some arbitrary effect.
113 *
114 * The functions in the @ops table are used to iterate over items to be
115 * presented and extract the readable content using the seq_file interface.
116 *
117 * The @write function is called with the data copied into a kernel space
118 * scratch buffer and has a NUL appended for convenience. The buffer may be
119 * modified by the @write function. @write should return 0 on success.
120 *
121 * The @data value is accessible from the @show and @write functions by calling
122 * PDE_DATA() on the file inode. The network namespace must be accessed by
123 * calling seq_file_net() on the seq_file struct.
124 */
125struct proc_dir_entry *proc_create_net_data_write(const char *name, umode_t mode,
126 struct proc_dir_entry *parent,
127 const struct seq_operations *ops,
128 proc_write_t write,
129 unsigned int state_size, void *data)
130{
131 struct proc_dir_entry *p;
132
133 p = proc_create_reg(name, mode, &parent, data);
134 if (!p)
135 return NULL;
136 p->proc_fops = &proc_net_seq_fops;
137 p->seq_ops = ops;
138 p->state_size = state_size;
139 p->write = write;
140 return proc_register(parent, p);
141}
142EXPORT_SYMBOL_GPL(proc_create_net_data_write);
143
Christoph Hellwig3617d942018-04-13 20:38:35 +0200144static int single_open_net(struct inode *inode, struct file *file)
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700145{
Christoph Hellwig3617d942018-04-13 20:38:35 +0200146 struct proc_dir_entry *de = PDE(inode);
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700147 struct net *net;
Christoph Hellwig3617d942018-04-13 20:38:35 +0200148 int err;
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700149
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700150 net = get_proc_net(inode);
Christoph Hellwig3617d942018-04-13 20:38:35 +0200151 if (!net)
152 return -ENXIO;
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700153
Christoph Hellwig3617d942018-04-13 20:38:35 +0200154 err = single_open(file, de->single_show, net);
155 if (err)
156 put_net(net);
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700157 return err;
158}
Pavel Emelyanovde05c552008-07-18 04:07:21 -0700159
Christoph Hellwig3617d942018-04-13 20:38:35 +0200160static int single_release_net(struct inode *ino, struct file *f)
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -0700161{
162 struct seq_file *seq = f->private_data;
163 put_net(seq->private);
164 return single_release(ino, f);
165}
Christoph Hellwig3617d942018-04-13 20:38:35 +0200166
167static const struct file_operations proc_net_single_fops = {
168 .open = single_open_net,
169 .read = seq_read,
David Howells564def72018-05-18 11:46:15 +0100170 .write = proc_simple_write,
Christoph Hellwig3617d942018-04-13 20:38:35 +0200171 .llseek = seq_lseek,
172 .release = single_release_net,
173};
174
175struct proc_dir_entry *proc_create_net_single(const char *name, umode_t mode,
176 struct proc_dir_entry *parent,
177 int (*show)(struct seq_file *, void *), void *data)
178{
179 struct proc_dir_entry *p;
180
181 p = proc_create_reg(name, mode, &parent, data);
182 if (!p)
183 return NULL;
184 p->proc_fops = &proc_net_single_fops;
185 p->single_show = show;
186 return proc_register(parent, p);
187}
188EXPORT_SYMBOL_GPL(proc_create_net_single);
Pavel Emelyanovb6fcbdb2008-07-18 04:07:44 -0700189
David Howells564def72018-05-18 11:46:15 +0100190/**
191 * proc_create_net_single_write - Create a writable net_ns-specific proc file
192 * @name: The name of the file.
193 * @mode: The file's access mode.
194 * @parent: The parent directory in which to create.
195 * @show: The seqfile show method with which to read the file.
196 * @write: The write method which which to 'modify' the file.
197 * @data: Data for retrieval by PDE_DATA().
198 *
199 * Create a network-namespaced proc file in the @parent directory with the
200 * specified @name and @mode that allows reading of a file that displays a
201 * single element rather than a series and also provides for the file accepting
202 * writes that have some arbitrary effect.
203 *
204 * The @show function is called to extract the readable content via the
205 * seq_file interface.
206 *
207 * The @write function is called with the data copied into a kernel space
208 * scratch buffer and has a NUL appended for convenience. The buffer may be
209 * modified by the @write function. @write should return 0 on success.
210 *
211 * The @data value is accessible from the @show and @write functions by calling
212 * PDE_DATA() on the file inode. The network namespace must be accessed by
213 * calling seq_file_single_net() on the seq_file struct.
214 */
215struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t mode,
216 struct proc_dir_entry *parent,
217 int (*show)(struct seq_file *, void *),
218 proc_write_t write,
219 void *data)
220{
221 struct proc_dir_entry *p;
222
223 p = proc_create_reg(name, mode, &parent, data);
224 if (!p)
225 return NULL;
226 p->proc_fops = &proc_net_single_fops;
227 p->single_show = show;
228 p->write = write;
229 return proc_register(parent, p);
230}
231EXPORT_SYMBOL_GPL(proc_create_net_single_write);
232
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800233static struct net *get_proc_task_net(struct inode *dir)
234{
235 struct task_struct *task;
236 struct nsproxy *ns;
237 struct net *net = NULL;
238
239 rcu_read_lock();
240 task = pid_task(proc_pid(dir), PIDTYPE_PID);
241 if (task != NULL) {
Eric W. Biederman728dba32014-02-03 19:13:49 -0800242 task_lock(task);
243 ns = task->nsproxy;
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800244 if (ns != NULL)
245 net = get_net(ns->net_ns);
Eric W. Biederman728dba32014-02-03 19:13:49 -0800246 task_unlock(task);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800247 }
248 rcu_read_unlock();
249
250 return net;
251}
252
253static struct dentry *proc_tgid_net_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -0400254 struct dentry *dentry, unsigned int flags)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800255{
256 struct dentry *de;
257 struct net *net;
258
259 de = ERR_PTR(-ENOENT);
260 net = get_proc_task_net(dir);
261 if (net != NULL) {
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800262 de = proc_lookup_de(dir, dentry, net->proc_net);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800263 put_net(net);
264 }
265 return de;
266}
267
David Howellsa528d352017-01-31 16:46:22 +0000268static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat,
269 u32 request_mask, unsigned int query_flags)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800270{
David Howellsa528d352017-01-31 16:46:22 +0000271 struct inode *inode = d_inode(path->dentry);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800272 struct net *net;
273
274 net = get_proc_task_net(inode);
275
276 generic_fillattr(inode, stat);
277
278 if (net != NULL) {
279 stat->nlink = net->proc_net->nlink;
280 put_net(net);
281 }
282
283 return 0;
284}
285
286const struct inode_operations proc_net_inode_operations = {
287 .lookup = proc_tgid_net_lookup,
288 .getattr = proc_tgid_net_getattr,
289};
290
Al Virof0c3b502013-05-16 12:07:31 -0400291static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800292{
293 int ret;
294 struct net *net;
295
296 ret = -EINVAL;
Al Virof0c3b502013-05-16 12:07:31 -0400297 net = get_proc_task_net(file_inode(file));
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800298 if (net != NULL) {
Alexey Dobriyan93ad5bc2018-02-06 15:37:31 -0800299 ret = proc_readdir_de(file, ctx, net->proc_net);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800300 put_net(net);
301 }
302 return ret;
303}
304
305const struct file_operations proc_net_operations = {
Alexey Dobriyanb4df2b92008-10-27 22:48:36 +0300306 .llseek = generic_file_llseek,
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800307 .read = generic_read_dir,
Al Virof50752e2016-04-20 17:13:54 -0400308 .iterate_shared = proc_tgid_net_readdir,
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800309};
310
Pavel Emelyanov46650792007-10-08 20:38:39 -0700311static __net_init int proc_net_ns_init(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +0200312{
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800313 struct proc_dir_entry *netd, *net_statd;
Dmitry Torokhovc1104862016-08-10 14:36:01 -0700314 kuid_t uid;
315 kgid_t gid;
David S. Miller3c12afe2007-09-12 14:18:18 +0200316 int err;
317
318 err = -ENOMEM;
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700319 netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL);
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800320 if (!netd)
David S. Miller3c12afe2007-09-12 14:18:18 +0200321 goto out;
322
Alexey Dobriyan4f113432018-04-10 16:32:20 -0700323 netd->subdir = RB_ROOT;
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800324 netd->data = net;
325 netd->nlink = 2;
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800326 netd->namelen = 3;
327 netd->parent = &proc_root;
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700328 netd->name = netd->inline_name;
David Howells09570f92011-07-27 21:47:03 +0300329 memcpy(netd->name, "net", 4);
David S. Miller3c12afe2007-09-12 14:18:18 +0200330
Dmitry Torokhovc1104862016-08-10 14:36:01 -0700331 uid = make_kuid(net->user_ns, 0);
332 if (!uid_valid(uid))
333 uid = netd->uid;
334
335 gid = make_kgid(net->user_ns, 0);
336 if (!gid_valid(gid))
337 gid = netd->gid;
338
339 proc_set_user(netd, uid, gid);
340
David S. Miller3c12afe2007-09-12 14:18:18 +0200341 err = -EEXIST;
Denis V. Luneve5d69b92008-01-10 03:51:41 -0800342 net_statd = proc_net_mkdir(net, "stat", netd);
David S. Miller3c12afe2007-09-12 14:18:18 +0200343 if (!net_statd)
344 goto free_net;
345
David S. Miller3c12afe2007-09-12 14:18:18 +0200346 net->proc_net = netd;
347 net->proc_net_stat = net_statd;
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800348 return 0;
David S. Miller3c12afe2007-09-12 14:18:18 +0200349
Pavel Emelyanove9720ac2008-03-07 11:08:40 -0800350free_net:
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700351 pde_free(netd);
David S. Miller3c12afe2007-09-12 14:18:18 +0200352out:
353 return err;
David S. Miller3c12afe2007-09-12 14:18:18 +0200354}
355
Pavel Emelyanov46650792007-10-08 20:38:39 -0700356static __net_exit void proc_net_ns_exit(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +0200357{
358 remove_proc_entry("stat", net->proc_net);
Alexey Dobriyanb4884f22018-04-10 16:31:52 -0700359 pde_free(net->proc_net);
David S. Miller3c12afe2007-09-12 14:18:18 +0200360}
361
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800362static struct pernet_operations __net_initdata proc_net_ns_ops = {
David S. Miller3c12afe2007-09-12 14:18:18 +0200363 .init = proc_net_ns_init,
364 .exit = proc_net_ns_exit,
365};
366
Pavel Emelyanov46650792007-10-08 20:38:39 -0700367int __init proc_net_init(void)
David S. Miller3c12afe2007-09-12 14:18:18 +0200368{
Linus Torvalds155134f2014-08-10 21:24:59 -0700369 proc_symlink("net", NULL, "self/net");
David S. Miller3c12afe2007-09-12 14:18:18 +0200370
371 return register_pernet_subsys(&proc_net_ns_ops);
372}