blob: 2bf23406637ae1eb849abb8633fd4658f5cf3332 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************
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:
Matt LaPlante0779bf22006-11-30 05:24:39 +01006* o WAN device management (registering, unregistering)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/stddef.h> /* offsetof(), etc. */
Randy Dunlap4fc268d2006-01-11 12:17:47 -080046#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/errno.h> /* return codes */
48#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/module.h> /* support for loadable modules */
Jesper Juhle49332b2005-05-01 08:59:08 -070050#include <linux/slab.h> /* kmalloc(), kfree() */
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +000051#include <linux/mutex.h>
Jesper Juhle49332b2005-05-01 08:59:08 -070052#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define KMEM_SAFETYZONE 8
63
Wang Chen7be60652008-11-20 04:26:21 -080064#define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev)))
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * Function Prototypes
68 */
69
70/*
71 * WAN device IOCTL handlers
72 */
73
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +000074static DEFINE_MUTEX(wanrouter_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static 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);
Hannes Eder9ee62632009-02-25 10:33:06 +000091static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#if 0
Adrian Bunk97353cb2007-02-05 18:07:27 -0800112static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
114#endif
115
116static int __init wanrouter_init(void)
117{
118 int err;
119
120 printk(KERN_INFO "%s v%u.%u %s\n",
121 wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
122 wanrouter_copyright);
123
124 err = wanrouter_proc_init();
125 if (err)
126 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
127 wanrouter_modname);
128
129 return err;
130}
131
132static void __exit wanrouter_cleanup (void)
133{
134 wanrouter_proc_cleanup();
135}
136
137/*
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?)
141 */
142subsys_initcall(wanrouter_init);
143module_exit(wanrouter_cleanup);
144
145/*
146 * Kernel APIs
147 */
148
149/*
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
157 *
158 * Return:
159 * 0 Ok
160 * < 0 error.
161 *
162 * Context: process
163 */
164
165
166int register_wan_device(struct wan_device *wandev)
167{
168 int err, namelen;
169
170 if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
171 (wandev->name == NULL))
172 return -EINVAL;
173
174 namelen = strlen(wandev->name);
175 if (!namelen || (namelen > WAN_DRVNAME_SZ))
176 return -EINVAL;
177
178 if (wanrouter_find_device(wandev->name))
179 return -EEXIST;
180
181#ifdef WANDEBUG
182 printk(KERN_INFO "%s: registering WAN device %s\n",
183 wanrouter_modname, wandev->name);
184#endif
185
186 /*
187 * Register /proc directory entry
188 */
189 err = wanrouter_proc_add(wandev);
190 if (err) {
191 printk(KERN_INFO
192 "%s: can't create /proc/net/router/%s entry!\n",
193 wanrouter_modname, wandev->name);
194 return err;
195 }
196
197 /*
198 * Initialize fields of the wan_device structure maintained by the
199 * router and update local data.
200 */
201
202 wandev->ndev = 0;
203 wandev->dev = NULL;
204 wandev->next = wanrouter_router_devlist;
205 wanrouter_router_devlist = wandev;
206 return 0;
207}
208
209/*
210 * Unregister WAN device.
211 * o shut down 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
215 *
216 * Return: 0 Ok
217 * <0 error.
218 * Context: process
219 */
220
221
222int unregister_wan_device(char *name)
223{
224 struct wan_device *wandev, *prev;
225
226 if (name == NULL)
227 return -EINVAL;
228
229 for (wandev = wanrouter_router_devlist, prev = NULL;
230 wandev && strcmp(wandev->name, name);
231 prev = wandev, wandev = wandev->next)
232 ;
233 if (wandev == NULL)
234 return -ENODEV;
235
236#ifdef WANDEBUG
237 printk(KERN_INFO "%s: unregistering WAN device %s\n",
238 wanrouter_modname, name);
239#endif
240
241 if (wandev->state != WAN_UNCONFIGURED)
242 wanrouter_device_shutdown(wandev);
243
244 if (prev)
245 prev->next = wandev->next;
246 else
247 wanrouter_router_devlist = wandev->next;
248
249 wanrouter_proc_delete(wandev);
250 return 0;
251}
252
Adrian Bunk97353cb2007-02-05 18:07:27 -0800253#if 0
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
256 * Encapsulate packet.
257 *
258 * Return: encapsulation header size
259 * < 0 - unsupported Ethertype
260 *
261 * Notes:
262 * 1. This function may be called on interrupt context.
263 */
264
265
266int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
267 unsigned short type)
268{
269 int hdr_len = 0;
270
271 switch (type) {
272 case ETH_P_IP: /* IP datagram encapsulation */
273 hdr_len += 1;
274 skb_push(skb, 1);
275 skb->data[0] = NLPID_IP;
276 break;
277
278 case ETH_P_IPX: /* SNAP encapsulation */
279 case ETH_P_ARP:
280 hdr_len += 7;
281 skb_push(skb, 7);
282 skb->data[0] = 0;
283 skb->data[1] = NLPID_SNAP;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300284 skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
285 sizeof(wanrouter_oui_ether));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 *((unsigned short*)&skb->data[5]) = htons(type);
287 break;
288
289 default: /* Unknown packet type */
290 printk(KERN_INFO
291 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
292 wanrouter_modname, type, dev->name);
293 hdr_len = -EINVAL;
294 }
295 return hdr_len;
296}
297
298
299/*
300 * Decapsulate packet.
301 *
302 * Return: Ethertype (in network order)
303 * 0 unknown encapsulation
304 *
305 * Notes:
306 * 1. This function may be called on interrupt context.
307 */
308
309
Alexey Dobriyanab611482005-07-12 12:08:43 -0700310__be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
Alexey Dobriyanab611482005-07-12 12:08:43 -0700313 __be16 ethertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 switch (skb->data[cnt]) {
316 case NLPID_IP: /* IP datagramm */
317 ethertype = htons(ETH_P_IP);
318 cnt += 1;
319 break;
320
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900321 case NLPID_SNAP: /* SNAP encapsulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
323 sizeof(wanrouter_oui_ether))){
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900324 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 "%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);
329 return 0;
330 }
Alexey Dobriyanab611482005-07-12 12:08:43 -0700331 ethertype = *((__be16*)&skb->data[cnt+4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 cnt += 6;
333 break;
334
335 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
336
337 default:
338 printk(KERN_INFO
339 "%s: unsupported NLPID 0x%02X on interface %s!\n",
340 wanrouter_modname, skb->data[cnt], dev->name);
341 return 0;
342 }
343 skb->protocol = ethertype;
344 skb->pkt_type = PACKET_HOST; /* Physically point to point */
345 skb_pull(skb, cnt);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700346 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return ethertype;
348}
349
Adrian Bunk97353cb2007-02-05 18:07:27 -0800350#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352/*
353 * WAN device IOCTL.
354 * o find WAN device associated with this node
355 * o execute requested action or pass command to the device driver
356 */
357
Alan Cox866988e2008-05-25 23:41:40 -0700358long wanrouter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Alan Cox866988e2008-05-25 23:41:40 -0700360 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 int err = 0;
362 struct proc_dir_entry *dent;
363 struct wan_device *wandev;
364 void __user *data = (void __user *)arg;
365
366 if (!capable(CAP_NET_ADMIN))
367 return -EPERM;
368
369 if ((cmd >> 8) != ROUTER_IOCTL)
370 return -EINVAL;
371
372 dent = PDE(inode);
373 if ((dent == NULL) || (dent->data == NULL))
374 return -EINVAL;
375
376 wandev = dent->data;
377 if (wandev->magic != ROUTER_MAGIC)
378 return -EINVAL;
379
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000380 mutex_lock(&wanrouter_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 switch (cmd) {
382 case ROUTER_SETUP:
383 err = wanrouter_device_setup(wandev, data);
384 break;
385
386 case ROUTER_DOWN:
387 err = wanrouter_device_shutdown(wandev);
388 break;
389
390 case ROUTER_STAT:
391 err = wanrouter_device_stat(wandev, data);
392 break;
393
394 case ROUTER_IFNEW:
395 err = wanrouter_device_new_if(wandev, data);
396 break;
397
398 case ROUTER_IFDEL:
399 err = wanrouter_device_del_if(wandev, data);
400 break;
401
402 case ROUTER_IFSTAT:
403 break;
404
405 default:
406 if ((cmd >= ROUTER_USER) &&
407 (cmd <= ROUTER_USER_MAX) &&
408 wandev->ioctl)
409 err = wandev->ioctl(wandev, cmd, arg);
410 else err = -EINVAL;
411 }
Arnd Bergmann15fd0cd2010-07-11 11:18:57 +0000412 mutex_unlock(&wanrouter_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return err;
414}
415
416/*
417 * WAN Driver IOCTL Handlers
418 */
419
420/*
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
426 */
427
428static int wanrouter_device_setup(struct wan_device *wandev,
429 wandev_conf_t __user *u_conf)
430{
431 void *data = NULL;
432 wandev_conf_t *conf;
433 int err = -EINVAL;
434
435 if (wandev->setup == NULL) { /* Nothing to do ? */
436 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
437 wandev->name);
438 return 0;
439 }
440
441 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
442 if (conf == NULL){
443 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
444 wandev->name);
445 return -ENOBUFS;
446 }
447
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",
450 wandev->name);
451 kfree(conf);
452 return -EFAULT;
453 }
454
455 if (conf->magic != ROUTER_MAGIC) {
456 kfree(conf);
457 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
458 wandev->name);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
462 if (conf->data_size && conf->data) {
Bill Nottingham75202e72007-05-31 21:33:35 -0700463 if (conf->data_size > 128000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 printk(KERN_INFO
465 "%s: ERROR, Invalid firmware data size %i !\n",
466 wandev->name, conf->data_size);
467 kfree(conf);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470
471 data = vmalloc(conf->data_size);
472 if (!data) {
473 printk(KERN_INFO
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900474 "%s: ERROR, Faild allocate kernel memory !\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 wandev->name);
476 kfree(conf);
477 return -ENOBUFS;
478 }
479 if (!copy_from_user(data, conf->data, conf->data_size)) {
480 conf->data = data;
481 err = wandev->setup(wandev, conf);
482 } else {
483 printk(KERN_INFO
484 "%s: ERROR, Faild to copy from user data !\n",
485 wandev->name);
486 err = -EFAULT;
487 }
488 vfree(data);
489 } else {
490 printk(KERN_INFO
491 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
492 wandev->name, conf->data_size);
493 }
494
495 kfree(conf);
496 return err;
497}
498
499/*
500 * Shutdown WAN device.
501 * o delete all not opened logical channels for this device
502 * o call driver's shutdown() entry point
503 */
504
505static int wanrouter_device_shutdown(struct wan_device *wandev)
506{
507 struct net_device *dev;
508 int err=0;
509
510 if (wandev->state == WAN_UNCONFIGURED)
511 return 0;
512
513 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
514
515 for (dev = wandev->dev; dev;) {
516 err = wanrouter_delete_interface(wandev, dev->name);
517 if (err)
518 return err;
519 /* The above function deallocates the current dev
Wang Chen7be60652008-11-20 04:26:21 -0800520 * structure. Therefore, we cannot use netdev_priv(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * as the next element: wandev->dev points to the
522 * next element */
523 dev = wandev->dev;
524 }
525
526 if (wandev->ndev)
527 return -EBUSY; /* there are opened interfaces */
528
529 if (wandev->shutdown)
530 err=wandev->shutdown(wandev);
531
532 return err;
533}
534
535/*
536 * Get WAN device status & statistics.
537 */
538
539static int wanrouter_device_stat(struct wan_device *wandev,
540 wandev_stat_t __user *u_stat)
541{
542 wandev_stat_t stat;
543
544 memset(&stat, 0, sizeof(stat));
545
546 /* Ask device driver to update device statistics */
547 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
548 wandev->update(wandev);
549
550 /* Fill out structure */
551 stat.ndev = wandev->ndev;
552 stat.state = wandev->state;
553
554 if (copy_to_user(u_stat, &stat, sizeof(stat)))
555 return -EFAULT;
556
557 return 0;
558}
559
560/*
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
568 */
569
570static int wanrouter_device_new_if(struct wan_device *wandev,
571 wanif_conf_t __user *u_conf)
572{
573 wanif_conf_t *cnf;
574 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 int err;
576
577 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
578 return -ENODEV;
579
580 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
581 if (!cnf)
582 return -ENOBUFS;
583
584 err = -EFAULT;
585 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
586 goto out;
587
588 err = -EINVAL;
589 if (cnf->magic != ROUTER_MAGIC)
590 goto out;
591
592 if (cnf->config_id == WANCONFIG_MPPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
594 wandev->name);
595 err = -EPROTONOSUPPORT;
596 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 err = wandev->new_if(wandev, dev, cnf);
599 }
600
601 if (!err) {
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.
605 */
606
607 if (dev->name == NULL) {
608 err = -EINVAL;
609 } else {
610
611 #ifdef WANDEBUG
612 printk(KERN_INFO "%s: registering interface %s...\n",
613 wanrouter_modname, dev->name);
614 #endif
615
616 err = register_netdev(dev);
617 if (!err) {
618 struct net_device *slave = NULL;
619 unsigned long smp_flags=0;
620
621 lock_adapter_irq(&wandev->lock, &smp_flags);
622
623 if (wandev->dev == NULL) {
624 wandev->dev = dev;
625 } else {
626 for (slave=wandev->dev;
Wang Chen7be60652008-11-20 04:26:21 -0800627 DEV_TO_SLAVE(slave);
628 slave = DEV_TO_SLAVE(slave))
629 DEV_TO_SLAVE(slave) = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631 ++wandev->ndev;
632
633 unlock_adapter_irq(&wandev->lock, &smp_flags);
634 err = 0; /* done !!! */
635 goto out;
636 }
637 }
638 if (wandev->del_if)
639 wandev->del_if(wandev, dev);
Wang Chen7be60652008-11-20 04:26:21 -0800640 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643out:
644 kfree(cnf);
645 return err;
646}
647
648
649/*
650 * Delete WAN logical channel.
651 * o verify user address space
652 * o copy configuration data to kernel address space
653 */
654
655static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
656{
657 char name[WAN_IFNAME_SZ + 1];
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900658 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (wandev->state == WAN_UNCONFIGURED)
661 return -ENODEV;
662
663 memset(name, 0, sizeof(name));
664
665 if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
666 return -EFAULT;
667
668 err = wanrouter_delete_interface(wandev, name);
669 if (err)
670 return err;
671
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.
679 */
680
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900681 if (!wandev->ndev && wandev->shutdown)
682 err = wandev->shutdown(wandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 return err;
685}
686
687/*
688 * Miscellaneous Functions
689 */
690
691/*
692 * Find WAN device by name.
693 * Return pointer to the WAN device data space or NULL if device not found.
694 */
695
696static struct wan_device *wanrouter_find_device(char *name)
697{
698 struct wan_device *wandev;
699
700 for (wandev = wanrouter_router_devlist;
701 wandev && strcmp(wandev->name, name);
702 wandev = wandev->next);
703 return wandev;
704}
705
706/*
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
713 *
714 * Return: 0 success
715 * -ENODEV channel not found.
716 * -EBUSY interface is open
717 *
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!
721 */
722
723static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
724{
725 struct net_device *dev = NULL, *prev = NULL;
726 unsigned long smp_flags=0;
727
728 lock_adapter_irq(&wandev->lock, &smp_flags);
729 dev = wandev->dev;
730 prev = NULL;
731 while (dev && strcmp(name, dev->name)) {
Wang Chen7be60652008-11-20 04:26:21 -0800732 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 prev = dev;
734 dev = *slave;
735 }
736 unlock_adapter_irq(&wandev->lock, &smp_flags);
737
738 if (dev == NULL)
739 return -ENODEV; /* interface not found */
740
741 if (netif_running(dev))
742 return -EBUSY; /* interface in use */
743
744 if (wandev->del_if)
745 wandev->del_if(wandev, dev);
746
747 lock_adapter_irq(&wandev->lock, &smp_flags);
748 if (prev) {
Wang Chen7be60652008-11-20 04:26:21 -0800749 struct net_device **prev_slave = netdev_priv(prev);
750 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 *prev_slave = *slave;
753 } else {
Wang Chen7be60652008-11-20 04:26:21 -0800754 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 wandev->dev = *slave;
756 }
757 --wandev->ndev;
758 unlock_adapter_irq(&wandev->lock, &smp_flags);
759
760 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unregister_netdev(dev);
763
764 free_netdev(dev);
765
766 return 0;
767}
768
Adrian Bunk97353cb2007-02-05 18:07:27 -0800769static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Hannes Eder9ee62632009-02-25 10:33:06 +0000770 __acquires(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900772 spin_lock_irqsave(lock, *smp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775
Adrian Bunk97353cb2007-02-05 18:07:27 -0800776static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Hannes Eder9ee62632009-02-25 10:33:06 +0000777 __releases(lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 spin_unlock_irqrestore(lock, *smp_flags);
780}
781
782EXPORT_SYMBOL(register_wan_device);
783EXPORT_SYMBOL(unregister_wan_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785MODULE_LICENSE("GPL");
786
787/*
788 * End
789 */