summaryrefslogtreecommitdiffstats
path: root/net/wanrouter
diff options
context:
space:
mode:
authorRalf Baechle <ralf@linux-mips.org>1997-04-29 21:13:14 +0000
committer <ralf@linux-mips.org>1997-04-29 21:13:14 +0000
commit19c9bba94152148523ba0f7ef7cffe3d45656b11 (patch)
tree40b1cb534496a7f1ca0f5c314a523c69f1fee464 /net/wanrouter
parent7206675c40394c78a90e74812bbdbf8cf3cca1be (diff)
Import of Linux/MIPS 2.1.36
Diffstat (limited to 'net/wanrouter')
-rw-r--r--net/wanrouter/Makefile17
-rw-r--r--net/wanrouter/patchlevel1
-rw-r--r--net/wanrouter/wanmain.c676
-rw-r--r--net/wanrouter/wanproc.c460
4 files changed, 1154 insertions, 0 deletions
diff --git a/net/wanrouter/Makefile b/net/wanrouter/Makefile
new file mode 100644
index 000000000..12afaee5d
--- /dev/null
+++ b/net/wanrouter/Makefile
@@ -0,0 +1,17 @@
+#
+# Makefile for the Linux WAN router layer.
+#
+# Note! Dependencies are done automagically by 'make dep', which also
+# removes any old dependencies. DON'T put your own dependencies here
+# unless it's something special (ie not a .c file).
+#
+# Note 2! The CFLAGS definition is now in the main makefile...
+
+O_TARGET := wanrouter.o
+O_OBJS := wanmain.o wanproc.o
+M_OBJS := $(O_TARGET)
+
+include $(TOPDIR)/Rules.make
+
+tar:
+ tar -cvf /dev/f1 .
diff --git a/net/wanrouter/patchlevel b/net/wanrouter/patchlevel
new file mode 100644
index 000000000..3eefcb9dd
--- /dev/null
+++ b/net/wanrouter/patchlevel
@@ -0,0 +1 @@
+1.0.0
diff --git a/net/wanrouter/wanmain.c b/net/wanrouter/wanmain.c
new file mode 100644
index 000000000..948bf81fa
--- /dev/null
+++ b/net/wanrouter/wanmain.c
@@ -0,0 +1,676 @@
+/*****************************************************************************
+* wanmain.c WAN Multiprotocol Router Module. Main code.
+*
+* This module is completely hardware-independent and provides
+* the following common services for the WAN Link Drivers:
+* o WAN device managenment (registering, unregistering)
+* o Network interface management
+* o Physical connection management (dial-up, incomming calls)
+* o Logical connection management (switched virtual circuits)
+* o Protocol encapsulation/decapsulation
+*
+* Author: Gene Kozin <genek@compuserve.com>
+*
+* Copyright: (c) 1995-1996 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.
+* ============================================================================
+* Dec 27, 1996 Gene Kozin Initial version (based on Sangoma's WANPIPE)
+* Jan 31, 1997 Alan Cox Hacked it about a bit for 2.1
+*****************************************************************************/
+
+#include <linux/stddef.h> /* offsetof(), etc. */
+#include <linux/errno.h> /* return codes */
+#include <linux/config.h> /* OS configuration options */
+#include <linux/kernel.h>
+#include <linux/module.h> /* support for loadable modules */
+#include <linux/malloc.h> /* kmalloc(), kfree() */
+#include <linux/mm.h> /* verify_area(), etc. */
+#include <linux/string.h> /* inline mem*, str* functions */
+#include <asm/segment.h> /* kernel <-> user copy */
+#include <asm/byteorder.h> /* htons(), etc. */
+#include <asm/uaccess.h> /* copy_to/from_user */
+#include <linux/wanrouter.h> /* WAN router API definitions */
+
+/****** Defines and Macros **************************************************/
+
+#ifndef min
+#define min(a,b) (((a)<(b))?(a):(b))
+#endif
+#ifndef max
+#define max(a,b) (((a)>(b))?(a):(b))
+#endif
+
+/****** Function Prototypes *************************************************/
+
+/*
+ * Kernel loadable module interface.
+ */
+
+#ifdef MODULE
+int init_module (void);
+void cleanup_module (void);
+#endif
+
+/*
+ * WAN device IOCTL handlers
+ */
+
+static int device_setup (wan_device_t* wandev, wandev_conf_t* u_conf);
+static int device_stat (wan_device_t* wandev, wandev_stat_t* u_stat);
+static int device_shutdown (wan_device_t* wandev);
+static int device_new_if (wan_device_t* wandev, wanif_conf_t* u_conf);
+static int device_del_if (wan_device_t* wandev, char* u_name);
+
+/*
+ * Miscellaneous
+ */
+
+static wan_device_t* find_device (char* name);
+static int delete_interface (wan_device_t* wandev, char* name, int forse);
+
+/*
+ * Global Data
+ */
+
+static char fullname[] = "WAN Router";
+static char copyright[] = "(c) 1995-1996 Sangoma Technologies Inc.";
+static char modname[] = ROUTER_NAME; /* short module name */
+static wan_device_t* devlist = NULL; /* list of registered devices */
+static int devcnt = 0;
+
+/*
+ * Organizationally Unique Identifiers for encapsulation/decapsulation
+ */
+
+static unsigned char oui_ether[] = { 0x00, 0x00, 0x00 };
+static unsigned char oui_802_2[] = { 0x00, 0x80, 0xC2 };
+
+#ifdef MODULE
+
+/*
+ * Kernel Loadable Module Entry Points
+ */
+
+/*
+ * Module 'insert' entry point.
+ * o print announcement
+ * o initialize static data
+ * o create /proc/net/router directory and static entries
+ *
+ * Return: 0 Ok
+ * < 0 error.
+ * Context: process
+ */
+
+int init_module (void)
+{
+ int err;
+
+ printk(KERN_INFO "%s v%u.%u %s\n",
+ fullname, ROUTER_VERSION, ROUTER_RELEASE, copyright);
+ err = wanrouter_proc_init();
+ if (err) printk(KERN_ERR
+ "%s: can't create entry in proc filesystem!\n", modname);
+ return err;
+}
+
+/*
+ * Module 'remove' entry point.
+ * o delete /proc/net/router directory and static entries.
+ */
+
+void cleanup_module (void)
+{
+ wanrouter_proc_cleanup();
+}
+
+#else
+
+void wanrouter_init(void)
+{
+ int err = wanrouter_proc_init();
+ if (err) printk(KERN_ERR
+ "%s: can't create entry in proc filesystem!\n", modname);
+}
+#endif
+
+/*
+ * Kernel APIs
+ */
+
+/*
+ * Register WAN device.
+ * o verify device credentials
+ * o create an entry for the device in the /proc/net/router directory
+ * o initialize internally maintained fields of the wan_device structure
+ * o link device data space to a singly-linked list
+ * o if it's the first device, then start kernel 'thread'
+ * o increment module use count
+ *
+ * Return:
+ * 0 Ok
+ * < 0 error.
+ *
+ * Context: process
+ */
+
+int register_wan_device(wan_device_t* wandev)
+{
+ int err, namelen;
+
+ if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
+ (wandev->name == NULL))
+ return -EINVAL;
+
+ namelen = strlen(wandev->name);
+ if (!namelen || (namelen > WAN_DRVNAME_SZ))
+ return -EINVAL;
+
+ if (find_device(wandev->name) != NULL)
+ return -EEXIST;
+
+#ifdef WANDEBUG
+ printk(KERN_INFO "%s: registering WAN device %s\n",
+ modname, wandev->name);
+#endif
+
+ /*
+ * Register /proc directory entry
+ */
+ err = wanrouter_proc_add(wandev);
+ if (err)
+ {
+ printk(KERN_ERR
+ "%s: can't create /proc/net/router/%s entry!\n",
+ modname, wandev->name)
+ ;
+ return err;
+ }
+
+ /*
+ * Initialize fields of the wan_device structure maintained by the
+ * router and update local data.
+ */
+
+ wandev->ndev = 0;
+ wandev->dev = NULL;
+ wandev->next = devlist;
+ devlist = wandev;
+ ++devcnt;
+ MOD_INC_USE_COUNT; /* prevent module from unloading */
+ return 0;
+}
+
+/*
+ * Unregister WAN device.
+ * o shut down device
+ * o unlink device data space from the linked list
+ * o delete device entry in the /proc/net/router directory
+ * o decrement module use count
+ *
+ * Return: 0 Ok
+ * <0 error.
+ * Context: process
+ */
+
+int unregister_wan_device(char* name)
+{
+ wan_device_t *wandev, *prev;
+
+ if (name == NULL)
+ return -EINVAL;
+
+ for (wandev = devlist, prev = NULL;
+ wandev && strcmp(wandev->name, name);
+ prev = wandev, wandev = wandev->next)
+ ;
+ if (wandev == NULL)
+ return -ENODEV;
+
+#ifdef WANDEBUG
+ printk(KERN_INFO "%s: unregistering WAN device %s\n", modname, name);
+#endif
+
+ if (wandev->state != WAN_UNCONFIGURED)
+ {
+ while(wandev->dev)
+ delete_interface(wandev, wandev->dev->name, 1);
+ if (wandev->shutdown)
+ wandev->shutdown(wandev);
+ }
+ if (prev)
+ prev->next = wandev->next;
+ else
+ devlist = wandev->next;
+ --devcnt;
+ wanrouter_proc_delete(wandev);
+ MOD_DEC_USE_COUNT;
+ return 0;
+}
+
+/*
+ * Encapsulate packet.
+ *
+ * Return: encapsulation header size
+ * < 0 - unsupported Ethertype
+ *
+ * Notes:
+ * 1. This function may be called on interrupt context.
+ */
+
+int wanrouter_encapsulate (struct sk_buff* skb, struct device* dev)
+{
+ int hdr_len = 0;
+
+ switch (skb->protocol)
+ {
+ case ETH_P_IP: /* IP datagram encapsulation */
+ hdr_len += 1;
+ skb_push(skb, 1);
+ skb->data[0] = NLPID_IP;
+ break;
+
+ case ETH_P_IPX: /* SNAP encapsulation */
+ case ETH_P_ARP:
+ hdr_len += 6;
+ skb_push(skb, 6);
+ skb->data[0] = NLPID_SNAP;
+ memcpy(&skb->data[1], oui_ether, sizeof(oui_ether));
+ *((unsigned short*)&skb->data[4]) = htons(skb->protocol);
+ break;
+
+ default: /* Unknown packet type */
+ printk(KERN_INFO
+ "%s: unsupported Ethertype 0x%04X on interface %s!\n",
+ modname, skb->protocol, dev->name);
+ hdr_len = -EINVAL;
+ }
+ return hdr_len;
+}
+
+/*
+ * Decapsulate packet.
+ *
+ * Return: Ethertype (in network order)
+ * 0 unknown encapsulation
+ *
+ * Notes:
+ * 1. This function may be called on interrupt context.
+ */
+
+unsigned short wanrouter_type_trans (struct sk_buff* skb, struct device* dev)
+{
+ int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
+ unsigned short ethertype;
+
+ switch (skb->data[cnt])
+ {
+ case NLPID_IP: /* IP datagramm */
+ ethertype = htons(ETH_P_IP);
+ cnt += 1;
+ break;
+
+ case NLPID_SNAP: /* SNAP encapsulation */
+ if (memcmp(&skb->data[cnt + 1], oui_ether, sizeof(oui_ether)))
+ {
+ printk(KERN_INFO
+ "%s: unsupported SNAP OUI %02X-%02X-%02X "
+ "on interface %s!\n", modname,
+ skb->data[cnt+1], skb->data[cnt+2],
+ skb->data[cnt+3], dev->name);
+ ;
+ return 0;
+ }
+ ethertype = *((unsigned short*)&skb->data[cnt+4]);
+ cnt += 6;
+ break;
+
+ /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
+
+ default:
+ printk(KERN_INFO
+ "%s: unsupported NLPID 0x%02X on interface %s!\n",
+ modname, skb->data[cnt], dev->name)
+ ;
+ return 0;
+ }
+ skb->protocol = ethertype;
+ skb->pkt_type = PACKET_HOST; /* Physically point to point */
+ skb->mac.raw = skb->data;
+ skb_pull(skb, cnt);
+ return ethertype;
+}
+
+/*
+ * WAN device IOCTL.
+ * o find WAN device associated with this node
+ * o execute requested action or pass command to the device driver
+ */
+
+int wanrouter_ioctl(struct inode* inode, struct file* file,
+ unsigned int cmd, unsigned long arg)
+{
+ int err = 0;
+ struct proc_dir_entry* dent;
+ wan_device_t* wandev;
+
+ if (!suser())
+ return -EPERM;
+
+ if ((cmd >> 8) != ROUTER_IOCTL)
+ return -EINVAL;
+
+ dent = inode->u.generic_ip;
+ if ((dent == NULL) || (dent->data == NULL))
+ return -EINVAL;
+
+ wandev = dent->data;
+ if (wandev->magic != ROUTER_MAGIC)
+ return -EINVAL;
+
+ switch (cmd)
+ {
+ case ROUTER_SETUP:
+ err = device_setup(wandev, (void*)arg);
+ break;
+
+ case ROUTER_DOWN:
+ err = device_shutdown(wandev);
+ break;
+
+ case ROUTER_STAT:
+ err = device_stat(wandev, (void*)arg);
+ break;
+
+ case ROUTER_IFNEW:
+ err = device_new_if(wandev, (void*)arg);
+ break;
+
+ case ROUTER_IFDEL:
+ err = device_del_if(wandev, (void*)arg);
+ break;
+
+ case ROUTER_IFSTAT:
+ break;
+
+ default:
+ if ((cmd >= ROUTER_USER) &&
+ (cmd <= ROUTER_USER_MAX) &&
+ wandev->ioctl)
+ err = wandev->ioctl(wandev, cmd, arg)
+ ;
+ else err = -EINVAL;
+ }
+ return err;
+}
+
+/*
+ * WAN Driver IOCTL Handlers
+ */
+
+/*
+ * Setup WAN link device.
+ * o verify user address space
+ * o allocate kernel memory and copy configuration data to kernel space
+ * o if configuration data includes extension, copy it to kernel space too
+ * o call driver's setup() entry point
+ */
+
+static int device_setup (wan_device_t* wandev, wandev_conf_t* u_conf)
+{
+ void* data;
+ wandev_conf_t* conf;
+ int err= -EINVAL;
+
+ if (wandev->setup == NULL) /* Nothing to do ? */
+ return 0;
+
+ conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
+ if (conf == NULL)
+ return -ENOBUFS;
+
+ if(copy_from_user(conf, u_conf, sizeof(wandev_conf_t)))
+ {
+ kfree(conf);
+ return -EFAULT;
+ }
+
+ if (conf->magic != ROUTER_MAGIC)
+ goto bail;
+
+ if (conf->data_size && conf->data)
+ {
+ if(conf->data_size > 1024 || conf->data_size < 0)
+ goto bail;
+ data = kmalloc(conf->data_size, GFP_KERNEL);
+ if (data)
+ {
+ if(!copy_from_user(data, conf->data, conf->data_size))
+ {
+ conf->data=data;
+ wandev->setup(wandev,conf);
+ }
+ else
+ err = -ENOBUFS;
+ }
+ if (data)
+ kfree(data);
+ }
+bail:
+ kfree(conf);
+ return err;
+}
+
+/*
+ * Shutdown WAN device.
+ * o delete all not opened logical channels for this device
+ * o call driver's shutdown() entry point
+ */
+
+static int device_shutdown (wan_device_t* wandev)
+{
+ struct device* dev;
+
+ if (wandev->state == WAN_UNCONFIGURED)
+ return 0;
+
+ for (dev = wandev->dev; dev;)
+ {
+ if (delete_interface(wandev, dev->name, 0))
+ dev = dev->slave;
+ }
+ if (wandev->ndev)
+ return -EBUSY; /* there are opened interfaces */
+
+ if (wandev->shutdown)
+ return wandev->shutdown(wandev);
+ return 0;
+}
+
+/*
+ * Get WAN device status & statistics.
+ */
+
+static int device_stat (wan_device_t* wandev, wandev_stat_t* u_stat)
+{
+ wandev_stat_t stat;
+
+ memset(&stat, 0, sizeof(stat));
+
+ /* Ask device driver to update device statistics */
+ if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
+ wandev->update(wandev);
+
+ /* Fill out structure */
+ stat.ndev = wandev->ndev;
+ stat.state = wandev->state;
+
+ if(copy_to_user(u_stat, &stat, sizeof(stat)))
+ return -EFAULT;
+ return 0;
+}
+
+/*
+ * Create new WAN interface.
+ * o verify user address space
+ * o copy configuration data to kernel address space
+ * o allocate network interface data space
+ * o call driver's new_if() entry point
+ * o make sure there is no interface name conflict
+ * o register network interface
+ */
+
+static int device_new_if (wan_device_t* wandev, wanif_conf_t* u_conf)
+{
+ wanif_conf_t conf;
+ struct device* dev;
+ int err;
+
+ if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
+ return -ENODEV;
+
+ if(copy_from_user(&conf, u_conf, sizeof(wanif_conf_t)))
+ return -EFAULT;
+
+ if (conf.magic != ROUTER_MAGIC)
+ return -EINVAL;
+
+ dev = kmalloc(sizeof(struct device), GFP_KERNEL);
+ if (dev == NULL)
+ return -ENOBUFS;
+
+ memset(dev, 0, sizeof(struct device));
+ err = wandev->new_if(wandev, dev, &conf);
+ if (!err)
+ {
+ /* Register network interface. This will invoke init()
+ * function supplied by the driver. If device registered
+ * successfully, add it to the interface list.
+ */
+ if (dev->name == NULL)
+ err = -EINVAL;
+
+ else if (dev_get(dev->name))
+ err = -EEXIST; /* name already exists */
+ else
+ {
+#ifdef WANDEBUG
+ printk(KERN_INFO "%s: registering interface %s...\n",
+ modname, dev->name);
+#endif
+ err = register_netdev(dev);
+ if (!err)
+ {
+ cli(); /***** critical section start *****/
+ dev->slave = wandev->dev;
+ wandev->dev = dev;
+ ++wandev->ndev;
+ sti(); /****** critical section end ******/
+ return 0; /* done !!! */
+ }
+ }
+ if (wandev->del_if)
+ wandev->del_if(wandev, dev);
+ }
+ kfree(dev);
+ return err;
+}
+
+/*
+ * Delete WAN logical channel.
+ * o verify user address space
+ * o copy configuration data to kernel address space
+ */
+
+static int device_del_if (wan_device_t* wandev, char* u_name)
+{
+ char name[WAN_IFNAME_SZ + 1];
+
+ if (wandev->state == WAN_UNCONFIGURED)
+ return -ENODEV;
+
+ memset(name, 0, sizeof(name));
+ if(copy_from_user(name, u_name, WAN_IFNAME_SZ))
+ return -EFAULT;
+ return delete_interface(wandev, name, 0);
+}
+
+/*
+ * Miscellaneous Functions
+ */
+
+/*
+ * Find WAN device by name.
+ * Return pointer to the WAN device data space or NULL if device not found.
+ */
+
+static wan_device_t* find_device (char* name)
+{
+ wan_device_t* wandev;
+
+ for (wandev = devlist;wandev && strcmp(wandev->name, name);
+ wandev = wandev->next);
+ return wandev;
+}
+
+/*
+ * Delete WAN logical channel identified by its name.
+ * o find logical channel by its name
+ * o call driver's del_if() entry point
+ * o unregister network interface
+ * o unlink channel data space from linked list of channels
+ * o release channel data space
+ *
+ * Return: 0 success
+ * -ENODEV channel not found.
+ * -EBUSY interface is open
+ *
+ * Note: If (force != 0), then device will be destroyed even if interface
+ * associated with it is open. It's caller's responsibility to make
+ * sure that opened interfaces are not removed!
+ */
+
+static int delete_interface (wan_device_t* wandev, char* name, int force)
+{
+ struct device *dev, *prev;
+
+ for (dev = wandev->dev, prev = NULL;
+ dev && strcmp(name, dev->name);
+ prev = dev, dev = dev->slave);
+
+ if (dev == NULL)
+ return -ENODEV; /* interface not found */
+
+ if (dev->start)
+ {
+ if (force)
+ {
+ printk(KERN_WARNING
+ "%s: deleting opened interface %s!\n",modname, name);
+ }
+ else
+ return -EBUSY; /* interface in use */
+ }
+ if (wandev->del_if)
+ wandev->del_if(wandev, dev);
+
+ cli(); /***** critical section start *****/
+ if (prev)
+ prev->slave = dev->slave;
+ else
+ wandev->dev = dev->slave;
+ --wandev->ndev;
+ sti(); /****** critical section end ******/
+
+ unregister_netdev(dev);
+ kfree(dev);
+ return 0;
+}
+
+/*
+ * End
+ */
diff --git a/net/wanrouter/wanproc.c b/net/wanrouter/wanproc.c
new file mode 100644
index 000000000..ce7140db0
--- /dev/null
+++ b/net/wanrouter/wanproc.c
@@ -0,0 +1,460 @@
+/*****************************************************************************
+* wanproc.c WAN Multiprotocol Router Module. proc filesystem interface.
+*
+* This module is completely hardware-independent and provides
+* access to the router using Linux /proc filesystem.
+*
+* Author: Gene Kozin <genek@compuserve.com>
+*
+* Copyright: (c) 1995-1996 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.
+* ============================================================================
+* Dec 13, 1996 Gene Kozin Initial version (based on Sangoma's WANPIPE)
+* Jan 30, 1997 Alan Cox Hacked around for 2.1
+*****************************************************************************/
+
+#include <linux/stddef.h> /* offsetof(), etc. */
+#include <linux/errno.h> /* return codes */
+#include <linux/kernel.h>
+#include <linux/malloc.h> /* kmalloc(), kfree() */
+#include <linux/mm.h> /* verify_area(), etc. */
+#include <linux/string.h> /* inline mem*, str* functions */
+#include <asm/segment.h> /* kernel <-> user copy */
+#include <asm/byteorder.h> /* htons(), etc. */
+#include <asm/uaccess.h> /* copy_to_user */
+#include <linux/wanrouter.h> /* WAN router API definitions */
+
+
+/****** Defines and Macros **************************************************/
+
+#ifndef min
+#define min(a,b) (((a)<(b))?(a):(b))
+#endif
+#ifndef max
+#define max(a,b) (((a)>(b))?(a):(b))
+#endif
+
+#define ROUTER_PAGE_SZ 4000 /* buffer size for printing proc info */
+
+/****** Data Types **********************************************************/
+
+typedef struct wan_stat_entry
+{
+ struct wan_stat_entry * next;
+ char *description; /* description string */
+ void *data; /* -> data */
+ unsigned data_type; /* data type */
+} wan_stat_entry_t;
+
+/****** Function Prototypes *************************************************/
+
+/* Proc filesystem interface */
+static int router_proc_perms (struct inode*, int);
+static long router_proc_read(struct inode* inode, struct file* file, char* buf,
+ unsigned long count);
+
+/* Methods for preparing data for reading proc entries */
+
+static int about_get_info(char* buf, char** start, off_t offs, int len, int dummy);
+static int config_get_info(char* buf, char** start, off_t offs, int len, int dummy);
+static int status_get_info(char* buf, char** start, off_t offs, int len, int dummy);
+static int wandev_get_info(char* buf, char** start, off_t offs, int len, int dummy);
+
+/* Miscellaneous */
+
+/*
+ * Global Data
+ */
+
+/*
+ * Names of the proc directory entries
+ */
+
+static char name_root[] = ROUTER_NAME;
+static char name_info[] = "about";
+static char name_conf[] = "config";
+static char name_stat[] = "status";
+
+/*
+ * Structures for interfacing with the /proc filesystem.
+ * Router creates its own directory /proc/net/router with the folowing
+ * entries:
+ * About general information (version, copyright, etc.)
+ * Conf device configuration
+ * Stat global device statistics
+ * <device> entry for each WAN device
+ */
+
+/*
+ * Generic /proc/net/router/<file> file and inode operations
+ */
+
+static struct file_operations router_fops =
+{
+ NULL, /* lseek */
+ router_proc_read, /* read */
+ NULL, /* write */
+ NULL, /* readdir */
+ NULL, /* select */
+ NULL, /* ioctl */
+ NULL, /* mmap */
+ NULL, /* no special open code */
+ NULL, /* no special release code */
+ NULL /* can't fsync */
+};
+
+static struct inode_operations router_inode =
+{
+ &router_fops,
+ NULL, /* create */
+ NULL, /* lookup */
+ NULL, /* link */
+ NULL, /* unlink */
+ NULL, /* symlink */
+ NULL, /* mkdir */
+ NULL, /* rmdir */
+ NULL, /* mknod */
+ NULL, /* rename */
+ NULL, /* readlink */
+ NULL, /* follow_link */
+ NULL, /* readpage */
+ NULL, /* writepage */
+ NULL, /* bmap */
+ NULL, /* truncate */
+ router_proc_perms
+};
+
+/*
+ * /proc/net/router/<device> file and inode operations
+ */
+
+static struct file_operations wandev_fops =
+{
+ NULL, /* lseek */
+ router_proc_read, /* read */
+ NULL, /* write */
+ NULL, /* readdir */
+ NULL, /* select */
+ wanrouter_ioctl, /* ioctl */
+ NULL, /* mmap */
+ NULL, /* no special open code */
+ NULL, /* no special release code */
+ NULL /* can't fsync */
+};
+
+static struct inode_operations wandev_inode =
+{
+ &wandev_fops,
+ NULL, /* create */
+ NULL, /* lookup */
+ NULL, /* link */
+ NULL, /* unlink */
+ NULL, /* symlink */
+ NULL, /* mkdir */
+ NULL, /* rmdir */
+ NULL, /* mknod */
+ NULL, /* rename */
+ NULL, /* readlink */
+ NULL, /* follow_link */
+ NULL, /* readpage */
+ NULL, /* writepage */
+ NULL, /* bmap */
+ NULL, /* truncate */
+ router_proc_perms
+};
+
+/*
+ * Proc filesystem derectory entries.
+ */
+
+/*
+ * /proc/net/router
+ */
+
+static struct proc_dir_entry proc_router =
+{
+ 0, /* .low_ino */
+ sizeof(name_root) - 1, /* .namelen */
+ name_root, /* .name */
+ 0555 | S_IFDIR, /* .mode */
+ 2, /* .nlink */
+ 0, /* .uid */
+ 0, /* .gid */
+ 0, /* .size */
+ &proc_dir_inode_operations, /* .ops */
+ NULL, /* .get_info */
+ NULL, /* .fill_node */
+ NULL, /* .next */
+ NULL, /* .parent */
+ NULL, /* .subdir */
+ NULL, /* .data */
+};
+
+/*
+ * /proc/net/router/about
+ */
+
+static struct proc_dir_entry proc_router_info =
+{
+ 0, /* .low_ino */
+ sizeof(name_info) - 1, /* .namelen */
+ name_info, /* .name */
+ 0444 | S_IFREG, /* .mode */
+ 1, /* .nlink */
+ 0, /* .uid */
+ 0, /* .gid */
+ 0, /* .size */
+ &router_inode, /* .ops */
+ &about_get_info, /* .get_info */
+ NULL, /* .fill_node */
+ NULL, /* .next */
+ NULL, /* .parent */
+ NULL, /* .subdir */
+ NULL, /* .data */
+};
+
+/*
+ * /proc/net/router/config
+ */
+
+static struct proc_dir_entry proc_router_conf =
+{
+ 0, /* .low_ino */
+ sizeof(name_conf) - 1, /* .namelen */
+ name_conf, /* .name */
+ 0444 | S_IFREG, /* .mode */
+ 1, /* .nlink */
+ 0, /* .uid */
+ 0, /* .gid */
+ 0, /* .size */
+ &router_inode, /* .ops */
+ &config_get_info, /* .get_info */
+ NULL, /* .fill_node */
+ NULL, /* .next */
+ NULL, /* .parent */
+ NULL, /* .subdir */
+ NULL, /* .data */
+};
+
+/*
+ * /proc/net/router/status
+ */
+
+static struct proc_dir_entry proc_router_stat =
+{
+ 0, /* .low_ino */
+ sizeof(name_stat) - 1, /* .namelen */
+ name_stat, /* .name */
+ 0444 | S_IFREG, /* .mode */
+ 1, /* .nlink */
+ 0, /* .uid */
+ 0, /* .gid */
+ 0, /* .size */
+ &router_inode, /* .ops */
+ status_get_info, /* .get_info */
+ NULL, /* .fill_node */
+ NULL, /* .next */
+ NULL, /* .parent */
+ NULL, /* .subdir */
+ NULL, /* .data */
+};
+
+/*
+ * Interface functions
+ */
+
+/*
+ * Initialize router proc interface.
+ */
+
+int wanrouter_proc_init (void)
+{
+ int err = proc_register(&proc_net, &proc_router);
+
+ if (!err)
+ {
+ proc_register(&proc_router, &proc_router_info);
+ proc_register(&proc_router, &proc_router_conf);
+ proc_register(&proc_router, &proc_router_stat);
+ }
+ return err;
+}
+
+/*
+ * Clean up router proc interface.
+ */
+
+void wanrouter_proc_cleanup (void)
+{
+ proc_unregister(&proc_router, proc_router_info.low_ino);
+ proc_unregister(&proc_router, proc_router_conf.low_ino);
+ proc_unregister(&proc_router, proc_router_stat.low_ino);
+ proc_unregister(&proc_net, proc_router.low_ino);
+}
+
+/*
+ * Add directory entry for WAN device.
+ */
+
+int wanrouter_proc_add (wan_device_t* wandev)
+{
+ if (wandev->magic != ROUTER_MAGIC)
+ return -EINVAL;
+
+ memset(&wandev->dent, 0, sizeof(wandev->dent));
+ wandev->dent.namelen = strlen(wandev->name);
+ wandev->dent.name = wandev->name;
+ wandev->dent.mode = 0444 | S_IFREG;
+ wandev->dent.nlink = 1;
+ wandev->dent.ops = &wandev_inode;
+ wandev->dent.get_info = &wandev_get_info;
+ wandev->dent.data = wandev;
+ return proc_register(&proc_router, &wandev->dent);
+}
+
+/*
+ * Delete directory entry for WAN device.
+ */
+
+int wanrouter_proc_delete(wan_device_t* wandev)
+{
+ if (wandev->magic != ROUTER_MAGIC)
+ return -EINVAL;
+ proc_unregister(&proc_router, wandev->dent.low_ino);
+ 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/router
+ * 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 long router_proc_read(struct inode* inode, struct file* file,
+ char* buf, unsigned long count)
+{
+ 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(ROUTER_PAGE_SZ, GFP_KERNEL);
+ if (page == NULL)
+ return -ENOBUFS;
+
+ pos = dent->get_info(page, dent->data, 0, 0, 0);
+ offs = file->f_pos;
+ if (offs < pos)
+ {
+ len = min(pos - offs, count);
+ if(copy_to_user(buf, (page + offs), len))
+ return -EFAULT;
+ file->f_pos += len;
+ }
+ else
+ len = 0;
+ kfree(page);
+ return len;
+}
+
+/*
+ * Prepare data for reading 'About' entry.
+ * Return length of data.
+ */
+
+static int about_get_info(char* buf, char** start, off_t offs, int len,
+ int dummy)
+{
+ int cnt = 0;
+
+ cnt += sprintf(&buf[cnt], "%12s : %u.%u\n",
+ "version", ROUTER_VERSION, ROUTER_RELEASE);
+ return cnt;
+}
+
+/*
+ * Prepare data for reading 'Config' entry.
+ * Return length of data.
+ * NOT YET IMPLEMENTED
+ */
+
+static int config_get_info(char* buf, char** start, off_t offs, int len,
+ int dummy)
+{
+ int cnt = 0;
+
+ cnt += sprintf(&buf[cnt], "%12s : %u.%u\n",
+ "version", ROUTER_VERSION, ROUTER_RELEASE);
+ return cnt;
+}
+
+/*
+ * Prepare data for reading 'Status' entry.
+ * Return length of data.
+ * NOT YET IMPLEMENTED
+ */
+
+static int status_get_info(char* buf, char** start, off_t offs, int len,
+ int dummy)
+{
+ int cnt = 0;
+
+ cnt += sprintf(&buf[cnt], "%12s : %u.%u\n",
+ "version", ROUTER_VERSION, ROUTER_RELEASE);
+ 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,
+ int dummy)
+{
+ wan_device_t* wandev = (void*)start;
+ int cnt = 0;
+
+ if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC))
+ return 0;
+ cnt += sprintf(&buf[cnt], "%12s : %s\n", "name", wandev->name);
+ return cnt;
+}
+
+/*
+ * End
+ */
+