1/***************************************************************************** 2* wanmain.c WAN Multiprotocol Router Module. Main code. 3* 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 11* 12* Author: Gideon Hack 13* 14* Copyright: (c) 1995-1999 Sangoma Technologies Inc. 15* 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 23* greater. 24* Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on 25* kernels 2.2.16 or greater. The SyncPPP 26* has changed. 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*****************************************************************************/ 44 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> 52#include <linux/mm.h> 53#include <linux/string.h> /* inline mem*, str* functions */ 54 55#include <asm/byteorder.h> /* htons(), etc. */ 56#include <linux/wanrouter.h> /* WAN router API definitions */ 57 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 62#define KMEM_SAFETYZONE 8 63 64#define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev))) 65 66/* 67 * Function Prototypes 68 */ 69 70/* 71 * WAN device IOCTL handlers 72 */ 73 74static DEFINE_MUTEX(wanrouter_mutex); 75static int wanrouter_device_setup(struct wan_device *wandev, 76 wandev_conf_t __user *u_conf); 77static int wanrouter_device_stat(struct wan_device *wandev, 78 wandev_stat_t __user *u_stat); 79static int wanrouter_device_shutdown(struct wan_device *wandev); 80static int wanrouter_device_new_if(struct wan_device *wandev, 81 wanif_conf_t __user *u_conf); 82static int wanrouter_device_del_if(struct wan_device *wandev, 83 char __user *u_name); 84 85/* 86 * Miscellaneous 87 */ 88 89static struct wan_device *wanrouter_find_device(char *name); 90static int wanrouter_delete_interface(struct wan_device *wandev, char *name); 91static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags) 92 __acquires(lock); 93static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags) 94 __releases(lock); 95 96 97 98/* 99 * Global Data 100 */ 101 102static char wanrouter_fullname[] = "Sangoma WANPIPE Router"; 103static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc."; 104static char wanrouter_modname[] = ROUTER_NAME; /* short module name */ 105struct wan_device* wanrouter_router_devlist; /* list of registered devices */ 106 107/* 108 * Organize Unique Identifiers for encapsulation/decapsulation 109 */ 110 111 112static int __init wanrouter_init(void) 113{ 114 int err; 115 116 printk(KERN_INFO "%s v%u.%u %s\n", 117 wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE, 118 wanrouter_copyright); 119 120 err = wanrouter_proc_init(); 121 if (err) 122 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n", 123 wanrouter_modname); 124 125 return err; 126} 127 128static void __exit wanrouter_cleanup (void) 129{ 130 wanrouter_proc_cleanup(); 131} 132 133/* 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?) 137 */ 138subsys_initcall(wanrouter_init); 139module_exit(wanrouter_cleanup); 140 141/* 142 * Kernel APIs 143 */ 144 145/* 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 153 * 154 * Return: 155 * 0 Ok 156 * < 0 error. 157 * 158 * Context: process 159 */ 160 161 162int register_wan_device(struct wan_device *wandev) 163{ 164 int err, namelen; 165 166 if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) || 167 (wandev->name == NULL)) 168 return -EINVAL; 169 170 namelen = strlen(wandev->name); 171 if (!namelen || (namelen > WAN_DRVNAME_SZ)) 172 return -EINVAL; 173 174 if (wanrouter_find_device(wandev->name)) 175 return -EEXIST; 176 177#ifdef WANDEBUG 178 printk(KERN_INFO "%s: registering WAN device %s\n", 179 wanrouter_modname, wandev->name); 180#endif 181 182 /* 183 * Register /proc directory entry 184 */ 185 err = wanrouter_proc_add(wandev); 186 if (err) { 187 printk(KERN_INFO 188 "%s: can't create /proc/net/router/%s entry!\n", 189 wanrouter_modname, wandev->name); 190 return err; 191 } 192 193 /* 194 * Initialize fields of the wan_device structure maintained by the 195 * router and update local data. 196 */ 197 198 wandev->ndev = 0; 199 wandev->dev = NULL; 200 wandev->next = wanrouter_router_devlist; 201 wanrouter_router_devlist = wandev; 202 return 0; 203} 204 205/* 206 * Unregister WAN device. 207 * o shut down 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 211 * 212 * Return: 0 Ok 213 * <0 error. 214 * Context: process 215 */ 216 217 218int unregister_wan_device(char *name) 219{ 220 struct wan_device *wandev, *prev; 221 222 if (name == NULL) 223 return -EINVAL; 224 225 for (wandev = wanrouter_router_devlist, prev = NULL; 226 wandev && strcmp(wandev->name, name); 227 prev = wandev, wandev = wandev->next) 228 ; 229 if (wandev == NULL) 230 return -ENODEV; 231 232#ifdef WANDEBUG 233 printk(KERN_INFO "%s: unregistering WAN device %s\n", 234 wanrouter_modname, name); 235#endif 236 237 if (wandev->state != WAN_UNCONFIGURED) 238 wanrouter_device_shutdown(wandev); 239 240 if (prev) 241 prev->next = wandev->next; 242 else 243 wanrouter_router_devlist = wandev->next; 244 245 wanrouter_proc_delete(wandev); 246 return 0; 247} 248 249 250/* 251 * WAN device IOCTL. 252 * o find WAN device associated with this node 253 * o execute requested action or pass command to the device driver 254 */ 255 256long wanrouter_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 257{ 258 struct inode *inode = file->f_path.dentry->d_inode; 259 int err = 0; 260 struct proc_dir_entry *dent; 261 struct wan_device *wandev; 262 void __user *data = (void __user *)arg; 263 264 if (!capable(CAP_NET_ADMIN)) 265 return -EPERM; 266 267 if ((cmd >> 8) != ROUTER_IOCTL) 268 return -EINVAL; 269 270 dent = PDE(inode); 271 if ((dent == NULL) || (dent->data == NULL)) 272 return -EINVAL; 273 274 wandev = dent->data; 275 if (wandev->magic != ROUTER_MAGIC) 276 return -EINVAL; 277 278 mutex_lock(&wanrouter_mutex); 279 switch (cmd) { 280 case ROUTER_SETUP: 281 err = wanrouter_device_setup(wandev, data); 282 break; 283 284 case ROUTER_DOWN: 285 err = wanrouter_device_shutdown(wandev); 286 break; 287 288 case ROUTER_STAT: 289 err = wanrouter_device_stat(wandev, data); 290 break; 291 292 case ROUTER_IFNEW: 293 err = wanrouter_device_new_if(wandev, data); 294 break; 295 296 case ROUTER_IFDEL: 297 err = wanrouter_device_del_if(wandev, data); 298 break; 299 300 case ROUTER_IFSTAT: 301 break; 302 303 default: 304 if ((cmd >= ROUTER_USER) && 305 (cmd <= ROUTER_USER_MAX) && 306 wandev->ioctl) 307 err = wandev->ioctl(wandev, cmd, arg); 308 else err = -EINVAL; 309 } 310 mutex_unlock(&wanrouter_mutex); 311 return err; 312} 313 314/* 315 * WAN Driver IOCTL Handlers 316 */ 317 318/* 319 * Setup WAN link device. 320 * o verify user address space 321 * o allocate kernel memory and copy configuration data to kernel space 322 * o if configuration data includes extension, copy it to kernel space too 323 * o call driver's setup() entry point 324 */ 325 326static int wanrouter_device_setup(struct wan_device *wandev, 327 wandev_conf_t __user *u_conf) 328{ 329 void *data = NULL; 330 wandev_conf_t *conf; 331 int err = -EINVAL; 332 333 if (wandev->setup == NULL) { /* Nothing to do ? */ 334 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n", 335 wandev->name); 336 return 0; 337 } 338 339 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL); 340 if (conf == NULL){ 341 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n", 342 wandev->name); 343 return -ENOBUFS; 344 } 345 346 if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) { 347 printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n", 348 wandev->name); 349 kfree(conf); 350 return -EFAULT; 351 } 352 353 if (conf->magic != ROUTER_MAGIC) { 354 kfree(conf); 355 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n", 356 wandev->name); 357 return -EINVAL; 358 } 359 360 if (conf->data_size && conf->data) { 361 if (conf->data_size > 128000) { 362 printk(KERN_INFO 363 "%s: ERROR, Invalid firmware data size %i !\n", 364 wandev->name, conf->data_size); 365 kfree(conf); 366 return -EINVAL; 367 } 368 369 data = vmalloc(conf->data_size); 370 if (!data) { 371 printk(KERN_INFO 372 "%s: ERROR, Faild allocate kernel memory !\n", 373 wandev->name); 374 kfree(conf); 375 return -ENOBUFS; 376 } 377 if (!copy_from_user(data, conf->data, conf->data_size)) { 378 conf->data = data; 379 err = wandev->setup(wandev, conf); 380 } else { 381 printk(KERN_INFO 382 "%s: ERROR, Faild to copy from user data !\n", 383 wandev->name); 384 err = -EFAULT; 385 } 386 vfree(data); 387 } else { 388 printk(KERN_INFO 389 "%s: ERROR, No firmware found ! Firmware size = %i !\n", 390 wandev->name, conf->data_size); 391 } 392 393 kfree(conf); 394 return err; 395} 396 397/* 398 * Shutdown WAN device. 399 * o delete all not opened logical channels for this device 400 * o call driver's shutdown() entry point 401 */ 402 403static int wanrouter_device_shutdown(struct wan_device *wandev) 404{ 405 struct net_device *dev; 406 int err=0; 407 408 if (wandev->state == WAN_UNCONFIGURED) 409 return 0; 410 411 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name); 412 413 for (dev = wandev->dev; dev;) { 414 err = wanrouter_delete_interface(wandev, dev->name); 415 if (err) 416 return err; 417 /* The above function deallocates the current dev 418 * structure. Therefore, we cannot use netdev_priv(dev) 419 * as the next element: wandev->dev points to the 420 * next element */ 421 dev = wandev->dev; 422 } 423 424 if (wandev->ndev) 425 return -EBUSY; /* there are opened interfaces */ 426 427 if (wandev->shutdown) 428 err=wandev->shutdown(wandev); 429 430 return err; 431} 432 433/* 434 * Get WAN device status & statistics. 435 */ 436 437static int wanrouter_device_stat(struct wan_device *wandev, 438 wandev_stat_t __user *u_stat) 439{ 440 wandev_stat_t stat; 441 442 memset(&stat, 0, sizeof(stat)); 443 444 /* Ask device driver to update device statistics */ 445 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update) 446 wandev->update(wandev); 447 448 /* Fill out structure */ 449 stat.ndev = wandev->ndev; 450 stat.state = wandev->state; 451 452 if (copy_to_user(u_stat, &stat, sizeof(stat))) 453 return -EFAULT; 454 455 return 0; 456} 457 458/* 459 * Create new WAN interface. 460 * o verify user address space 461 * o copy configuration data to kernel address space 462 * o allocate network interface data space 463 * o call driver's new_if() entry point 464 * o make sure there is no interface name conflict 465 * o register network interface 466 */ 467 468static int wanrouter_device_new_if(struct wan_device *wandev, 469 wanif_conf_t __user *u_conf) 470{ 471 wanif_conf_t *cnf; 472 struct net_device *dev = NULL; 473 int err; 474 475 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL)) 476 return -ENODEV; 477 478 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL); 479 if (!cnf) 480 return -ENOBUFS; 481 482 err = -EFAULT; 483 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t))) 484 goto out; 485 486 err = -EINVAL; 487 if (cnf->magic != ROUTER_MAGIC) 488 goto out; 489 490 if (cnf->config_id == WANCONFIG_MPPP) { 491 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n", 492 wandev->name); 493 err = -EPROTONOSUPPORT; 494 goto out; 495 } else { 496 err = wandev->new_if(wandev, dev, cnf); 497 } 498 499 if (!err) { 500 /* Register network interface. This will invoke init() 501 * function supplied by the driver. If device registered 502 * successfully, add it to the interface list. 503 */ 504 505 if (dev->name == NULL) { 506 err = -EINVAL; 507 } else { 508 509 #ifdef WANDEBUG 510 printk(KERN_INFO "%s: registering interface %s...\n", 511 wanrouter_modname, dev->name); 512 #endif 513 514 err = register_netdev(dev); 515 if (!err) { 516 struct net_device *slave = NULL; 517 unsigned long smp_flags=0; 518 519 lock_adapter_irq(&wandev->lock, &smp_flags); 520 521 if (wandev->dev == NULL) { 522 wandev->dev = dev; 523 } else { 524 for (slave=wandev->dev; 525 DEV_TO_SLAVE(slave); 526 slave = DEV_TO_SLAVE(slave)) 527 DEV_TO_SLAVE(slave) = dev; 528 } 529 ++wandev->ndev; 530 531 unlock_adapter_irq(&wandev->lock, &smp_flags); 532 err = 0; /* done !!! */ 533 goto out; 534 } 535 } 536 if (wandev->del_if) 537 wandev->del_if(wandev, dev); 538 free_netdev(dev); 539 } 540 541out: 542 kfree(cnf); 543 return err; 544} 545 546 547/* 548 * Delete WAN logical channel. 549 * o verify user address space 550 * o copy configuration data to kernel address space 551 */ 552 553static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name) 554{ 555 char name[WAN_IFNAME_SZ + 1]; 556 int err = 0; 557 558 if (wandev->state == WAN_UNCONFIGURED) 559 return -ENODEV; 560 561 memset(name, 0, sizeof(name)); 562 563 if (copy_from_user(name, u_name, WAN_IFNAME_SZ)) 564 return -EFAULT; 565 566 err = wanrouter_delete_interface(wandev, name); 567 if (err) 568 return err; 569 570 /* If last interface being deleted, shutdown card 571 * This helps with administration at leaf nodes 572 * (You can tell if the person at the other end of the phone 573 * has an interface configured) and avoids DoS vulnerabilities 574 * in binary driver files - this fixes a problem with the current 575 * Sangoma driver going into strange states when all the network 576 * interfaces are deleted and the link irrecoverably disconnected. 577 */ 578 579 if (!wandev->ndev && wandev->shutdown) 580 err = wandev->shutdown(wandev); 581 582 return err; 583} 584 585/* 586 * Miscellaneous Functions 587 */ 588 589/* 590 * Find WAN device by name. 591 * Return pointer to the WAN device data space or NULL if device not found. 592 */ 593 594static struct wan_device *wanrouter_find_device(char *name) 595{ 596 struct wan_device *wandev; 597 598 for (wandev = wanrouter_router_devlist; 599 wandev && strcmp(wandev->name, name); 600 wandev = wandev->next); 601 return wandev; 602} 603 604/* 605 * Delete WAN logical channel identified by its name. 606 * o find logical channel by its name 607 * o call driver's del_if() entry point 608 * o unregister network interface 609 * o unlink channel data space from linked list of channels 610 * o release channel data space 611 * 612 * Return: 0 success 613 * -ENODEV channel not found. 614 * -EBUSY interface is open 615 * 616 * Note: If (force != 0), then device will be destroyed even if interface 617 * associated with it is open. It's caller's responsibility to make 618 * sure that opened interfaces are not removed! 619 */ 620 621static int wanrouter_delete_interface(struct wan_device *wandev, char *name) 622{ 623 struct net_device *dev = NULL, *prev = NULL; 624 unsigned long smp_flags=0; 625 626 lock_adapter_irq(&wandev->lock, &smp_flags); 627 dev = wandev->dev; 628 prev = NULL; 629 while (dev && strcmp(name, dev->name)) { 630 struct net_device **slave = netdev_priv(dev); 631 prev = dev; 632 dev = *slave; 633 } 634 unlock_adapter_irq(&wandev->lock, &smp_flags); 635 636 if (dev == NULL) 637 return -ENODEV; /* interface not found */ 638 639 if (netif_running(dev)) 640 return -EBUSY; /* interface in use */ 641 642 if (wandev->del_if) 643 wandev->del_if(wandev, dev); 644 645 lock_adapter_irq(&wandev->lock, &smp_flags); 646 if (prev) { 647 struct net_device **prev_slave = netdev_priv(prev); 648 struct net_device **slave = netdev_priv(dev); 649 650 *prev_slave = *slave; 651 } else { 652 struct net_device **slave = netdev_priv(dev); 653 wandev->dev = *slave; 654 } 655 --wandev->ndev; 656 unlock_adapter_irq(&wandev->lock, &smp_flags); 657 658 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name); 659 660 unregister_netdev(dev); 661 662 free_netdev(dev); 663 664 return 0; 665} 666 667static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags) 668 __acquires(lock) 669{ 670 spin_lock_irqsave(lock, *smp_flags); 671} 672 673 674static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags) 675 __releases(lock) 676{ 677 spin_unlock_irqrestore(lock, *smp_flags); 678} 679 680EXPORT_SYMBOL(register_wan_device); 681EXPORT_SYMBOL(unregister_wan_device); 682 683MODULE_LICENSE("GPL"); 684 685/* 686 * End 687 */ 688