ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / wanrouter / wanmain.c
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 managenment (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/config.h>
46 #include <linux/stddef.h>       /* offsetof(), etc. */
47 #include <linux/errno.h>        /* return codes */
48 #include <linux/kernel.h>
49 #include <linux/init.h>
50 #include <linux/module.h>       /* support for loadable modules */
51 #include <linux/slab.h> /* kmalloc(), kfree() */
52 #include <linux/mm.h>           /* verify_area(), etc. */
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 #include <net/syncppp.h>
62
63 #define KMEM_SAFETYZONE 8
64
65 /***********FOR DEBUGGING PURPOSES*********************************************
66 static void * dbg_kmalloc(unsigned int size, int prio, int line) {
67         int i = 0;
68         void * v = kmalloc(size+sizeof(unsigned int)+2*KMEM_SAFETYZONE*8,prio);
69         char * c1 = v;
70         c1 += sizeof(unsigned int);
71         *((unsigned int *)v) = size;
72
73         for (i = 0; i < KMEM_SAFETYZONE; i++) {
74                 c1[0] = 'D'; c1[1] = 'E'; c1[2] = 'A'; c1[3] = 'D';
75                 c1[4] = 'B'; c1[5] = 'E'; c1[6] = 'E'; c1[7] = 'F';
76                 c1 += 8;
77         }
78         c1 += size;
79         for (i = 0; i < KMEM_SAFETYZONE; i++) {
80                 c1[0] = 'M'; c1[1] = 'U'; c1[2] = 'N'; c1[3] = 'G';
81                 c1[4] = 'W'; c1[5] = 'A'; c1[6] = 'L'; c1[7] = 'L';
82                 c1 += 8;
83         }
84         v = ((char *)v) + sizeof(unsigned int) + KMEM_SAFETYZONE*8;
85         printk(KERN_INFO "line %d  kmalloc(%d,%d) = %p\n",line,size,prio,v);
86         return v;
87 }
88 static void dbg_kfree(void * v, int line) {
89         unsigned int * sp = (unsigned int *)(((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8));
90         unsigned int size = *sp;
91         char * c1 = ((char *)v) - KMEM_SAFETYZONE*8;
92         int i = 0;
93         for (i = 0; i < KMEM_SAFETYZONE; i++) {
94                 if (   c1[0] != 'D' || c1[1] != 'E' || c1[2] != 'A' || c1[3] != 'D'
95                     || c1[4] != 'B' || c1[5] != 'E' || c1[6] != 'E' || c1[7] != 'F') {
96                         printk(KERN_INFO "kmalloced block at %p has been corrupted (underrun)!\n",v);
97                         printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
98                                         c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
99                 }
100                 c1 += 8;
101         }
102         c1 += size;
103         for (i = 0; i < KMEM_SAFETYZONE; i++) {
104                 if (   c1[0] != 'M' || c1[1] != 'U' || c1[2] != 'N' || c1[3] != 'G'
105                     || c1[4] != 'W' || c1[5] != 'A' || c1[6] != 'L' || c1[7] != 'L'
106                    ) {
107                         printk(KERN_INFO "kmalloced block at %p has been corrupted (overrun):\n",v);
108                         printk(KERN_INFO " %4x: %2x %2x %2x %2x %2x %2x %2x %2x\n", i*8,
109                                         c1[0],c1[1],c1[2],c1[3],c1[4],c1[5],c1[6],c1[7] );
110                 }
111                 c1 += 8;
112         }
113         printk(KERN_INFO "line %d  kfree(%p)\n",line,v);
114         v = ((char *)v) - (sizeof(unsigned int) + KMEM_SAFETYZONE*8);
115         kfree(v);
116 }
117
118 #define kmalloc(x,y) dbg_kmalloc(x,y,__LINE__)
119 #define kfree(x) dbg_kfree(x,__LINE__)
120 *****************************************************************************/
121
122 /*
123  *      Function Prototypes
124  */
125
126 /*
127  *      WAN device IOCTL handlers
128  */
129
130 static int wanrouter_device_setup(struct wan_device *wandev,
131                                   wandev_conf_t *u_conf);
132 static int wanrouter_device_stat(struct wan_device *wandev,
133                                  wandev_stat_t *u_stat);
134 static int wanrouter_device_shutdown(struct wan_device *wandev);
135 static int wanrouter_device_new_if(struct wan_device *wandev,
136                                    wanif_conf_t *u_conf);
137 static int wanrouter_device_del_if(struct wan_device *wandev, char *u_name);
138
139 /*
140  *      Miscellaneous
141  */
142
143 static struct wan_device *wanrouter_find_device(char *name);
144 static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
145 void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
146 void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
147
148
149
150 /*
151  *      Global Data
152  */
153
154 static char wanrouter_fullname[]  = "Sangoma WANPIPE Router";
155 static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
156 static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
157 struct wan_device* wanrouter_router_devlist; /* list of registered devices */
158
159 /*
160  *      Organize Unique Identifiers for encapsulation/decapsulation
161  */
162
163 static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
164 #if 0
165 static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
166 #endif
167
168 static int __init wanrouter_init(void)
169 {
170         int err;
171
172         printk(KERN_INFO "%s v%u.%u %s\n",
173                wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
174                wanrouter_copyright);
175
176         err = wanrouter_proc_init();
177         if (err)
178                 printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
179                        wanrouter_modname);
180
181         return err;
182 }
183
184 static void __exit wanrouter_cleanup (void)
185 {
186         wanrouter_proc_cleanup();
187 }
188
189 /*
190  * This is just plain dumb.  We should move the bugger to drivers/net/wan,
191  * slap it first in directory and make it module_init().  The only reason
192  * for subsys_initcall() here is that net goes after drivers (why, BTW?)
193  */
194 subsys_initcall(wanrouter_init);
195 module_exit(wanrouter_cleanup);
196
197 /*
198  *      Kernel APIs
199  */
200
201 /*
202  *      Register WAN device.
203  *      o verify device credentials
204  *      o create an entry for the device in the /proc/net/router directory
205  *      o initialize internally maintained fields of the wan_device structure
206  *      o link device data space to a singly-linked list
207  *      o if it's the first device, then start kernel 'thread'
208  *      o increment module use count
209  *
210  *      Return:
211  *      0       Ok
212  *      < 0     error.
213  *
214  *      Context:        process
215  */
216
217
218 int register_wan_device(struct wan_device *wandev)
219 {
220         int err, namelen;
221
222         if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
223             (wandev->name == NULL))
224                 return -EINVAL;
225
226         namelen = strlen(wandev->name);
227         if (!namelen || (namelen > WAN_DRVNAME_SZ))
228                 return -EINVAL;
229
230         if (wanrouter_find_device(wandev->name))
231                 return -EEXIST;
232
233 #ifdef WANDEBUG
234         printk(KERN_INFO "%s: registering WAN device %s\n",
235                wanrouter_modname, wandev->name);
236 #endif
237
238         /*
239          *      Register /proc directory entry
240          */
241         err = wanrouter_proc_add(wandev);
242         if (err) {
243                 printk(KERN_INFO
244                         "%s: can't create /proc/net/router/%s entry!\n",
245                         wanrouter_modname, wandev->name);
246                 return err;
247         }
248
249         /*
250          *      Initialize fields of the wan_device structure maintained by the
251          *      router and update local data.
252          */
253
254         wandev->ndev = 0;
255         wandev->dev  = NULL;
256         wandev->next = wanrouter_router_devlist;
257         wanrouter_router_devlist = wandev;
258         return 0;
259 }
260
261 /*
262  *      Unregister WAN device.
263  *      o shut down device
264  *      o unlink device data space from the linked list
265  *      o delete device entry in the /proc/net/router directory
266  *      o decrement module use count
267  *
268  *      Return:         0       Ok
269  *                      <0      error.
270  *      Context:        process
271  */
272
273
274 int unregister_wan_device(char *name)
275 {
276         struct wan_device *wandev, *prev;
277
278         if (name == NULL)
279                 return -EINVAL;
280
281         for (wandev = wanrouter_router_devlist, prev = NULL;
282                 wandev && strcmp(wandev->name, name);
283                 prev = wandev, wandev = wandev->next)
284                 ;
285         if (wandev == NULL)
286                 return -ENODEV;
287
288 #ifdef WANDEBUG
289         printk(KERN_INFO "%s: unregistering WAN device %s\n",
290                wanrouter_modname, name);
291 #endif
292
293         if (wandev->state != WAN_UNCONFIGURED)
294                 wanrouter_device_shutdown(wandev);
295
296         if (prev)
297                 prev->next = wandev->next;
298         else
299                 wanrouter_router_devlist = wandev->next;
300
301         wanrouter_proc_delete(wandev);
302         return 0;
303 }
304
305 /*
306  *      Encapsulate packet.
307  *
308  *      Return: encapsulation header size
309  *              < 0     - unsupported Ethertype
310  *
311  *      Notes:
312  *      1. This function may be called on interrupt context.
313  */
314
315
316 int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
317                           unsigned short type)
318 {
319         int hdr_len = 0;
320
321         switch (type) {
322         case ETH_P_IP:          /* IP datagram encapsulation */
323                 hdr_len += 1;
324                 skb_push(skb, 1);
325                 skb->data[0] = NLPID_IP;
326                 break;
327
328         case ETH_P_IPX:         /* SNAP encapsulation */
329         case ETH_P_ARP:
330                 hdr_len += 7;
331                 skb_push(skb, 7);
332                 skb->data[0] = 0;
333                 skb->data[1] = NLPID_SNAP;
334                 memcpy(&skb->data[2], wanrouter_oui_ether,
335                        sizeof(wanrouter_oui_ether));
336                 *((unsigned short*)&skb->data[5]) = htons(type);
337                 break;
338
339         default:                /* Unknown packet type */
340                 printk(KERN_INFO
341                         "%s: unsupported Ethertype 0x%04X on interface %s!\n",
342                         wanrouter_modname, type, dev->name);
343                 hdr_len = -EINVAL;
344         }
345         return hdr_len;
346 }
347
348
349 /*
350  *      Decapsulate packet.
351  *
352  *      Return: Ethertype (in network order)
353  *                      0       unknown encapsulation
354  *
355  *      Notes:
356  *      1. This function may be called on interrupt context.
357  */
358
359
360 unsigned short wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
361 {
362         int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
363         unsigned short ethertype;
364
365         switch (skb->data[cnt]) {
366         case NLPID_IP:          /* IP datagramm */
367                 ethertype = htons(ETH_P_IP);
368                 cnt += 1;
369                 break;
370
371         case NLPID_SNAP:        /* SNAP encapsulation */
372                 if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
373                            sizeof(wanrouter_oui_ether))){
374                         printk(KERN_INFO
375                                 "%s: unsupported SNAP OUI %02X-%02X-%02X "
376                                 "on interface %s!\n", wanrouter_modname,
377                                 skb->data[cnt+1], skb->data[cnt+2],
378                                 skb->data[cnt+3], dev->name);
379                         return 0;
380                 }
381                 ethertype = *((unsigned short*)&skb->data[cnt+4]);
382                 cnt += 6;
383                 break;
384
385         /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
386
387         default:
388                 printk(KERN_INFO
389                         "%s: unsupported NLPID 0x%02X on interface %s!\n",
390                         wanrouter_modname, skb->data[cnt], dev->name);
391                 return 0;
392         }
393         skb->protocol = ethertype;
394         skb->pkt_type = PACKET_HOST;    /*      Physically point to point */
395         skb_pull(skb, cnt);
396         skb->mac.raw  = skb->data;
397         return ethertype;
398 }
399
400
401 /*
402  *      WAN device IOCTL.
403  *      o find WAN device associated with this node
404  *      o execute requested action or pass command to the device driver
405  */
406
407 int wanrouter_ioctl(struct inode *inode, struct file *file,
408                 unsigned int cmd, unsigned long arg)
409 {
410         int err = 0;
411         struct proc_dir_entry *dent;
412         struct wan_device *wandev;
413
414         if (!capable(CAP_NET_ADMIN))
415                 return -EPERM;
416
417         if ((cmd >> 8) != ROUTER_IOCTL)
418                 return -EINVAL;
419
420         dent = PDE(inode);
421         if ((dent == NULL) || (dent->data == NULL))
422                 return -EINVAL;
423
424         wandev = dent->data;
425         if (wandev->magic != ROUTER_MAGIC)
426                 return -EINVAL;
427
428         switch (cmd) {
429         case ROUTER_SETUP:
430                 err = wanrouter_device_setup(wandev, (void*)arg);
431                 break;
432
433         case ROUTER_DOWN:
434                 err = wanrouter_device_shutdown(wandev);
435                 break;
436
437         case ROUTER_STAT:
438                 err = wanrouter_device_stat(wandev, (void*)arg);
439                 break;
440
441         case ROUTER_IFNEW:
442                 err = wanrouter_device_new_if(wandev, (void*)arg);
443                 break;
444
445         case ROUTER_IFDEL:
446                 err = wanrouter_device_del_if(wandev, (void*)arg);
447                 break;
448
449         case ROUTER_IFSTAT:
450                 break;
451
452         default:
453                 if ((cmd >= ROUTER_USER) &&
454                     (cmd <= ROUTER_USER_MAX) &&
455                     wandev->ioctl)
456                         err = wandev->ioctl(wandev, cmd, arg);
457                 else err = -EINVAL;
458         }
459         return err;
460 }
461
462 /*
463  *      WAN Driver IOCTL Handlers
464  */
465
466 /*
467  *      Setup WAN link device.
468  *      o verify user address space
469  *      o allocate kernel memory and copy configuration data to kernel space
470  *      o if configuration data includes extension, copy it to kernel space too
471  *      o call driver's setup() entry point
472  */
473
474 static int wanrouter_device_setup(struct wan_device *wandev,
475                                   wandev_conf_t *u_conf)
476 {
477         void *data = NULL;
478         wandev_conf_t *conf;
479         int err = -EINVAL;
480
481         if (wandev->setup == NULL) {    /* Nothing to do ? */
482                 printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
483                                 wandev->name);
484                 return 0;
485         }
486
487         conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
488         if (conf == NULL){
489                 printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
490                                 wandev->name);
491                 return -ENOBUFS;
492         }
493
494         if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
495                 printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
496                                 wandev->name);
497                 kfree(conf);
498                 return -EFAULT;
499         }
500
501         if (conf->magic != ROUTER_MAGIC) {
502                 kfree(conf);
503                 printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
504                                 wandev->name);
505                 return -EINVAL;
506         }
507
508         if (conf->data_size && conf->data) {
509                 if (conf->data_size > 128000 || conf->data_size < 0) {
510                         printk(KERN_INFO
511                             "%s: ERROR, Invalid firmware data size %i !\n",
512                                         wandev->name, conf->data_size);
513                         kfree(conf);
514                         return -EINVAL;
515                 }
516
517                 data = vmalloc(conf->data_size);
518                 if (!data) {
519                         printk(KERN_INFO
520                                 "%s: ERROR, Faild allocate kernel memory !\n",
521                                 wandev->name);
522                         kfree(conf);
523                         return -ENOBUFS;
524                 }
525                 if (!copy_from_user(data, conf->data, conf->data_size)) {
526                         conf->data = data;
527                         err = wandev->setup(wandev, conf);
528                 } else {
529                         printk(KERN_INFO
530                              "%s: ERROR, Faild to copy from user data !\n",
531                                wandev->name);
532                         err = -EFAULT;
533                 }
534                 vfree(data);
535         } else {
536                 printk(KERN_INFO
537                     "%s: ERROR, No firmware found ! Firmware size = %i !\n",
538                                 wandev->name, conf->data_size);
539         }
540
541         kfree(conf);
542         return err;
543 }
544
545 /*
546  *      Shutdown WAN device.
547  *      o delete all not opened logical channels for this device
548  *      o call driver's shutdown() entry point
549  */
550
551 static int wanrouter_device_shutdown(struct wan_device *wandev)
552 {
553         struct net_device *dev;
554         int err=0;
555
556         if (wandev->state == WAN_UNCONFIGURED)
557                 return 0;
558
559         printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
560
561         for (dev = wandev->dev; dev;) {
562                 err = wanrouter_delete_interface(wandev, dev->name);
563                 if (err)
564                         return err;
565                 /* The above function deallocates the current dev
566                  * structure. Therefore, we cannot use dev->priv
567                  * as the next element: wandev->dev points to the
568                  * next element */
569                 dev = wandev->dev;
570         }
571
572         if (wandev->ndev)
573                 return -EBUSY;  /* there are opened interfaces  */
574
575         if (wandev->shutdown)
576                 err=wandev->shutdown(wandev);
577
578         return err;
579 }
580
581 /*
582  *      Get WAN device status & statistics.
583  */
584
585 static int wanrouter_device_stat(struct wan_device *wandev,
586                                  wandev_stat_t *u_stat)
587 {
588         wandev_stat_t stat;
589
590         memset(&stat, 0, sizeof(stat));
591
592         /* Ask device driver to update device statistics */
593         if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
594                 wandev->update(wandev);
595
596         /* Fill out structure */
597         stat.ndev  = wandev->ndev;
598         stat.state = wandev->state;
599
600         if (copy_to_user(u_stat, &stat, sizeof(stat)))
601                 return -EFAULT;
602
603         return 0;
604 }
605
606 /*
607  *      Create new WAN interface.
608  *      o verify user address space
609  *      o copy configuration data to kernel address space
610  *      o allocate network interface data space
611  *      o call driver's new_if() entry point
612  *      o make sure there is no interface name conflict
613  *      o register network interface
614  */
615
616 static int wanrouter_device_new_if(struct wan_device *wandev,
617                                    wanif_conf_t *u_conf)
618 {
619         wanif_conf_t *cnf;
620         struct net_device *dev = NULL;
621 #ifdef CONFIG_WANPIPE_MULTPPP
622         struct ppp_device *pppdev=NULL;
623 #endif
624         int err;
625
626         if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
627                 return -ENODEV;
628
629         cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
630         if (!cnf)
631                 return -ENOBUFS;
632
633         err = -EFAULT;
634         if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
635                 goto out;
636
637         err = -EINVAL;
638         if (cnf->magic != ROUTER_MAGIC)
639                 goto out;
640
641         if (cnf->config_id == WANCONFIG_MPPP) {
642 #ifdef CONFIG_WANPIPE_MULTPPP
643                 pppdev = kmalloc(sizeof(struct ppp_device), GFP_KERNEL);
644                 err = -ENOBUFS;
645                 if (pppdev == NULL)
646                         goto out;
647                 memset(pppdev, 0, sizeof(struct ppp_device));
648                 pppdev->dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
649                 if (pppdev->dev == NULL) {
650                         kfree(pppdev);
651                         err = -ENOBUFS;
652                         goto out;
653                 }
654                 memset(pppdev->dev, 0, sizeof(struct net_device));
655                 err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
656                 dev = pppdev->dev;
657 #else
658                 printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
659                                 wandev->name);
660                 err = -EPROTONOSUPPORT;
661                 goto out;
662 #endif
663         } else {
664                 dev = kmalloc(sizeof(struct net_device), GFP_KERNEL);
665                 err = -ENOBUFS;
666                 if (dev == NULL)
667                         goto out;
668                 memset(dev, 0, sizeof(struct net_device));
669                 err = wandev->new_if(wandev, dev, cnf);
670         }
671
672         if (!err) {
673                 /* Register network interface. This will invoke init()
674                  * function supplied by the driver.  If device registered
675                  * successfully, add it to the interface list.
676                  */
677
678                 if (dev->name == NULL) {
679                         err = -EINVAL;
680                 } else {
681
682                         #ifdef WANDEBUG
683                         printk(KERN_INFO "%s: registering interface %s...\n",
684                                 wanrouter_modname, dev->name);
685                         #endif
686
687                         err = register_netdev(dev);
688                         if (!err) {
689                                 struct net_device *slave = NULL;
690                                 unsigned long smp_flags=0;
691
692                                 lock_adapter_irq(&wandev->lock, &smp_flags);
693
694                                 if (wandev->dev == NULL) {
695                                         wandev->dev = dev;
696                                 } else {
697                                         for (slave=wandev->dev;
698                                          *((struct net_device **)slave->priv);
699                                  slave = *((struct net_device **)slave->priv));
700
701                                      *((struct net_device **)slave->priv) = dev;
702                                 }
703                                 ++wandev->ndev;
704
705                                 unlock_adapter_irq(&wandev->lock, &smp_flags);
706                                 err = 0;        /* done !!! */
707                                 goto out;
708                         }
709                 }
710                 if (wandev->del_if)
711                         wandev->del_if(wandev, dev);
712         }
713
714         /* This code has moved from del_if() function */
715         if (dev->priv) {
716                 kfree(dev->priv);
717                 dev->priv = NULL;
718         }
719
720 #ifdef CONFIG_WANPIPE_MULTPPP
721         if (cnf->config_id == WANCONFIG_MPPP)
722                 kfree(pppdev);
723         else
724                 kfree(dev);
725 #else
726         /* Sync PPP is disabled */
727         if (cnf->config_id != WANCONFIG_MPPP)
728                 kfree(dev);
729 #endif
730
731 out:
732         kfree(cnf);
733         return err;
734 }
735
736
737 /*
738  *      Delete WAN logical channel.
739  *       o verify user address space
740  *       o copy configuration data to kernel address space
741  */
742
743 static int wanrouter_device_del_if(struct wan_device *wandev, char *u_name)
744 {
745         char name[WAN_IFNAME_SZ + 1];
746         int err = 0;
747
748         if (wandev->state == WAN_UNCONFIGURED)
749                 return -ENODEV;
750
751         memset(name, 0, sizeof(name));
752
753         if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
754                 return -EFAULT;
755
756         err = wanrouter_delete_interface(wandev, name);
757         if (err)
758                 return err;
759
760         /* If last interface being deleted, shutdown card
761          * This helps with administration at leaf nodes
762          * (You can tell if the person at the other end of the phone
763          * has an interface configured) and avoids DoS vulnerabilities
764          * in binary driver files - this fixes a problem with the current
765          * Sangoma driver going into strange states when all the network
766          * interfaces are deleted and the link irrecoverably disconnected.
767          */
768
769         if (!wandev->ndev && wandev->shutdown)
770                 err = wandev->shutdown(wandev);
771
772         return err;
773 }
774
775 /*
776  *      Miscellaneous Functions
777  */
778
779 /*
780  *      Find WAN device by name.
781  *      Return pointer to the WAN device data space or NULL if device not found.
782  */
783
784 static struct wan_device *wanrouter_find_device(char *name)
785 {
786         struct wan_device *wandev;
787
788         for (wandev = wanrouter_router_devlist;
789              wandev && strcmp(wandev->name, name);
790                 wandev = wandev->next);
791         return wandev;
792 }
793
794 /*
795  *      Delete WAN logical channel identified by its name.
796  *      o find logical channel by its name
797  *      o call driver's del_if() entry point
798  *      o unregister network interface
799  *      o unlink channel data space from linked list of channels
800  *      o release channel data space
801  *
802  *      Return: 0               success
803  *              -ENODEV         channel not found.
804  *              -EBUSY          interface is open
805  *
806  *      Note: If (force != 0), then device will be destroyed even if interface
807  *      associated with it is open. It's caller's responsibility to make
808  *      sure that opened interfaces are not removed!
809  */
810
811 static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
812 {
813         struct net_device *dev = NULL, *prev = NULL;
814         unsigned long smp_flags=0;
815
816         lock_adapter_irq(&wandev->lock, &smp_flags);
817         dev = wandev->dev;
818         prev = NULL;
819         while (dev && strcmp(name, dev->name)) {
820                 struct net_device **slave = dev->priv;
821                 prev = dev;
822                 dev = *slave;
823         }
824         unlock_adapter_irq(&wandev->lock, &smp_flags);
825
826         if (dev == NULL)
827                 return -ENODEV; /* interface not found */
828
829         if (netif_running(dev))
830                 return -EBUSY;  /* interface in use */
831
832         if (wandev->del_if)
833                 wandev->del_if(wandev, dev);
834
835         lock_adapter_irq(&wandev->lock, &smp_flags);
836         if (prev) {
837                 struct net_device **prev_slave = prev->priv;
838                 struct net_device **slave = dev->priv;
839
840                 *prev_slave = *slave;
841         } else {
842                 struct net_device **slave = dev->priv;
843                 wandev->dev = *slave;
844         }
845         --wandev->ndev;
846         unlock_adapter_irq(&wandev->lock, &smp_flags);
847
848         printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
849
850         /* Due to new interface linking method using dev->priv,
851          * this code has moved from del_if() function.*/
852         if (dev->priv){
853                 kfree(dev->priv);
854                 dev->priv=NULL;
855         }
856
857         unregister_netdev(dev);
858
859         free_netdev(dev);
860
861         return 0;
862 }
863
864 void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
865 {
866         spin_lock_irqsave(lock, *smp_flags);
867 }
868
869
870 void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
871 {
872         spin_unlock_irqrestore(lock, *smp_flags);
873 }
874
875 EXPORT_SYMBOL(register_wan_device);
876 EXPORT_SYMBOL(unregister_wan_device);
877 EXPORT_SYMBOL(wanrouter_encapsulate);
878 EXPORT_SYMBOL(wanrouter_type_trans);
879 EXPORT_SYMBOL(lock_adapter_irq);
880 EXPORT_SYMBOL(unlock_adapter_irq);
881
882 MODULE_LICENSE("GPL");
883
884 /*
885  *      End
886  */