blob: 7f07152bc109e579118870d03f150e7620fd8725 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Function Prototypes
65 */
66
67/*
68 * WAN device IOCTL handlers
69 */
70
71static int wanrouter_device_setup(struct wan_device *wandev,
72 wandev_conf_t __user *u_conf);
73static int wanrouter_device_stat(struct wan_device *wandev,
74 wandev_stat_t __user *u_stat);
75static int wanrouter_device_shutdown(struct wan_device *wandev);
76static int wanrouter_device_new_if(struct wan_device *wandev,
77 wanif_conf_t __user *u_conf);
78static int wanrouter_device_del_if(struct wan_device *wandev,
79 char __user *u_name);
80
81/*
82 * Miscellaneous
83 */
84
85static struct wan_device *wanrouter_find_device(char *name);
86static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
Adrian Bunk97353cb2007-02-05 18:07:27 -080087static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
88static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90
91
92/*
93 * Global Data
94 */
95
96static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
97static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
98static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
99struct wan_device* wanrouter_router_devlist; /* list of registered devices */
100
101/*
102 * Organize Unique Identifiers for encapsulation/decapsulation
103 */
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#if 0
Adrian Bunk97353cb2007-02-05 18:07:27 -0800106static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
108#endif
109
110static int __init wanrouter_init(void)
111{
112 int err;
113
114 printk(KERN_INFO "%s v%u.%u %s\n",
115 wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
116 wanrouter_copyright);
117
118 err = wanrouter_proc_init();
119 if (err)
120 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
121 wanrouter_modname);
122
123 return err;
124}
125
126static void __exit wanrouter_cleanup (void)
127{
128 wanrouter_proc_cleanup();
129}
130
131/*
132 * This is just plain dumb. We should move the bugger to drivers/net/wan,
133 * slap it first in directory and make it module_init(). The only reason
134 * for subsys_initcall() here is that net goes after drivers (why, BTW?)
135 */
136subsys_initcall(wanrouter_init);
137module_exit(wanrouter_cleanup);
138
139/*
140 * Kernel APIs
141 */
142
143/*
144 * Register WAN device.
145 * o verify device credentials
146 * o create an entry for the device in the /proc/net/router directory
147 * o initialize internally maintained fields of the wan_device structure
148 * o link device data space to a singly-linked list
149 * o if it's the first device, then start kernel 'thread'
150 * o increment module use count
151 *
152 * Return:
153 * 0 Ok
154 * < 0 error.
155 *
156 * Context: process
157 */
158
159
160int register_wan_device(struct wan_device *wandev)
161{
162 int err, namelen;
163
164 if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
165 (wandev->name == NULL))
166 return -EINVAL;
167
168 namelen = strlen(wandev->name);
169 if (!namelen || (namelen > WAN_DRVNAME_SZ))
170 return -EINVAL;
171
172 if (wanrouter_find_device(wandev->name))
173 return -EEXIST;
174
175#ifdef WANDEBUG
176 printk(KERN_INFO "%s: registering WAN device %s\n",
177 wanrouter_modname, wandev->name);
178#endif
179
180 /*
181 * Register /proc directory entry
182 */
183 err = wanrouter_proc_add(wandev);
184 if (err) {
185 printk(KERN_INFO
186 "%s: can't create /proc/net/router/%s entry!\n",
187 wanrouter_modname, wandev->name);
188 return err;
189 }
190
191 /*
192 * Initialize fields of the wan_device structure maintained by the
193 * router and update local data.
194 */
195
196 wandev->ndev = 0;
197 wandev->dev = NULL;
198 wandev->next = wanrouter_router_devlist;
199 wanrouter_router_devlist = wandev;
200 return 0;
201}
202
203/*
204 * Unregister WAN device.
205 * o shut down device
206 * o unlink device data space from the linked list
207 * o delete device entry in the /proc/net/router directory
208 * o decrement module use count
209 *
210 * Return: 0 Ok
211 * <0 error.
212 * Context: process
213 */
214
215
216int unregister_wan_device(char *name)
217{
218 struct wan_device *wandev, *prev;
219
220 if (name == NULL)
221 return -EINVAL;
222
223 for (wandev = wanrouter_router_devlist, prev = NULL;
224 wandev && strcmp(wandev->name, name);
225 prev = wandev, wandev = wandev->next)
226 ;
227 if (wandev == NULL)
228 return -ENODEV;
229
230#ifdef WANDEBUG
231 printk(KERN_INFO "%s: unregistering WAN device %s\n",
232 wanrouter_modname, name);
233#endif
234
235 if (wandev->state != WAN_UNCONFIGURED)
236 wanrouter_device_shutdown(wandev);
237
238 if (prev)
239 prev->next = wandev->next;
240 else
241 wanrouter_router_devlist = wandev->next;
242
243 wanrouter_proc_delete(wandev);
244 return 0;
245}
246
Adrian Bunk97353cb2007-02-05 18:07:27 -0800247#if 0
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * Encapsulate packet.
251 *
252 * Return: encapsulation header size
253 * < 0 - unsupported Ethertype
254 *
255 * Notes:
256 * 1. This function may be called on interrupt context.
257 */
258
259
260int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
261 unsigned short type)
262{
263 int hdr_len = 0;
264
265 switch (type) {
266 case ETH_P_IP: /* IP datagram encapsulation */
267 hdr_len += 1;
268 skb_push(skb, 1);
269 skb->data[0] = NLPID_IP;
270 break;
271
272 case ETH_P_IPX: /* SNAP encapsulation */
273 case ETH_P_ARP:
274 hdr_len += 7;
275 skb_push(skb, 7);
276 skb->data[0] = 0;
277 skb->data[1] = NLPID_SNAP;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300278 skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
279 sizeof(wanrouter_oui_ether));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *((unsigned short*)&skb->data[5]) = htons(type);
281 break;
282
283 default: /* Unknown packet type */
284 printk(KERN_INFO
285 "%s: unsupported Ethertype 0x%04X on interface %s!\n",
286 wanrouter_modname, type, dev->name);
287 hdr_len = -EINVAL;
288 }
289 return hdr_len;
290}
291
292
293/*
294 * Decapsulate packet.
295 *
296 * Return: Ethertype (in network order)
297 * 0 unknown encapsulation
298 *
299 * Notes:
300 * 1. This function may be called on interrupt context.
301 */
302
303
Alexey Dobriyanab611482005-07-12 12:08:43 -0700304__be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
Alexey Dobriyanab611482005-07-12 12:08:43 -0700307 __be16 ethertype;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 switch (skb->data[cnt]) {
310 case NLPID_IP: /* IP datagramm */
311 ethertype = htons(ETH_P_IP);
312 cnt += 1;
313 break;
314
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900315 case NLPID_SNAP: /* SNAP encapsulation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
317 sizeof(wanrouter_oui_ether))){
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900318 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 "%s: unsupported SNAP OUI %02X-%02X-%02X "
320 "on interface %s!\n", wanrouter_modname,
321 skb->data[cnt+1], skb->data[cnt+2],
322 skb->data[cnt+3], dev->name);
323 return 0;
324 }
Alexey Dobriyanab611482005-07-12 12:08:43 -0700325 ethertype = *((__be16*)&skb->data[cnt+4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 cnt += 6;
327 break;
328
329 /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
330
331 default:
332 printk(KERN_INFO
333 "%s: unsupported NLPID 0x%02X on interface %s!\n",
334 wanrouter_modname, skb->data[cnt], dev->name);
335 return 0;
336 }
337 skb->protocol = ethertype;
338 skb->pkt_type = PACKET_HOST; /* Physically point to point */
339 skb_pull(skb, cnt);
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700340 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return ethertype;
342}
343
Adrian Bunk97353cb2007-02-05 18:07:27 -0800344#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346/*
347 * WAN device IOCTL.
348 * o find WAN device associated with this node
349 * o execute requested action or pass command to the device driver
350 */
351
Alan Cox866988e2008-05-25 23:41:40 -0700352long wanrouter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Alan Cox866988e2008-05-25 23:41:40 -0700354 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int err = 0;
356 struct proc_dir_entry *dent;
357 struct wan_device *wandev;
358 void __user *data = (void __user *)arg;
359
360 if (!capable(CAP_NET_ADMIN))
361 return -EPERM;
362
363 if ((cmd >> 8) != ROUTER_IOCTL)
364 return -EINVAL;
365
366 dent = PDE(inode);
367 if ((dent == NULL) || (dent->data == NULL))
368 return -EINVAL;
369
370 wandev = dent->data;
371 if (wandev->magic != ROUTER_MAGIC)
372 return -EINVAL;
373
Alan Cox866988e2008-05-25 23:41:40 -0700374 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 switch (cmd) {
376 case ROUTER_SETUP:
377 err = wanrouter_device_setup(wandev, data);
378 break;
379
380 case ROUTER_DOWN:
381 err = wanrouter_device_shutdown(wandev);
382 break;
383
384 case ROUTER_STAT:
385 err = wanrouter_device_stat(wandev, data);
386 break;
387
388 case ROUTER_IFNEW:
389 err = wanrouter_device_new_if(wandev, data);
390 break;
391
392 case ROUTER_IFDEL:
393 err = wanrouter_device_del_if(wandev, data);
394 break;
395
396 case ROUTER_IFSTAT:
397 break;
398
399 default:
400 if ((cmd >= ROUTER_USER) &&
401 (cmd <= ROUTER_USER_MAX) &&
402 wandev->ioctl)
403 err = wandev->ioctl(wandev, cmd, arg);
404 else err = -EINVAL;
405 }
Alan Cox866988e2008-05-25 23:41:40 -0700406 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return err;
408}
409
410/*
411 * WAN Driver IOCTL Handlers
412 */
413
414/*
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
420 */
421
422static int wanrouter_device_setup(struct wan_device *wandev,
423 wandev_conf_t __user *u_conf)
424{
425 void *data = NULL;
426 wandev_conf_t *conf;
427 int err = -EINVAL;
428
429 if (wandev->setup == NULL) { /* Nothing to do ? */
430 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
431 wandev->name);
432 return 0;
433 }
434
435 conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
436 if (conf == NULL){
437 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
438 wandev->name);
439 return -ENOBUFS;
440 }
441
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",
444 wandev->name);
445 kfree(conf);
446 return -EFAULT;
447 }
448
449 if (conf->magic != ROUTER_MAGIC) {
450 kfree(conf);
451 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
452 wandev->name);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900453 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 if (conf->data_size && conf->data) {
Bill Nottingham75202e72007-05-31 21:33:35 -0700457 if (conf->data_size > 128000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk(KERN_INFO
459 "%s: ERROR, Invalid firmware data size %i !\n",
460 wandev->name, conf->data_size);
461 kfree(conf);
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900462 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 data = vmalloc(conf->data_size);
466 if (!data) {
467 printk(KERN_INFO
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900468 "%s: ERROR, Faild allocate kernel memory !\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 wandev->name);
470 kfree(conf);
471 return -ENOBUFS;
472 }
473 if (!copy_from_user(data, conf->data, conf->data_size)) {
474 conf->data = data;
475 err = wandev->setup(wandev, conf);
476 } else {
477 printk(KERN_INFO
478 "%s: ERROR, Faild to copy from user data !\n",
479 wandev->name);
480 err = -EFAULT;
481 }
482 vfree(data);
483 } else {
484 printk(KERN_INFO
485 "%s: ERROR, No firmware found ! Firmware size = %i !\n",
486 wandev->name, conf->data_size);
487 }
488
489 kfree(conf);
490 return err;
491}
492
493/*
494 * Shutdown WAN device.
495 * o delete all not opened logical channels for this device
496 * o call driver's shutdown() entry point
497 */
498
499static int wanrouter_device_shutdown(struct wan_device *wandev)
500{
501 struct net_device *dev;
502 int err=0;
503
504 if (wandev->state == WAN_UNCONFIGURED)
505 return 0;
506
507 printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
508
509 for (dev = wandev->dev; dev;) {
510 err = wanrouter_delete_interface(wandev, dev->name);
511 if (err)
512 return err;
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
516 * next element */
517 dev = wandev->dev;
518 }
519
520 if (wandev->ndev)
521 return -EBUSY; /* there are opened interfaces */
522
523 if (wandev->shutdown)
524 err=wandev->shutdown(wandev);
525
526 return err;
527}
528
529/*
530 * Get WAN device status & statistics.
531 */
532
533static int wanrouter_device_stat(struct wan_device *wandev,
534 wandev_stat_t __user *u_stat)
535{
536 wandev_stat_t stat;
537
538 memset(&stat, 0, sizeof(stat));
539
540 /* Ask device driver to update device statistics */
541 if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
542 wandev->update(wandev);
543
544 /* Fill out structure */
545 stat.ndev = wandev->ndev;
546 stat.state = wandev->state;
547
548 if (copy_to_user(u_stat, &stat, sizeof(stat)))
549 return -EFAULT;
550
551 return 0;
552}
553
554/*
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
562 */
563
564static int wanrouter_device_new_if(struct wan_device *wandev,
565 wanif_conf_t __user *u_conf)
566{
567 wanif_conf_t *cnf;
568 struct net_device *dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int err;
570
571 if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
572 return -ENODEV;
573
574 cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
575 if (!cnf)
576 return -ENOBUFS;
577
578 err = -EFAULT;
579 if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
580 goto out;
581
582 err = -EINVAL;
583 if (cnf->magic != ROUTER_MAGIC)
584 goto out;
585
586 if (cnf->config_id == WANCONFIG_MPPP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
588 wandev->name);
589 err = -EPROTONOSUPPORT;
590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 } else {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700592 dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 err = -ENOBUFS;
594 if (dev == NULL)
595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 err = wandev->new_if(wandev, dev, cnf);
597 }
598
599 if (!err) {
600 /* Register network interface. This will invoke init()
601 * function supplied by the driver. If device registered
602 * successfully, add it to the interface list.
603 */
604
605 if (dev->name == NULL) {
606 err = -EINVAL;
607 } else {
608
609 #ifdef WANDEBUG
610 printk(KERN_INFO "%s: registering interface %s...\n",
611 wanrouter_modname, dev->name);
612 #endif
613
614 err = register_netdev(dev);
615 if (!err) {
616 struct net_device *slave = NULL;
617 unsigned long smp_flags=0;
618
619 lock_adapter_irq(&wandev->lock, &smp_flags);
620
621 if (wandev->dev == NULL) {
622 wandev->dev = dev;
623 } else {
624 for (slave=wandev->dev;
625 *((struct net_device **)slave->priv);
626 slave = *((struct net_device **)slave->priv));
627
628 *((struct net_device **)slave->priv) = dev;
629 }
630 ++wandev->ndev;
631
632 unlock_adapter_irq(&wandev->lock, &smp_flags);
633 err = 0; /* done !!! */
634 goto out;
635 }
636 }
637 if (wandev->del_if)
638 wandev->del_if(wandev, dev);
639 }
640
641 /* This code has moved from del_if() function */
Jesper Juhla51482b2005-11-08 09:41:34 -0800642 kfree(dev->priv);
643 dev->priv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Sync PPP is disabled */
646 if (cnf->config_id != WANCONFIG_MPPP)
647 kfree(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648out:
649 kfree(cnf);
650 return err;
651}
652
653
654/*
655 * Delete WAN logical channel.
656 * o verify user address space
657 * o copy configuration data to kernel address space
658 */
659
660static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
661{
662 char name[WAN_IFNAME_SZ + 1];
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900663 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 if (wandev->state == WAN_UNCONFIGURED)
666 return -ENODEV;
667
668 memset(name, 0, sizeof(name));
669
670 if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
671 return -EFAULT;
672
673 err = wanrouter_delete_interface(wandev, name);
674 if (err)
675 return err;
676
677 /* If last interface being deleted, shutdown card
678 * This helps with administration at leaf nodes
679 * (You can tell if the person at the other end of the phone
680 * has an interface configured) and avoids DoS vulnerabilities
681 * in binary driver files - this fixes a problem with the current
682 * Sangoma driver going into strange states when all the network
683 * interfaces are deleted and the link irrecoverably disconnected.
684 */
685
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900686 if (!wandev->ndev && wandev->shutdown)
687 err = wandev->shutdown(wandev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 return err;
690}
691
692/*
693 * Miscellaneous Functions
694 */
695
696/*
697 * Find WAN device by name.
698 * Return pointer to the WAN device data space or NULL if device not found.
699 */
700
701static struct wan_device *wanrouter_find_device(char *name)
702{
703 struct wan_device *wandev;
704
705 for (wandev = wanrouter_router_devlist;
706 wandev && strcmp(wandev->name, name);
707 wandev = wandev->next);
708 return wandev;
709}
710
711/*
712 * Delete WAN logical channel identified by its name.
713 * o find logical channel by its name
714 * o call driver's del_if() entry point
715 * o unregister network interface
716 * o unlink channel data space from linked list of channels
717 * o release channel data space
718 *
719 * Return: 0 success
720 * -ENODEV channel not found.
721 * -EBUSY interface is open
722 *
723 * Note: If (force != 0), then device will be destroyed even if interface
724 * associated with it is open. It's caller's responsibility to make
725 * sure that opened interfaces are not removed!
726 */
727
728static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
729{
730 struct net_device *dev = NULL, *prev = NULL;
731 unsigned long smp_flags=0;
732
733 lock_adapter_irq(&wandev->lock, &smp_flags);
734 dev = wandev->dev;
735 prev = NULL;
736 while (dev && strcmp(name, dev->name)) {
737 struct net_device **slave = dev->priv;
738 prev = dev;
739 dev = *slave;
740 }
741 unlock_adapter_irq(&wandev->lock, &smp_flags);
742
743 if (dev == NULL)
744 return -ENODEV; /* interface not found */
745
746 if (netif_running(dev))
747 return -EBUSY; /* interface in use */
748
749 if (wandev->del_if)
750 wandev->del_if(wandev, dev);
751
752 lock_adapter_irq(&wandev->lock, &smp_flags);
753 if (prev) {
754 struct net_device **prev_slave = prev->priv;
755 struct net_device **slave = dev->priv;
756
757 *prev_slave = *slave;
758 } else {
759 struct net_device **slave = dev->priv;
760 wandev->dev = *slave;
761 }
762 --wandev->ndev;
763 unlock_adapter_irq(&wandev->lock, &smp_flags);
764
765 printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
766
767 /* Due to new interface linking method using dev->priv,
768 * this code has moved from del_if() function.*/
Jesper Juhla51482b2005-11-08 09:41:34 -0800769 kfree(dev->priv);
770 dev->priv=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 unregister_netdev(dev);
773
774 free_netdev(dev);
775
776 return 0;
777}
778
Adrian Bunk97353cb2007-02-05 18:07:27 -0800779static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
YOSHIFUJI Hideaki4ba61222007-02-09 23:25:25 +0900781 spin_lock_irqsave(lock, *smp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784
Adrian Bunk97353cb2007-02-05 18:07:27 -0800785static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 spin_unlock_irqrestore(lock, *smp_flags);
788}
789
790EXPORT_SYMBOL(register_wan_device);
791EXPORT_SYMBOL(unregister_wan_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793MODULE_LICENSE("GPL");
794
795/*
796 * End
797 */