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/version.h>
46 #include <linux/config.h>
47 #include <linux/stddef.h> /* offsetof(), etc. */
48 #include <linux/errno.h> /* return codes */
49 #include <linux/kernel.h>
50 #include <linux/module.h> /* support for loadable modules */
51 #include <linux/slab.h> /* kmalloc(), kfree() */
52 #include <linux/mm.h> /* verify_area(), etc. */
53 #include <linux/string.h> /* inline mem*, str* functions */
55 #include <asm/byteorder.h> /* htons(), etc. */
56 #include <linux/wanrouter.h> /* WAN router API definitions */
58 #include <linux/vmalloc.h> /* vmalloc, vfree */
59 #include <asm/uaccess.h> /* copy_to/from_user */
60 #include <linux/init.h> /* __initfunc et al. */
61 #include <net/syncppp.h>
63 #define KMEM_SAFETYZONE 8
65 /***********FOR DEBUGGING PURPOSES*********************************************
66 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
68 void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
70 c1 += sizeof(unsigned int);
71 *((unsigned int *)v) = size;
73 for (i = 0; i < KMEM_SAFETYZONE; i++) {
74 c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
75 c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
79 for (i = 0; i < KMEM_SAFETYZONE; i++) {
80 c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
81 c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
84 v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
85 printk(KERN_INFO "line %d kmalloc(%d,%d) = %p\n",line,size,prio,v);
88 static void dbg_kfree(void * v, int line) {
89 unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
90 unsigned int size = *sp;
91 char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
93 for (i = 0; i < KMEM_SAFETYZONE; i++) {
94 if ( c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
95 || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
96 printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
97 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
98 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
103 for (i = 0; i < KMEM_SAFETYZONE; i++) {
104 if ( c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
105 || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
107 printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
108 printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
109 c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
113 printk(KERN_INFO "line %d kfree(%p)\n",line,v);
114 v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
118 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
119 #define kfree(x) dbg_kfree(x,__LINE__)
120 *****************************************************************************/
123 * Function Prototypes
127 * WAN device IOCTL handlers
130 static int wanrouter_device_setup(struct wan_device
*wandev
,
131 wandev_conf_t
*u_conf
);
132 static int wanrouter_device_stat(struct wan_device
*wandev
,
133 wandev_stat_t
*u_stat
);
134 static int wanrouter_device_shutdown(struct wan_device
*wandev
);
135 static int wanrouter_device_new_if(struct wan_device
*wandev
,
136 wanif_conf_t
*u_conf
);
137 static int wanrouter_device_del_if(struct wan_device
*wandev
, char *u_name
);
143 static struct wan_device
*wanrouter_find_device(char *name
);
144 static int wanrouter_delete_interface(struct wan_device
*wandev
, char *name
);
145 void lock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
);
146 void unlock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
);
154 static char wanrouter_fullname
[] = "Sangoma WANPIPE Router";
155 static char wanrouter_copyright
[] = "(c) 1995-2000 Sangoma Technologies Inc.";
156 static char wanrouter_modname
[] = ROUTER_NAME
; /* short module name */
157 struct wan_device
* wanrouter_router_devlist
; /* list of registered devices */
160 * Organize Unique Identifiers for encapsulation/decapsulation
163 static unsigned char wanrouter_oui_ether
[] = { 0x00, 0x00, 0x00 };
165 static unsigned char wanrouter_oui_802_2
[] = { 0x00, 0x80, 0xC2 };
170 int wanrouter_init(void)
173 extern int wanpipe_init(void);
174 extern int sdladrv_init(void);
176 printk(KERN_INFO
"%s v%u.%u %s\n",
177 wanrouter_fullname
, ROUTER_VERSION
, ROUTER_RELEASE
,
178 wanrouter_copyright
);
180 err
= wanrouter_proc_init();
182 printk(KERN_INFO
"%s: can't create entry in proc filesystem!\n",
186 * Initialise compiled in boards
189 #ifdef CONFIG_VENDOR_SANGOMA
197 static void __exit
wanrouter_cleanup (void)
199 wanrouter_proc_cleanup();
205 * Kernel Loadable Module Entry Points
209 * Module 'insert' entry point.
210 * o print announcement
211 * o initialize static data
212 * o create /proc/net/router directory and static entries
219 int init_module (void)
223 printk(KERN_INFO
"%s v%u.%u %s\n",
224 wanrouter_fullname
, ROUTER_VERSION
, ROUTER_RELEASE
,
225 wanrouter_copyright
);
227 err
= wanrouter_proc_init();
230 printk(KERN_INFO
"%s: can't create entry in proc filesystem!\n",
236 * Module 'remove' entry point.
237 * o delete /proc/net/router directory and static entries.
240 void cleanup_module (void)
242 wanrouter_proc_cleanup();
252 * Register WAN device.
253 * o verify device credentials
254 * o create an entry for the device in the /proc/net/router directory
255 * o initialize internally maintained fields of the wan_device structure
256 * o link device data space to a singly-linked list
257 * o if it's the first device, then start kernel 'thread'
258 * o increment module use count
268 int register_wan_device(struct wan_device
*wandev
)
272 if ((wandev
== NULL
) || (wandev
->magic
!= ROUTER_MAGIC
) ||
273 (wandev
->name
== NULL
))
276 namelen
= strlen(wandev
->name
);
277 if (!namelen
|| (namelen
> WAN_DRVNAME_SZ
))
280 if (wanrouter_find_device(wandev
->name
))
284 printk(KERN_INFO
"%s: registering WAN device %s\n",
285 wanrouter_modname
, wandev
->name
);
289 * Register /proc directory entry
291 err
= wanrouter_proc_add(wandev
);
294 "%s: can't create /proc/net/router/%s entry!\n",
295 wanrouter_modname
, wandev
->name
);
300 * Initialize fields of the wan_device structure maintained by the
301 * router and update local data.
306 wandev
->next
= wanrouter_router_devlist
;
307 wanrouter_router_devlist
= wandev
;
308 MOD_INC_USE_COUNT
; /* prevent module from unloading */
313 * Unregister WAN device.
315 * o unlink device data space from the linked list
316 * o delete device entry in the /proc/net/router directory
317 * o decrement module use count
325 int unregister_wan_device(char *name
)
327 struct wan_device
*wandev
, *prev
;
332 for (wandev
= wanrouter_router_devlist
, prev
= NULL
;
333 wandev
&& strcmp(wandev
->name
, name
);
334 prev
= wandev
, wandev
= wandev
->next
)
340 printk(KERN_INFO
"%s: unregistering WAN device %s\n",
341 wanrouter_modname
, name
);
344 if (wandev
->state
!= WAN_UNCONFIGURED
)
345 wanrouter_device_shutdown(wandev
);
348 prev
->next
= wandev
->next
;
350 wanrouter_router_devlist
= wandev
->next
;
352 wanrouter_proc_delete(wandev
);
358 * Encapsulate packet.
360 * Return: encapsulation header size
361 * < 0 - unsupported Ethertype
364 * 1. This function may be called on interrupt context.
368 int wanrouter_encapsulate(struct sk_buff
*skb
, struct net_device
*dev
,
374 case ETH_P_IP
: /* IP datagram encapsulation */
377 skb
->data
[0] = NLPID_IP
;
380 case ETH_P_IPX
: /* SNAP encapsulation */
385 skb
->data
[1] = NLPID_SNAP
;
386 memcpy(&skb
->data
[2], wanrouter_oui_ether
,
387 sizeof(wanrouter_oui_ether
));
388 *((unsigned short*)&skb
->data
[5]) = htons(type
);
391 default: /* Unknown packet type */
393 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
394 wanrouter_modname
, type
, dev
->name
);
402 * Decapsulate packet.
404 * Return: Ethertype (in network order)
405 * 0 unknown encapsulation
408 * 1. This function may be called on interrupt context.
412 unsigned short wanrouter_type_trans(struct sk_buff
*skb
, struct net_device
*dev
)
414 int cnt
= skb
->data
[0] ? 0 : 1; /* there may be a pad present */
415 unsigned short ethertype
;
417 switch (skb
->data
[cnt
]) {
418 case NLPID_IP
: /* IP datagramm */
419 ethertype
= htons(ETH_P_IP
);
423 case NLPID_SNAP
: /* SNAP encapsulation */
424 if (memcmp(&skb
->data
[cnt
+ 1], wanrouter_oui_ether
,
425 sizeof(wanrouter_oui_ether
))){
427 "%s: unsupported SNAP OUI %02X-%02X-%02X "
428 "on interface %s!\n", wanrouter_modname
,
429 skb
->data
[cnt
+1], skb
->data
[cnt
+2],
430 skb
->data
[cnt
+3], dev
->name
);
433 ethertype
= *((unsigned short*)&skb
->data
[cnt
+4]);
437 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
441 "%s: unsupported NLPID 0x%02X on interface %s!\n",
442 wanrouter_modname
, skb
->data
[cnt
], dev
->name
);
445 skb
->protocol
= ethertype
;
446 skb
->pkt_type
= PACKET_HOST
; /* Physically point to point */
448 skb
->mac
.raw
= skb
->data
;
455 * o find WAN device associated with this node
456 * o execute requested action or pass command to the device driver
459 int wanrouter_ioctl(struct inode
*inode
, struct file
*file
,
460 unsigned int cmd
, unsigned long arg
)
463 struct proc_dir_entry
*dent
;
464 struct wan_device
*wandev
;
466 if (!capable(CAP_NET_ADMIN
))
469 if ((cmd
>> 8) != ROUTER_IOCTL
)
473 if ((dent
== NULL
) || (dent
->data
== NULL
))
477 if (wandev
->magic
!= ROUTER_MAGIC
)
482 err
= wanrouter_device_setup(wandev
, (void*)arg
);
486 err
= wanrouter_device_shutdown(wandev
);
490 err
= wanrouter_device_stat(wandev
, (void*)arg
);
494 err
= wanrouter_device_new_if(wandev
, (void*)arg
);
498 err
= wanrouter_device_del_if(wandev
, (void*)arg
);
505 if ((cmd
>= ROUTER_USER
) &&
506 (cmd
<= ROUTER_USER_MAX
) &&
508 err
= wandev
->ioctl(wandev
, cmd
, arg
);
515 * WAN Driver IOCTL Handlers
519 * Setup WAN link device.
520 * o verify user address space
521 * o allocate kernel memory and copy configuration data to kernel space
522 * o if configuration data includes extension, copy it to kernel space too
523 * o call driver's setup() entry point
526 static int wanrouter_device_setup(struct wan_device
*wandev
,
527 wandev_conf_t
*u_conf
)
533 if (wandev
->setup
== NULL
) { /* Nothing to do ? */
534 printk(KERN_INFO
"%s: ERROR, No setup script: wandev->setup()\n",
539 conf
= kmalloc(sizeof(wandev_conf_t
), GFP_KERNEL
);
541 printk(KERN_INFO
"%s: ERROR, Failed to allocate kernel memory !\n",
546 if (copy_from_user(conf
, u_conf
, sizeof(wandev_conf_t
))) {
547 printk(KERN_INFO
"%s: Failed to copy user config data to kernel space!\n",
553 if (conf
->magic
!= ROUTER_MAGIC
) {
555 printk(KERN_INFO
"%s: ERROR, Invalid MAGIC Number\n",
560 if (conf
->data_size
&& conf
->data
) {
561 if (conf
->data_size
> 128000 || conf
->data_size
< 0) {
563 "%s: ERROR, Invalid firmware data size %i !\n",
564 wandev
->name
, conf
->data_size
);
569 data
= vmalloc(conf
->data_size
);
572 "%s: ERROR, Faild allocate kernel memory !\n",
577 if (!copy_from_user(data
, conf
->data
, conf
->data_size
)) {
579 err
= wandev
->setup(wandev
, conf
);
582 "%s: ERROR, Faild to copy from user data !\n",
589 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
590 wandev
->name
, conf
->data_size
);
598 * Shutdown WAN device.
599 * o delete all not opened logical channels for this device
600 * o call driver's shutdown() entry point
603 static int wanrouter_device_shutdown(struct wan_device
*wandev
)
605 struct net_device
*dev
;
608 if (wandev
->state
== WAN_UNCONFIGURED
)
611 printk(KERN_INFO
"\n%s: Shutting Down!\n",wandev
->name
);
613 for (dev
= wandev
->dev
; dev
;) {
614 err
= wanrouter_delete_interface(wandev
, dev
->name
);
617 /* The above function deallocates the current dev
618 * structure. Therefore, we cannot use dev->priv
619 * as the next element: wandev->dev points to the
625 return -EBUSY
; /* there are opened interfaces */
627 if (wandev
->shutdown
)
628 err
=wandev
->shutdown(wandev
);
634 * Get WAN device status & statistics.
637 static int wanrouter_device_stat(struct wan_device
*wandev
,
638 wandev_stat_t
*u_stat
)
642 memset(&stat
, 0, sizeof(stat
));
644 /* Ask device driver to update device statistics */
645 if ((wandev
->state
!= WAN_UNCONFIGURED
) && wandev
->update
)
646 wandev
->update(wandev
);
648 /* Fill out structure */
649 stat
.ndev
= wandev
->ndev
;
650 stat
.state
= wandev
->state
;
652 if (copy_to_user(u_stat
, &stat
, sizeof(stat
)))
659 * Create new WAN interface.
660 * o verify user address space
661 * o copy configuration data to kernel address space
662 * o allocate network interface data space
663 * o call driver's new_if() entry point
664 * o make sure there is no interface name conflict
665 * o register network interface
668 static int wanrouter_device_new_if(struct wan_device
*wandev
,
669 wanif_conf_t
*u_conf
)
672 struct net_device
*dev
= NULL
;
673 #ifdef CONFIG_WANPIPE_MULTPPP
674 struct ppp_device
*pppdev
=NULL
;
678 if ((wandev
->state
== WAN_UNCONFIGURED
) || (wandev
->new_if
== NULL
))
681 if (copy_from_user(&conf
, u_conf
, sizeof(wanif_conf_t
)))
684 if (conf
.magic
!= ROUTER_MAGIC
)
687 if (conf
.config_id
== WANCONFIG_MPPP
) {
688 #ifdef CONFIG_WANPIPE_MULTPPP
689 pppdev
= kmalloc(sizeof(struct ppp_device
), GFP_KERNEL
);
692 memset(pppdev
, 0, sizeof(struct ppp_device
));
693 pppdev
->dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
694 if (pppdev
->dev
== NULL
) {
698 memset(pppdev
->dev
, 0, sizeof(struct net_device
));
699 err
= wandev
->new_if(wandev
,
700 (struct net_device
*)pppdev
, &conf
);
703 printk(KERN_INFO
"%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
705 return -EPROTONOSUPPORT
;
708 dev
= kmalloc(sizeof(struct net_device
), GFP_KERNEL
);
711 memset(dev
, 0, sizeof(struct net_device
));
712 err
= wandev
->new_if(wandev
, dev
, &conf
);
716 /* Register network interface. This will invoke init()
717 * function supplied by the driver. If device registered
718 * successfully, add it to the interface list.
721 if (dev
->name
== NULL
) {
723 } else if (dev_get(dev
->name
)) {
724 err
= -EEXIST
; /* name already exists */
728 printk(KERN_INFO
"%s: registering interface %s...\n",
729 wanrouter_modname
, dev
->name
);
732 err
= register_netdev(dev
);
734 struct net_device
*slave
= NULL
;
735 unsigned long smp_flags
=0;
737 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
739 if (wandev
->dev
== NULL
) {
742 for (slave
=wandev
->dev
;
743 *((struct net_device
**)slave
->priv
);
744 slave
= *((struct net_device
**)slave
->priv
));
746 *((struct net_device
**)slave
->priv
) = dev
;
750 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
751 return 0; /* done !!! */
755 wandev
->del_if(wandev
, dev
);
758 /* This code has moved from del_if() function */
765 #ifdef CONFIG_WANPIPE_MULTPPP
766 if (conf
.config_id
== WANCONFIG_MPPP
)
771 /* Sync PPP is disabled */
772 if (conf
.config_id
!= WANCONFIG_MPPP
)
781 * Delete WAN logical channel.
782 * o verify user address space
783 * o copy configuration data to kernel address space
786 static int wanrouter_device_del_if(struct wan_device
*wandev
, char *u_name
)
788 char name
[WAN_IFNAME_SZ
+ 1];
791 if (wandev
->state
== WAN_UNCONFIGURED
)
794 memset(name
, 0, sizeof(name
));
796 if (copy_from_user(name
, u_name
, WAN_IFNAME_SZ
))
799 err
= wanrouter_delete_interface(wandev
, name
);
803 /* If last interface being deleted, shutdown card
804 * This helps with administration at leaf nodes
805 * (You can tell if the person at the other end of the phone
806 * has an interface configured) and avoids DoS vulnerabilities
807 * in binary driver files - this fixes a problem with the current
808 * Sangoma driver going into strange states when all the network
809 * interfaces are deleted and the link irrecoverably disconnected.
812 if (!wandev
->ndev
&& wandev
->shutdown
)
813 err
= wandev
->shutdown(wandev
);
819 * Miscellaneous Functions
823 * Find WAN device by name.
824 * Return pointer to the WAN device data space or NULL if device not found.
827 static struct wan_device
*wanrouter_find_device(char *name
)
829 struct wan_device
*wandev
;
831 for (wandev
= wanrouter_router_devlist
;
832 wandev
&& strcmp(wandev
->name
, name
);
833 wandev
= wandev
->next
);
838 * Delete WAN logical channel identified by its name.
839 * o find logical channel by its name
840 * o call driver's del_if() entry point
841 * o unregister network interface
842 * o unlink channel data space from linked list of channels
843 * o release channel data space
846 * -ENODEV channel not found.
847 * -EBUSY interface is open
849 * Note: If (force != 0), then device will be destroyed even if interface
850 * associated with it is open. It's caller's responsibility to make
851 * sure that opened interfaces are not removed!
854 static int wanrouter_delete_interface(struct wan_device
*wandev
, char *name
)
856 struct net_device
*dev
= NULL
, *prev
= NULL
;
857 unsigned long smp_flags
=0;
859 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
862 while (dev
&& strcmp(name
, dev
->name
)) {
863 struct net_device
**slave
= dev
->priv
;
867 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
870 return -ENODEV
; /* interface not found */
872 if (netif_running(dev
))
873 return -EBUSY
; /* interface in use */
876 wandev
->del_if(wandev
, dev
);
878 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
880 struct net_device
**prev_slave
= prev
->priv
;
881 struct net_device
**slave
= dev
->priv
;
883 *prev_slave
= *slave
;
885 struct net_device
**slave
= dev
->priv
;
886 wandev
->dev
= *slave
;
889 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
891 printk(KERN_INFO
"%s: unregistering '%s'\n", wandev
->name
, dev
->name
);
893 /* Due to new interface linking method using dev->priv,
894 * this code has moved from del_if() function.*/
900 unregister_netdev(dev
);
907 void lock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
909 spin_lock_irqsave(lock
, *smp_flags
);
913 void unlock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
915 spin_unlock_irqrestore(lock
, *smp_flags
);
918 EXPORT_SYMBOL(register_wan_device
);
919 EXPORT_SYMBOL(unregister_wan_device
);
920 EXPORT_SYMBOL(wanrouter_encapsulate
);
921 EXPORT_SYMBOL(wanrouter_type_trans
);
922 EXPORT_SYMBOL(lock_adapter_irq
);
923 EXPORT_SYMBOL(unlock_adapter_irq
);
925 MODULE_LICENSE("GPL");