Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/proc/net.c |
| 4 | * |
| 5 | * Copyright (C) 2007 |
| 6 | * |
| 7 | * Author: Eric Biederman <ebiederm@xmission.com> |
| 8 | * |
| 9 | * proc net directory handling functions |
| 10 | */ |
| 11 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 12 | #include <linux/uaccess.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 13 | |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/time.h> |
| 16 | #include <linux/proc_fs.h> |
| 17 | #include <linux/stat.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/sched.h> |
Ingo Molnar | f719ff9b | 2017-02-06 10:57:33 +0100 | [diff] [blame] | 21 | #include <linux/sched/task.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/bitops.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 24 | #include <linux/mount.h> |
| 25 | #include <linux/nsproxy.h> |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 26 | #include <linux/uidgid.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 27 | #include <net/net_namespace.h> |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 28 | #include <linux/seq_file.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 29 | |
| 30 | #include "internal.h" |
| 31 | |
David Howells | 4abfd02 | 2013-04-12 02:09:03 +0100 | [diff] [blame] | 32 | static inline struct net *PDE_NET(struct proc_dir_entry *pde) |
| 33 | { |
| 34 | return pde->parent->data; |
| 35 | } |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 36 | |
Adrian Bunk | 8086cd4 | 2008-07-22 14:19:19 -0700 | [diff] [blame] | 37 | static struct net *get_proc_net(const struct inode *inode) |
| 38 | { |
| 39 | return maybe_get_net(PDE_NET(PDE(inode))); |
| 40 | } |
| 41 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 42 | static int seq_open_net(struct inode *inode, struct file *file) |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 43 | { |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 44 | unsigned int state_size = PDE(inode)->state_size; |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 45 | struct seq_net_private *p; |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 46 | struct net *net; |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 47 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 48 | WARN_ON_ONCE(state_size < sizeof(*p)); |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 49 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 50 | if (file->f_mode & FMODE_WRITE && !PDE(inode)->write) |
| 51 | return -EACCES; |
| 52 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 53 | net = get_proc_net(inode); |
| 54 | if (!net) |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 55 | return -ENXIO; |
| 56 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 57 | p = __seq_open_private(file, PDE(inode)->seq_ops, state_size); |
| 58 | if (!p) { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 59 | put_net(net); |
| 60 | return -ENOMEM; |
| 61 | } |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 62 | #ifdef CONFIG_NET_NS |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 63 | p->net = net; |
Eric Dumazet | 04a931e | 2021-12-09 23:44:23 -0800 | [diff] [blame] | 64 | netns_tracker_alloc(net, &p->ns_tracker, GFP_KERNEL); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 65 | #endif |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 66 | return 0; |
| 67 | } |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 68 | |
Eric Dumazet | 04a931e | 2021-12-09 23:44:23 -0800 | [diff] [blame] | 69 | static void seq_file_net_put_net(struct seq_file *seq) |
| 70 | { |
| 71 | #ifdef CONFIG_NET_NS |
| 72 | struct seq_net_private *priv = seq->private; |
| 73 | |
| 74 | put_net_track(priv->net, &priv->ns_tracker); |
| 75 | #else |
| 76 | put_net(&init_net); |
| 77 | #endif |
| 78 | } |
| 79 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 80 | static int seq_release_net(struct inode *ino, struct file *f) |
| 81 | { |
| 82 | struct seq_file *seq = f->private_data; |
| 83 | |
Eric Dumazet | 04a931e | 2021-12-09 23:44:23 -0800 | [diff] [blame] | 84 | seq_file_net_put_net(seq); |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 85 | seq_release_private(ino, f); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 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, |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
Yonghong Song | f9c7927 | 2020-07-23 11:41:10 -0700 | [diff] [blame] | 97 | int bpf_iter_init_seq_net(void *priv_data, struct bpf_iter_aux_info *aux) |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 98 | { |
| 99 | #ifdef CONFIG_NET_NS |
| 100 | struct seq_net_private *p = priv_data; |
| 101 | |
Eric Dumazet | 04a931e | 2021-12-09 23:44:23 -0800 | [diff] [blame] | 102 | p->net = get_net_track(current->nsproxy->net_ns, &p->ns_tracker, |
| 103 | GFP_KERNEL); |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 104 | #endif |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | void bpf_iter_fini_seq_net(void *priv_data) |
| 109 | { |
| 110 | #ifdef CONFIG_NET_NS |
| 111 | struct seq_net_private *p = priv_data; |
| 112 | |
Eric Dumazet | 04a931e | 2021-12-09 23:44:23 -0800 | [diff] [blame] | 113 | put_net_track(p->net, &p->ns_tracker); |
Yonghong Song | 138d0be | 2020-05-09 10:59:10 -0700 | [diff] [blame] | 114 | #endif |
| 115 | } |
| 116 | |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 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) |
| 120 | { |
| 121 | struct proc_dir_entry *p; |
| 122 | |
| 123 | p = proc_create_reg(name, mode, &parent, data); |
| 124 | if (!p) |
| 125 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 126 | pde_force_lookup(p); |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 127 | p->proc_ops = &proc_net_seq_ops; |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 128 | p->seq_ops = ops; |
| 129 | p->state_size = state_size; |
| 130 | return proc_register(parent, p); |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(proc_create_net_data); |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 133 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 134 | /** |
| 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. |
Randy Dunlap | d2928e8 | 2020-12-15 20:42:32 -0800 | [diff] [blame] | 140 | * @write: The write method with which to 'modify' the file. |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 141 | * @data: Data for retrieval by pde_data(). |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 142 | * |
| 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. |
| 147 | * |
| 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. |
| 150 | * |
| 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. |
| 154 | * |
| 155 | * The @data value is accessible from the @show and @write functions by calling |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 156 | * pde_data() on the file inode. The network namespace must be accessed by |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 157 | * calling seq_file_net() on the seq_file struct. |
| 158 | */ |
| 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, |
| 162 | proc_write_t write, |
| 163 | unsigned int state_size, void *data) |
| 164 | { |
| 165 | struct proc_dir_entry *p; |
| 166 | |
| 167 | p = proc_create_reg(name, mode, &parent, data); |
| 168 | if (!p) |
| 169 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 170 | pde_force_lookup(p); |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 171 | p->proc_ops = &proc_net_seq_ops; |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 172 | p->seq_ops = ops; |
| 173 | p->state_size = state_size; |
| 174 | p->write = write; |
| 175 | return proc_register(parent, p); |
| 176 | } |
| 177 | EXPORT_SYMBOL_GPL(proc_create_net_data_write); |
| 178 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 179 | static int single_open_net(struct inode *inode, struct file *file) |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 180 | { |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 181 | struct proc_dir_entry *de = PDE(inode); |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 182 | struct net *net; |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 183 | int err; |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 184 | |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 185 | net = get_proc_net(inode); |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 186 | if (!net) |
| 187 | return -ENXIO; |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 188 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 189 | err = single_open(file, de->single_show, net); |
| 190 | if (err) |
| 191 | put_net(net); |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 192 | return err; |
| 193 | } |
Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 194 | |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 195 | static int single_release_net(struct inode *ino, struct file *f) |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 196 | { |
| 197 | struct seq_file *seq = f->private_data; |
| 198 | put_net(seq->private); |
| 199 | return single_release(ino, f); |
| 200 | } |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 201 | |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 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, |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
| 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) |
| 213 | { |
| 214 | struct proc_dir_entry *p; |
| 215 | |
| 216 | p = proc_create_reg(name, mode, &parent, data); |
| 217 | if (!p) |
| 218 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 219 | pde_force_lookup(p); |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 220 | p->proc_ops = &proc_net_single_ops; |
Christoph Hellwig | 3617d94 | 2018-04-13 20:38:35 +0200 | [diff] [blame] | 221 | p->single_show = show; |
| 222 | return proc_register(parent, p); |
| 223 | } |
| 224 | EXPORT_SYMBOL_GPL(proc_create_net_single); |
Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 225 | |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 226 | /** |
| 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. |
Randy Dunlap | d2928e8 | 2020-12-15 20:42:32 -0800 | [diff] [blame] | 232 | * @write: The write method with which to 'modify' the file. |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 233 | * @data: Data for retrieval by pde_data(). |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 234 | * |
| 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. |
| 239 | * |
| 240 | * The @show function is called to extract the readable content via the |
| 241 | * seq_file interface. |
| 242 | * |
| 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. |
| 246 | * |
| 247 | * The @data value is accessible from the @show and @write functions by calling |
Muchun Song | 359745d | 2022-01-21 22:14:23 -0800 | [diff] [blame] | 248 | * pde_data() on the file inode. The network namespace must be accessed by |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 249 | * calling seq_file_single_net() on the seq_file struct. |
| 250 | */ |
| 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 *), |
| 254 | proc_write_t write, |
| 255 | void *data) |
| 256 | { |
| 257 | struct proc_dir_entry *p; |
| 258 | |
| 259 | p = proc_create_reg(name, mode, &parent, data); |
| 260 | if (!p) |
| 261 | return NULL; |
Alexey Dobriyan | 1fde6f2 | 2019-02-01 14:20:01 -0800 | [diff] [blame] | 262 | pde_force_lookup(p); |
Alexey Dobriyan | d56c0d4 | 2020-02-03 17:37:14 -0800 | [diff] [blame] | 263 | p->proc_ops = &proc_net_single_ops; |
David Howells | 564def7 | 2018-05-18 11:46:15 +0100 | [diff] [blame] | 264 | p->single_show = show; |
| 265 | p->write = write; |
| 266 | return proc_register(parent, p); |
| 267 | } |
| 268 | EXPORT_SYMBOL_GPL(proc_create_net_single_write); |
| 269 | |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 270 | static struct net *get_proc_task_net(struct inode *dir) |
| 271 | { |
| 272 | struct task_struct *task; |
| 273 | struct nsproxy *ns; |
| 274 | struct net *net = NULL; |
| 275 | |
| 276 | rcu_read_lock(); |
| 277 | task = pid_task(proc_pid(dir), PIDTYPE_PID); |
| 278 | if (task != NULL) { |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 279 | task_lock(task); |
| 280 | ns = task->nsproxy; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 281 | if (ns != NULL) |
| 282 | net = get_net(ns->net_ns); |
Eric W. Biederman | 728dba3 | 2014-02-03 19:13:49 -0800 | [diff] [blame] | 283 | task_unlock(task); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 284 | } |
| 285 | rcu_read_unlock(); |
| 286 | |
| 287 | return net; |
| 288 | } |
| 289 | |
| 290 | static struct dentry *proc_tgid_net_lookup(struct inode *dir, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 291 | struct dentry *dentry, unsigned int flags) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 292 | { |
| 293 | struct dentry *de; |
| 294 | struct net *net; |
| 295 | |
| 296 | de = ERR_PTR(-ENOENT); |
| 297 | net = get_proc_task_net(dir); |
| 298 | if (net != NULL) { |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 299 | de = proc_lookup_de(dir, dentry, net->proc_net); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 300 | put_net(net); |
| 301 | } |
| 302 | return de; |
| 303 | } |
| 304 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 305 | static int proc_tgid_net_getattr(struct user_namespace *mnt_userns, |
| 306 | const struct path *path, struct kstat *stat, |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 307 | u32 request_mask, unsigned int query_flags) |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 308 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 309 | struct inode *inode = d_inode(path->dentry); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 310 | struct net *net; |
| 311 | |
| 312 | net = get_proc_task_net(inode); |
| 313 | |
Christian Brauner | 0d56a45 | 2021-01-21 14:19:30 +0100 | [diff] [blame] | 314 | generic_fillattr(&init_user_ns, inode, stat); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 315 | |
| 316 | if (net != NULL) { |
| 317 | stat->nlink = net->proc_net->nlink; |
| 318 | put_net(net); |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | const struct inode_operations proc_net_inode_operations = { |
| 325 | .lookup = proc_tgid_net_lookup, |
| 326 | .getattr = proc_tgid_net_getattr, |
| 327 | }; |
| 328 | |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 329 | 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] | 330 | { |
| 331 | int ret; |
| 332 | struct net *net; |
| 333 | |
| 334 | ret = -EINVAL; |
Al Viro | f0c3b50 | 2013-05-16 12:07:31 -0400 | [diff] [blame] | 335 | net = get_proc_task_net(file_inode(file)); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 336 | if (net != NULL) { |
Alexey Dobriyan | 93ad5bc | 2018-02-06 15:37:31 -0800 | [diff] [blame] | 337 | ret = proc_readdir_de(file, ctx, net->proc_net); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 338 | put_net(net); |
| 339 | } |
| 340 | return ret; |
| 341 | } |
| 342 | |
| 343 | const struct file_operations proc_net_operations = { |
Alexey Dobriyan | b4df2b9 | 2008-10-27 22:48:36 +0300 | [diff] [blame] | 344 | .llseek = generic_file_llseek, |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 345 | .read = generic_read_dir, |
Al Viro | f50752e | 2016-04-20 17:13:54 -0400 | [diff] [blame] | 346 | .iterate_shared = proc_tgid_net_readdir, |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 347 | }; |
| 348 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 349 | static __net_init int proc_net_ns_init(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 350 | { |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 351 | struct proc_dir_entry *netd, *net_statd; |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 352 | kuid_t uid; |
| 353 | kgid_t gid; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 354 | int err; |
| 355 | |
| 356 | err = -ENOMEM; |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 357 | netd = kmem_cache_zalloc(proc_dir_entry_cache, GFP_KERNEL); |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 358 | if (!netd) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 359 | goto out; |
| 360 | |
Alexey Dobriyan | 4f11343 | 2018-04-10 16:32:20 -0700 | [diff] [blame] | 361 | netd->subdir = RB_ROOT; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 362 | netd->data = net; |
| 363 | netd->nlink = 2; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 364 | netd->namelen = 3; |
| 365 | netd->parent = &proc_root; |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 366 | netd->name = netd->inline_name; |
David Howells | 09570f9 | 2011-07-27 21:47:03 +0300 | [diff] [blame] | 367 | memcpy(netd->name, "net", 4); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 368 | |
Dmitry Torokhov | c110486 | 2016-08-10 14:36:01 -0700 | [diff] [blame] | 369 | uid = make_kuid(net->user_ns, 0); |
| 370 | if (!uid_valid(uid)) |
| 371 | uid = netd->uid; |
| 372 | |
| 373 | gid = make_kgid(net->user_ns, 0); |
| 374 | if (!gid_valid(gid)) |
| 375 | gid = netd->gid; |
| 376 | |
| 377 | proc_set_user(netd, uid, gid); |
| 378 | |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 379 | err = -EEXIST; |
Denis V. Lunev | e5d69b9 | 2008-01-10 03:51:41 -0800 | [diff] [blame] | 380 | net_statd = proc_net_mkdir(net, "stat", netd); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 381 | if (!net_statd) |
| 382 | goto free_net; |
| 383 | |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 384 | net->proc_net = netd; |
| 385 | net->proc_net_stat = net_statd; |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 386 | return 0; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 387 | |
Pavel Emelyanov | e9720ac | 2008-03-07 11:08:40 -0800 | [diff] [blame] | 388 | free_net: |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 389 | pde_free(netd); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 390 | out: |
| 391 | return err; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 392 | } |
| 393 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 394 | static __net_exit void proc_net_ns_exit(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 395 | { |
| 396 | remove_proc_entry("stat", net->proc_net); |
Alexey Dobriyan | b4884f2 | 2018-04-10 16:31:52 -0700 | [diff] [blame] | 397 | pde_free(net->proc_net); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 398 | } |
| 399 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 400 | static struct pernet_operations __net_initdata proc_net_ns_ops = { |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 401 | .init = proc_net_ns_init, |
| 402 | .exit = proc_net_ns_exit, |
| 403 | }; |
| 404 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 405 | int __init proc_net_init(void) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 406 | { |
Linus Torvalds | 155134f | 2014-08-10 21:24:59 -0700 | [diff] [blame] | 407 | proc_symlink("net", NULL, "self/net"); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 408 | |
| 409 | return register_pernet_subsys(&proc_net_ns_ops); |
| 410 | } |