wanmain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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/init.h>
  49. #include <linux/module.h> /* support for loadable modules */
  50. #include <linux/slab.h> /* kmalloc(), kfree() */
  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. #include <net/syncppp.h>
  59. #define KMEM_SAFETYZONE 8
  60. /*
  61. * Function Prototypes
  62. */
  63. /*
  64. * WAN device IOCTL handlers
  65. */
  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. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  82. /*
  83. * Global Data
  84. */
  85. static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
  86. static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
  87. static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
  88. struct wan_device* wanrouter_router_devlist; /* list of registered devices */
  89. /*
  90. * Organize Unique Identifiers for encapsulation/decapsulation
  91. */
  92. #if 0
  93. static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
  94. static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
  95. #endif
  96. static int __init wanrouter_init(void)
  97. {
  98. int err;
  99. printk(KERN_INFO "%s v%u.%u %s\n",
  100. wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
  101. wanrouter_copyright);
  102. err = wanrouter_proc_init();
  103. if (err)
  104. printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
  105. wanrouter_modname);
  106. return err;
  107. }
  108. static void __exit wanrouter_cleanup (void)
  109. {
  110. wanrouter_proc_cleanup();
  111. }
  112. /*
  113. * This is just plain dumb. We should move the bugger to drivers/net/wan,
  114. * slap it first in directory and make it module_init(). The only reason
  115. * for subsys_initcall() here is that net goes after drivers (why, BTW?)
  116. */
  117. subsys_initcall(wanrouter_init);
  118. module_exit(wanrouter_cleanup);
  119. /*
  120. * Kernel APIs
  121. */
  122. /*
  123. * Register WAN device.
  124. * o verify device credentials
  125. * o create an entry for the device in the /proc/net/router directory
  126. * o initialize internally maintained fields of the wan_device structure
  127. * o link device data space to a singly-linked list
  128. * o if it's the first device, then start kernel 'thread'
  129. * o increment module use count
  130. *
  131. * Return:
  132. * 0 Ok
  133. * < 0 error.
  134. *
  135. * Context: process
  136. */
  137. int register_wan_device(struct wan_device *wandev)
  138. {
  139. int err, namelen;
  140. if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
  141. (wandev->name == NULL))
  142. return -EINVAL;
  143. namelen = strlen(wandev->name);
  144. if (!namelen || (namelen > WAN_DRVNAME_SZ))
  145. return -EINVAL;
  146. if (wanrouter_find_device(wandev->name))
  147. return -EEXIST;
  148. #ifdef WANDEBUG
  149. printk(KERN_INFO "%s: registering WAN device %s\n",
  150. wanrouter_modname, wandev->name);
  151. #endif
  152. /*
  153. * Register /proc directory entry
  154. */
  155. err = wanrouter_proc_add(wandev);
  156. if (err) {
  157. printk(KERN_INFO
  158. "%s: can't create /proc/net/router/%s entry!\n",
  159. wanrouter_modname, wandev->name);
  160. return err;
  161. }
  162. /*
  163. * Initialize fields of the wan_device structure maintained by the
  164. * router and update local data.
  165. */
  166. wandev->ndev = 0;
  167. wandev->dev = NULL;
  168. wandev->next = wanrouter_router_devlist;
  169. wanrouter_router_devlist = wandev;
  170. return 0;
  171. }
  172. /*
  173. * Unregister WAN device.
  174. * o shut down device
  175. * o unlink device data space from the linked list
  176. * o delete device entry in the /proc/net/router directory
  177. * o decrement module use count
  178. *
  179. * Return: 0 Ok
  180. * <0 error.
  181. * Context: process
  182. */
  183. int unregister_wan_device(char *name)
  184. {
  185. struct wan_device *wandev, *prev;
  186. if (name == NULL)
  187. return -EINVAL;
  188. for (wandev = wanrouter_router_devlist, prev = NULL;
  189. wandev && strcmp(wandev->name, name);
  190. prev = wandev, wandev = wandev->next)
  191. ;
  192. if (wandev == NULL)
  193. return -ENODEV;
  194. #ifdef WANDEBUG
  195. printk(KERN_INFO "%s: unregistering WAN device %s\n",
  196. wanrouter_modname, name);
  197. #endif
  198. if (wandev->state != WAN_UNCONFIGURED)
  199. wanrouter_device_shutdown(wandev);
  200. if (prev)
  201. prev->next = wandev->next;
  202. else
  203. wanrouter_router_devlist = wandev->next;
  204. wanrouter_proc_delete(wandev);
  205. return 0;
  206. }
  207. #if 0
  208. /*
  209. * Encapsulate packet.
  210. *
  211. * Return: encapsulation header size
  212. * < 0 - unsupported Ethertype
  213. *
  214. * Notes:
  215. * 1. This function may be called on interrupt context.
  216. */
  217. int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
  218. unsigned short type)
  219. {
  220. int hdr_len = 0;
  221. switch (type) {
  222. case ETH_P_IP: /* IP datagram encapsulation */
  223. hdr_len += 1;
  224. skb_push(skb, 1);
  225. skb->data[0] = NLPID_IP;
  226. break;
  227. case ETH_P_IPX: /* SNAP encapsulation */
  228. case ETH_P_ARP:
  229. hdr_len += 7;
  230. skb_push(skb, 7);
  231. skb->data[0] = 0;
  232. skb->data[1] = NLPID_SNAP;
  233. skb_copy_to_linear_data_offset(skb, 2, wanrouter_oui_ether,
  234. sizeof(wanrouter_oui_ether));
  235. *((unsigned short*)&skb->data[5]) = htons(type);
  236. break;
  237. default: /* Unknown packet type */
  238. printk(KERN_INFO
  239. "%s: unsupported Ethertype 0x%04X on interface %s!\n",
  240. wanrouter_modname, type, dev->name);
  241. hdr_len = -EINVAL;
  242. }
  243. return hdr_len;
  244. }
  245. /*
  246. * Decapsulate packet.
  247. *
  248. * Return: Ethertype (in network order)
  249. * 0 unknown encapsulation
  250. *
  251. * Notes:
  252. * 1. This function may be called on interrupt context.
  253. */
  254. __be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
  255. {
  256. int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
  257. __be16 ethertype;
  258. switch (skb->data[cnt]) {
  259. case NLPID_IP: /* IP datagramm */
  260. ethertype = htons(ETH_P_IP);
  261. cnt += 1;
  262. break;
  263. case NLPID_SNAP: /* SNAP encapsulation */
  264. if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
  265. sizeof(wanrouter_oui_ether))){
  266. printk(KERN_INFO
  267. "%s: unsupported SNAP OUI %02X-%02X-%02X "
  268. "on interface %s!\n", wanrouter_modname,
  269. skb->data[cnt+1], skb->data[cnt+2],
  270. skb->data[cnt+3], dev->name);
  271. return 0;
  272. }
  273. ethertype = *((__be16*)&skb->data[cnt+4]);
  274. cnt += 6;
  275. break;
  276. /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
  277. default:
  278. printk(KERN_INFO
  279. "%s: unsupported NLPID 0x%02X on interface %s!\n",
  280. wanrouter_modname, skb->data[cnt], dev->name);
  281. return 0;
  282. }
  283. skb->protocol = ethertype;
  284. skb->pkt_type = PACKET_HOST; /* Physically point to point */
  285. skb_pull(skb, cnt);
  286. skb_reset_mac_header(skb);
  287. return ethertype;
  288. }
  289. #endif /* 0 */
  290. /*
  291. * WAN device IOCTL.
  292. * o find WAN device associated with this node
  293. * o execute requested action or pass command to the device driver
  294. */
  295. int wanrouter_ioctl(struct inode *inode, struct file *file,
  296. unsigned int cmd, unsigned long arg)
  297. {
  298. int err = 0;
  299. struct proc_dir_entry *dent;
  300. struct wan_device *wandev;
  301. void __user *data = (void __user *)arg;
  302. if (!capable(CAP_NET_ADMIN))
  303. return -EPERM;
  304. if ((cmd >> 8) != ROUTER_IOCTL)
  305. return -EINVAL;
  306. dent = PDE(inode);
  307. if ((dent == NULL) || (dent->data == NULL))
  308. return -EINVAL;
  309. wandev = dent->data;
  310. if (wandev->magic != ROUTER_MAGIC)
  311. return -EINVAL;
  312. switch (cmd) {
  313. case ROUTER_SETUP:
  314. err = wanrouter_device_setup(wandev, data);
  315. break;
  316. case ROUTER_DOWN:
  317. err = wanrouter_device_shutdown(wandev);
  318. break;
  319. case ROUTER_STAT:
  320. err = wanrouter_device_stat(wandev, data);
  321. break;
  322. case ROUTER_IFNEW:
  323. err = wanrouter_device_new_if(wandev, data);
  324. break;
  325. case ROUTER_IFDEL:
  326. err = wanrouter_device_del_if(wandev, data);
  327. break;
  328. case ROUTER_IFSTAT:
  329. break;
  330. default:
  331. if ((cmd >= ROUTER_USER) &&
  332. (cmd <= ROUTER_USER_MAX) &&
  333. wandev->ioctl)
  334. err = wandev->ioctl(wandev, cmd, arg);
  335. else err = -EINVAL;
  336. }
  337. 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. #ifdef CONFIG_WANPIPE_MULTPPP
  473. struct ppp_device *pppdev=NULL;
  474. #endif
  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. #ifdef CONFIG_WANPIPE_MULTPPP
  489. pppdev = kzalloc(sizeof(struct ppp_device), GFP_KERNEL);
  490. err = -ENOBUFS;
  491. if (pppdev == NULL)
  492. goto out;
  493. pppdev->dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  494. if (pppdev->dev == NULL) {
  495. kfree(pppdev);
  496. err = -ENOBUFS;
  497. goto out;
  498. }
  499. err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
  500. dev = pppdev->dev;
  501. #else
  502. printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
  503. wandev->name);
  504. err = -EPROTONOSUPPORT;
  505. goto out;
  506. #endif
  507. } else {
  508. dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  509. err = -ENOBUFS;
  510. if (dev == NULL)
  511. goto out;
  512. err = wandev->new_if(wandev, dev, cnf);
  513. }
  514. if (!err) {
  515. /* Register network interface. This will invoke init()
  516. * function supplied by the driver. If device registered
  517. * successfully, add it to the interface list.
  518. */
  519. if (dev->name == NULL) {
  520. err = -EINVAL;
  521. } else {
  522. #ifdef WANDEBUG
  523. printk(KERN_INFO "%s: registering interface %s...\n",
  524. wanrouter_modname, dev->name);
  525. #endif
  526. err = register_netdev(dev);
  527. if (!err) {
  528. struct net_device *slave = NULL;
  529. unsigned long smp_flags=0;
  530. lock_adapter_irq(&wandev->lock, &smp_flags);
  531. if (wandev->dev == NULL) {
  532. wandev->dev = dev;
  533. } else {
  534. for (slave=wandev->dev;
  535. *((struct net_device **)slave->priv);
  536. slave = *((struct net_device **)slave->priv));
  537. *((struct net_device **)slave->priv) = dev;
  538. }
  539. ++wandev->ndev;
  540. unlock_adapter_irq(&wandev->lock, &smp_flags);
  541. err = 0; /* done !!! */
  542. goto out;
  543. }
  544. }
  545. if (wandev->del_if)
  546. wandev->del_if(wandev, dev);
  547. }
  548. /* This code has moved from del_if() function */
  549. kfree(dev->priv);
  550. dev->priv = NULL;
  551. #ifdef CONFIG_WANPIPE_MULTPPP
  552. if (cnf->config_id == WANCONFIG_MPPP)
  553. kfree(pppdev);
  554. else
  555. kfree(dev);
  556. #else
  557. /* Sync PPP is disabled */
  558. if (cnf->config_id != WANCONFIG_MPPP)
  559. kfree(dev);
  560. #endif
  561. out:
  562. kfree(cnf);
  563. return err;
  564. }
  565. /*
  566. * Delete WAN logical channel.
  567. * o verify user address space
  568. * o copy configuration data to kernel address space
  569. */
  570. static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
  571. {
  572. char name[WAN_IFNAME_SZ + 1];
  573. int err = 0;
  574. if (wandev->state == WAN_UNCONFIGURED)
  575. return -ENODEV;
  576. memset(name, 0, sizeof(name));
  577. if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
  578. return -EFAULT;
  579. err = wanrouter_delete_interface(wandev, name);
  580. if (err)
  581. return err;
  582. /* If last interface being deleted, shutdown card
  583. * This helps with administration at leaf nodes
  584. * (You can tell if the person at the other end of the phone
  585. * has an interface configured) and avoids DoS vulnerabilities
  586. * in binary driver files - this fixes a problem with the current
  587. * Sangoma driver going into strange states when all the network
  588. * interfaces are deleted and the link irrecoverably disconnected.
  589. */
  590. if (!wandev->ndev && wandev->shutdown)
  591. err = wandev->shutdown(wandev);
  592. return err;
  593. }
  594. /*
  595. * Miscellaneous Functions
  596. */
  597. /*
  598. * Find WAN device by name.
  599. * Return pointer to the WAN device data space or NULL if device not found.
  600. */
  601. static struct wan_device *wanrouter_find_device(char *name)
  602. {
  603. struct wan_device *wandev;
  604. for (wandev = wanrouter_router_devlist;
  605. wandev && strcmp(wandev->name, name);
  606. wandev = wandev->next);
  607. return wandev;
  608. }
  609. /*
  610. * Delete WAN logical channel identified by its name.
  611. * o find logical channel by its name
  612. * o call driver's del_if() entry point
  613. * o unregister network interface
  614. * o unlink channel data space from linked list of channels
  615. * o release channel data space
  616. *
  617. * Return: 0 success
  618. * -ENODEV channel not found.
  619. * -EBUSY interface is open
  620. *
  621. * Note: If (force != 0), then device will be destroyed even if interface
  622. * associated with it is open. It's caller's responsibility to make
  623. * sure that opened interfaces are not removed!
  624. */
  625. static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
  626. {
  627. struct net_device *dev = NULL, *prev = NULL;
  628. unsigned long smp_flags=0;
  629. lock_adapter_irq(&wandev->lock, &smp_flags);
  630. dev = wandev->dev;
  631. prev = NULL;
  632. while (dev && strcmp(name, dev->name)) {
  633. struct net_device **slave = dev->priv;
  634. prev = dev;
  635. dev = *slave;
  636. }
  637. unlock_adapter_irq(&wandev->lock, &smp_flags);
  638. if (dev == NULL)
  639. return -ENODEV; /* interface not found */
  640. if (netif_running(dev))
  641. return -EBUSY; /* interface in use */
  642. if (wandev->del_if)
  643. wandev->del_if(wandev, dev);
  644. lock_adapter_irq(&wandev->lock, &smp_flags);
  645. if (prev) {
  646. struct net_device **prev_slave = prev->priv;
  647. struct net_device **slave = dev->priv;
  648. *prev_slave = *slave;
  649. } else {
  650. struct net_device **slave = dev->priv;
  651. wandev->dev = *slave;
  652. }
  653. --wandev->ndev;
  654. unlock_adapter_irq(&wandev->lock, &smp_flags);
  655. printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
  656. /* Due to new interface linking method using dev->priv,
  657. * this code has moved from del_if() function.*/
  658. kfree(dev->priv);
  659. dev->priv=NULL;
  660. unregister_netdev(dev);
  661. free_netdev(dev);
  662. return 0;
  663. }
  664. static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  665. {
  666. spin_lock_irqsave(lock, *smp_flags);
  667. }
  668. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  669. {
  670. spin_unlock_irqrestore(lock, *smp_flags);
  671. }
  672. EXPORT_SYMBOL(register_wan_device);
  673. EXPORT_SYMBOL(unregister_wan_device);
  674. MODULE_LICENSE("GPL");
  675. /*
  676. * End
  677. */