1/*
2 *	X.25 Packet Layer release 002
3 *
4 *	This is ALPHA test software. This code may break your machine,
5 *	randomly fail to work with new releases, misbehave and/or generally
6 *	screw up. It might even work.
7 *
8 *	This code REQUIRES 2.4 with seq_file support
9 *
10 *	This module:
11 *		This module is free software; you can redistribute it and/or
12 *		modify it under the terms of the GNU General Public License
13 *		as published by the Free Software Foundation; either version
14 *		2 of the License, or (at your option) any later version.
15 *
16 *	History
17 *	2002/10/06	Arnaldo Carvalho de Melo  seq_file support
18 */
19
20#include <linux/init.h>
21#include <linux/proc_fs.h>
22#include <linux/seq_file.h>
23#include <net/sock.h>
24#include <net/x25.h>
25
26#ifdef CONFIG_PROC_FS
27static __inline__ struct x25_route *x25_get_route_idx(loff_t pos)
28{
29	struct list_head *route_entry;
30	struct x25_route *rt = NULL;
31
32	list_for_each(route_entry, &x25_route_list) {
33		rt = list_entry(route_entry, struct x25_route, node);
34		if (!pos--)
35			goto found;
36	}
37	rt = NULL;
38found:
39	return rt;
40}
41
42static void *x25_seq_route_start(struct seq_file *seq, loff_t *pos)
43{
44	loff_t l = *pos;
45
46	read_lock_bh(&x25_route_list_lock);
47	return l ? x25_get_route_idx(--l) : SEQ_START_TOKEN;
48}
49
50static void *x25_seq_route_next(struct seq_file *seq, void *v, loff_t *pos)
51{
52	struct x25_route *rt;
53
54	++*pos;
55	if (v == SEQ_START_TOKEN) {
56		rt = NULL;
57		if (!list_empty(&x25_route_list))
58			rt = list_entry(x25_route_list.next,
59					struct x25_route, node);
60		goto out;
61	}
62	rt = v;
63	if (rt->node.next != &x25_route_list)
64		rt = list_entry(rt->node.next, struct x25_route, node);
65	else
66		rt = NULL;
67out:
68	return rt;
69}
70
71static void x25_seq_route_stop(struct seq_file *seq, void *v)
72{
73	read_unlock_bh(&x25_route_list_lock);
74}
75
76static int x25_seq_route_show(struct seq_file *seq, void *v)
77{
78	struct x25_route *rt;
79
80	if (v == SEQ_START_TOKEN) {
81		seq_puts(seq, "Address          Digits  Device\n");
82		goto out;
83	}
84
85	rt = v;
86	seq_printf(seq, "%-15s  %-6d  %-5s\n",
87		   rt->address.x25_addr, rt->sigdigits,
88		   rt->dev ? rt->dev->name : "???");
89out:
90	return 0;
91}
92
93static __inline__ struct sock *x25_get_socket_idx(loff_t pos)
94{
95	struct sock *s;
96	struct hlist_node *node;
97
98	sk_for_each(s, node, &x25_list)
99		if (!pos--)
100			goto found;
101	s = NULL;
102found:
103	return s;
104}
105
106static void *x25_seq_socket_start(struct seq_file *seq, loff_t *pos)
107{
108	loff_t l = *pos;
109
110	read_lock_bh(&x25_list_lock);
111	return l ? x25_get_socket_idx(--l) : SEQ_START_TOKEN;
112}
113
114static void *x25_seq_socket_next(struct seq_file *seq, void *v, loff_t *pos)
115{
116	struct sock *s;
117
118	++*pos;
119	if (v == SEQ_START_TOKEN) {
120		s = sk_head(&x25_list);
121		goto out;
122	}
123	s = sk_next(v);
124out:
125	return s;
126}
127
128static void x25_seq_socket_stop(struct seq_file *seq, void *v)
129{
130	read_unlock_bh(&x25_list_lock);
131}
132
133static int x25_seq_socket_show(struct seq_file *seq, void *v)
134{
135	struct sock *s;
136	struct x25_sock *x25;
137	struct net_device *dev;
138	const char *devname;
139
140	if (v == SEQ_START_TOKEN) {
141		seq_printf(seq, "dest_addr  src_addr   dev   lci st vs vr "
142				"va   t  t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
143		goto out;
144	}
145
146	s = v;
147	x25 = x25_sk(s);
148
149	if (!x25->neighbour || (dev = x25->neighbour->dev) == NULL)
150		devname = "???";
151	else
152		devname = x25->neighbour->dev->name;
153
154	seq_printf(seq, "%-10s %-10s %-5s %3.3X  %d  %d  %d  %d %3lu %3lu "
155			"%3lu %3lu %3lu %5d %5d %ld\n",
156		   !x25->dest_addr.x25_addr[0] ? "*" : x25->dest_addr.x25_addr,
157		   !x25->source_addr.x25_addr[0] ? "*" : x25->source_addr.x25_addr,
158		   devname, x25->lci & 0x0FFF, x25->state, x25->vs, x25->vr,
159		   x25->va, x25_display_timer(s) / HZ, x25->t2  / HZ,
160		   x25->t21 / HZ, x25->t22 / HZ, x25->t23 / HZ,
161		   atomic_read(&s->sk_wmem_alloc),
162		   atomic_read(&s->sk_rmem_alloc),
163		   s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
164out:
165	return 0;
166}
167
168static __inline__ struct x25_forward *x25_get_forward_idx(loff_t pos)
169{
170	struct x25_forward *f;
171	struct list_head *entry;
172
173	list_for_each(entry, &x25_forward_list) {
174		f = list_entry(entry, struct x25_forward, node);
175		if (!pos--)
176			goto found;
177	}
178
179	f = NULL;
180found:
181	return f;
182}
183
184static void *x25_seq_forward_start(struct seq_file *seq, loff_t *pos)
185{
186	loff_t l = *pos;
187
188	read_lock_bh(&x25_forward_list_lock);
189	return l ? x25_get_forward_idx(--l) : SEQ_START_TOKEN;
190}
191
192static void *x25_seq_forward_next(struct seq_file *seq, void *v, loff_t *pos)
193{
194	struct x25_forward *f;
195
196	++*pos;
197	if (v == SEQ_START_TOKEN) {
198		f = NULL;
199		if (!list_empty(&x25_forward_list))
200			f = list_entry(x25_forward_list.next,
201					struct x25_forward, node);
202		goto out;
203	}
204	f = v;
205	if (f->node.next != &x25_forward_list)
206		f = list_entry(f->node.next, struct x25_forward, node);
207	else
208		f = NULL;
209out:
210	return f;
211
212}
213
214static void x25_seq_forward_stop(struct seq_file *seq, void *v)
215{
216	read_unlock_bh(&x25_forward_list_lock);
217}
218
219static int x25_seq_forward_show(struct seq_file *seq, void *v)
220{
221	struct x25_forward *f;
222
223	if (v == SEQ_START_TOKEN) {
224		seq_printf(seq, "lci dev1       dev2\n");
225		goto out;
226	}
227
228	f = v;
229
230	seq_printf(seq, "%d %-10s %-10s\n",
231			f->lci, f->dev1->name, f->dev2->name);
232
233out:
234	return 0;
235}
236
237static struct seq_operations x25_seq_route_ops = {
238	.start  = x25_seq_route_start,
239	.next   = x25_seq_route_next,
240	.stop   = x25_seq_route_stop,
241	.show   = x25_seq_route_show,
242};
243
244static struct seq_operations x25_seq_socket_ops = {
245	.start  = x25_seq_socket_start,
246	.next   = x25_seq_socket_next,
247	.stop   = x25_seq_socket_stop,
248	.show   = x25_seq_socket_show,
249};
250
251static struct seq_operations x25_seq_forward_ops = {
252	.start  = x25_seq_forward_start,
253	.next   = x25_seq_forward_next,
254	.stop   = x25_seq_forward_stop,
255	.show   = x25_seq_forward_show,
256};
257
258static int x25_seq_socket_open(struct inode *inode, struct file *file)
259{
260	return seq_open(file, &x25_seq_socket_ops);
261}
262
263static int x25_seq_route_open(struct inode *inode, struct file *file)
264{
265	return seq_open(file, &x25_seq_route_ops);
266}
267
268static int x25_seq_forward_open(struct inode *inode, struct file *file)
269{
270	return seq_open(file, &x25_seq_forward_ops);
271}
272
273static const struct file_operations x25_seq_socket_fops = {
274	.owner		= THIS_MODULE,
275	.open		= x25_seq_socket_open,
276	.read		= seq_read,
277	.llseek		= seq_lseek,
278	.release	= seq_release,
279};
280
281static const struct file_operations x25_seq_route_fops = {
282	.owner		= THIS_MODULE,
283	.open		= x25_seq_route_open,
284	.read		= seq_read,
285	.llseek		= seq_lseek,
286	.release	= seq_release,
287};
288
289static struct file_operations x25_seq_forward_fops = {
290	.owner		= THIS_MODULE,
291	.open		= x25_seq_forward_open,
292	.read		= seq_read,
293	.llseek		= seq_lseek,
294	.release	= seq_release,
295};
296
297static struct proc_dir_entry *x25_proc_dir;
298
299int __init x25_proc_init(void)
300{
301	struct proc_dir_entry *p;
302	int rc = -ENOMEM;
303
304	x25_proc_dir = proc_mkdir("x25", proc_net);
305	if (!x25_proc_dir)
306		goto out;
307
308	p = create_proc_entry("route", S_IRUGO, x25_proc_dir);
309	if (!p)
310		goto out_route;
311	p->proc_fops = &x25_seq_route_fops;
312
313	p = create_proc_entry("socket", S_IRUGO, x25_proc_dir);
314	if (!p)
315		goto out_socket;
316	p->proc_fops = &x25_seq_socket_fops;
317
318	p = create_proc_entry("forward", S_IRUGO, x25_proc_dir);
319	if (!p)
320		goto out_forward;
321	p->proc_fops = &x25_seq_forward_fops;
322	rc = 0;
323
324out:
325	return rc;
326out_forward:
327	remove_proc_entry("socket", x25_proc_dir);
328out_socket:
329	remove_proc_entry("route", x25_proc_dir);
330out_route:
331	remove_proc_entry("x25", proc_net);
332	goto out;
333}
334
335void __exit x25_proc_exit(void)
336{
337	remove_proc_entry("forward", x25_proc_dir);
338	remove_proc_entry("route", x25_proc_dir);
339	remove_proc_entry("socket", x25_proc_dir);
340	remove_proc_entry("x25", proc_net);
341}
342
343#else /* CONFIG_PROC_FS */
344
345int __init x25_proc_init(void)
346{
347	return 0;
348}
349
350void __exit x25_proc_exit(void)
351{
352}
353#endif /* CONFIG_PROC_FS */
354