David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 12 | |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/proc_fs.h> |
| 16 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 18 | #include <linux/init.h> |
| 19 | #include <linux/sched.h> |
Ingo Molnar | f719ff9b | 2017-02-06 10:57:33 +0100 | [diff] [blame] | 20 | #include <linux/sched/task.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/bitops.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 23 | #include <linux/mount.h> |
| 24 | #include <linux/nsproxy.h> |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 25 | #include <linux/uidgid.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 26 | #include <net/net_namespace.h> |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 27 | #include <linux/seq_file.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 28 | |
| 29 | #include "internal.h" |
| 30 | |
David Howells | 4abfd02 | 2013-04-12 02:09:03 +0100 | [diff] [blame] | 31 | static inline struct net *PDE_NET(struct proc_dir_entry *pde) |
| 32 | { |
| 33 | return pde->parent->data; |
| 34 | } |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 35 | |
Adrian Bunk | 8086cd4 | 2008-07-22 14:19:19 -0700 | [diff] [blame] | 36 | static struct net *get_proc_net(const struct inode *inode) |
| 37 | { |
| 38 | return maybe_get_net(PDE_NET(PDE(inode))); |
| 39 | } |
| 40 | |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 41 | static int proc_net_d_revalidate(struct dentry *dentry, unsigned int flags) |
| 42 | { |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static const struct dentry_operations proc_net_dentry_ops = { |
| 47 | .d_revalidate = proc_net_d_revalidate, |
| 48 | .d_delete = always_delete_dentry, |
| 49 | }; |
| 50 | |
| 51 | static void pde_force_lookup(struct proc_dir_entry *pde) |
| 52 | { |
| 53 | /* /proc/net/ entries can be changed under us by setns(CLONE_NEWNET) */ |
| 54 | pde->proc_dops = &proc_net_dentry_ops; |
| 55 | } |
| 56 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 57 | static int seq_open_net(struct inode *inode, struct file *file) |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 58 | { |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 59 | unsigned int state_size = PDE(inode)->state_size; |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 60 | struct seq_net_private *p; |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 61 | struct net *net; |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 62 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 63 | WARN_ON_ONCE(state_size < sizeof(*p)); |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 64 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 65 | if (file->f_mode & FMODE_WRITE && !PDE(inode)->write) |
| 66 | return -EACCES; |
| 67 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 68 | net = get_proc_net(inode); |
| 69 | if (!net) |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 70 | return -ENXIO; |
| 71 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 72 | p = __seq_open_private(file, PDE(inode)->seq_ops, state_size); |
| 73 | if (!p) { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 74 | put_net(net); |
| 75 | return -ENOMEM; |
| 76 | } |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 77 | #ifdef CONFIG_NET_NS |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 78 | p->net = net; |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 79 | #endif |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 80 | return 0; |
| 81 | } |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 82 | |
| 83 | static int seq_release_net(struct inode *ino, struct file *f) |
| 84 | { |
| 85 | struct seq_file *seq = f->private_data; |
| 86 | |
| 87 | put_net(seq_file_net(seq)); |
| 88 | seq_release_private(ino, f); |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static const struct file_operations proc_net_seq_fops = { |
| 93 | .open = seq_open_net, |
| 94 | .read = seq_read, |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 95 | .write = proc_simple_write, |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 96 | .llseek = seq_lseek, |
| 97 | .release = seq_release_net, |
| 98 | }; |
| 99 | |
| 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) |
| 103 | { |
| 104 | struct proc_dir_entry *p; |
| 105 | |
| 106 | p = proc_create_reg(name, mode, &parent, data); |
| 107 | if (!p) |
| 108 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 109 | pde_force_lookup(p); |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 110 | p->proc_fops = &proc_net_seq_fops; |
| 111 | p->seq_ops = ops; |
| 112 | p->state_size = state_size; |
| 113 | return proc_register(parent, p); |
| 114 | } |
| 115 | EXPORT_SYMBOL_GPL(proc_create_net_data); |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 116 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 117 | /** |
| 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(). |
| 125 | * |
| 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. |
| 130 | * |
| 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. |
| 133 | * |
| 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. |
| 137 | * |
| 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. |
| 141 | */ |
| 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, |
| 145 | proc_write_t write, |
| 146 | unsigned int state_size, void *data) |
| 147 | { |
| 148 | struct proc_dir_entry *p; |
| 149 | |
| 150 | p = proc_create_reg(name, mode, &parent, data); |
| 151 | if (!p) |
| 152 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 153 | pde_force_lookup(p); |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 154 | p->proc_fops = &proc_net_seq_fops; |
| 155 | p->seq_ops = ops; |
| 156 | p->state_size = state_size; |
| 157 | p->write = write; |
| 158 | return proc_register(parent, p); |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(proc_create_net_data_write); |
| 161 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 162 | static int single_open_net(struct inode *inode, struct file *file) |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 163 | { |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 164 | struct proc_dir_entry *de = PDE(inode); |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 165 | struct net *net; |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 166 | int err; |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 167 | |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 168 | net = get_proc_net(inode); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 169 | if (!net) |
| 170 | return -ENXIO; |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 171 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 172 | err = single_open(file, de->single_show, net); |
| 173 | if (err) |
| 174 | put_net(net); |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 175 | return err; |
| 176 | } |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 177 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 178 | static int single_release_net(struct inode *ino, struct file *f) |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 179 | { |
| 180 | struct seq_file *seq = f->private_data; |
| 181 | put_net(seq->private); |
| 182 | return single_release(ino, f); |
| 183 | } |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 184 | |
| 185 | static const struct file_operations proc_net_single_fops = { |
| 186 | .open = single_open_net, |
| 187 | .read = seq_read, |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 188 | .write = proc_simple_write, |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 189 | .llseek = seq_lseek, |
| 190 | .release = single_release_net, |
| 191 | }; |
| 192 | |
| 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) |
| 196 | { |
| 197 | struct proc_dir_entry *p; |
| 198 | |
| 199 | p = proc_create_reg(name, mode, &parent, data); |
| 200 | if (!p) |
| 201 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 202 | pde_force_lookup(p); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 203 | p->proc_fops = &proc_net_single_fops; |
| 204 | p->single_show = show; |
| 205 | return proc_register(parent, p); |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(proc_create_net_single); |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 208 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 209 | /** |
| 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(). |
| 217 | * |
| 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. |
| 222 | * |
| 223 | * The @show function is called to extract the readable content via the |
| 224 | * seq_file interface. |
| 225 | * |
| 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. |
| 229 | * |
| 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. |
| 233 | */ |
| 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 *), |
| 237 | proc_write_t write, |
| 238 | void *data) |
| 239 | { |
| 240 | struct proc_dir_entry *p; |
| 241 | |
| 242 | p = proc_create_reg(name, mode, &parent, data); |
| 243 | if (!p) |
| 244 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 245 | pde_force_lookup(p); |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 246 | p->proc_fops = &proc_net_single_fops; |
| 247 | p->single_show = show; |
| 248 | p->write = write; |
| 249 | return proc_register(parent, p); |
| 250 | } |
| 251 | EXPORT_SYMBOL_GPL(proc_create_net_single_write); |
| 252 | |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 253 | static struct net *get_proc_task_net(struct inode *dir) |
| 254 | { |
| 255 | struct task_struct *task; |
| 256 | struct nsproxy *ns; |
| 257 | struct net *net = NULL; |
| 258 | |
| 259 | rcu_read_lock(); |
| 260 | task = pid_task(proc_pid(dir), PIDTYPE_PID); |
| 261 | if (task != NULL) { |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 262 | task_lock(task); |
| 263 | ns = task->nsproxy; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 264 | if (ns != NULL) |
| 265 | net = get_net(ns->net_ns); |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 266 | task_unlock(task); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 267 | } |
| 268 | rcu_read_unlock(); |
| 269 | |
| 270 | return net; |
| 271 | } |
| 272 | |
| 273 | static struct dentry *proc_tgid_net_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 274 | struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 275 | { |
| 276 | struct dentry *de; |
| 277 | struct net *net; |
| 278 | |
| 279 | de = ERR_PTR(-ENOENT); |
| 280 | net = get_proc_task_net(dir); |
| 281 | if (net != NULL) { |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 282 | de = proc_lookup_de(dir, dentry, net->proc_net); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 283 | put_net(net); |
| 284 | } |
| 285 | return de; |
| 286 | } |
| 287 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 288 | static int proc_tgid_net_getattr(const struct path *path, struct kstat *stat, |
| 289 | u32 request_mask, unsigned int query_flags) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 290 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 291 | struct inode *inode = d_inode(path->dentry); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 292 | struct net *net; |
| 293 | |
| 294 | net = get_proc_task_net(inode); |
| 295 | |
| 296 | generic_fillattr(inode, stat); |
| 297 | |
| 298 | if (net != NULL) { |
| 299 | stat->nlink = net->proc_net->nlink; |
| 300 | put_net(net); |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | const struct inode_operations proc_net_inode_operations = { |
| 307 | .lookup = proc_tgid_net_lookup, |
| 308 | .getattr = proc_tgid_net_getattr, |
| 309 | }; |
| 310 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 311 | static int proc_tgid_net_readdir(struct file *file, struct dir_context *ctx) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 312 | { |
| 313 | int ret; |
| 314 | struct net *net; |
| 315 | |
| 316 | ret = -EINVAL; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 317 | net = get_proc_task_net(file_inode(file)); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 318 | if (net != NULL) { |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 319 | ret = proc_readdir_de(file, ctx, net->proc_net); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 320 | put_net(net); |
| 321 | } |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | const struct file_operations proc_net_operations = { |
Alexey Dobriyan | b4df2b9 | 2008-10-27 22:48:36 +0300 | [diff] [blame] | 326 | .llseek = generic_file_llseek, |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 327 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 328 | .iterate_shared = proc_tgid_net_readdir, |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 329 | }; |
| 330 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 331 | static __net_init int proc_net_ns_init(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 332 | { |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 333 | struct proc_dir_entry *netd, *net_statd; |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 334 | kuid_t uid; |
| 335 | kgid_t gid; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 336 | int err; |
| 337 | |
| 338 | err = -ENOMEM; |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 339 | netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 340 | if (!netd) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 341 | goto out; |
| 342 | |
Alexey Dobriyan | 4f11343 | 2018-04-10 16:32:20 -0700 | [diff] [blame] | 343 | netd->subdir = RB_ROOT; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 344 | netd->data = net; |
| 345 | netd->nlink = 2; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 346 | netd->namelen = 3; |
| 347 | netd->parent = &proc_root; |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 348 | netd->name = netd->inline_name; |
David Howells | 09570f9 | 2011-07-27 21:47:03 +0300 | [diff] [blame] | 349 | memcpy(netd->name, "net", 4); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 350 | |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 351 | uid = make_kuid(net->user_ns, 0); |
| 352 | if (!uid_valid(uid)) |
| 353 | uid = netd->uid; |
| 354 | |
| 355 | gid = make_kgid(net->user_ns, 0); |
| 356 | if (!gid_valid(gid)) |
| 357 | gid = netd->gid; |
| 358 | |
| 359 | proc_set_user(netd, uid, gid); |
| 360 | |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 361 | err = -EEXIST; |
Denis V. Lunev | e5d69b9 | 2008-01-10 03:51:41 -0800 | [diff] [blame] | 362 | net_statd = proc_net_mkdir(net, "stat", netd); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 363 | if (!net_statd) |
| 364 | goto free_net; |
| 365 | |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 366 | net->proc_net = netd; |
| 367 | net->proc_net_stat = net_statd; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 368 | return 0; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 369 | |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 370 | free_net: |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 371 | pde_free(netd); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 372 | out: |
| 373 | return err; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 374 | } |
| 375 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 376 | static __net_exit void proc_net_ns_exit(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 377 | { |
| 378 | remove_proc_entry("stat", net->proc_net); |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 379 | pde_free(net->proc_net); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 382 | static struct pernet_operations __net_initdata proc_net_ns_ops = { |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 383 | .init = proc_net_ns_init, |
| 384 | .exit = proc_net_ns_exit, |
| 385 | }; |
| 386 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 387 | int __init proc_net_init(void) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 388 | { |
Linus Torvalds | 155134f | 2014-08-10 21:24:59 -0700 | [diff] [blame] | 389 | proc_symlink("net", NULL, "self/net"); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 390 | |
| 391 | return register_pernet_subsys(&proc_net_ns_ops); |
| 392 | } |