1 /*****************************************************************************
2 * wanmain.c WAN Multiprotocol Router Module. Main code.
4 * This module is completely hardware-independent and provides
5 * the following common services for the WAN Link Drivers:
6 * o WAN device managenment (registering, unregistering)
7 * o Network interface management
8 * o Physical connection management (dial-up, incoming calls)
9 * o Logical connection management (switched virtual circuits)
10 * o Protocol encapsulation/decapsulation
14 * Copyright: (c) 1995-1999 Sangoma Technologies Inc.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 * ============================================================================
21 * Nov 24, 2000 Nenad Corbic Updated for 2.4.X kernels
22 * Nov 07, 2000 Nenad Corbic Fixed the Mulit-Port PPP for kernels 2.2.16 and
24 * Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on
25 * kernels 2.2.16 or greater. The SyncPPP
27 * Jul 13, 2000 Nenad Corbic Added SyncPPP support
28 * Added extra debugging in device_setup().
29 * Oct 01, 1999 Gideon Hack Update for s514 PCI card
30 * Dec 27, 1996 Gene Kozin Initial version (based on Sangoma's WANPIPE)
31 * Jan 16, 1997 Gene Kozin router_devlist made public
32 * Jan 31, 1997 Alan Cox Hacked it about a bit for 2.1
33 * Jun 27, 1997 Alan Cox realigned with vendor code
34 * Oct 15, 1997 Farhan Thawar changed wan_encapsulate to add a pad byte of 0
35 * Apr 20, 1998 Alan Cox Fixed 2.1 symbols
36 * May 17, 1998 K. Baranowski Fixed SNAP encapsulation in wan_encapsulate
37 * Dec 15, 1998 Arnaldo Melo support for firmwares of up to 128000 bytes
38 * check wandev->setup return value
39 * Dec 22, 1998 Arnaldo Melo vmalloc/vfree used in device_setup to allocate
40 * kernel memory and copy configuration data to
41 * kernel space (for big firmwares)
42 * Jun 02, 1999 Gideon Hack Updates for Linux 2.0.X and 2.2.X kernels.
43 *****************************************************************************/
45 #include <linux/config.h>
46 #include <linux/stddef.h> /* offsetof(), etc. */
47 #include <linux/capability.h>
48 #include <linux/errno.h> /* return codes */
49 #include <linux/kernel.h>
50 #include <linux/init.h>
51 #include <linux/module.h> /* support for loadable modules */
52 #include <linux/slab.h> /* kmalloc(), kfree() */
54 #include <linux/string.h> /* inline mem*, str* functions */
56 #include <asm/byteorder.h> /* htons(), etc. */
57 #include <linux/wanrouter.h> /* WAN router API definitions */
59 #include <linux/vmalloc.h> /* vmalloc, vfree */
60 #include <asm/uaccess.h> /* copy_to/from_user */
61 #include <linux/init.h> /* __initfunc et al. */
62 #include <net/syncppp.h>
64 #define KMEM_SAFETYZONE 8
66 /***********FOR DEBUGGING PURPOSES*********************************************
67 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
69 void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
71 c1 += sizeof(unsigned int);
72 *((unsigned int *)v) = size;
74 for (i = 0; i < KMEM_SAFETYZONE; i++) {
75 c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
76 c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
80 for (i = 0; i < KMEM_SAFETYZONE; i++) {
81 c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
82 c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
85 v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
86 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
89 static void dbg_kfree(void * v, int line) {
90 unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
91 unsigned int size = *sp;
92 char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
94 for (i = 0; i < KMEM_SAFETYZONE; i++) {
95 if ( c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
96 || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
97 printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
98 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
99 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
104 for (i = 0; i < KMEM_SAFETYZONE; i++) {
105 if ( c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
106 || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
108 printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
109 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
110 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
114 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
115 v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
119 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
120 #define kfree(x) dbg_kfree(x,__LINE__)
121 *****************************************************************************/
124 * Function Prototypes
128 * WAN device IOCTL handlers
131 static int wanrouter_device_setup(struct wan_device *wandev,
132 wandev_conf_t __user *u_conf);
133 static int wanrouter_device_stat(struct wan_device *wandev,
134 wandev_stat_t __user *u_stat);
135 static int wanrouter_device_shutdown(struct wan_device *wandev);
136 static int wanrouter_device_new_if(struct wan_device *wandev,
137 wanif_conf_t __user *u_conf);
138 static int wanrouter_device_del_if(struct wan_device *wandev,
139 char __user *u_name);
145 static struct wan_device *wanrouter_find_device(char *name);
146 static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
147 void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
148 void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
156 static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
157 static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
158 static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
159 struct wan_device* wanrouter_router_devlist; /* list of registered devices */
162 * Organize Unique Identifiers for encapsulation/decapsulation
165 static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
167 static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
170 static int __init wanrouter_init(void)
174 printk(KERN_INFO "%s v%u.%u %s\n",
175 wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
176 wanrouter_copyright);
178 err = wanrouter_proc_init();
180 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
186 static void __exit wanrouter_cleanup (void)
188 wanrouter_proc_cleanup();
192 * This is just plain dumb. We should move the bugger to drivers/net/wan,
193 * slap it first in directory and make it module_init(). The only reason
194 * for subsys_initcall() here is that net goes after drivers (why, BTW?)
196 subsys_initcall(wanrouter_init);
197 module_exit(wanrouter_cleanup);
204 * Register WAN device.
205 * o verify device credentials
206 * o create an entry for the device in the /proc/net/router directory
207 * o initialize internally maintained fields of the wan_device structure
208 * o link device data space to a singly-linked list
209 * o if it's the first device, then start kernel 'thread'
210 * o increment module use count
220 int register_wan_device(struct wan_device *wandev)
224 if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
225 (wandev->name == NULL))
228 namelen = strlen(wandev->name);
229 if (!namelen || (namelen > WAN_DRVNAME_SZ))
232 if (wanrouter_find_device(wandev->name))
236 printk(KERN_INFO "%s: registering WAN device %s\n",
237 wanrouter_modname, wandev->name);
241 * Register /proc directory entry
243 err = wanrouter_proc_add(wandev);
246 "%s: can't create /proc/net/router/%s entry!\n",
247 wanrouter_modname, wandev->name);
252 * Initialize fields of the wan_device structure maintained by the
253 * router and update local data.
258 wandev->next = wanrouter_router_devlist;
259 wanrouter_router_devlist = wandev;
264 * Unregister WAN device.
266 * o unlink device data space from the linked list
267 * o delete device entry in the /proc/net/router directory
268 * o decrement module use count
276 int unregister_wan_device(char *name)
278 struct wan_device *wandev, *prev;
283 for (wandev = wanrouter_router_devlist, prev = NULL;
284 wandev && strcmp(wandev->name, name);
285 prev = wandev, wandev = wandev->next)
291 printk(KERN_INFO "%s: unregistering WAN device %s\n",
292 wanrouter_modname, name);
295 if (wandev->state != WAN_UNCONFIGURED)
296 wanrouter_device_shutdown(wandev);
299 prev->next = wandev->next;
301 wanrouter_router_devlist = wandev->next;
303 wanrouter_proc_delete(wandev);
308 * Encapsulate packet.
310 * Return: encapsulation header size
311 * < 0 - unsupported Ethertype
314 * 1. This function may be called on interrupt context.
318 int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
324 case ETH_P_IP: /* IP datagram encapsulation */
327 skb->data[0] = NLPID_IP;
330 case ETH_P_IPX: /* SNAP encapsulation */
335 skb->data[1] = NLPID_SNAP;
336 memcpy(&skb->data[2], wanrouter_oui_ether,
337 sizeof(wanrouter_oui_ether));
338 *((unsigned short*)&skb->data[5]) = htons(type);
341 default: /* Unknown packet type */
343 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
344 wanrouter_modname, type, dev->name);
352 * Decapsulate packet.
354 * Return: Ethertype (in network order)
355 * 0 unknown encapsulation
358 * 1. This function may be called on interrupt context.
362 __be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
364 int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
367 switch (skb->data[cnt]) {
368 case NLPID_IP: /* IP datagramm */
369 ethertype = htons(ETH_P_IP);
373 case NLPID_SNAP: /* SNAP encapsulation */
374 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
375 sizeof(wanrouter_oui_ether))){
377 "%s: unsupported SNAP OUI %02X-%02X-%02X "
378 "on interface %s!\n", wanrouter_modname,
379 skb->data[cnt+1], skb->data[cnt+2],
380 skb->data[cnt+3], dev->name);
383 ethertype = *((__be16*)&skb->data[cnt+4]);
387 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
391 "%s: unsupported NLPID 0x%02X on interface %s!\n",
392 wanrouter_modname, skb->data[cnt], dev->name);
395 skb->protocol = ethertype;
396 skb->pkt_type = PACKET_HOST; /* Physically point to point */
398 skb->mac.raw = skb->data;
405 * o find WAN device associated with this node
406 * o execute requested action or pass command to the device driver
409 int wanrouter_ioctl(struct inode *inode, struct file *file,
410 unsigned int cmd, unsigned long arg)
413 struct proc_dir_entry *dent;
414 struct wan_device *wandev;
415 void __user *data = (void __user *)arg;
417 if (!capable(CAP_NET_ADMIN))
420 if ((cmd >> 8) != ROUTER_IOCTL)
424 if ((dent == NULL) || (dent->data == NULL))
428 if (wandev->magic != ROUTER_MAGIC)
433 err = wanrouter_device_setup(wandev, data);
437 err = wanrouter_device_shutdown(wandev);
441 err = wanrouter_device_stat(wandev, data);
445 err = wanrouter_device_new_if(wandev, data);
449 err = wanrouter_device_del_if(wandev, data);
456 if ((cmd >= ROUTER_USER) &&
457 (cmd <= ROUTER_USER_MAX) &&
459 err = wandev->ioctl(wandev, cmd, arg);
466 * WAN Driver IOCTL Handlers
470 * Setup WAN link device.
471 * o verify user address space
472 * o allocate kernel memory and copy configuration data to kernel space
473 * o if configuration data includes extension, copy it to kernel space too
474 * o call driver's setup() entry point
477 static int wanrouter_device_setup(struct wan_device *wandev,
478 wandev_conf_t __user *u_conf)
484 if (wandev->setup == NULL) { /* Nothing to do ? */
485 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
490 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
492 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
497 if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
498 printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
504 if (conf->magic != ROUTER_MAGIC) {
506 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
511 if (conf->data_size && conf->data) {
512 if (conf->data_size > 128000 || conf->data_size < 0) {
514 "%s: ERROR, Invalid firmware data size %i !\n",
515 wandev->name, conf->data_size);
520 data = vmalloc(conf->data_size);
523 "%s: ERROR, Faild allocate kernel memory !\n",
528 if (!copy_from_user(data, conf->data, conf->data_size)) {
530 err = wandev->setup(wandev, conf);
533 "%s: ERROR, Faild to copy from user data !\n",
540 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
541 wandev->name, conf->data_size);
549 * Shutdown WAN device.
550 * o delete all not opened logical channels for this device
551 * o call driver's shutdown() entry point
554 static int wanrouter_device_shutdown(struct wan_device *wandev)
556 struct net_device *dev;
559 if (wandev->state == WAN_UNCONFIGURED)
562 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
564 for (dev = wandev->dev; dev;) {
565 err = wanrouter_delete_interface(wandev, dev->name);
568 /* The above function deallocates the current dev
569 * structure. Therefore, we cannot use dev->priv
570 * as the next element: wandev->dev points to the
576 return -EBUSY; /* there are opened interfaces */
578 if (wandev->shutdown)
579 err=wandev->shutdown(wandev);
585 * Get WAN device status & statistics.
588 static int wanrouter_device_stat(struct wan_device *wandev,
589 wandev_stat_t __user *u_stat)
593 memset(&stat, 0, sizeof(stat));
595 /* Ask device driver to update device statistics */
596 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
597 wandev->update(wandev);
599 /* Fill out structure */
600 stat.ndev = wandev->ndev;
601 stat.state = wandev->state;
603 if (copy_to_user(u_stat, &stat, sizeof(stat)))
610 * Create new WAN interface.
611 * o verify user address space
612 * o copy configuration data to kernel address space
613 * o allocate network interface data space
614 * o call driver's new_if() entry point
615 * o make sure there is no interface name conflict
616 * o register network interface
619 static int wanrouter_device_new_if(struct wan_device *wandev,
620 wanif_conf_t __user *u_conf)
623 struct net_device *dev = NULL;
624 #ifdef CONFIG_WANPIPE_MULTPPP
625 struct ppp_device *pppdev=NULL;
629 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
632 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
637 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
641 if (cnf->magic != ROUTER_MAGIC)
644 if (cnf->config_id == WANCONFIG_MPPP) {
645 #ifdef CONFIG_WANPIPE_MULTPPP
646 pppdev = kmalloc(sizeof(struct ppp_device), GFP_KERNEL);
650 memset(pppdev, 0, sizeof(struct ppp_device));
651 pppdev->dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
652 if (pppdev->dev == NULL) {
657 memset(pppdev->dev, 0, sizeof(struct net_device));
658 err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
661 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
663 err = -EPROTONOSUPPORT;
667 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
671 memset(dev, 0, sizeof(struct net_device));
672 err = wandev->new_if(wandev, dev, cnf);
676 /* Register network interface. This will invoke init()
677 * function supplied by the driver. If device registered
678 * successfully, add it to the interface list.
681 if (dev->name == NULL) {
686 printk(KERN_INFO "%s: registering interface %s...\n",
687 wanrouter_modname, dev->name);
690 err = register_netdev(dev);
692 struct net_device *slave = NULL;
693 unsigned long smp_flags=0;
695 lock_adapter_irq(&wandev->lock, &smp_flags);
697 if (wandev->dev == NULL) {
700 for (slave=wandev->dev;
701 *((struct net_device **)slave->priv);
702 slave = *((struct net_device **)slave->priv));
704 *((struct net_device **)slave->priv) = dev;
708 unlock_adapter_irq(&wandev->lock, &smp_flags);
709 err = 0; /* done !!! */
714 wandev->del_if(wandev, dev);
717 /* This code has moved from del_if() function */
721 #ifdef CONFIG_WANPIPE_MULTPPP
722 if (cnf->config_id == WANCONFIG_MPPP)
727 /* Sync PPP is disabled */
728 if (cnf->config_id != WANCONFIG_MPPP)
739 * Delete WAN logical channel.
740 * o verify user address space
741 * o copy configuration data to kernel address space
744 static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
746 char name[WAN_IFNAME_SZ + 1];
749 if (wandev->state == WAN_UNCONFIGURED)
752 memset(name, 0, sizeof(name));
754 if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
757 err = wanrouter_delete_interface(wandev, name);
761 /* If last interface being deleted, shutdown card
762 * This helps with administration at leaf nodes
763 * (You can tell if the person at the other end of the phone
764 * has an interface configured) and avoids DoS vulnerabilities
765 * in binary driver files - this fixes a problem with the current
766 * Sangoma driver going into strange states when all the network
767 * interfaces are deleted and the link irrecoverably disconnected.
770 if (!wandev->ndev && wandev->shutdown)
771 err = wandev->shutdown(wandev);
777 * Miscellaneous Functions
781 * Find WAN device by name.
782 * Return pointer to the WAN device data space or NULL if device not found.
785 static struct wan_device *wanrouter_find_device(char *name)
787 struct wan_device *wandev;
789 for (wandev = wanrouter_router_devlist;
790 wandev && strcmp(wandev->name, name);
791 wandev = wandev->next);
796 * Delete WAN logical channel identified by its name.
797 * o find logical channel by its name
798 * o call driver's del_if() entry point
799 * o unregister network interface
800 * o unlink channel data space from linked list of channels
801 * o release channel data space
804 * -ENODEV channel not found.
805 * -EBUSY interface is open
807 * Note: If (force != 0), then device will be destroyed even if interface
808 * associated with it is open. It's caller's responsibility to make
809 * sure that opened interfaces are not removed!
812 static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
814 struct net_device *dev = NULL, *prev = NULL;
815 unsigned long smp_flags=0;
817 lock_adapter_irq(&wandev->lock, &smp_flags);
820 while (dev && strcmp(name, dev->name)) {
821 struct net_device **slave = dev->priv;
825 unlock_adapter_irq(&wandev->lock, &smp_flags);
828 return -ENODEV; /* interface not found */
830 if (netif_running(dev))
831 return -EBUSY; /* interface in use */
834 wandev->del_if(wandev, dev);
836 lock_adapter_irq(&wandev->lock, &smp_flags);
838 struct net_device **prev_slave = prev->priv;
839 struct net_device **slave = dev->priv;
841 *prev_slave = *slave;
843 struct net_device **slave = dev->priv;
844 wandev->dev = *slave;
847 unlock_adapter_irq(&wandev->lock, &smp_flags);
849 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
851 /* Due to new interface linking method using dev->priv,
852 * this code has moved from del_if() function.*/
856 unregister_netdev(dev);
863 void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
865 spin_lock_irqsave(lock, *smp_flags);
869 void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
871 spin_unlock_irqrestore(lock, *smp_flags);
874 EXPORT_SYMBOL(register_wan_device);
875 EXPORT_SYMBOL(unregister_wan_device);
876 EXPORT_SYMBOL(wanrouter_encapsulate);
877 EXPORT_SYMBOL(wanrouter_type_trans);
878 EXPORT_SYMBOL(lock_adapter_irq);
879 EXPORT_SYMBOL(unlock_adapter_irq);
881 MODULE_LICENSE("GPL");