Blame view

net/wanrouter/wanproc.c 9.15 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  /*****************************************************************************
  * wanproc.c	WAN Router Module. /proc filesystem interface.
  *
  *		This module is completely hardware-independent and provides
  *		access to the router using Linux /proc filesystem.
  *
  * Author: 	Gideon Hack
  *
  * Copyright:	(c) 1995-1999 Sangoma Technologies Inc.
  *
  *		This program is free software; you can redistribute it and/or
  *		modify it under the terms of the GNU General Public License
  *		as published by the Free Software Foundation; either version
  *		2 of the License, or (at your option) any later version.
  * ============================================================================
  * Jun 02, 1999  Gideon Hack	Updates for Linux 2.2.X kernels.
  * Jun 29, 1997	Alan Cox	Merged with 1.0.3 vendor code
  * Jan 29, 1997	Gene Kozin	v1.0.1. Implemented /proc read routines
  * Jan 30, 1997	Alan Cox	Hacked around for 2.1
  * Dec 13, 1996	Gene Kozin	Initial version (based on Sangoma's WANPIPE)
  *****************************************************************************/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
24
25
26
27
28
  #include <linux/init.h>		/* __initfunc et al. */
  #include <linux/stddef.h>	/* offsetof(), etc. */
  #include <linux/errno.h>	/* return codes */
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/wanrouter.h>	/* WAN router API definitions */
  #include <linux/seq_file.h>
15fd0cd9a   Arnd Bergmann   net: autoconvert ...
29
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30

457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
31
  #include <net/net_namespace.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
34
35
36
37
38
39
40
  #include <asm/io.h>
  
  #define PROC_STATS_FORMAT "%30s: %12lu
  "
  
  /****** Defines and Macros **************************************************/
  
  #define PROT_DECODE(prot) ((prot == WANCONFIG_FR) ? " FR" :\
  			      (prot == WANCONFIG_X25) ? " X25" : \
4ba6122b4   YOSHIFUJI Hideaki   [NET] WANROUTER: ...
41
  				 (prot == WANCONFIG_PPP) ? " PPP" : \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
  				    (prot == WANCONFIG_CHDLC) ? " CHDLC": \
  				       (prot == WANCONFIG_MPPP) ? " MPPP" : \
4ba6122b4   YOSHIFUJI Hideaki   [NET] WANROUTER: ...
44
  					   " Unknown" )
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  
  /****** Function Prototypes *************************************************/
  
  #ifdef CONFIG_PROC_FS
  
  /* Miscellaneous */
  
  /*
   *	Structures for interfacing with the /proc filesystem.
   *	Router creates its own directory /proc/net/router with the folowing
   *	entries:
   *	config		device configuration
   *	status		global device statistics
   *	<device>	entry for each WAN device
   */
  
  /*
   *	Generic /proc/net/router/<file> file and inode operations
   */
  
  /*
   *	/proc/net/router
   */
