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