wanmain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*****************************************************************************
  2. * wanmain.c WAN Multiprotocol Router Module. Main code.
  3. *
  4. * This module is completely hardware-independent and provides
  5. * the following common services for the WAN Link Drivers:
  6. * o WAN device management (registering, unregistering)
  7. * o Network interface management
  8. * o Physical connection management (dial-up, incoming calls)
  9. * o Logical connection management (switched virtual circuits)
  10. * o Protocol encapsulation/decapsulation
  11. *
  12. * Author: Gideon Hack
  13. *
  14. * Copyright: (c) 1995-1999 Sangoma Technologies Inc.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License
  18. * as published by the Free Software Foundation; either version
  19. * 2 of the License, or (at your option) any later version.
  20. * ============================================================================
  21. * Nov 24, 2000 Nenad Corbic Updated for 2.4.X kernels
  22. * Nov 07, 2000 Nenad Corbic Fixed the Mulit-Port PPP for kernels 2.2.16 and
  23. * greater.
  24. * Aug 2, 2000 Nenad Corbic Block the Multi-Port PPP from running on
  25. * kernels 2.2.16 or greater. The SyncPPP
  26. * has changed.
  27. * Jul 13, 2000 Nenad Corbic Added SyncPPP support
  28. * Added extra debugging in device_setup().
  29. * Oct 01, 1999 Gideon Hack Update for s514 PCI card
  30. * Dec 27, 1996 Gene Kozin Initial version (based on Sangoma's WANPIPE)
  31. * Jan 16, 1997 Gene Kozin router_devlist made public
  32. * Jan 31, 1997 Alan Cox Hacked it about a bit for 2.1
  33. * Jun 27, 1997 Alan Cox realigned with vendor code
  34. * Oct 15, 1997 Farhan Thawar changed wan_encapsulate to add a pad byte of 0
  35. * Apr 20, 1998 Alan Cox Fixed 2.1 symbols
  36. * May 17, 1998 K. Baranowski Fixed SNAP encapsulation in wan_encapsulate
  37. * Dec 15, 1998 Arnaldo Melo support for firmwares of up to 128000 bytes
  38. * check wandev->setup return value
  39. * Dec 22, 1998 Arnaldo Melo vmalloc/vfree used in device_setup to allocate
  40. * kernel memory and copy configuration data to
  41. * kernel space (for big firmwares)
  42. * Jun 02, 1999 Gideon Hack Updates for Linux 2.0.X and 2.2.X kernels.
  43. *****************************************************************************/
  44. #include <linux/stddef.h> /* offsetof(), etc. */
  45. #include <linux/capability.h>
  46. #include <linux/errno.h> /* return codes */
  47. #include <linux/kernel.h>
  48. #include <linux/module.h> /* support for loadable modules */
  49. #include <linux/slab.h> /* kmalloc(), kfree() */
  50. #include <linux/mm.h>
  51. #include <linux/string.h> /* inline mem*, str* functions */
  52. #include <asm/byteorder.h> /* htons(), etc. */
  53. #include <linux/wanrouter.h> /* WAN router API definitions */
  54. #include <linux/vmalloc.h> /* vmalloc, vfree */
  55. #include <asm/uaccess.h> /* copy_to/from_user */
  56. #include <linux/init.h> /* __initfunc et al. */
  57. #define KMEM_SAFETYZONE 8
  58. #define DEV_TO_SLAVE(dev) (*((struct net_device **)netdev_priv(dev)))
  59. /*
  60. * Function Prototypes
  61. */
  62. /*
  63. * WAN device IOCTL handlers
  64. */
  65. static int wanrouter_device_setup(struct wan_device *wandev,
  66. wandev_conf_t __user *u_conf);
  67. static int wanrouter_device_stat(struct wan_device *wandev,
  68. wandev_stat_t __user *u_stat);
  69. static int wanrouter_device_shutdown(struct wan_device *wandev);
  70. static int wanrouter_device_new_if(struct wan_device *wandev,
  71. wanif_conf_t __user *u_conf);
  72. static int wanrouter_device_del_if(struct wan_device *wandev,
  73. char __user *u_name);
  74. /*
  75. * Miscellaneous
  76. */
  77. static struct wan_device *wanrouter_find_device(char *name);
  78. static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
  79. static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  80. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  81. /*
  82. * Global Data
  83. */
  84. static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
  85. static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
  86. static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
  87. struct wan_device* wanrouter_router_devlist; /* list of registered devices */
  88. /*
  89. * Organize Unique Identifiers for encapsulation/decapsulation
  90. */
  91. #if 0
  92. static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
  93. static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
  94. #endif
  95. static int __init wanrouter_init(void)
  96. {
  97. int err;
  98. printk(KERN_INFO "%s v%u.%u %s\n",
  99. wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
  100. wanrouter_copyright);
  101. err = wanrouter_proc_init();
  102. if (err)
  103. printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
  104. wanrouter_modname);
  105. return err;
  106. }
  107. static void __exit wanrouter_cleanup (void)
  108. {
  109. wanrouter_proc_cleanup();
  110. }
  111. /*
  112. * This is just plain dumb. We should move the bugger to drivers/net/wan,
  113. * slap it first in directory and make it module_init(). The only reason
  114. * for subsys_initcall() here is that net goes after drivers (why, BTW?)
  115. */
  116. subsys_initcall(wanrouter_init);
  117. module_exit(wanrouter_cleanup);
  118. /*
  119. * Kernel APIs
  120. */
  121. /*
  122. * Register WAN device.
  123. * o verify device credentials
  124. * o create an entry for the device in the /proc/net/router directory
  125. * o initialize internally maintained fields of the wan_device structure
  126. * o link device data space to a singly-linked list
  127. * o if it's the first device, then start kernel 'thread'
  128. * o increment module use count
  129. *
  130. * Return:
  131. * 0 Ok
  132. * < 0 error.
  133. *
  134. * Context: process
  135. */
  136. int register_wan_device(struct wan_device *wandev)
  137. {
  138. int err, namelen;
  139. if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
  140. (wandev->name == NULL))
  141. return -EINVAL;
  142. namelen = strlen(wandev->name);
  143. if (!namelen || (namelen > WAN_DRVNAME_SZ))
  144. return -EINVAL;
  145. if (wanrouter_find_device(wandev->name))
  146. return -EEXIST;
  147. #ifdef WANDEBUG
  148. printk(KERN_INFO "%s: registering WAN device %s\n",
  149. wanrouter_modname, wandev->name);
  150. #endif
  151. /*
  152. * Register /proc directory entry
  153. */
  154. err = wanrouter_proc_add(wandev);
  155. if (err) {
  156. printk(KERN_INFO
  157. "%s: can't create /proc/net/router/%s entry!\n",
  158. wanrouter_modname, wandev->name);
  159. return err;
  160. }
  161. /*
  162. * Initialize fields of the wan_device structure maintained by the
  163. * router and update local data.
  164. */
  165. wandev->ndev = 0;
  166. wandev->dev = NULL;
  167. wandev->next = wanrouter_router_devlist;
  168. wanrouter_router_devlist = wandev;
  169. return 0;
  170. }
  171. /*
  172. * Unregister WAN device.
  173. * o shut down device
  174. * o unlink device data space from the linked list
  175. * o delete device entry in the /proc/net/router directory
  176. * o decrement module use count
  177. *
  178. * Return: 0 Ok
  179. * <0 error.
  180. * Context: process
  181. */
  182. int unregister_wan_device(char *name)
  183. {
  184. struct wan_device *wandev, *prev;
  185. if (name == NULL)
  186. return -EINVAL;
  187. for (wandev = wanrouter_router_devlist, prev = NULL;
  188. wandev && strcmp(wandev->name, name);
  189. prev = wandev, wandev = wandev->next)
  190. ;
  191. if (wandev == NULL)
  192. return -ENODEV;
  193. #ifdef WANDEBUG
  194. printk(KERN_INFO "%s: unregistering WAN device %s\n",
  195. wanrouter_modname, name);
  196. #endif
  197. if (wandev->state != WAN_UNCONFIGURED)
  198. wanrouter_device_shutdown(wandev);
  199. if (prev)
  200. prev->next = wandev->next;
  201. else
  202. wanrouter_router_devlist = wandev->next;
  203. wanrouter_proc_delete(wandev);
  204. return 0;
  205. }
  206. #if 0
  207. /*
  208. * Encapsulate packet.
  209. *
  210. * Return: encapsulation header size
  211. * < 0 - unsupported Ethertype
  212. *
  213. * Notes:
  214. * 1. This function may be called on interrupt context.
  215. */
  216. int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
  217. unsigned short type)
  218. {
  219. int hdr_len = 0;
  220. switch (type) {
  221. case ETH_P_IP: /* IP datagram encapsulation */
  222. hdr_len += 1;
  223. skb_push(skb, 1);
  224. skb->data[0] = NLPID_IP;
  225. break;
  226. case ETH_P_IPX: /* SNAP encapsulation */
  227. case ETH_P_ARP:
  228. hdr_len += 7;
  229. skb_push(skb, 7);
  230. skb->data[0] = 0;
  231. skb->data[1] = NLPID_SNAP;
  232. skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
  233. sizeof(wanrouter_oui_ether));
  234. *((unsigned short*)&skb->data[5]) = htons(type);
  235. break;
  236. default: /* Unknown packet type */
  237. printk(KERN_INFO
  238. "%s: unsupported Ethertype 0x%04X on interface %s!\n",
  239. wanrouter_modname, type, dev->name);
  240. hdr_len = -EINVAL;
  241. }
  242. return hdr_len;
  243. }
  244. /*
  245. * Decapsulate packet.
  246. *
  247. * Return: Ethertype (in network order)
  248. * 0 unknown encapsulation
  249. *
  250. * Notes:
  251. * 1. This function may be called on interrupt context.
  252. */
  253. __be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
  254. {
  255. int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
  256. __be16 ethertype;
  257. switch (skb->data[cnt]) {
  258. case NLPID_IP: /* IP datagramm */
  259. ethertype = htons(ETH_P_IP);
  260. cnt += 1;
  261. break;
  262. case NLPID_SNAP: /* SNAP encapsulation */
  263. if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
  264. sizeof(wanrouter_oui_ether))){
  265. printk(KERN_INFO
  266. "%s: unsupported SNAP OUI %02X-%02X-%02X "
  267. "on interface %s!\n", wanrouter_modname,
  268. skb->data[cnt+1], skb->data[cnt+2],
  269. skb->data[cnt+3], dev->name);
  270. return 0;
  271. }
  272. ethertype = *((__be16*)&skb->data[cnt+4]);
  273. cnt += 6;
  274. break;
  275. /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
  276. default:
  277. printk(KERN_INFO
  278. "%s: unsupported NLPID 0x%02X on interface %s!\n",
  279. wanrouter_modname, skb->data[cnt], dev->name);
  280. return 0;
  281. }
  282. skb->protocol = ethertype;
  283. skb->pkt_type = PACKET_HOST; /* Physically point to point */
  284. skb_pull(skb, cnt);
  285. skb_reset_mac_header(skb);
  286. return ethertype;
  287. }
  288. #endif /* 0 */
  289. /*
  290. * WAN device IOCTL.
  291. * o find WAN device associated with this node
  292. * o execute requested action or pass command to the device driver
  293. */
  294. long wanrouter_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  295. {
  296. struct inode *inode = file->f_path.dentry->d_inode;
  297. int err = 0;
  298. struct proc_dir_entry *dent;
  299. struct wan_device *wandev;
  300. void __user *data = (void __user *)arg;
  301. if (!capable(CAP_NET_ADMIN))
  302. return -EPERM;
  303. if ((cmd >> 8) != ROUTER_IOCTL)
  304. return -EINVAL;
  305. dent = PDE(inode);
  306. if ((dent == NULL) || (dent->data == NULL))
  307. return -EINVAL;
  308. wandev = dent->data;
  309. if (wandev->magic != ROUTER_MAGIC)
  310. return -EINVAL;
  311. lock_kernel();
  312. switch (cmd) {
  313. case ROUTER_SETUP:
  314. err = wanrouter_device_setup(wandev, data);
  315. break;
  316. case ROUTER_DOWN:
  317. err = wanrouter_device_shutdown(wandev);
  318. break;
  319. case ROUTER_STAT:
  320. err = wanrouter_device_stat(wandev, data);
  321. break;
  322. case ROUTER_IFNEW:
  323. err = wanrouter_device_new_if(wandev, data);
  324. break;
  325. case ROUTER_IFDEL:
  326. err = wanrouter_device_del_if(wandev, data);
  327. break;
  328. case ROUTER_IFSTAT:
  329. break;
  330. default:
  331. if ((cmd >= ROUTER_USER) &&
  332. (cmd <= ROUTER_USER_MAX) &&
  333. wandev->ioctl)
  334. err = wandev->ioctl(wandev, cmd, arg);
  335. else err = -EINVAL;
  336. }
  337. unlock_kernel();
  338. return err;
  339. }
  340. /*
  341. * WAN Driver IOCTL Handlers
  342. */
  343. /*
  344. * Setup WAN link device.
  345. * o verify user address space
  346. * o allocate kernel memory and copy configuration data to kernel space
  347. * o if configuration data includes extension, copy it to kernel space too
  348. * o call driver's setup() entry point
  349. */
  350. static int wanrouter_device_setup(struct wan_device *wandev,
  351. wandev_conf_t __user *u_conf)
  352. {
  353. void *data = NULL;
  354. wandev_conf_t *conf;
  355. int err = -EINVAL;
  356. if (wandev->setup == NULL) { /* Nothing to do ? */
  357. printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
  358. wandev->name);
  359. return 0;
  360. }
  361. conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
  362. if (conf == NULL){
  363. printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
  364. wandev->name);
  365. return -ENOBUFS;
  366. }
  367. if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
  368. printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
  369. wandev->name);
  370. kfree(conf);
  371. return -EFAULT;
  372. }
  373. if (conf->magic != ROUTER_MAGIC) {
  374. kfree(conf);
  375. printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
  376. wandev->name);
  377. return -EINVAL;
  378. }
  379. if (conf->data_size && conf->data) {
  380. if (conf->data_size > 128000) {
  381. printk(KERN_INFO
  382. "%s: ERROR, Invalid firmware data size %i !\n",
  383. wandev->name, conf->data_size);
  384. kfree(conf);
  385. return -EINVAL;
  386. }
  387. data = vmalloc(conf->data_size);
  388. if (!data) {
  389. printk(KERN_INFO
  390. "%s: ERROR, Faild allocate kernel memory !\n",
  391. wandev->name);
  392. kfree(conf);
  393. return -ENOBUFS;
  394. }
  395. if (!copy_from_user(data, conf->data, conf->data_size)) {
  396. conf->data = data;
  397. err = wandev->setup(wandev, conf);
  398. } else {
  399. printk(KERN_INFO
  400. "%s: ERROR, Faild to copy from user data !\n",
  401. wandev->name);
  402. err = -EFAULT;
  403. }
  404. vfree(data);
  405. } else {
  406. printk(KERN_INFO
  407. "%s: ERROR, No firmware found ! Firmware size = %i !\n",
  408. wandev->name, conf->data_size);
  409. }
  410. kfree(conf);
  411. return err;
  412. }
  413. /*
  414. * Shutdown WAN device.
  415. * o delete all not opened logical channels for this device
  416. * o call driver's shutdown() entry point
  417. */
  418. static int wanrouter_device_shutdown(struct wan_device *wandev)
  419. {
  420. struct net_device *dev;
  421. int err=0;
  422. if (wandev->state == WAN_UNCONFIGURED)
  423. return 0;
  424. printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
  425. for (dev = wandev->dev; dev;) {
  426. err = wanrouter_delete_interface(wandev, dev->name);
  427. if (err)
  428. return err;
  429. /* The above function deallocates the current dev
  430. * structure. Therefore, we cannot use netdev_priv(dev)
  431. * as the next element: wandev->dev points to the
  432. * next element */
  433. dev = wandev->dev;
  434. }
  435. if (wandev->ndev)
  436. return -EBUSY; /* there are opened interfaces */
  437. if (wandev->shutdown)
  438. err=wandev->shutdown(wandev);
  439. return err;
  440. }
  441. /*
  442. * Get WAN device status & statistics.
  443. */
  444. static int wanrouter_device_stat(struct wan_device *wandev,
  445. wandev_stat_t __user *u_stat)
  446. {
  447. wandev_stat_t stat;
  448. memset(&stat, 0, sizeof(stat));
  449. /* Ask device driver to update device statistics */
  450. if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
  451. wandev->update(wandev);
  452. /* Fill out structure */
  453. stat.ndev = wandev->ndev;
  454. stat.state = wandev->state;
  455. if (copy_to_user(u_stat, &stat, sizeof(stat)))
  456. return -EFAULT;
  457. return 0;
  458. }
  459. /*
  460. * Create new WAN interface.
  461. * o verify user address space
  462. * o copy configuration data to kernel address space
  463. * o allocate network interface data space
  464. * o call driver's new_if() entry point
  465. * o make sure there is no interface name conflict
  466. * o register network interface
  467. */
  468. static int wanrouter_device_new_if(struct wan_device *wandev,
  469. wanif_conf_t __user *u_conf)
  470. {
  471. wanif_conf_t *cnf;
  472. struct net_device *dev = NULL;
  473. int err;
  474. if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
  475. return -ENODEV;
  476. cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
  477. if (!cnf)
  478. return -ENOBUFS;
  479. err = -EFAULT;
  480. if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
  481. goto out;
  482. err = -EINVAL;
  483. if (cnf->magic != ROUTER_MAGIC)
  484. goto out;
  485. if (cnf->config_id == WANCONFIG_MPPP) {
  486. printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
  487. wandev->name);
  488. err = -EPROTONOSUPPORT;
  489. goto out;
  490. } else {
  491. err = wandev->new_if(wandev, dev, cnf);
  492. }
  493. if (!err) {
  494. /* Register network interface. This will invoke init()
  495. * function supplied by the driver. If device registered
  496. * successfully, add it to the interface list.
  497. */
  498. if (dev->name == NULL) {
  499. err = -EINVAL;
  500. } else {
  501. #ifdef WANDEBUG
  502. printk(KERN_INFO "%s: registering interface %s...\n",
  503. wanrouter_modname, dev->name);
  504. #endif
  505. err = register_netdev(dev);
  506. if (!err) {
  507. struct net_device *slave = NULL;
  508. unsigned long smp_flags=0;
  509. lock_adapter_irq(&wandev->lock, &smp_flags);
  510. if (wandev->dev == NULL) {
  511. wandev->dev = dev;
  512. } else {
  513. for (slave=wandev->dev;
  514. DEV_TO_SLAVE(slave);
  515. slave = DEV_TO_SLAVE(slave))
  516. DEV_TO_SLAVE(slave) = dev;
  517. }
  518. ++wandev->ndev;
  519. unlock_adapter_irq(&wandev->lock, &smp_flags);
  520. err = 0; /* done !!! */
  521. goto out;
  522. }
  523. }
  524. if (wandev->del_if)
  525. wandev->del_if(wandev, dev);
  526. free_netdev(dev);
  527. }
  528. out:
  529. kfree(cnf);
  530. return err;
  531. }
  532. /*
  533. * Delete WAN logical channel.
  534. * o verify user address space
  535. * o copy configuration data to kernel address space
  536. */
  537. static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
  538. {
  539. char name[WAN_IFNAME_SZ + 1];
  540. int err = 0;
  541. if (wandev->state == WAN_UNCONFIGURED)
  542. return -ENODEV;
  543. memset(name, 0, sizeof(name));
  544. if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
  545. return -EFAULT;
  546. err = wanrouter_delete_interface(wandev, name);
  547. if (err)
  548. return err;
  549. /* If last interface being deleted, shutdown card
  550. * This helps with administration at leaf nodes
  551. * (You can tell if the person at the other end of the phone
  552. * has an interface configured) and avoids DoS vulnerabilities
  553. * in binary driver files - this fixes a problem with the current
  554. * Sangoma driver going into strange states when all the network
  555. * interfaces are deleted and the link irrecoverably disconnected.
  556. */
  557. if (!wandev->ndev && wandev->shutdown)
  558. err = wandev->shutdown(wandev);
  559. return err;
  560. }
  561. /*
  562. * Miscellaneous Functions
  563. */
  564. /*
  565. * Find WAN device by name.
  566. * Return pointer to the WAN device data space or NULL if device not found.
  567. */
  568. static struct wan_device *wanrouter_find_device(char *name)
  569. {
  570. struct wan_device *wandev;
  571. for (wandev = wanrouter_router_devlist;
  572. wandev && strcmp(wandev->name, name);
  573. wandev = wandev->next);
  574. return wandev;
  575. }
  576. /*
  577. * Delete WAN logical channel identified by its name.
  578. * o find logical channel by its name
  579. * o call driver's del_if() entry point
  580. * o unregister network interface
  581. * o unlink channel data space from linked list of channels
  582. * o release channel data space
  583. *
  584. * Return: 0 success
  585. * -ENODEV channel not found.
  586. * -EBUSY interface is open
  587. *
  588. * Note: If (force != 0), then device will be destroyed even if interface
  589. * associated with it is open. It's caller's responsibility to make
  590. * sure that opened interfaces are not removed!
  591. */
  592. static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
  593. {
  594. struct net_device *dev = NULL, *prev = NULL;
  595. unsigned long smp_flags=0;
  596. lock_adapter_irq(&wandev->lock, &smp_flags);
  597. dev = wandev->dev;
  598. prev = NULL;
  599. while (dev && strcmp(name, dev->name)) {
  600. struct net_device **slave = netdev_priv(dev);
  601. prev = dev;
  602. dev = *slave;
  603. }
  604. unlock_adapter_irq(&wandev->lock, &smp_flags);
  605. if (dev == NULL)
  606. return -ENODEV; /* interface not found */
  607. if (netif_running(dev))
  608. return -EBUSY; /* interface in use */
  609. if (wandev->del_if)
  610. wandev->del_if(wandev, dev);
  611. lock_adapter_irq(&wandev->lock, &smp_flags);
  612. if (prev) {
  613. struct net_device **prev_slave = netdev_priv(prev);
  614. struct net_device **slave = netdev_priv(dev);
  615. *prev_slave = *slave;
  616. } else {
  617. struct net_device **slave = netdev_priv(dev);
  618. wandev->dev = *slave;
  619. }
  620. --wandev->ndev;
  621. unlock_adapter_irq(&wandev->lock, &smp_flags);
  622. printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
  623. unregister_netdev(dev);
  624. free_netdev(dev);
  625. return 0;
  626. }
  627. static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  628. {
  629. spin_lock_irqsave(lock, *smp_flags);
  630. }
  631. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  632. {
  633. spin_unlock_irqrestore(lock, *smp_flags);
  634. }
  635. EXPORT_SYMBOL(register_wan_device);
  636. EXPORT_SYMBOL(unregister_wan_device);
  637. MODULE_LICENSE("GPL");
  638. /*
  639. * End
  640. */