wanmain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  81. 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. static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
  93. #if 0
  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. /*
  208. * Encapsulate packet.
  209. *
  210. * Return: encapsulation header size
  211. * < 0 - unsupported Ethertype
  212. *
  213. * Notes:
  214. * 1. This function may be called on interrupt context.
  215. */
  216. int wanrouter_encapsulate(struct sk_buff *skb, struct net_device *dev,
  217. unsigned short type)
  218. {
  219. int hdr_len = 0;
  220. switch (type) {
  221. case ETH_P_IP: /* IP datagram encapsulation */
  222. hdr_len += 1;
  223. skb_push(skb, 1);
  224. skb->data[0] = NLPID_IP;
  225. break;
  226. case ETH_P_IPX: /* SNAP encapsulation */
  227. case ETH_P_ARP:
  228. hdr_len += 7;
  229. skb_push(skb, 7);
  230. skb->data[0] = 0;
  231. skb->data[1] = NLPID_SNAP;
  232. memcpy(&skb->data[2], wanrouter_oui_ether,
  233. sizeof(wanrouter_oui_ether));
  234. *((unsigned short*)&skb->data[5]) = htons(type);
  235. break;
  236. default: /* Unknown packet type */
  237. printk(KERN_INFO
  238. "%s: unsupported Ethertype 0x%04X on interface %s!\n",
  239. wanrouter_modname, type, dev->name);
  240. hdr_len = -EINVAL;
  241. }
  242. return hdr_len;
  243. }
  244. /*
  245. * Decapsulate packet.
  246. *
  247. * Return: Ethertype (in network order)
  248. * 0 unknown encapsulation
  249. *
  250. * Notes:
  251. * 1. This function may be called on interrupt context.
  252. */
  253. __be16 wanrouter_type_trans(struct sk_buff *skb, struct net_device *dev)
  254. {
  255. int cnt = skb->data[0] ? 0 : 1; /* there may be a pad present */
  256. __be16 ethertype;
  257. switch (skb->data[cnt]) {
  258. case NLPID_IP: /* IP datagramm */
  259. ethertype = htons(ETH_P_IP);
  260. cnt += 1;
  261. break;
  262. case NLPID_SNAP: /* SNAP encapsulation */
  263. if (memcmp(&skb->data[cnt + 1], wanrouter_oui_ether,
  264. sizeof(wanrouter_oui_ether))){
  265. printk(KERN_INFO
  266. "%s: unsupported SNAP OUI %02X-%02X-%02X "
  267. "on interface %s!\n", wanrouter_modname,
  268. skb->data[cnt+1], skb->data[cnt+2],
  269. skb->data[cnt+3], dev->name);
  270. return 0;
  271. }
  272. ethertype = *((__be16*)&skb->data[cnt+4]);
  273. cnt += 6;
  274. break;
  275. /* add other protocols, e.g. CLNP, ESIS, ISIS, if needed */
  276. default:
  277. printk(KERN_INFO
  278. "%s: unsupported NLPID 0x%02X on interface %s!\n",
  279. wanrouter_modname, skb->data[cnt], dev->name);
  280. return 0;
  281. }
  282. skb->protocol = ethertype;
  283. skb->pkt_type = PACKET_HOST; /* Physically point to point */
  284. skb_pull(skb, cnt);
  285. skb->mac.raw = skb->data;
  286. return ethertype;
  287. }
  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. int wanrouter_ioctl(struct inode *inode, struct file *file,
  294. unsigned int cmd, unsigned long arg)
  295. {
  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. switch (cmd) {
  311. case ROUTER_SETUP:
  312. err = wanrouter_device_setup(wandev, data);
  313. break;
  314. case ROUTER_DOWN:
  315. err = wanrouter_device_shutdown(wandev);
  316. break;
  317. case ROUTER_STAT:
  318. err = wanrouter_device_stat(wandev, data);
  319. break;
  320. case ROUTER_IFNEW:
  321. err = wanrouter_device_new_if(wandev, data);
  322. break;
  323. case ROUTER_IFDEL:
  324. err = wanrouter_device_del_if(wandev, data);
  325. break;
  326. case ROUTER_IFSTAT:
  327. break;
  328. default:
  329. if ((cmd >= ROUTER_USER) &&
  330. (cmd <= ROUTER_USER_MAX) &&
  331. wandev->ioctl)
  332. err = wandev->ioctl(wandev, cmd, arg);
  333. else err = -EINVAL;
  334. }
  335. return err;
  336. }
  337. /*
  338. * WAN Driver IOCTL Handlers
  339. */
  340. /*
  341. * Setup WAN link device.
  342. * o verify user address space
  343. * o allocate kernel memory and copy configuration data to kernel space
  344. * o if configuration data includes extension, copy it to kernel space too
  345. * o call driver's setup() entry point
  346. */
  347. static int wanrouter_device_setup(struct wan_device *wandev,
  348. wandev_conf_t __user *u_conf)
  349. {
  350. void *data = NULL;
  351. wandev_conf_t *conf;
  352. int err = -EINVAL;
  353. if (wandev->setup == NULL) { /* Nothing to do ? */
  354. printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
  355. wandev->name);
  356. return 0;
  357. }
  358. conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
  359. if (conf == NULL){
  360. printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
  361. wandev->name);
  362. return -ENOBUFS;
  363. }
  364. if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
  365. printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
  366. wandev->name);
  367. kfree(conf);
  368. return -EFAULT;
  369. }
  370. if (conf->magic != ROUTER_MAGIC) {
  371. kfree(conf);
  372. printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
  373. wandev->name);
  374. return -EINVAL;
  375. }
  376. if (conf->data_size && conf->data) {
  377. if (conf->data_size > 128000 || conf->data_size < 0) {
  378. printk(KERN_INFO
  379. "%s: ERROR, Invalid firmware data size %i !\n",
  380. wandev->name, conf->data_size);
  381. kfree(conf);
  382. return -EINVAL;
  383. }
  384. data = vmalloc(conf->data_size);
  385. if (!data) {
  386. printk(KERN_INFO
  387. "%s: ERROR, Faild allocate kernel memory !\n",
  388. wandev->name);
  389. kfree(conf);
  390. return -ENOBUFS;
  391. }
  392. if (!copy_from_user(data, conf->data, conf->data_size)) {
  393. conf->data = data;
  394. err = wandev->setup(wandev, conf);
  395. } else {
  396. printk(KERN_INFO
  397. "%s: ERROR, Faild to copy from user data !\n",
  398. wandev->name);
  399. err = -EFAULT;
  400. }
  401. vfree(data);
  402. } else {
  403. printk(KERN_INFO
  404. "%s: ERROR, No firmware found ! Firmware size = %i !\n",
  405. wandev->name, conf->data_size);
  406. }
  407. kfree(conf);
  408. return err;
  409. }
  410. /*
  411. * Shutdown WAN device.
  412. * o delete all not opened logical channels for this device
  413. * o call driver's shutdown() entry point
  414. */
  415. static int wanrouter_device_shutdown(struct wan_device *wandev)
  416. {
  417. struct net_device *dev;
  418. int err=0;
  419. if (wandev->state == WAN_UNCONFIGURED)
  420. return 0;
  421. printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
  422. for (dev = wandev->dev; dev;) {
  423. err = wanrouter_delete_interface(wandev, dev->name);
  424. if (err)
  425. return err;
  426. /* The above function deallocates the current dev
  427. * structure. Therefore, we cannot use dev->priv
  428. * as the next element: wandev->dev points to the
  429. * next element */
  430. dev = wandev->dev;
  431. }
  432. if (wandev->ndev)
  433. return -EBUSY; /* there are opened interfaces */
  434. if (wandev->shutdown)
  435. err=wandev->shutdown(wandev);
  436. return err;
  437. }
  438. /*
  439. * Get WAN device status & statistics.
  440. */
  441. static int wanrouter_device_stat(struct wan_device *wandev,
  442. wandev_stat_t __user *u_stat)
  443. {
  444. wandev_stat_t stat;
  445. memset(&stat, 0, sizeof(stat));
  446. /* Ask device driver to update device statistics */
  447. if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
  448. wandev->update(wandev);
  449. /* Fill out structure */
  450. stat.ndev = wandev->ndev;
  451. stat.state = wandev->state;
  452. if (copy_to_user(u_stat, &stat, sizeof(stat)))
  453. return -EFAULT;
  454. return 0;
  455. }
  456. /*
  457. * Create new WAN interface.
  458. * o verify user address space
  459. * o copy configuration data to kernel address space
  460. * o allocate network interface data space
  461. * o call driver's new_if() entry point
  462. * o make sure there is no interface name conflict
  463. * o register network interface
  464. */
  465. static int wanrouter_device_new_if(struct wan_device *wandev,
  466. wanif_conf_t __user *u_conf)
  467. {
  468. wanif_conf_t *cnf;
  469. struct net_device *dev = NULL;
  470. #ifdef CONFIG_WANPIPE_MULTPPP
  471. struct ppp_device *pppdev=NULL;
  472. #endif
  473. int err;
  474. if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
  475. return -ENODEV;
  476. cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
  477. if (!cnf)
  478. return -ENOBUFS;
  479. err = -EFAULT;
  480. if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
  481. goto out;
  482. err = -EINVAL;
  483. if (cnf->magic != ROUTER_MAGIC)
  484. goto out;
  485. if (cnf->config_id == WANCONFIG_MPPP) {
  486. #ifdef CONFIG_WANPIPE_MULTPPP
  487. pppdev = kzalloc(sizeof(struct ppp_device), GFP_KERNEL);
  488. err = -ENOBUFS;
  489. if (pppdev == NULL)
  490. goto out;
  491. pppdev->dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  492. if (pppdev->dev == NULL) {
  493. kfree(pppdev);
  494. err = -ENOBUFS;
  495. goto out;
  496. }
  497. err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
  498. dev = pppdev->dev;
  499. #else
  500. printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
  501. wandev->name);
  502. err = -EPROTONOSUPPORT;
  503. goto out;
  504. #endif
  505. } else {
  506. dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  507. err = -ENOBUFS;
  508. if (dev == NULL)
  509. goto out;
  510. err = wandev->new_if(wandev, dev, cnf);
  511. }
  512. if (!err) {
  513. /* Register network interface. This will invoke init()
  514. * function supplied by the driver. If device registered
  515. * successfully, add it to the interface list.
  516. */
  517. if (dev->name == NULL) {
  518. err = -EINVAL;
  519. } else {
  520. #ifdef WANDEBUG
  521. printk(KERN_INFO "%s: registering interface %s...\n",
  522. wanrouter_modname, dev->name);
  523. #endif
  524. err = register_netdev(dev);
  525. if (!err) {
  526. struct net_device *slave = NULL;
  527. unsigned long smp_flags=0;
  528. lock_adapter_irq(&wandev->lock, &smp_flags);
  529. if (wandev->dev == NULL) {
  530. wandev->dev = dev;
  531. } else {
  532. for (slave=wandev->dev;
  533. *((struct net_device **)slave->priv);
  534. slave = *((struct net_device **)slave->priv));
  535. *((struct net_device **)slave->priv) = dev;
  536. }
  537. ++wandev->ndev;
  538. unlock_adapter_irq(&wandev->lock, &smp_flags);
  539. err = 0; /* done !!! */
  540. goto out;
  541. }
  542. }
  543. if (wandev->del_if)
  544. wandev->del_if(wandev, dev);
  545. }
  546. /* This code has moved from del_if() function */
  547. kfree(dev->priv);
  548. dev->priv = NULL;
  549. #ifdef CONFIG_WANPIPE_MULTPPP
  550. if (cnf->config_id == WANCONFIG_MPPP)
  551. kfree(pppdev);
  552. else
  553. kfree(dev);
  554. #else
  555. /* Sync PPP is disabled */
  556. if (cnf->config_id != WANCONFIG_MPPP)
  557. kfree(dev);
  558. #endif
  559. out:
  560. kfree(cnf);
  561. return err;
  562. }
  563. /*
  564. * Delete WAN logical channel.
  565. * o verify user address space
  566. * o copy configuration data to kernel address space
  567. */
  568. static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
  569. {
  570. char name[WAN_IFNAME_SZ + 1];
  571. int err = 0;
  572. if (wandev->state == WAN_UNCONFIGURED)
  573. return -ENODEV;
  574. memset(name, 0, sizeof(name));
  575. if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
  576. return -EFAULT;
  577. err = wanrouter_delete_interface(wandev, name);
  578. if (err)
  579. return err;
  580. /* If last interface being deleted, shutdown card
  581. * This helps with administration at leaf nodes
  582. * (You can tell if the person at the other end of the phone
  583. * has an interface configured) and avoids DoS vulnerabilities
  584. * in binary driver files - this fixes a problem with the current
  585. * Sangoma driver going into strange states when all the network
  586. * interfaces are deleted and the link irrecoverably disconnected.
  587. */
  588. if (!wandev->ndev && wandev->shutdown)
  589. err = wandev->shutdown(wandev);
  590. return err;
  591. }
  592. /*
  593. * Miscellaneous Functions
  594. */
  595. /*
  596. * Find WAN device by name.
  597. * Return pointer to the WAN device data space or NULL if device not found.
  598. */
  599. static struct wan_device *wanrouter_find_device(char *name)
  600. {
  601. struct wan_device *wandev;
  602. for (wandev = wanrouter_router_devlist;
  603. wandev && strcmp(wandev->name, name);
  604. wandev = wandev->next);
  605. return wandev;
  606. }
  607. /*
  608. * Delete WAN logical channel identified by its name.
  609. * o find logical channel by its name
  610. * o call driver's del_if() entry point
  611. * o unregister network interface
  612. * o unlink channel data space from linked list of channels
  613. * o release channel data space
  614. *
  615. * Return: 0 success
  616. * -ENODEV channel not found.
  617. * -EBUSY interface is open
  618. *
  619. * Note: If (force != 0), then device will be destroyed even if interface
  620. * associated with it is open. It's caller's responsibility to make
  621. * sure that opened interfaces are not removed!
  622. */
  623. static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
  624. {
  625. struct net_device *dev = NULL, *prev = NULL;
  626. unsigned long smp_flags=0;
  627. lock_adapter_irq(&wandev->lock, &smp_flags);
  628. dev = wandev->dev;
  629. prev = NULL;
  630. while (dev && strcmp(name, dev->name)) {
  631. struct net_device **slave = dev->priv;
  632. prev = dev;
  633. dev = *slave;
  634. }
  635. unlock_adapter_irq(&wandev->lock, &smp_flags);
  636. if (dev == NULL)
  637. return -ENODEV; /* interface not found */
  638. if (netif_running(dev))
  639. return -EBUSY; /* interface in use */
  640. if (wandev->del_if)
  641. wandev->del_if(wandev, dev);
  642. lock_adapter_irq(&wandev->lock, &smp_flags);
  643. if (prev) {
  644. struct net_device **prev_slave = prev->priv;
  645. struct net_device **slave = dev->priv;
  646. *prev_slave = *slave;
  647. } else {
  648. struct net_device **slave = dev->priv;
  649. wandev->dev = *slave;
  650. }
  651. --wandev->ndev;
  652. unlock_adapter_irq(&wandev->lock, &smp_flags);
  653. printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
  654. /* Due to new interface linking method using dev->priv,
  655. * this code has moved from del_if() function.*/
  656. kfree(dev->priv);
  657. dev->priv=NULL;
  658. unregister_netdev(dev);
  659. free_netdev(dev);
  660. return 0;
  661. }
  662. void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  663. {
  664. spin_lock_irqsave(lock, *smp_flags);
  665. }
  666. void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  667. {
  668. spin_unlock_irqrestore(lock, *smp_flags);
  669. }
  670. EXPORT_SYMBOL(register_wan_device);
  671. EXPORT_SYMBOL(unregister_wan_device);
  672. EXPORT_SYMBOL(wanrouter_encapsulate);
  673. EXPORT_SYMBOL(wanrouter_type_trans);
  674. EXPORT_SYMBOL(lock_adapter_irq);
  675. EXPORT_SYMBOL(unlock_adapter_irq);
  676. MODULE_LICENSE("GPL");
  677. /*
  678. * End
  679. */