15fd0cd9a   Arnd Bergmann   net: autoconvert ...
68
  static DEFINE_MUTEX(config_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  static struct proc_dir_entry *proc_router;
  
  /* Strings */
  
  /*
   *	Interface functions
   */
  
  /****** Proc filesystem entry points ****************************************/
  
  /*
   *	Iterator
   */
  static void *r_start(struct seq_file *m, loff_t *pos)
9ee62630f   Hannes Eder   wanrouter: fix sp...
83
  	__acquires(kernel_lock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
  {
  	struct wan_device *wandev;
  	loff_t l = *pos;
15fd0cd9a   Arnd Bergmann   net: autoconvert ...
87
  	mutex_lock(&config_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  	if (!l--)
  		return SEQ_START_TOKEN;
  	for (wandev = wanrouter_router_devlist; l-- && wandev;
  	     wandev = wandev->next)
  		;
  	return wandev;
  }
  
  static void *r_next(struct seq_file *m, void *v, loff_t *pos)
  {
  	struct wan_device *wandev = v;
  	(*pos)++;
  	return (v == SEQ_START_TOKEN) ? wanrouter_router_devlist : wandev->next;
  }
  
  static void r_stop(struct seq_file *m, void *v)
9ee62630f   Hannes Eder   wanrouter: fix sp...
104
  	__releases(kernel_lock)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
  {
15fd0cd9a   Arnd Bergmann   net: autoconvert ...
106
  	mutex_unlock(&config_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  }
  
  static int config_show(struct seq_file *m, void *v)
  {
  	struct wan_device *p = v;
  	if (v == SEQ_START_TOKEN) {
  		seq_puts(m, "Device name    | port |IRQ|DMA|  mem.addr  |"
  			    "mem.size|option1|option2|option3|option4
  ");
  		return 0;
  	}
  	if (!p->state)
  		return 0;
  	seq_printf(m, "%-15s|0x%-4X|%3u|%3u| 0x%-8lX |0x%-6X|%7u|%7u|%7u|%7u
  ",
  			p->name, p->ioport, p->irq, p->dma, p->maddr, p->msize,
  			p->hw_opt[0], p->hw_opt[1], p->hw_opt[2], p->hw_opt[3]);
  	return 0;
  }
  
  static int status_show(struct seq_file *m, void *v)
  {
  	struct wan_device *p = v;
  	if (v == SEQ_START_TOKEN) {
  		seq_puts(m, "Device name    |protocol|station|interface|"
  			    "clocking|baud rate| MTU |ndev|link state
  ");
  		return 0;
  	}
  	if (!p->state)
  		return 0;
  	seq_printf(m, "%-15s|%-8s| %-7s| %-9s|%-8s|%9u|%5u|%3u |",
  		p->name,
  		PROT_DECODE(p->config_id),
  		p->config_id == WANCONFIG_FR ?
  			(p->station ? "Node" : "CPE") :
  			(p->config_id == WANCONFIG_X25 ?
  			(p->station ? "DCE" : "DTE") :
  			("N/A")),
  		p->interface ? "V.35" : "RS-232",
  		p->clocking ? "internal" : "external",
  		p->bps,
  		p->mtu,
  		p->ndev);
  
  	switch (p->state) {
  	case WAN_UNCONFIGURED:
  		seq_printf(m, "%-12s
  ", "unconfigured");
  		break;
  	case WAN_DISCONNECTED:
  		seq_printf(m, "%-12s
  ", "disconnected");
  		break;
  	case WAN_CONNECTING:
  		seq_printf(m, "%-12s
  ", "connecting");
  		break;
  	case WAN_CONNECTED:
  		seq_printf(m, "%-12s
  ", "connected");
  		break;
  	default:
  		seq_printf(m, "%-12s
  ", "invalid");
  		break;
  	}
  	return 0;
  }
56b3d975b   Philippe De Muyter   [NET]: Make all i...
176
  static const struct seq_operations config_op = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
181
  	.start	= r_start,
  	.next	= r_next,
  	.stop	= r_stop,
  	.show	= config_show,
  };
56b3d975b   Philippe De Muyter   [NET]: Make all i...
182
  static const struct seq_operations status_op = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  	.start	= r_start,
  	.next	= r_next,
  	.stop	= r_stop,
  	.show	= status_show,
  };
  
  static int config_open(struct inode *inode, struct file *file)
  {
  	return seq_open(file, &config_op);
  }
  
  static int status_open(struct inode *inode, struct file *file)
  {
  	return seq_open(file, &status_op);
  }
da7071d7e   Arjan van de Ven   [PATCH] mark stru...
198
  static const struct file_operations config_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
200
201
202
203
204
  	.owner	 = THIS_MODULE,
  	.open	 = config_open,
  	.read	 = seq_read,
  	.llseek	 = seq_lseek,
  	.release = seq_release,
  };
da7071d7e   Arjan van de Ven   [PATCH] mark stru...
205
  static const struct file_operations status_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  	.owner	 = THIS_MODULE,
  	.open	 = status_open,
  	.read	 = seq_read,
  	.llseek	 = seq_lseek,
  	.release = seq_release,
  };
  
  static int wandev_show(struct seq_file *m, void *v)
  {
  	struct wan_device *wandev = m->private;
  
  	if (wandev->magic != ROUTER_MAGIC)
  		return 0;
  
  	if (!wandev->state) {
  		seq_puts(m, "device is not configured!
  ");
  		return 0;
  	}
  
  	/* Update device statistics */
  	if (wandev->update) {
  		int err = wandev->update(wandev);
  		if (err == -EAGAIN) {
  			seq_puts(m, "Device is busy!
  ");
  			return 0;
  		}
  		if (err) {
  			seq_puts(m, "Device is not configured!
  ");
  			return 0;
  		}
  	}
  
  	seq_printf(m, PROC_STATS_FORMAT,
  		"total packets received", wandev->stats.rx_packets);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"total packets transmitted", wandev->stats.tx_packets);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"total bytes received", wandev->stats.rx_bytes);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"total bytes transmitted", wandev->stats.tx_bytes);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"bad packets received", wandev->stats.rx_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"packet transmit problems", wandev->stats.tx_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"received frames dropped", wandev->stats.rx_dropped);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"transmit frames dropped", wandev->stats.tx_dropped);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"multicast packets received", wandev->stats.multicast);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"transmit collisions", wandev->stats.collisions);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"receive length errors", wandev->stats.rx_length_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"receiver overrun errors", wandev->stats.rx_over_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"CRC errors", wandev->stats.rx_crc_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"frame format errors (aborts)", wandev->stats.rx_frame_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"receiver fifo overrun", wandev->stats.rx_fifo_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"receiver missed packet", wandev->stats.rx_missed_errors);
  	seq_printf(m, PROC_STATS_FORMAT,
  		"aborted frames transmitted", wandev->stats.tx_aborted_errors);
  	return 0;
  }
  
  static int wandev_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, wandev_show, PDE(inode)->data);
  }
