Commit | Line | Data |
---|---|---|
3c12afe7 DM |
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> | |
e372c414 | 25 | #include <linux/seq_file.h> |
3c12afe7 DM |
26 | |
27 | #include "internal.h" | |
28 | ||
29 | ||
e372c414 DL |
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 | ||
3c12afe7 DM |
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 | { | |
70 | struct proc_dir_entry *res; | |
71 | ||
72 | res = create_proc_entry(name, mode, net->proc_net); | |
73 | if (res) | |
74 | res->proc_fops = fops; | |
75 | return res; | |
76 | } | |
36ac3135 | 77 | EXPORT_SYMBOL_GPL(proc_net_fops_create); |
3c12afe7 DM |
78 | |
79 | void proc_net_remove(struct net *net, const char *name) | |
80 | { | |
81 | remove_proc_entry(name, net->proc_net); | |
82 | } | |
36ac3135 | 83 | EXPORT_SYMBOL_GPL(proc_net_remove); |
3c12afe7 | 84 | |
077130c0 EB |
85 | struct net *get_proc_net(const struct inode *inode) |
86 | { | |
87 | return maybe_get_net(PDE_NET(PDE(inode))); | |
88 | } | |
89 | EXPORT_SYMBOL_GPL(get_proc_net); | |
90 | ||
2b1e300a | 91 | static struct proc_dir_entry *shadow_pde; |
3c12afe7 | 92 | |
2b1e300a | 93 | static struct proc_dir_entry *proc_net_shadow(struct task_struct *task, |
3c12afe7 DM |
94 | struct proc_dir_entry *de) |
95 | { | |
2b1e300a | 96 | return task->nsproxy->net_ns->proc_net; |
3c12afe7 DM |
97 | } |
98 | ||
4665079c | 99 | static __net_init int proc_net_ns_init(struct net *net) |
3c12afe7 DM |
100 | { |
101 | struct proc_dir_entry *root, *netd, *net_statd; | |
102 | int err; | |
103 | ||
104 | err = -ENOMEM; | |
105 | root = kzalloc(sizeof(*root), GFP_KERNEL); | |
106 | if (!root) | |
107 | goto out; | |
108 | ||
109 | err = -EEXIST; | |
110 | netd = proc_mkdir("net", root); | |
111 | if (!netd) | |
112 | goto free_root; | |
113 | ||
114 | err = -EEXIST; | |
115 | net_statd = proc_mkdir("stat", netd); | |
116 | if (!net_statd) | |
117 | goto free_net; | |
118 | ||
119 | root->data = net; | |
120 | netd->data = net; | |
121 | net_statd->data = net; | |
122 | ||
123 | net->proc_net_root = root; | |
124 | net->proc_net = netd; | |
125 | net->proc_net_stat = net_statd; | |
126 | err = 0; | |
127 | ||
128 | out: | |
129 | return err; | |
130 | free_net: | |
131 | remove_proc_entry("net", root); | |
132 | free_root: | |
133 | kfree(root); | |
134 | goto out; | |
135 | } | |
136 | ||
4665079c | 137 | static __net_exit void proc_net_ns_exit(struct net *net) |
3c12afe7 DM |
138 | { |
139 | remove_proc_entry("stat", net->proc_net); | |
140 | remove_proc_entry("net", net->proc_net_root); | |
141 | kfree(net->proc_net_root); | |
142 | } | |
143 | ||
022cbae6 | 144 | static struct pernet_operations __net_initdata proc_net_ns_ops = { |
3c12afe7 DM |
145 | .init = proc_net_ns_init, |
146 | .exit = proc_net_ns_exit, | |
147 | }; | |
148 | ||
4665079c | 149 | int __init proc_net_init(void) |
3c12afe7 | 150 | { |
2b1e300a EB |
151 | shadow_pde = proc_mkdir("net", NULL); |
152 | shadow_pde->shadow_proc = proc_net_shadow; | |
3c12afe7 DM |
153 | |
154 | return register_pernet_subsys(&proc_net_ns_ops); | |
155 | } |