blob: 39701dec1dbae7c400cfcfcf72ad471d489eb776 [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() */
51#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/string.h> /* inline mem*, str* functions */
53
54#include <asm/byteorder.h> /* htons(), etc. */
55#include <linux/wanrouter.h> /* WAN router API definitions */
56
57#include <linux/vmalloc.h> /* vmalloc, vfree */
58#include <asm/uaccess.h> /* copy_to/from_user */
59#include <linux/init.h> /* __initfunc et al. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define KMEM_SAFETYZONE 8
62
Wang Chen7be60652008-11-20 04:26:21 -080063#define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev)))
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/*
66 * Function Prototypes
67 */
68
69/*
70 * WAN device IOCTL handlers
71 */
72
73static int wanrouter_device_setup(struct wan_device *wandev,
74 wandev_conf_t __user *u_conf);
75static int wanrouter_device_stat(struct wan_device *wandev,
76 wandev_stat_t __user *u_stat);
77static int wanrouter_device_shutdown(struct wan_device *wandev);
78static int wanrouter_device_new_if(struct wan_device *wandev,
79 wanif_conf_t __user *u_conf);
80static int wanrouter_device_del_if(struct wan_device *wandev,
81 char __user *u_name);
82
83/*
84 * Miscellaneous
85 */
86
87static struct wan_device *wanrouter_find_device(char *name);
88static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
Adrian Bunk97353cb2007-02-05 18:07:27 -080089static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
90static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92
93
94/*
95 * Global Data
96 */
97
98static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
99static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
100static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
101struct wan_device* wanrouter_router_devlist; /* list of registered devices */
102
103/*
104 * Organize Unique Identifiers for encapsulation/decapsulation
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#if 0
Adrian Bunk97353cb2007-02-05 18:07:27 -0800108static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
110#endif
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
Adrian Bunk97353cb2007-02-05 18:07:27 -0800249#if 0
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * Encapsulate packet.
253 *
254 * Return: encapsulation header size
255 * < 0 - unsupported Ethertype
256 *
257 * Notes:
258 * 1. This function may be called on interrupt context.
259 */
260
261
262int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
263 unsigned short type)
264{
265 int hdr_len = 0;
266
267 switch (type) {
268 case ETH_P_IP: /* IP datagram encapsulation */
269 hdr_len += 1;
270 skb_push(skb, 1);
271 skb->data[0] = NLPID_IP;
272 break;
273
274 case ETH_P_IPX: /* SNAP encapsulation */
275 case ETH_P_ARP:
276 hdr_len += 7;
277 skb_push(skb, 7);
278 skb->data[0] = 0;
279 skb->data[1] = NLPID_SNAP;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300280 skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
281 sizeof(wanrouter_oui_ether));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *((unsigned short*)&skb->data[5]) = htons(type);
283 break;
284
285 default: /* Unknown packet type */
286 printk(KERN_INFO
287 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
288 wanrouter_modname, type, dev->name);
289 hdr_len = -EINVAL;
290 }
291 return hdr_len;
292}
293
294
295/*
296 * Decapsulate packet.
297 *
298 * Return: Ethertype (in network order)
299 * 0 unknown encapsulation
300 *
301 * Notes:
302 * 1. This function may be called on interrupt context.
303 */
304
305
Alexey Dobriyanab611482005-07-12 12:08:43 -0700306__be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
Alexey Dobriyanab611482005-07-12 12:08:43 -0700309 __be16 ethertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 switch (skb->data[cnt]) {
312 case NLPID_IP: /* IP datagramm */
313 ethertype = htons(ETH_P_IP);
314 cnt += 1;
315 break;
316
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900317 case NLPID_SNAP: /* SNAP encapsulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
319 sizeof(wanrouter_oui_ether))){
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900320 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 "%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);
325 return 0;
326 }
Alexey Dobriyanab611482005-07-12 12:08:43 -0700327 ethertype = *((__be16*)&skb->data[cnt+4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 cnt += 6;
329 break;
330
331 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
332
333 default:
334 printk(KERN_INFO
335 "%s: unsupported NLPID 0x%02X on interface %s!\n",
336 wanrouter_modname, skb->data[cnt], dev->name);
337 return 0;
338 }
339 skb->protocol = ethertype;
340 skb->pkt_type = PACKET_HOST; /* Physically point to point */
341 skb_pull(skb, cnt);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700342 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return ethertype;
344}
345
Adrian Bunk97353cb2007-02-05 18:07:27 -0800346#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348/*
349 * WAN device IOCTL.
350 * o find WAN device associated with this node
351 * o execute requested action or pass command to the device driver
352 */
353
Alan Cox866988e2008-05-25 23:41:40 -0700354long wanrouter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Alan Cox866988e2008-05-25 23:41:40 -0700356 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 int err = 0;
358 struct proc_dir_entry *dent;
359 struct wan_device *wandev;
360 void __user *data = (void __user *)arg;
361
362 if (!capable(CAP_NET_ADMIN))
363 return -EPERM;
364
365 if ((cmd >> 8) != ROUTER_IOCTL)
366 return -EINVAL;
367
368 dent = PDE(inode);
369 if ((dent == NULL) || (dent->data == NULL))
370 return -EINVAL;
371
372 wandev = dent->data;
373 if (wandev->magic != ROUTER_MAGIC)
374 return -EINVAL;
375
Alan Cox866988e2008-05-25 23:41:40 -0700376 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 switch (cmd) {
378 case ROUTER_SETUP:
379 err = wanrouter_device_setup(wandev, data);
380 break;
381
382 case ROUTER_DOWN:
383 err = wanrouter_device_shutdown(wandev);
384 break;
385
386 case ROUTER_STAT:
387 err = wanrouter_device_stat(wandev, data);
388 break;
389
390 case ROUTER_IFNEW:
391 err = wanrouter_device_new_if(wandev, data);
392 break;
393
394 case ROUTER_IFDEL:
395 err = wanrouter_device_del_if(wandev, data);
396 break;
397
398 case ROUTER_IFSTAT:
399 break;
400
401 default:
402 if ((cmd >= ROUTER_USER) &&
403 (cmd <= ROUTER_USER_MAX) &&
404 wandev->ioctl)
405 err = wandev->ioctl(wandev, cmd, arg);
406 else err = -EINVAL;
407 }
Alan Cox866988e2008-05-25 23:41:40 -0700408 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return err;
410}
411
412/*
413 * WAN Driver IOCTL Handlers
414 */
415
416/*
417 * Setup WAN link device.
418 * o verify user address space
419 * o allocate kernel memory and copy configuration data to kernel space
420 * o if configuration data includes extension, copy it to kernel space too
421 * o call driver's setup() entry point
422 */
423
424static int wanrouter_device_setup(struct wan_device *wandev,
425 wandev_conf_t __user *u_conf)
426{
427 void *data = NULL;
428 wandev_conf_t *conf;
429 int err = -EINVAL;
430
431 if (wandev->setup == NULL) { /* Nothing to do ? */
432 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
433 wandev->name);
434 return 0;
435 }
436
437 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
438 if (conf == NULL){
439 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
440 wandev->name);
441 return -ENOBUFS;
442 }
443
444 if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
445 printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
446 wandev->name);
447 kfree(conf);
448 return -EFAULT;
449 }
450
451 if (conf->magic != ROUTER_MAGIC) {
452 kfree(conf);
453 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
454 wandev->name);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900455 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 if (conf->data_size && conf->data) {
Bill Nottingham75202e72007-05-31 21:33:35 -0700459 if (conf->data_size > 128000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 printk(KERN_INFO
461 "%s: ERROR, Invalid firmware data size %i !\n",
462 wandev->name, conf->data_size);
463 kfree(conf);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900464 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 data = vmalloc(conf->data_size);
468 if (!data) {
469 printk(KERN_INFO
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900470 "%s: ERROR, Faild allocate kernel memory !\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 wandev->name);
472 kfree(conf);
473 return -ENOBUFS;
474 }
475 if (!copy_from_user(data, conf->data, conf->data_size)) {
476 conf->data = data;
477 err = wandev->setup(wandev, conf);
478 } else {
479 printk(KERN_INFO
480 "%s: ERROR, Faild to copy from user data !\n",
481 wandev->name);
482 err = -EFAULT;
483 }
484 vfree(data);
485 } else {
486 printk(KERN_INFO
487 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
488 wandev->name, conf->data_size);
489 }
490
491 kfree(conf);
492 return err;
493}
494
495/*
496 * Shutdown WAN device.
497 * o delete all not opened logical channels for this device
498 * o call driver's shutdown() entry point
499 */
500
501static int wanrouter_device_shutdown(struct wan_device *wandev)
502{
503 struct net_device *dev;
504 int err=0;
505
506 if (wandev->state == WAN_UNCONFIGURED)
507 return 0;
508
509 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
510
511 for (dev = wandev->dev; dev;) {
512 err = wanrouter_delete_interface(wandev, dev->name);
513 if (err)
514 return err;
515 /* The above function deallocates the current dev
Wang Chen7be60652008-11-20 04:26:21 -0800516 * structure. Therefore, we cannot use netdev_priv(dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * as the next element: wandev->dev points to the
518 * next element */
519 dev = wandev->dev;
520 }
521
522 if (wandev->ndev)
523 return -EBUSY; /* there are opened interfaces */
524
525 if (wandev->shutdown)
526 err=wandev->shutdown(wandev);
527
528 return err;
529}
530
531/*
532 * Get WAN device status & statistics.
533 */
534
535static int wanrouter_device_stat(struct wan_device *wandev,
536 wandev_stat_t __user *u_stat)
537{
538 wandev_stat_t stat;
539
540 memset(&stat, 0, sizeof(stat));
541
542 /* Ask device driver to update device statistics */
543 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
544 wandev->update(wandev);
545
546 /* Fill out structure */
547 stat.ndev = wandev->ndev;
548 stat.state = wandev->state;
549
550 if (copy_to_user(u_stat, &stat, sizeof(stat)))
551 return -EFAULT;
552
553 return 0;
554}
555
556/*
557 * Create new WAN interface.
558 * o verify user address space
559 * o copy configuration data to kernel address space
560 * o allocate network interface data space
561 * o call driver's new_if() entry point
562 * o make sure there is no interface name conflict
563 * o register network interface
564 */
565
566static int wanrouter_device_new_if(struct wan_device *wandev,
567 wanif_conf_t __user *u_conf)
568{
569 wanif_conf_t *cnf;
570 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 int err;
572
573 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
574 return -ENODEV;
575
576 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
577 if (!cnf)
578 return -ENOBUFS;
579
580 err = -EFAULT;
581 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
582 goto out;
583
584 err = -EINVAL;
585 if (cnf->magic != ROUTER_MAGIC)
586 goto out;
587
588 if (cnf->config_id == WANCONFIG_MPPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
590 wandev->name);
591 err = -EPROTONOSUPPORT;
592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 err = wandev->new_if(wandev, dev, cnf);
595 }
596
597 if (!err) {
598 /* Register network interface. This will invoke init()
599 * function supplied by the driver. If device registered
600 * successfully, add it to the interface list.
601 */
602
603 if (dev->name == NULL) {
604 err = -EINVAL;
605 } else {
606
607 #ifdef WANDEBUG
608 printk(KERN_INFO "%s: registering interface %s...\n",
609 wanrouter_modname, dev->name);
610 #endif
611
612 err = register_netdev(dev);
613 if (!err) {
614 struct net_device *slave = NULL;
615 unsigned long smp_flags=0;
616
617 lock_adapter_irq(&wandev->lock, &smp_flags);
618
619 if (wandev->dev == NULL) {
620 wandev->dev = dev;
621 } else {
622 for (slave=wandev->dev;
Wang Chen7be60652008-11-20 04:26:21 -0800623 DEV_TO_SLAVE(slave);
624 slave = DEV_TO_SLAVE(slave))
625 DEV_TO_SLAVE(slave) = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
627 ++wandev->ndev;
628
629 unlock_adapter_irq(&wandev->lock, &smp_flags);
630 err = 0; /* done !!! */
631 goto out;
632 }
633 }
634 if (wandev->del_if)
635 wandev->del_if(wandev, dev);
Wang Chen7be60652008-11-20 04:26:21 -0800636 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639out:
640 kfree(cnf);
641 return err;
642}
643
644
645/*
646 * Delete WAN logical channel.
647 * o verify user address space
648 * o copy configuration data to kernel address space
649 */
650
651static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
652{
653 char name[WAN_IFNAME_SZ + 1];
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900654 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (wandev->state == WAN_UNCONFIGURED)
657 return -ENODEV;
658
659 memset(name, 0, sizeof(name));
660
661 if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
662 return -EFAULT;
663
664 err = wanrouter_delete_interface(wandev, name);
665 if (err)
666 return err;
667
668 /* If last interface being deleted, shutdown card
669 * This helps with administration at leaf nodes
670 * (You can tell if the person at the other end of the phone
671 * has an interface configured) and avoids DoS vulnerabilities
672 * in binary driver files - this fixes a problem with the current
673 * Sangoma driver going into strange states when all the network
674 * interfaces are deleted and the link irrecoverably disconnected.
675 */
676
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900677 if (!wandev->ndev && wandev->shutdown)
678 err = wandev->shutdown(wandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 return err;
681}
682
683/*
684 * Miscellaneous Functions
685 */
686
687/*
688 * Find WAN device by name.
689 * Return pointer to the WAN device data space or NULL if device not found.
690 */
691
692static struct wan_device *wanrouter_find_device(char *name)
693{
694 struct wan_device *wandev;
695
696 for (wandev = wanrouter_router_devlist;
697 wandev && strcmp(wandev->name, name);
698 wandev = wandev->next);
699 return wandev;
700}
701
702/*
703 * Delete WAN logical channel identified by its name.
704 * o find logical channel by its name
705 * o call driver's del_if() entry point
706 * o unregister network interface
707 * o unlink channel data space from linked list of channels
708 * o release channel data space
709 *
710 * Return: 0 success
711 * -ENODEV channel not found.
712 * -EBUSY interface is open
713 *
714 * Note: If (force != 0), then device will be destroyed even if interface
715 * associated with it is open. It's caller's responsibility to make
716 * sure that opened interfaces are not removed!
717 */
718
719static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
720{
721 struct net_device *dev = NULL, *prev = NULL;
722 unsigned long smp_flags=0;
723
724 lock_adapter_irq(&wandev->lock, &smp_flags);
725 dev = wandev->dev;
726 prev = NULL;
727 while (dev && strcmp(name, dev->name)) {
Wang Chen7be60652008-11-20 04:26:21 -0800728 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 prev = dev;
730 dev = *slave;
731 }
732 unlock_adapter_irq(&wandev->lock, &smp_flags);
733
734 if (dev == NULL)
735 return -ENODEV; /* interface not found */
736
737 if (netif_running(dev))
738 return -EBUSY; /* interface in use */
739
740 if (wandev->del_if)
741 wandev->del_if(wandev, dev);
742
743 lock_adapter_irq(&wandev->lock, &smp_flags);
744 if (prev) {
Wang Chen7be60652008-11-20 04:26:21 -0800745 struct net_device **prev_slave = netdev_priv(prev);
746 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 *prev_slave = *slave;
749 } else {
Wang Chen7be60652008-11-20 04:26:21 -0800750 struct net_device **slave = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 wandev->dev = *slave;
752 }
753 --wandev->ndev;
754 unlock_adapter_irq(&wandev->lock, &smp_flags);
755
756 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unregister_netdev(dev);
759
760 free_netdev(dev);
761
762 return 0;
763}
764
Adrian Bunk97353cb2007-02-05 18:07:27 -0800765static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900767 spin_lock_irqsave(lock, *smp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770
Adrian Bunk97353cb2007-02-05 18:07:27 -0800771static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 spin_unlock_irqrestore(lock, *smp_flags);
774}
775
776EXPORT_SYMBOL(register_wan_device);
777EXPORT_SYMBOL(unregister_wan_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779MODULE_LICENSE("GPL");
780
781/*
782 * End
783 */