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 | |
| 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. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 29 | struct 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 Lezcano | 36ac313 | 2007-09-12 14:51:47 +0200 | [diff] [blame] | 39 | EXPORT_SYMBOL_GPL(proc_net_fops_create); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 40 | |
| 41 | void proc_net_remove(struct net *net, const char *name) |
| 42 | { |
| 43 | remove_proc_entry(name, net->proc_net); |
| 44 | } |
Daniel Lezcano | 36ac313 | 2007-09-12 14:51:47 +0200 | [diff] [blame] | 45 | EXPORT_SYMBOL_GPL(proc_net_remove); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 46 | |
Eric W. Biederman | 077130c | 2007-09-13 09:18:57 +0200 | [diff] [blame] | 47 | struct net *get_proc_net(const struct inode *inode) |
| 48 | { |
| 49 | return maybe_get_net(PDE_NET(PDE(inode))); |
| 50 | } |
| 51 | EXPORT_SYMBOL_GPL(get_proc_net); |
| 52 | |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 53 | static struct proc_dir_entry *shadow_pde; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 54 | |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 55 | static struct proc_dir_entry *proc_net_shadow(struct task_struct *task, |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 56 | struct proc_dir_entry *de) |
| 57 | { |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 58 | return task->nsproxy->net_ns->proc_net; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 61 | static __net_init int proc_net_ns_init(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 62 | { |
| 63 | struct proc_dir_entry *root, *netd, *net_statd; |
| 64 | int err; |
| 65 | |
| 66 | err = -ENOMEM; |
| 67 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 68 | if (!root) |
| 69 | goto out; |
| 70 | |
| 71 | err = -EEXIST; |
| 72 | netd = proc_mkdir("net", root); |
| 73 | if (!netd) |
| 74 | goto free_root; |
| 75 | |
| 76 | err = -EEXIST; |
| 77 | net_statd = proc_mkdir("stat", netd); |
| 78 | if (!net_statd) |
| 79 | goto free_net; |
| 80 | |
| 81 | root->data = net; |
| 82 | netd->data = net; |
| 83 | net_statd->data = net; |
| 84 | |
| 85 | net->proc_net_root = root; |
| 86 | net->proc_net = netd; |
| 87 | net->proc_net_stat = net_statd; |
| 88 | err = 0; |
| 89 | |
| 90 | out: |
| 91 | return err; |
| 92 | free_net: |
| 93 | remove_proc_entry("net", root); |
| 94 | free_root: |
| 95 | kfree(root); |
| 96 | goto out; |
| 97 | } |
| 98 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 99 | static __net_exit void proc_net_ns_exit(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 100 | { |
| 101 | remove_proc_entry("stat", net->proc_net); |
| 102 | remove_proc_entry("net", net->proc_net_root); |
| 103 | kfree(net->proc_net_root); |
| 104 | } |
| 105 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 106 | static struct pernet_operations __net_initdata proc_net_ns_ops = { |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 107 | .init = proc_net_ns_init, |
| 108 | .exit = proc_net_ns_exit, |
| 109 | }; |
| 110 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 111 | int __init proc_net_init(void) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 112 | { |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 113 | shadow_pde = proc_mkdir("net", NULL); |
| 114 | shadow_pde->shadow_proc = proc_net_shadow; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 115 | |
| 116 | return register_pernet_subsys(&proc_net_ns_ops); |
| 117 | } |