ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / appletalk / atalk_proc.c
1 /*
2  *      atalk_proc.c - proc support for Appletalk
3  *
4  *      Copyright(c) Arnaldo Carvalho de Melo <acme@conectiva.com.br>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation, version 2.
9  */
10
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/proc_fs.h>
14 #include <linux/seq_file.h>
15 #include <net/sock.h>
16 #include <linux/atalk.h>
17
18 #ifdef CONFIG_PROC_FS
19 extern struct file_operations atalk_seq_arp_fops;
20
21 static __inline__ struct atalk_iface *atalk_get_interface_idx(loff_t pos)
22 {
23         struct atalk_iface *i;
24
25         for (i = atalk_interfaces; pos && i; i = i->next)
26                 --pos;
27
28         return i;
29 }
30
31 static void *atalk_seq_interface_start(struct seq_file *seq, loff_t *pos)
32 {
33         loff_t l = *pos;
34
35         read_lock_bh(&atalk_interfaces_lock);
36         return l ? atalk_get_interface_idx(--l) : SEQ_START_TOKEN;
37 }
38
39 static void *atalk_seq_interface_next(struct seq_file *seq, void *v, loff_t *pos)
40 {
41         struct atalk_iface *i;
42
43         ++*pos;
44         if (v == SEQ_START_TOKEN) {
45                 i = NULL;
46                 if (atalk_interfaces)
47                         i = atalk_interfaces;
48                 goto out;
49         }
50         i = v;
51         i = i->next;
52 out:
53         return i;
54 }
55
56 static void atalk_seq_interface_stop(struct seq_file *seq, void *v)
57 {
58         read_unlock_bh(&atalk_interfaces_lock);
59 }
60
61 static int atalk_seq_interface_show(struct seq_file *seq, void *v)
62 {
63         struct atalk_iface *iface;
64
65         if (v == SEQ_START_TOKEN) {
66                 seq_puts(seq, "Interface        Address   Networks  "
67                               "Status\n");
68                 goto out;
69         }
70
71         iface = v;
72         seq_printf(seq, "%-16s %04X:%02X  %04X-%04X  %d\n",
73                    iface->dev->name, ntohs(iface->address.s_net),
74                    iface->address.s_node, ntohs(iface->nets.nr_firstnet),
75                    ntohs(iface->nets.nr_lastnet), iface->status);
76 out:
77         return 0;
78 }
79
80 static __inline__ struct atalk_route *atalk_get_route_idx(loff_t pos)
81 {
82         struct atalk_route *r;
83
84         for (r = atalk_routes; pos && r; r = r->next)
85                 --pos;
86
87         return r;
88 }
89
90 static void *atalk_seq_route_start(struct seq_file *seq, loff_t *pos)
91 {
92         loff_t l = *pos;
93
94         read_lock_bh(&atalk_routes_lock);
95         return l ? atalk_get_route_idx(--l) : SEQ_START_TOKEN;
96 }
97
98 static void *atalk_seq_route_next(struct seq_file *seq, void *v, loff_t *pos)
99 {
100         struct atalk_route *r;
101
102         ++*pos;
103         if (v == SEQ_START_TOKEN) {
104                 r = NULL;
105                 if (atalk_routes)
106                         r = atalk_routes;
107                 goto out;
108         }
109         r = v;
110         r = r->next;
111 out:
112         return r;
113 }
114
115 static void atalk_seq_route_stop(struct seq_file *seq, void *v)
116 {
117         read_unlock_bh(&atalk_routes_lock);
118 }
119
120 static int atalk_seq_route_show(struct seq_file *seq, void *v)
121 {
122         struct atalk_route *rt;
123
124         if (v == SEQ_START_TOKEN) {
125                 seq_puts(seq, "Target        Router  Flags Dev\n");
126                 goto out;
127         }
128
129         if (atrtr_default.dev) {
130                 rt = &atrtr_default;
131                 seq_printf(seq, "Default     %04X:%02X  %-4d  %s\n",
132                                ntohs(rt->gateway.s_net), rt->gateway.s_node,
133                                rt->flags, rt->dev->name);
134         }
135
136         rt = v;
137         seq_printf(seq, "%04X:%02X     %04X:%02X  %-4d  %s\n",
138                    ntohs(rt->target.s_net), rt->target.s_node,
139                    ntohs(rt->gateway.s_net), rt->gateway.s_node,
140                    rt->flags, rt->dev->name);
141 out:
142         return 0;
143 }
144
145 static __inline__ struct sock *atalk_get_socket_idx(loff_t pos)
146 {
147         struct sock *s;
148         struct hlist_node *node;
149
150         sk_for_each(s, node, &atalk_sockets)
151                 if (!pos--)
152                         goto found;
153         s = NULL;
154 found:
155         return s;
156 }
157
158 static void *atalk_seq_socket_start(struct seq_file *seq, loff_t *pos)
159 {
160         loff_t l = *pos;
161
162         read_lock_bh(&atalk_sockets_lock);
163         return l ? atalk_get_socket_idx(--l) : SEQ_START_TOKEN;
164 }
165
166 static void *atalk_seq_socket_next(struct seq_file *seq, void *v, loff_t *pos)
167 {
168         struct sock *i;
169
170         ++*pos;
171         if (v == SEQ_START_TOKEN) {
172                 i = sk_head(&atalk_sockets);
173                 goto out;
174         }
175         i = sk_next(v);
176 out:
177         return i;
178 }
179
180 static void atalk_seq_socket_stop(struct seq_file *seq, void *v)
181 {
182         read_unlock_bh(&atalk_sockets_lock);
183 }
184
185 static int atalk_seq_socket_show(struct seq_file *seq, void *v)
186 {
187         struct sock *s;
188         struct atalk_sock *at;
189
190         if (v == SEQ_START_TOKEN) {
191                 seq_printf(seq, "Type Local_addr  Remote_addr Tx_queue "
192                                 "Rx_queue St UID\n");
193                 goto out;
194         }
195
196         s = v;
197         at = at_sk(s);
198
199         seq_printf(seq, "%02X   %04X:%02X:%02X  %04X:%02X:%02X  %08X:%08X "
200                         "%02X %d\n",
201                    s->sk_type, ntohs(at->src_net), at->src_node, at->src_port,
202                    ntohs(at->dest_net), at->dest_node, at->dest_port,
203                    atomic_read(&s->sk_wmem_alloc),
204                    atomic_read(&s->sk_rmem_alloc),
205                    s->sk_state, SOCK_INODE(s->sk_socket)->i_uid);
206 out:
207         return 0;
208 }
209
210 struct seq_operations atalk_seq_interface_ops = {
211         .start  = atalk_seq_interface_start,
212         .next   = atalk_seq_interface_next,
213         .stop   = atalk_seq_interface_stop,
214         .show   = atalk_seq_interface_show,
215 };
216
217 struct seq_operations atalk_seq_route_ops = {
218         .start  = atalk_seq_route_start,
219         .next   = atalk_seq_route_next,
220         .stop   = atalk_seq_route_stop,
221         .show   = atalk_seq_route_show,
222 };
223
224 struct seq_operations atalk_seq_socket_ops = {
225         .start  = atalk_seq_socket_start,
226         .next   = atalk_seq_socket_next,
227         .stop   = atalk_seq_socket_stop,
228         .show   = atalk_seq_socket_show,
229 };
230
231 static int atalk_seq_interface_open(struct inode *inode, struct file *file)
232 {
233         return seq_open(file, &atalk_seq_interface_ops);
234 }
235
236 static int atalk_seq_route_open(struct inode *inode, struct file *file)
237 {
238         return seq_open(file, &atalk_seq_route_ops);
239 }
240
241 static int atalk_seq_socket_open(struct inode *inode, struct file *file)
242 {
243         return seq_open(file, &atalk_seq_socket_ops);
244 }
245
246 static struct file_operations atalk_seq_interface_fops = {
247         .owner          = THIS_MODULE,
248         .open           = atalk_seq_interface_open,
249         .read           = seq_read,
250         .llseek         = seq_lseek,
251         .release        = seq_release,
252 };
253
254 static struct file_operations atalk_seq_route_fops = {
255         .owner          = THIS_MODULE,
256         .open           = atalk_seq_route_open,
257         .read           = seq_read,
258         .llseek         = seq_lseek,
259         .release        = seq_release,
260 };
261
262 static struct file_operations atalk_seq_socket_fops = {
263         .owner          = THIS_MODULE,
264         .open           = atalk_seq_socket_open,
265         .read           = seq_read,
266         .llseek         = seq_lseek,
267         .release        = seq_release,
268 };
269
270 static struct proc_dir_entry *atalk_proc_dir;
271
272 int __init atalk_proc_init(void)
273 {
274         struct proc_dir_entry *p;
275         int rc = -ENOMEM;
276
277         atalk_proc_dir = proc_mkdir("atalk", proc_net);
278         if (!atalk_proc_dir)
279                 goto out;
280         atalk_proc_dir->owner = THIS_MODULE;
281
282         p = create_proc_entry("interface", S_IRUGO, atalk_proc_dir);
283         if (!p)
284                 goto out_interface;
285         p->proc_fops = &atalk_seq_interface_fops;
286
287         p = create_proc_entry("route", S_IRUGO, atalk_proc_dir);
288         if (!p)
289                 goto out_route;
290         p->proc_fops = &atalk_seq_route_fops;
291
292         p = create_proc_entry("socket", S_IRUGO, atalk_proc_dir);
293         if (!p)
294                 goto out_socket;
295         p->proc_fops = &atalk_seq_socket_fops;
296
297         p = create_proc_entry("arp", S_IRUGO, atalk_proc_dir);
298         if (!p) 
299                 goto out_arp;
300         p->proc_fops = &atalk_seq_arp_fops;
301
302         rc = 0;
303 out:
304         return rc;
305 out_arp:
306         remove_proc_entry("socket", atalk_proc_dir);
307 out_socket:
308         remove_proc_entry("route", atalk_proc_dir);
309 out_route:
310         remove_proc_entry("interface", atalk_proc_dir);
311 out_interface:
312         remove_proc_entry("atalk", proc_net);
313         goto out;
314 }
315
316 void __exit atalk_proc_exit(void)
317 {
318         remove_proc_entry("interface", atalk_proc_dir);
319         remove_proc_entry("route", atalk_proc_dir);
320         remove_proc_entry("socket", atalk_proc_dir);
321         remove_proc_entry("arp", atalk_proc_dir);
322         remove_proc_entry("atalk", proc_net);
323 }
324
325 #else /* CONFIG_PROC_FS */
326 int __init atalk_proc_init(void)
327 {
328         return 0;
329 }
330
331 void __exit atalk_proc_exit(void)
332 {
333 }
334 #endif /* CONFIG_PROC_FS */