summaryrefslogtreecommitdiffstats
path: root/net/wanrouter/wanproc.c
blob: 84833dd055dc741a223f3f59641e556b7771ee67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*****************************************************************************
* 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.
* ============================================================================
* Jan 20, 2001  Arnaldo C.Melo  Fix leak on error in router_proc_read, cleanups
* 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)
*****************************************************************************/

#include <linux/version.h>
#include <linux/config.h>
#include <linux/stddef.h>	/* offsetof(), etc. */
#include <linux/errno.h>	/* return codes */
#include <linux/kernel.h>
#include <linux/slab.h>	/* kmalloc(), kfree() */
#include <linux/mm.h>		/* verify_area(), etc. */
#include <linux/string.h>	/* inline mem*, str* functions */
#include <linux/init.h>		/* __init et al. */
#include <asm/segment.h>	/* kernel <-> user copy */
#include <asm/byteorder.h>	/* htons(), etc. */
#include <asm/uaccess.h>	/* copy_to_user */
#include <asm/io.h>
#include <linux/wanrouter.h>	/* WAN router API definitions */


/****** Defines and Macros **************************************************/

#define PROC_STATS_FORMAT "%30s: %12lu\n"

#ifndef	min
#define min(a,b) (((a)<(b))?(a):(b))
#endif

#define	PROC_BUFSZ	4000	/* buffer size for printing proc info */

/****** Function Prototypes *************************************************/

#ifdef CONFIG_PROC_FS

/* Proc filesystem interface */
static int router_proc_perms(struct inode *, int);
static ssize_t router_proc_read(struct file* file, char* buf, size_t count, 					loff_t *ppos);

/* Methods for preparing data for reading proc entries */

static int config_get_info(char* buf, char** start, off_t offs, int len);
static int status_get_info(char* buf, char** start, off_t offs, int len);
static int wandev_get_info(char* buf, char** start, off_t offs, int len);

/* Miscellaneous */

/*
 *	Structures for interfacing with the /proc filesystem.
 *	Router creates its own directory /proc/net/wanrouter with the folowing
 *	entries:
 *	config		device configuration
 *	status		global device statistics
 *	<device>	entry for each WAN device
 */

/*
 *	Generic /proc/net/wanrouter/<file> file and inode operations 
 */
static struct file_operations router_fops =
{
	read:		router_proc_read,
};

static struct inode_operations router_inode =
{
	permission:	router_proc_perms,
};

/*
 *	/proc/net/wanrouter/<device> file operations
 */

static struct file_operations wandev_fops =
{
	read:		router_proc_read,
	ioctl:		wanrouter_ioctl,
};

/*
 *	/proc/net/wanrouter 
 */

static struct proc_dir_entry *proc_router;

/* Strings */
static char conf_hdr[] =
	"Device name    | port |IRQ|DMA| mem.addr |mem.size|"
	"option1|option2|option3|option4\n";
	
static char stat_hdr[] =
	"Device name    |station|interface|clocking|baud rate| MTU |ndev"
	"|link state\n";


/*
 *	Interface functions
 */

/*
 *	Initialize router proc interface.
 */

int __init wanrouter_proc_init (void)
{
	struct proc_dir_entry *p;
	proc_router = proc_mkdir(ROUTER_NAME, proc_net);
	if (!proc_router)
		goto fail;

	p = create_proc_entry("config",0,proc_router);
	if (!p)
		goto fail_config;
	p->proc_fops = &router_fops;
	p->proc_iops = &router_inode;
	p->get_info = config_get_info;
	p = create_proc_entry("status",0,proc_router);
	if (!p)
		goto fail_stat;
	p->proc_fops = &router_fops;
	p->proc_iops = &router_inode;
	p->get_info = status_get_info;
	return 0;
fail_stat:
	remove_proc_entry("config", proc_router);
fail_config:
	remove_proc_entry(ROUTER_NAME, proc_net);
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);
	remove_proc_entry(ROUTER_NAME,proc_net);
}

/*
 *	Add directory entry for WAN device.
 */

int wanrouter_proc_add (wan_device_t* wandev)
{
	if (wandev->magic != ROUTER_MAGIC)
		return -EINVAL;
		
	wandev->dent = create_proc_entry(wandev->name, 0, proc_router);
	if (!wandev->dent)
		return -ENOMEM;
	wandev->dent->proc_fops	= &wandev_fops;
	wandev->dent->proc_iops	= &router_inode;
	wandev->dent->get_info	= wandev_get_info;
	wandev->dent->data	= wandev;
	return 0;
}

/*
 *	Delete directory entry for WAN device.
 */
 
int wanrouter_proc_delete(wan_device_t* wandev)
{
	if (wandev->magic != ROUTER_MAGIC)
		return -EINVAL;
	remove_proc_entry(wandev->name, proc_router);
	return 0;
}

/****** Proc filesystem entry points ****************************************/

/*
 *	Verify access rights.
 */

static int router_proc_perms (struct inode* inode, int op)
{
	return 0;
}

/*
 *	Read router proc directory entry.
 *	This is universal routine for reading all entries in /proc/net/wanrouter
 *	directory.  Each directory entry contains a pointer to the 'method' for
 *	preparing data for that entry.
 *	o verify arguments
 *	o allocate kernel buffer
 *	o call get_info() to prepare data
 *	o copy data to user space
 *	o release kernel buffer
 *
 *	Return:	number of bytes copied to user space (0, if no data)
 *		<0	error
 */

