wanmain.c 19 KB

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