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> |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 26 | |
| 27 | #include "internal.h" |
| 28 | |
| 29 | |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 30 | int seq_open_net(struct inode *ino, struct file *f, |
| 31 | const struct seq_operations *ops, int size) |
| 32 | { |
| 33 | struct net *net; |
| 34 | struct seq_net_private *p; |
| 35 | |
| 36 | BUG_ON(size < sizeof(*p)); |
| 37 | |
| 38 | net = get_proc_net(ino); |
| 39 | if (net == NULL) |
| 40 | return -ENXIO; |
| 41 | |
| 42 | p = __seq_open_private(f, ops, size); |
| 43 | if (p == NULL) { |
| 44 | put_net(net); |
| 45 | return -ENOMEM; |
| 46 | } |
| 47 | p->net = net; |
| 48 | return 0; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(seq_open_net); |
| 51 | |
| 52 | int seq_release_net(struct inode *ino, struct file *f) |
| 53 | { |
| 54 | struct seq_file *seq; |
| 55 | struct seq_net_private *p; |
| 56 | |
| 57 | seq = f->private_data; |
| 58 | p = seq->private; |
| 59 | |
| 60 | put_net(p->net); |
| 61 | seq_release_private(ino, f); |
| 62 | return 0; |
| 63 | } |
| 64 | EXPORT_SYMBOL_GPL(seq_release_net); |
| 65 | |
| 66 | |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 67 | struct proc_dir_entry *proc_net_fops_create(struct net *net, |
| 68 | const char *name, mode_t mode, const struct file_operations *fops) |
| 69 | { |
Alexey Dobriyan | 2d3a4e3 | 2008-02-08 04:18:37 -0800 | [diff] [blame] | 70 | return proc_create(name, mode, net->proc_net, fops); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 71 | } |
Daniel Lezcano | 36ac313 | 2007-09-12 14:51:47 +0200 | [diff] [blame] | 72 | EXPORT_SYMBOL_GPL(proc_net_fops_create); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 73 | |
| 74 | void proc_net_remove(struct net *net, const char *name) |
| 75 | { |
| 76 | remove_proc_entry(name, net->proc_net); |
| 77 | } |
Daniel Lezcano | 36ac313 | 2007-09-12 14:51:47 +0200 | [diff] [blame] | 78 | EXPORT_SYMBOL_GPL(proc_net_remove); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 79 | |
Eric W. Biederman | 077130c | 2007-09-13 09:18:57 +0200 | [diff] [blame] | 80 | struct net *get_proc_net(const struct inode *inode) |
| 81 | { |
| 82 | return maybe_get_net(PDE_NET(PDE(inode))); |
| 83 | } |
| 84 | EXPORT_SYMBOL_GPL(get_proc_net); |
| 85 | |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 86 | static struct proc_dir_entry *shadow_pde; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 87 | |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 88 | 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] | 89 | struct proc_dir_entry *de) |
| 90 | { |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 91 | return task->nsproxy->net_ns->proc_net; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Denis V. Lunev | e5d69b9 | 2008-01-10 03:51:41 -0800 | [diff] [blame] | 94 | struct proc_dir_entry *proc_net_mkdir(struct net *net, const char *name, |
| 95 | struct proc_dir_entry *parent) |
| 96 | { |
| 97 | struct proc_dir_entry *pde; |
| 98 | pde = proc_mkdir_mode(name, S_IRUGO | S_IXUGO, parent); |
| 99 | if (pde != NULL) |
| 100 | pde->data = net; |
| 101 | return pde; |
| 102 | } |
| 103 | EXPORT_SYMBOL_GPL(proc_net_mkdir); |
| 104 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 105 | static __net_init int proc_net_ns_init(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 106 | { |
| 107 | struct proc_dir_entry *root, *netd, *net_statd; |
| 108 | int err; |
| 109 | |
| 110 | err = -ENOMEM; |
| 111 | root = kzalloc(sizeof(*root), GFP_KERNEL); |
| 112 | if (!root) |
| 113 | goto out; |
| 114 | |
| 115 | err = -EEXIST; |
Denis V. Lunev | e5d69b9 | 2008-01-10 03:51:41 -0800 | [diff] [blame] | 116 | netd = proc_net_mkdir(net, "net", root); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 117 | if (!netd) |
| 118 | goto free_root; |
| 119 | |
| 120 | err = -EEXIST; |
Denis V. Lunev | e5d69b9 | 2008-01-10 03:51:41 -0800 | [diff] [blame] | 121 | net_statd = proc_net_mkdir(net, "stat", netd); |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 122 | if (!net_statd) |
| 123 | goto free_net; |
| 124 | |
| 125 | root->data = net; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 126 | |
| 127 | net->proc_net_root = root; |
| 128 | net->proc_net = netd; |
| 129 | net->proc_net_stat = net_statd; |
| 130 | err = 0; |
| 131 | |
| 132 | out: |
| 133 | return err; |
| 134 | free_net: |
| 135 | remove_proc_entry("net", root); |
| 136 | free_root: |
| 137 | kfree(root); |
| 138 | goto out; |
| 139 | } |
| 140 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 141 | static __net_exit void proc_net_ns_exit(struct net *net) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 142 | { |
| 143 | remove_proc_entry("stat", net->proc_net); |
| 144 | remove_proc_entry("net", net->proc_net_root); |
| 145 | kfree(net->proc_net_root); |
| 146 | } |
| 147 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 148 | static struct pernet_operations __net_initdata proc_net_ns_ops = { |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 149 | .init = proc_net_ns_init, |
| 150 | .exit = proc_net_ns_exit, |
| 151 | }; |
| 152 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 153 | int __init proc_net_init(void) |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 154 | { |
Eric W. Biederman | 2b1e300 | 2007-12-02 00:33:17 +1100 | [diff] [blame] | 155 | shadow_pde = proc_mkdir("net", NULL); |
| 156 | shadow_pde->shadow_proc = proc_net_shadow; |
David S. Miller | 3c12afe | 2007-09-12 14:18:18 +0200 | [diff] [blame] | 157 | |
| 158 | return register_pernet_subsys(&proc_net_ns_ops); |
| 159 | } |