static ssize_t router_proc_read(struct file* file, char* buf, size_t count,
				loff_t *ppos)
{
	struct inode *inode = file->f_dentry->d_inode;
	struct proc_dir_entry* dent;
	char* page;
	int pos, offs, len;

	if (count <= 0)
		return 0;
		
	dent = inode->u.generic_ip;
	if ((dent == NULL) || (dent->get_info == NULL))
		return 0;
		
	page = kmalloc(PROC_BUFSZ, GFP_KERNEL);
	if (page == NULL)
		return -ENOBUFS;
		
	pos = dent->get_info(page, dent->data, 0, 0);
	offs = file->f_pos;
	if (offs < pos) {
		len = min(pos - offs, count);
		if(copy_to_user(buf, (page + offs), len)) {
			len = -EFAULT;
			goto out;
		}
		file->f_pos += len;
	}
	else
		len = 0;
out:	kfree(page);
	return len;
}

/*
 *	Prepare data for reading 'Config' entry.
 *	Return length of data.
 */

static int config_get_info(char* buf, char** start, off_t offs, int len)
{
	int cnt = sizeof(conf_hdr) - 1;
	wan_device_t* wandev;
	strcpy(buf, conf_hdr);
	for (wandev = router_devlist;
	     wandev && (cnt < (PROC_BUFSZ - 120));
	     wandev = wandev->next) {
		if (wandev->state) cnt += sprintf(&buf[cnt],
			"%-15s|0x%-4X|%3u|%3u| 0x%-8lX |0x%-6X|%7u|%7u|%7u|%7u\n",
			wandev->name,
			wandev->ioport,
			wandev->irq,
			wandev->dma,
			wandev->maddr,
			wandev->msize,
			wandev->hw_opt[0],
			wandev->hw_opt[1],
			wandev->hw_opt[2],
			wandev->hw_opt[3]);
	}

	return cnt;
}

/*
 *	Prepare data for reading 'Status' entry.
 *	Return length of data.
 */

static int status_get_info(char* buf, char** start, off_t offs, int len)
{
	int cnt = 0;
	wan_device_t* wandev;

	cnt += sprintf(&buf[cnt], "\nSTATUS FOR PORT 0\n\n");
	strcpy(&buf[cnt], stat_hdr);
	cnt += sizeof(stat_hdr) - 1;

	for (wandev = router_devlist;
	     wandev && (cnt < (PROC_BUFSZ - 80));
	     wandev = wandev->next) {
		if (!wandev->state) continue;
		cnt += sprintf(&buf[cnt],
			"%-15s|%-7s|%-9s|%-8s|%9u|%5u|%3u |",
			wandev->name,
			wandev->station ? " DCE" : " DTE",
			wandev->interface ? " V.35" : " RS-232",
			wandev->clocking ? "internal" : "external",
			wandev->bps,
			wandev->mtu,
			wandev->ndev);

		switch (wandev->state) {

		case WAN_UNCONFIGURED:
			cnt += sprintf(&buf[cnt], "%-12s\n", "unconfigured");
			break;

		case WAN_DISCONNECTED:
			cnt += sprintf(&buf[cnt], "%-12s\n", "disconnected");
			break;

		case WAN_CONNECTING:
			cnt += sprintf(&buf[cnt], "%-12s\n", "connecting");
			break;

		case WAN_CONNECTED:
			cnt += sprintf(&buf[cnt], "%-12s\n", "connected");
			break;

		default:
			cnt += sprintf(&buf[cnt], "%-12s\n", "invalid");
			break;
		}
	}
	return cnt;
}

/*
 *	Prepare data for reading <device> entry.
 *	Return length of data.
 *
 *	On entry, the 'start' argument will contain a pointer to WAN device
 *	data space.
 */

static int wandev_get_info(char* buf, char** start, off_t offs, int len)
{
	wan_device_t* wandev = (void*)start;
	int cnt = 0;
	int rslt = 0;

	if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC))
		return 0;
	if (!wandev->state)
		return sprintf(&buf[cnt], "device is not configured!\n");

	/* Update device statistics */
	if (wandev->update) {

		rslt = wandev->update(wandev);
		if(rslt) {
			switch (rslt) {
			case -EAGAIN:
				return sprintf(&buf[cnt], "Device is busy!\n");

			default:
				return sprintf(&buf[cnt],
					"Device is not configured!\n");
			}
		}
	}

        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "total packets received", wandev->stats.rx_packets);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "total packets transmitted", wandev->stats.tx_packets);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "total bytes received", wandev->stats.rx_bytes);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "total bytes transmitted", wandev->stats.tx_bytes);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "bad packets received", wandev->stats.rx_errors);
       	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "packet transmit problems", wandev->stats.tx_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "received frames dropped", wandev->stats.rx_dropped);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "transmit frames dropped", wandev->stats.tx_dropped);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "multicast packets received", wandev->stats.multicast);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "transmit collisions", wandev->stats.collisions);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "receive length errors", wandev->stats.rx_length_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
		"receiver overrun errors", wandev->stats.rx_over_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
		"CRC errors", wandev->stats.rx_crc_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
		"frame format errors (aborts)", wandev->stats.rx_frame_errors);
        cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
                "receiver fifo overrun", wandev->stats.rx_fifo_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
		"receiver missed packet", wandev->stats.rx_missed_errors);
	cnt += sprintf(&buf[cnt], PROC_STATS_FORMAT,
		"aborted frames transmitted", wandev->stats.tx_aborted_errors);
  	return cnt;
}

/*
 *	End
 */
 
#else

/*
 *	No /proc - output stubs
 */
 
int __init wanrouter_proc_init(void)
{
	return 0;
}

void wanrouter_proc_cleanup(void)
{
	return;
}

int wanrouter_proc_add(wan_device_t *wandev)
{
	return 0;
}

int wanrouter_proc_delete(wan_device_t *wandev)
{
	return 0;
}

#endif

/*
 *	End
 */