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/init.h>
50 #include <linux/module.h> /* support for loadable modules */
51 #include <linux/slab.h> /* kmalloc(), kfree() */
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
70 * WAN device IOCTL handlers
73 static int wanrouter_device_setup(struct wan_device *wandev,
74 wandev_conf_t __user *u_conf);
75 static int wanrouter_device_stat(struct wan_device *wandev,
76 wandev_stat_t __user *u_stat);
77 static int wanrouter_device_shutdown(struct wan_device *wandev);
78 static int wanrouter_device_new_if(struct wan_device *wandev,
79 wanif_conf_t __user *u_conf);
80 static int wanrouter_device_del_if(struct wan_device *wandev,
87 static struct wan_device *wanrouter_find_device(char *name);
88 static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
89 static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
90 static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
98 static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
99 static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
100 static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
101 struct wan_device* wanrouter_router_devlist; /* list of registered devices */
104 * Organize Unique Identifiers for encapsulation/decapsulation
108 static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
109 static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
112 static int __init wanrouter_init(void)
116 printk(KERN_INFO "%s v%u.%u %s\n",
117 wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
118 wanrouter_copyright);
120 err = wanrouter_proc_init();
122 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
128 static void __exit wanrouter_cleanup (void)
130 wanrouter_proc_cleanup();
134 * This is just plain dumb. We should move the bugger to drivers/net/wan,
135 * slap it first in directory and make it module_init(). The only reason
136 * for subsys_initcall() here is that net goes after drivers (why, BTW?)
138 subsys_initcall(wanrouter_init);
139 module_exit(wanrouter_cleanup);
146 * Register WAN device.
147 * o verify device credentials
148 * o create an entry for the device in the /proc/net/router directory
149 * o initialize internally maintained fields of the wan_device structure
150 * o link device data space to a singly-linked list
151 * o if it's the first device, then start kernel 'thread'
152 * o increment module use count
162 int register_wan_device(struct wan_device *wandev)
166 if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
167 (wandev->name == NULL))
170 namelen = strlen(wandev->name);
171 if (!namelen || (namelen > WAN_DRVNAME_SZ))
174 if (wanrouter_find_device(wandev->name))
178 printk(KERN_INFO "%s: registering WAN device %s\n",
179 wanrouter_modname, wandev->name);
183 * Register /proc directory entry
185 err = wanrouter_proc_add(wandev);
188 "%s: can't create /proc/net/router/%s entry!\n",
189 wanrouter_modname, wandev->name);
194 * Initialize fields of the wan_device structure maintained by the
195 * router and update local data.
200 wandev->next = wanrouter_router_devlist;
201 wanrouter_router_devlist = wandev;
206 * Unregister WAN device.
208 * o unlink device data space from the linked list
209 * o delete device entry in the /proc/net/router directory
210 * o decrement module use count
218 int unregister_wan_device(char *name)
220 struct wan_device *wandev, *prev;
225 for (wandev = wanrouter_router_devlist, prev = NULL;
226 wandev && strcmp(wandev->name, name);
227 prev = wandev, wandev = wandev->next)
233 printk(KERN_INFO "%s: unregistering WAN device %s\n",
234 wanrouter_modname, name);
237 if (wandev->state != WAN_UNCONFIGURED)
238 wanrouter_device_shutdown(wandev);
241 prev->next = wandev->next;
243 wanrouter_router_devlist = wandev->next;
245 wanrouter_proc_delete(wandev);
252 * Encapsulate packet.
254 * Return: encapsulation header size
255 * < 0 - unsupported Ethertype
258 * 1. This function may be called on interrupt context.
262 int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
268 case ETH_P_IP: /* IP datagram encapsulation */
271 skb->data[0] = NLPID_IP;
274 case ETH_P_IPX: /* SNAP encapsulation */
279 skb->data[1] = NLPID_SNAP;
280 skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
281 sizeof(wanrouter_oui_ether));
282 *((unsigned short*)&skb->data[5]) = htons(type);
285 default: /* Unknown packet type */
287 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
288 wanrouter_modname, type, dev->name);
296 * Decapsulate packet.
298 * Return: Ethertype (in network order)
299 * 0 unknown encapsulation
302 * 1. This function may be called on interrupt context.
306 __be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
308 int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
311 switch (skb->data[cnt]) {
312 case NLPID_IP: /* IP datagramm */
313 ethertype = htons(ETH_P_IP);
317 case NLPID_SNAP: /* SNAP encapsulation */
318 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
319 sizeof(wanrouter_oui_ether))){
321 "%s: unsupported SNAP OUI %02X-%02X-%02X "
322 "on interface %s!\n", wanrouter_modname,
323 skb->data[cnt+1], skb->data[cnt+2],
324 skb->data[cnt+3], dev->name);
327 ethertype = *((__be16*)&skb->data[cnt+4]);
331 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
335 "%s: unsupported NLPID 0x%02X on interface %s!\n",
336 wanrouter_modname, skb->data[cnt], dev->name);
339 skb->protocol = ethertype;
340 skb->pkt_type = PACKET_HOST; /* Physically point to point */
342 skb_reset_mac_header(skb);
350 * o find WAN device associated with this node
351 * o execute requested action or pass command to the device driver
354 int wanrouter_ioctl(struct inode *inode, struct file *file,
355 unsigned int cmd, unsigned long arg)
358 struct proc_dir_entry *dent;
359 struct wan_device *wandev;
360 void __user *data = (void __user *)arg;
362 if (!capable(CAP_NET_ADMIN))
365 if ((cmd >> 8) != ROUTER_IOCTL)
369 if ((dent == NULL) || (dent->data == NULL))
373 if (wandev->magic != ROUTER_MAGIC)
378 err = wanrouter_device_setup(wandev, data);
382 err = wanrouter_device_shutdown(wandev);
386 err = wanrouter_device_stat(wandev, data);
390 err = wanrouter_device_new_if(wandev, data);
394 err = wanrouter_device_del_if(wandev, data);
401 if ((cmd >= ROUTER_USER) &&
402 (cmd <= ROUTER_USER_MAX) &&
404 err = wandev->ioctl(wandev, cmd, arg);
411 * WAN Driver IOCTL Handlers
415 * Setup WAN link device.
416 * o verify user address space
417 * o allocate kernel memory and copy configuration data to kernel space
418 * o if configuration data includes extension, copy it to kernel space too
419 * o call driver's setup() entry point
422 static int wanrouter_device_setup(struct wan_device *wandev,
423 wandev_conf_t __user *u_conf)
429 if (wandev->setup == NULL) { /* Nothing to do ? */
430 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
435 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
437 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
442 if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
443 printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
449 if (conf->magic != ROUTER_MAGIC) {
451 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
456 if (conf->data_size && conf->data) {
457 if (conf->data_size > 128000) {
459 "%s: ERROR, Invalid firmware data size %i !\n",
460 wandev->name, conf->data_size);
465 data = vmalloc(conf->data_size);
468 "%s: ERROR, Faild allocate kernel memory !\n",
473 if (!copy_from_user(data, conf->data, conf->data_size)) {
475 err = wandev->setup(wandev, conf);
478 "%s: ERROR, Faild to copy from user data !\n",
485 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
486 wandev->name, conf->data_size);
494 * Shutdown WAN device.
495 * o delete all not opened logical channels for this device
496 * o call driver's shutdown() entry point
499 static int wanrouter_device_shutdown(struct wan_device *wandev)
501 struct net_device *dev;
504 if (wandev->state == WAN_UNCONFIGURED)
507 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
509 for (dev = wandev->dev; dev;) {
510 err = wanrouter_delete_interface(wandev, dev->name);
513 /* The above function deallocates the current dev
514 * structure. Therefore, we cannot use dev->priv
515 * as the next element: wandev->dev points to the
521 return -EBUSY; /* there are opened interfaces */
523 if (wandev->shutdown)
524 err=wandev->shutdown(wandev);
530 * Get WAN device status & statistics.
533 static int wanrouter_device_stat(struct wan_device *wandev,
534 wandev_stat_t __user *u_stat)
538 memset(&stat, 0, sizeof(stat));
540 /* Ask device driver to update device statistics */
541 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
542 wandev->update(wandev);
544 /* Fill out structure */
545 stat.ndev = wandev->ndev;
546 stat.state = wandev->state;
548 if (copy_to_user(u_stat, &stat, sizeof(stat)))
555 * Create new WAN interface.
556 * o verify user address space
557 * o copy configuration data to kernel address space
558 * o allocate network interface data space
559 * o call driver's new_if() entry point
560 * o make sure there is no interface name conflict
561 * o register network interface
564 static int wanrouter_device_new_if(struct wan_device *wandev,
565 wanif_conf_t __user *u_conf)
568 struct net_device *dev = NULL;
569 #ifdef CONFIG_WANPIPE_MULTPPP
570 struct ppp_device *pppdev=NULL;
574 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
577 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
582 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
586 if (cnf->magic != ROUTER_MAGIC)
589 if (cnf->config_id == WANCONFIG_MPPP) {
590 #ifdef CONFIG_WANPIPE_MULTPPP
591 pppdev = kzalloc(sizeof(struct ppp_device), GFP_KERNEL);
595 pppdev->dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
596 if (pppdev->dev == NULL) {
601 err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
604 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
606 err = -EPROTONOSUPPORT;
610 dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
614 err = wandev->new_if(wandev, dev, cnf);
618 /* Register network interface. This will invoke init()
619 * function supplied by the driver. If device registered
620 * successfully, add it to the interface list.
623 if (dev->name == NULL) {
628 printk(KERN_INFO "%s: registering interface %s...\n",
629 wanrouter_modname, dev->name);
632 err = register_netdev(dev);
634 struct net_device *slave = NULL;
635 unsigned long smp_flags=0;
637 lock_adapter_irq(&wandev->lock, &smp_flags);
639 if (wandev->dev == NULL) {
642 for (slave=wandev->dev;
643 *((struct net_device **)slave->priv);
644 slave = *((struct net_device **)slave->priv));
646 *((struct net_device **)slave->priv) = dev;
650 unlock_adapter_irq(&wandev->lock, &smp_flags);
651 err = 0; /* done !!! */
656 wandev->del_if(wandev, dev);
659 /* This code has moved from del_if() function */
663 #ifdef CONFIG_WANPIPE_MULTPPP
664 if (cnf->config_id == WANCONFIG_MPPP)
669 /* Sync PPP is disabled */
670 if (cnf->config_id != WANCONFIG_MPPP)
681 * Delete WAN logical channel.
682 * o verify user address space
683 * o copy configuration data to kernel address space
686 static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
688 char name[WAN_IFNAME_SZ + 1];
691 if (wandev->state == WAN_UNCONFIGURED)
694 memset(name, 0, sizeof(name));
696 if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
699 err = wanrouter_delete_interface(wandev, name);
703 /* If last interface being deleted, shutdown card
704 * This helps with administration at leaf nodes
705 * (You can tell if the person at the other end of the phone
706 * has an interface configured) and avoids DoS vulnerabilities
707 * in binary driver files - this fixes a problem with the current
708 * Sangoma driver going into strange states when all the network
709 * interfaces are deleted and the link irrecoverably disconnected.
712 if (!wandev->ndev && wandev->shutdown)
713 err = wandev->shutdown(wandev);
719 * Miscellaneous Functions
723 * Find WAN device by name.
724 * Return pointer to the WAN device data space or NULL if device not found.
727 static struct wan_device *wanrouter_find_device(char *name)
729 struct wan_device *wandev;
731 for (wandev = wanrouter_router_devlist;
732 wandev && strcmp(wandev->name, name);
733 wandev = wandev->next);
738 * Delete WAN logical channel identified by its name.
739 * o find logical channel by its name
740 * o call driver's del_if() entry point
741 * o unregister network interface
742 * o unlink channel data space from linked list of channels
743 * o release channel data space
746 * -ENODEV channel not found.
747 * -EBUSY interface is open
749 * Note: If (force != 0), then device will be destroyed even if interface
750 * associated with it is open. It's caller's responsibility to make
751 * sure that opened interfaces are not removed!
754 static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
756 struct net_device *dev = NULL, *prev = NULL;
757 unsigned long smp_flags=0;
759 lock_adapter_irq(&wandev->lock, &smp_flags);
762 while (dev && strcmp(name, dev->name)) {
763 struct net_device **slave = dev->priv;
767 unlock_adapter_irq(&wandev->lock, &smp_flags);
770 return -ENODEV; /* interface not found */
772 if (netif_running(dev))
773 return -EBUSY; /* interface in use */
776 wandev->del_if(wandev, dev);
778 lock_adapter_irq(&wandev->lock, &smp_flags);
780 struct net_device **prev_slave = prev->priv;
781 struct net_device **slave = dev->priv;
783 *prev_slave = *slave;
785 struct net_device **slave = dev->priv;
786 wandev->dev = *slave;
789 unlock_adapter_irq(&wandev->lock, &smp_flags);
791 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
793 /* Due to new interface linking method using dev->priv,
794 * this code has moved from del_if() function.*/
798 unregister_netdev(dev);
805 static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
807 spin_lock_irqsave(lock, *smp_flags);
811 static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
813 spin_unlock_irqrestore(lock, *smp_flags);
816 EXPORT_SYMBOL(register_wan_device);
817 EXPORT_SYMBOL(unregister_wan_device);
819 MODULE_LICENSE("GPL");