wanmain.c 19 KB

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