wanmain.c 19 KB

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