blob: 131f9c68be5f1d3b2fa036189f7864ca371c63ae [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
11#include <asm/uaccess.h>
12
13#include <linux/errno.h>
14#include <linux/time.h>
15#include <linux/proc_fs.h>
16#include <linux/stat.h>
17#include <linux/init.h>
18#include <linux/sched.h>
19#include <linux/module.h>
20#include <linux/bitops.h>
21#include <linux/smp_lock.h>
22#include <linux/mount.h>
23#include <linux/nsproxy.h>
24#include <net/net_namespace.h>
25
26#include "internal.h"
27
28
David S. Miller3c12afe2007-09-12 14:18:18 +020029struct proc_dir_entry *proc_net_fops_create(struct net *net,
30 const char *name, mode_t mode, const struct file_operations *fops)
31{
32 struct proc_dir_entry *res;
33
34 res = create_proc_entry(name, mode, net->proc_net);
35 if (res)
36 res->proc_fops = fops;
37 return res;
38}
Daniel Lezcano36ac3132007-09-12 14:51:47 +020039EXPORT_SYMBOL_GPL(proc_net_fops_create);
David S. Miller3c12afe2007-09-12 14:18:18 +020040
41void proc_net_remove(struct net *net, const char *name)
42{
43 remove_proc_entry(name, net->proc_net);
44}
Daniel Lezcano36ac3132007-09-12 14:51:47 +020045EXPORT_SYMBOL_GPL(proc_net_remove);
David S. Miller3c12afe2007-09-12 14:18:18 +020046
Eric W. Biederman077130c2007-09-13 09:18:57 +020047struct net *get_proc_net(const struct inode *inode)
48{
49 return maybe_get_net(PDE_NET(PDE(inode)));
50}
51EXPORT_SYMBOL_GPL(get_proc_net);
52
David S. Miller3c12afe2007-09-12 14:18:18 +020053static struct proc_dir_entry *proc_net_shadow;
54
55static struct dentry *proc_net_shadow_dentry(struct dentry *parent,
56 struct proc_dir_entry *de)
57{
58 struct dentry *shadow = NULL;
59 struct inode *inode;
60 if (!de)
61 goto out;
62 de_get(de);
63 inode = proc_get_inode(parent->d_inode->i_sb, de->low_ino, de);
64 if (!inode)
65 goto out_de_put;
66 shadow = d_alloc_name(parent, de->name);
67 if (!shadow)
68 goto out_iput;
69 shadow->d_op = parent->d_op; /* proc_dentry_operations */
70 d_instantiate(shadow, inode);
71out:
72 return shadow;
73out_iput:
74 iput(inode);
75out_de_put:
76 de_put(de);
77 goto out;
78}
79
80static void *proc_net_follow_link(struct dentry *parent, struct nameidata *nd)
81{
82 struct net *net = current->nsproxy->net_ns;
83 struct dentry *shadow;
84 shadow = proc_net_shadow_dentry(parent, net->proc_net);
85 if (!shadow)
86 return ERR_PTR(-ENOENT);
87
88 dput(nd->dentry);
89 /* My dentry count is 1 and that should be enough as the
90 * shadow dentry is thrown away immediately.
91 */
92 nd->dentry = shadow;
93 return NULL;
94}
95
96static struct dentry *proc_net_lookup(struct inode *dir, struct dentry *dentry,
97 struct nameidata *nd)
98{
99 struct net *net = current->nsproxy->net_ns;
100 struct dentry *shadow;
101
102 shadow = proc_net_shadow_dentry(nd->dentry, net->proc_net);
103 if (!shadow)
104 return ERR_PTR(-ENOENT);
105
106 dput(nd->dentry);
107 nd->dentry = shadow;
108
109 return shadow->d_inode->i_op->lookup(shadow->d_inode, dentry, nd);
110}
111
112static int proc_net_setattr(struct dentry *dentry, struct iattr *iattr)
113{
114 struct net *net = current->nsproxy->net_ns;
115 struct dentry *shadow;
116 int ret;
117
118 shadow = proc_net_shadow_dentry(dentry->d_parent, net->proc_net);
119 if (!shadow)
120 return -ENOENT;
121 ret = shadow->d_inode->i_op->setattr(shadow, iattr);
122 dput(shadow);
123 return ret;
124}
125
126static const struct file_operations proc_net_dir_operations = {
127 .read = generic_read_dir,
128};
129
130static struct inode_operations proc_net_dir_inode_operations = {
131 .follow_link = proc_net_follow_link,
132 .lookup = proc_net_lookup,
133 .setattr = proc_net_setattr,
134};
135
Pavel Emelyanov46650792007-10-08 20:38:39 -0700136static __net_init int proc_net_ns_init(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +0200137{
138 struct proc_dir_entry *root, *netd, *net_statd;
139 int err;
140
141 err = -ENOMEM;
142 root = kzalloc(sizeof(*root), GFP_KERNEL);
143 if (!root)
144 goto out;
145
146 err = -EEXIST;
147 netd = proc_mkdir("net", root);
148 if (!netd)
149 goto free_root;
150
151 err = -EEXIST;
152 net_statd = proc_mkdir("stat", netd);
153 if (!net_statd)
154 goto free_net;
155
156 root->data = net;
157 netd->data = net;
158 net_statd->data = net;
159
160 net->proc_net_root = root;
161 net->proc_net = netd;
162 net->proc_net_stat = net_statd;
163 err = 0;
164
165out:
166 return err;
167free_net:
168 remove_proc_entry("net", root);
169free_root:
170 kfree(root);
171 goto out;
172}
173
Pavel Emelyanov46650792007-10-08 20:38:39 -0700174static __net_exit void proc_net_ns_exit(struct net *net)
David S. Miller3c12afe2007-09-12 14:18:18 +0200175{
176 remove_proc_entry("stat", net->proc_net);
177 remove_proc_entry("net", net->proc_net_root);
178 kfree(net->proc_net_root);
179}
180
Denis V. Lunev022cbae2007-11-13 03:23:50 -0800181static struct pernet_operations __net_initdata proc_net_ns_ops = {
David S. Miller3c12afe2007-09-12 14:18:18 +0200182 .init = proc_net_ns_init,
183 .exit = proc_net_ns_exit,
184};
185
Pavel Emelyanov46650792007-10-08 20:38:39 -0700186int __init proc_net_init(void)
David S. Miller3c12afe2007-09-12 14:18:18 +0200187{
188 proc_net_shadow = proc_mkdir("net", NULL);
189 proc_net_shadow->proc_iops = &proc_net_dir_inode_operations;
190 proc_net_shadow->proc_fops = &proc_net_dir_operations;
191
192 return register_pernet_subsys(&proc_net_ns_ops);
193}