da7071d7e   Arjan van de Ven   [PATCH] mark stru...
282
  static const struct file_operations wandev_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
283
284
285
286
287
  	.owner	 = THIS_MODULE,
  	.open	 = wandev_open,
  	.read	 = seq_read,
  	.llseek	 = seq_lseek,
  	.release = single_release,
866988eda   Alan Cox   wanrouter: Push d...
288
  	.unlocked_ioctl  = wanrouter_ioctl,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
296
297
  };
  
  /*
   *	Initialize router proc interface.
   */
  
  int __init wanrouter_proc_init(void)
  {
  	struct proc_dir_entry *p;
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
298
  	proc_router = proc_mkdir(ROUTER_NAME, init_net.proc_net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
300
  	if (!proc_router)
  		goto fail;
6d37a1581   Wang Chen   [WANROUTER]: Use ...
301
  	p = proc_create("config", S_IRUGO, proc_router, &config_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
302
303
  	if (!p)
  		goto fail_config;
6d37a1581   Wang Chen   [WANROUTER]: Use ...
304
  	p = proc_create("status", S_IRUGO, proc_router, &status_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
305
306
  	if (!p)
  		goto fail_stat;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
  	return 0;
  fail_stat:
  	remove_proc_entry("config", proc_router);
  fail_config:
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
311
  	remove_proc_entry(ROUTER_NAME, init_net.proc_net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
312
313
314
315
316
317
318
319
320
321
322
323
  fail:
  	return -ENOMEM;
  }
  
  /*
   *	Clean up router proc interface.
   */
  
  void wanrouter_proc_cleanup(void)
  {
  	remove_proc_entry("config", proc_router);
  	remove_proc_entry("status", proc_router);
457c4cbc5   Eric W. Biederman   [NET]: Make /proc...
324
  	remove_proc_entry(ROUTER_NAME, init_net.proc_net);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
326
327
328
329
330
331
332
333
334
  }
  
  /*
   *	Add directory entry for WAN device.
   */
  
  int wanrouter_proc_add(struct wan_device* wandev)
  {
  	if (wandev->magic != ROUTER_MAGIC)
  		return -EINVAL;
6d37a1581   Wang Chen   [WANROUTER]: Use ...
335
336
  	wandev->dent = proc_create(wandev->name, S_IRUGO,
  				   proc_router, &wandev_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
337
338
  	if (!wandev->dent)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
  	wandev->dent->data	= wandev;
  	return 0;
  }
  
  /*
   *	Delete directory entry for WAN device.
   */
  int wanrouter_proc_delete(struct wan_device* wandev)
  {
  	if (wandev->magic != ROUTER_MAGIC)
  		return -EINVAL;
  	remove_proc_entry(wandev->name, proc_router);
  	return 0;
  }
  
  #else
  
  /*
   *	No /proc - output stubs
   */
  
  int __init wanrouter_proc_init(void)
  {
  	return 0;
  }
  
  void wanrouter_proc_cleanup(void)
  {
  }
  
  int wanrouter_proc_add(struct wan_device *wandev)
  {
  	return 0;
  }
  
  int wanrouter_proc_delete(struct wan_device *wandev)
  {
  	return 0;
  }
  
  #endif
  
  /*
   *	End
   */