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 management (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/stddef.h> /* offsetof(), etc. */
46 #include <linux/capability.h>
47 #include <linux/errno.h> /* return codes */
48 #include <linux/kernel.h>
49 #include <linux/module.h> /* support for loadable modules */
50 #include <linux/slab.h> /* kmalloc(), kfree() */
51 #include <linux/mutex.h>
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. */
62 #define KMEM_SAFETYZONE 8
64 #define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev)))
71 * WAN device IOCTL handlers
74 static DEFINE_MUTEX(wanrouter_mutex
);
75 static int wanrouter_device_setup(struct wan_device
*wandev
,
76 wandev_conf_t __user
*u_conf
);
77 static int wanrouter_device_stat(struct wan_device
*wandev
,
78 wandev_stat_t __user
*u_stat
);
79 static int wanrouter_device_shutdown(struct wan_device
*wandev
);
80 static int wanrouter_device_new_if(struct wan_device
*wandev
,
81 wanif_conf_t __user
*u_conf
);
82 static int wanrouter_device_del_if(struct wan_device
*wandev
,
89 static struct wan_device
*wanrouter_find_device(char *name
);
90 static int wanrouter_delete_interface(struct wan_device
*wandev
, char *name
);
91 static void lock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
93 static void unlock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
102 static char wanrouter_fullname
[] = "Sangoma WANPIPE Router";
103 static char wanrouter_copyright
[] = "(c) 1995-2000 Sangoma Technologies Inc.";
104 static char wanrouter_modname
[] = ROUTER_NAME
; /* short module name */
105 struct wan_device
* wanrouter_router_devlist
; /* list of registered devices */
108 * Organize Unique Identifiers for encapsulation/decapsulation
112 static unsigned char wanrouter_oui_ether
[] = { 0x00, 0x00, 0x00 };
113 static unsigned char wanrouter_oui_802_2
[] = { 0x00, 0x80, 0xC2 };
116 static int __init
wanrouter_init(void)
120 printk(KERN_INFO
"%s v%u.%u %s\n",
121 wanrouter_fullname
, ROUTER_VERSION
, ROUTER_RELEASE
,
122 wanrouter_copyright
);
124 err
= wanrouter_proc_init();
126 printk(KERN_INFO
"%s: can't create entry in proc filesystem!\n",
132 static void __exit
wanrouter_cleanup (void)
134 wanrouter_proc_cleanup();
138 * This is just plain dumb. We should move the bugger to drivers/net/wan,
139 * slap it first in directory and make it module_init(). The only reason
140 * for subsys_initcall() here is that net goes after drivers (why, BTW?)
142 subsys_initcall(wanrouter_init
);
143 module_exit(wanrouter_cleanup
);
150 * Register WAN device.
151 * o verify device credentials
152 * o create an entry for the device in the /proc/net/router directory
153 * o initialize internally maintained fields of the wan_device structure
154 * o link device data space to a singly-linked list
155 * o if it's the first device, then start kernel 'thread'
156 * o increment module use count
166 int register_wan_device(struct wan_device
*wandev
)
170 if ((wandev
== NULL
) || (wandev
->magic
!= ROUTER_MAGIC
) ||
171 (wandev
->name
== NULL
))
174 namelen
= strlen(wandev
->name
);
175 if (!namelen
|| (namelen
> WAN_DRVNAME_SZ
))
178 if (wanrouter_find_device(wandev
->name
))
182 printk(KERN_INFO
"%s: registering WAN device %s\n",
183 wanrouter_modname
, wandev
->name
);
187 * Register /proc directory entry
189 err
= wanrouter_proc_add(wandev
);
192 "%s: can't create /proc/net/router/%s entry!\n",
193 wanrouter_modname
, wandev
->name
);
198 * Initialize fields of the wan_device structure maintained by the
199 * router and update local data.
204 wandev
->next
= wanrouter_router_devlist
;
205 wanrouter_router_devlist
= wandev
;
210 * Unregister WAN device.
212 * o unlink device data space from the linked list
213 * o delete device entry in the /proc/net/router directory
214 * o decrement module use count
222 int unregister_wan_device(char *name
)
224 struct wan_device
*wandev
, *prev
;
229 for (wandev
= wanrouter_router_devlist
, prev
= NULL
;
230 wandev
&& strcmp(wandev
->name
, name
);
231 prev
= wandev
, wandev
= wandev
->next
)
237 printk(KERN_INFO
"%s: unregistering WAN device %s\n",
238 wanrouter_modname
, name
);
241 if (wandev
->state
!= WAN_UNCONFIGURED
)
242 wanrouter_device_shutdown(wandev
);
245 prev
->next
= wandev
->next
;
247 wanrouter_router_devlist
= wandev
->next
;
249 wanrouter_proc_delete(wandev
);
256 * Encapsulate packet.
258 * Return: encapsulation header size
259 * < 0 - unsupported Ethertype
262 * 1. This function may be called on interrupt context.
266 int wanrouter_encapsulate(struct sk_buff
*skb
, struct net_device
*dev
,
272 case ETH_P_IP
: /* IP datagram encapsulation */
275 skb
->data
[0] = NLPID_IP
;
278 case ETH_P_IPX
: /* SNAP encapsulation */
283 skb
->data
[1] = NLPID_SNAP
;
284 skb_copy_to_linear_data_offset(skb
, 2, wanrouter_oui_ether
,
285 sizeof(wanrouter_oui_ether
));
286 *((unsigned short*)&skb
->data
[5]) = htons(type
);
289 default: /* Unknown packet type */
291 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
292 wanrouter_modname
, type
, dev
->name
);
300 * Decapsulate packet.
302 * Return: Ethertype (in network order)
303 * 0 unknown encapsulation
306 * 1. This function may be called on interrupt context.
310 __be16
wanrouter_type_trans(struct sk_buff
*skb
, struct net_device
*dev
)
312 int cnt
= skb
->data
[0] ? 0 : 1; /* there may be a pad present */
315 switch (skb
->data
[cnt
]) {
316 case NLPID_IP
: /* IP datagramm */
317 ethertype
= htons(ETH_P_IP
);
321 case NLPID_SNAP
: /* SNAP encapsulation */
322 if (memcmp(&skb
->data
[cnt
+ 1], wanrouter_oui_ether
,
323 sizeof(wanrouter_oui_ether
))){
325 "%s: unsupported SNAP OUI %02X-%02X-%02X "
326 "on interface %s!\n", wanrouter_modname
,
327 skb
->data
[cnt
+1], skb
->data
[cnt
+2],
328 skb
->data
[cnt
+3], dev
->name
);
331 ethertype
= *((__be16
*)&skb
->data
[cnt
+4]);
335 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
339 "%s: unsupported NLPID 0x%02X on interface %s!\n",
340 wanrouter_modname
, skb
->data
[cnt
], dev
->name
);
343 skb
->protocol
= ethertype
;
344 skb
->pkt_type
= PACKET_HOST
; /* Physically point to point */
346 skb_reset_mac_header(skb
);
354 * o find WAN device associated with this node
355 * o execute requested action or pass command to the device driver
358 long wanrouter_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
360 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
362 struct proc_dir_entry
*dent
;
363 struct wan_device
*wandev
;
364 void __user
*data
= (void __user
*)arg
;
366 if (!capable(CAP_NET_ADMIN
))
369 if ((cmd
>> 8) != ROUTER_IOCTL
)
373 if ((dent
== NULL
) || (dent
->data
== NULL
))
377 if (wandev
->magic
!= ROUTER_MAGIC
)
380 mutex_lock(&wanrouter_mutex
);
383 err
= wanrouter_device_setup(wandev
, data
);
387 err
= wanrouter_device_shutdown(wandev
);
391 err
= wanrouter_device_stat(wandev
, data
);
395 err
= wanrouter_device_new_if(wandev
, data
);
399 err
= wanrouter_device_del_if(wandev
, data
);
406 if ((cmd
>= ROUTER_USER
) &&
407 (cmd
<= ROUTER_USER_MAX
) &&
409 err
= wandev
->ioctl(wandev
, cmd
, arg
);
412 mutex_unlock(&wanrouter_mutex
);
417 * WAN Driver IOCTL Handlers
421 * Setup WAN link device.
422 * o verify user address space
423 * o allocate kernel memory and copy configuration data to kernel space
424 * o if configuration data includes extension, copy it to kernel space too
425 * o call driver's setup() entry point
428 static int wanrouter_device_setup(struct wan_device
*wandev
,
429 wandev_conf_t __user
*u_conf
)
435 if (wandev
->setup
== NULL
) { /* Nothing to do ? */
436 printk(KERN_INFO
"%s: ERROR, No setup script: wandev->setup()\n",
441 conf
= kmalloc(sizeof(wandev_conf_t
), GFP_KERNEL
);
443 printk(KERN_INFO
"%s: ERROR, Failed to allocate kernel memory !\n",
448 if (copy_from_user(conf
, u_conf
, sizeof(wandev_conf_t
))) {
449 printk(KERN_INFO
"%s: Failed to copy user config data to kernel space!\n",
455 if (conf
->magic
!= ROUTER_MAGIC
) {
457 printk(KERN_INFO
"%s: ERROR, Invalid MAGIC Number\n",
462 if (conf
->data_size
&& conf
->data
) {
463 if (conf
->data_size
> 128000) {
465 "%s: ERROR, Invalid firmware data size %i !\n",
466 wandev
->name
, conf
->data_size
);
471 data
= vmalloc(conf
->data_size
);
474 "%s: ERROR, Failed allocate kernel memory !\n",
479 if (!copy_from_user(data
, conf
->data
, conf
->data_size
)) {
481 err
= wandev
->setup(wandev
, conf
);
484 "%s: ERROR, Failed to copy from user data !\n",
491 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
492 wandev
->name
, conf
->data_size
);
500 * Shutdown WAN device.
501 * o delete all not opened logical channels for this device
502 * o call driver's shutdown() entry point
505 static int wanrouter_device_shutdown(struct wan_device
*wandev
)
507 struct net_device
*dev
;
510 if (wandev
->state
== WAN_UNCONFIGURED
)
513 printk(KERN_INFO
"\n%s: Shutting Down!\n",wandev
->name
);
515 for (dev
= wandev
->dev
; dev
;) {
516 err
= wanrouter_delete_interface(wandev
, dev
->name
);
519 /* The above function deallocates the current dev
520 * structure. Therefore, we cannot use netdev_priv(dev)
521 * as the next element: wandev->dev points to the
527 return -EBUSY
; /* there are opened interfaces */
529 if (wandev
->shutdown
)
530 err
=wandev
->shutdown(wandev
);
536 * Get WAN device status & statistics.
539 static int wanrouter_device_stat(struct wan_device
*wandev
,
540 wandev_stat_t __user
*u_stat
)
544 memset(&stat
, 0, sizeof(stat
));
546 /* Ask device driver to update device statistics */
547 if ((wandev
->state
!= WAN_UNCONFIGURED
) && wandev
->update
)
548 wandev
->update(wandev
);
550 /* Fill out structure */
551 stat
.ndev
= wandev
->ndev
;
552 stat
.state
= wandev
->state
;
554 if (copy_to_user(u_stat
, &stat
, sizeof(stat
)))
561 * Create new WAN interface.
562 * o verify user address space
563 * o copy configuration data to kernel address space
564 * o allocate network interface data space
565 * o call driver's new_if() entry point
566 * o make sure there is no interface name conflict
567 * o register network interface
570 static int wanrouter_device_new_if(struct wan_device
*wandev
,
571 wanif_conf_t __user
*u_conf
)
574 struct net_device
*dev
= NULL
;
577 if ((wandev
->state
== WAN_UNCONFIGURED
) || (wandev
->new_if
== NULL
))
580 cnf
= kmalloc(sizeof(wanif_conf_t
), GFP_KERNEL
);
585 if (copy_from_user(cnf
, u_conf
, sizeof(wanif_conf_t
)))
589 if (cnf
->magic
!= ROUTER_MAGIC
)
592 if (cnf
->config_id
== WANCONFIG_MPPP
) {
593 printk(KERN_INFO
"%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
595 err
= -EPROTONOSUPPORT
;
598 err
= wandev
->new_if(wandev
, dev
, cnf
);
602 /* Register network interface. This will invoke init()
603 * function supplied by the driver. If device registered
604 * successfully, add it to the interface list.
607 if (dev
->name
== NULL
) {
612 printk(KERN_INFO
"%s: registering interface %s...\n",
613 wanrouter_modname
, dev
->name
);
616 err
= register_netdev(dev
);
618 struct net_device
*slave
= NULL
;
619 unsigned long smp_flags
=0;
621 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
623 if (wandev
->dev
== NULL
) {
626 for (slave
=wandev
->dev
;
628 slave
= DEV_TO_SLAVE(slave
))
629 DEV_TO_SLAVE(slave
) = dev
;
633 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
634 err
= 0; /* done !!! */
639 wandev
->del_if(wandev
, dev
);
650 * Delete WAN logical channel.
651 * o verify user address space
652 * o copy configuration data to kernel address space
655 static int wanrouter_device_del_if(struct wan_device
*wandev
, char __user
*u_name
)
657 char name
[WAN_IFNAME_SZ
+ 1];
660 if (wandev
->state
== WAN_UNCONFIGURED
)
663 memset(name
, 0, sizeof(name
));
665 if (copy_from_user(name
, u_name
, WAN_IFNAME_SZ
))
668 err
= wanrouter_delete_interface(wandev
, name
);
672 /* If last interface being deleted, shutdown card
673 * This helps with administration at leaf nodes
674 * (You can tell if the person at the other end of the phone
675 * has an interface configured) and avoids DoS vulnerabilities
676 * in binary driver files - this fixes a problem with the current
677 * Sangoma driver going into strange states when all the network
678 * interfaces are deleted and the link irrecoverably disconnected.
681 if (!wandev
->ndev
&& wandev
->shutdown
)
682 err
= wandev
->shutdown(wandev
);
688 * Miscellaneous Functions
692 * Find WAN device by name.
693 * Return pointer to the WAN device data space or NULL if device not found.
696 static struct wan_device
*wanrouter_find_device(char *name
)
698 struct wan_device
*wandev
;
700 for (wandev
= wanrouter_router_devlist
;
701 wandev
&& strcmp(wandev
->name
, name
);
702 wandev
= wandev
->next
);
707 * Delete WAN logical channel identified by its name.
708 * o find logical channel by its name
709 * o call driver's del_if() entry point
710 * o unregister network interface
711 * o unlink channel data space from linked list of channels
712 * o release channel data space
715 * -ENODEV channel not found.
716 * -EBUSY interface is open
718 * Note: If (force != 0), then device will be destroyed even if interface
719 * associated with it is open. It's caller's responsibility to make
720 * sure that opened interfaces are not removed!
723 static int wanrouter_delete_interface(struct wan_device
*wandev
, char *name
)
725 struct net_device
*dev
= NULL
, *prev
= NULL
;
726 unsigned long smp_flags
=0;
728 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
731 while (dev
&& strcmp(name
, dev
->name
)) {
732 struct net_device
**slave
= netdev_priv(dev
);
736 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
739 return -ENODEV
; /* interface not found */
741 if (netif_running(dev
))
742 return -EBUSY
; /* interface in use */
745 wandev
->del_if(wandev
, dev
);
747 lock_adapter_irq(&wandev
->lock
, &smp_flags
);
749 struct net_device
**prev_slave
= netdev_priv(prev
);
750 struct net_device
**slave
= netdev_priv(dev
);
752 *prev_slave
= *slave
;
754 struct net_device
**slave
= netdev_priv(dev
);
755 wandev
->dev
= *slave
;
758 unlock_adapter_irq(&wandev
->lock
, &smp_flags
);
760 printk(KERN_INFO
"%s: unregistering '%s'\n", wandev
->name
, dev
->name
);
762 unregister_netdev(dev
);
769 static void lock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
772 spin_lock_irqsave(lock
, *smp_flags
);
776 static void unlock_adapter_irq(spinlock_t
*lock
, unsigned long *smp_flags
)
779 spin_unlock_irqrestore(lock
, *smp_flags
);
782 EXPORT_SYMBOL(register_wan_device
);
783 EXPORT_SYMBOL(unregister_wan_device
);
785 MODULE_LICENSE("GPL");