wanmain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. #include <net/syncppp.h>
  58. #define KMEM_SAFETYZONE 8
  59. /*
  60. * Function Prototypes
  61. */
  62. /*
  63. * WAN device IOCTL handlers
  64. */
  65. static int wanrouter_device_setup(struct wan_device *wandev,
  66. wandev_conf_t __user *u_conf);
  67. static int wanrouter_device_stat(struct wan_device *wandev,
  68. wandev_stat_t __user *u_stat);
  69. static int wanrouter_device_shutdown(struct wan_device *wandev);
  70. static int wanrouter_device_new_if(struct wan_device *wandev,
  71. wanif_conf_t __user *u_conf);
  72. static int wanrouter_device_del_if(struct wan_device *wandev,
  73. char __user *u_name);
  74. /*
  75. * Miscellaneous
  76. */
  77. static struct wan_device *wanrouter_find_device(char *name);
  78. static int wanrouter_delete_interface(struct wan_device *wandev, char *name);
  79. static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  80. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags);
  81. /*
  82. * Global Data
  83. */
  84. static char wanrouter_fullname[] = "Sangoma WANPIPE Router";
  85. static char wanrouter_copyright[] = "(c) 1995-2000 Sangoma Technologies Inc.";
  86. static char wanrouter_modname[] = ROUTER_NAME; /* short module name */
  87. struct wan_device* wanrouter_router_devlist; /* list of registered devices */
  88. /*
  89. * Organize Unique Identifiers for encapsulation/decapsulation
  90. */
  91. #if 0
  92. static unsigned char wanrouter_oui_ether[] = { 0x00, 0x00, 0x00 };
  93. static unsigned char wanrouter_oui_802_2[] = { 0x00, 0x80, 0xC2 };
  94. #endif
  95. static int __init wanrouter_init(void)
  96. {
  97. int err;
  98. printk(KERN_INFO "%s v%u.%u %s\n",
  99. wanrouter_fullname, ROUTER_VERSION, ROUTER_RELEASE,
  100. wanrouter_copyright);
  101. err = wanrouter_proc_init();
  102. if (err)
  103. printk(KERN_INFO "%s: can't create entry in proc filesystem!\n",
  104. wanrouter_modname);
  105. return err;
  106. }
  107. static void __exit wanrouter_cleanup (void)
  108. {
  109. wanrouter_proc_cleanup();
  110. }
  111. /*
  112. * This is just plain dumb. We should move the bugger to drivers/net/wan,
  113. * slap it first in directory and make it module_init(). The only reason
  114. * for subsys_initcall() here is that net goes after drivers (why, BTW?)
  115. */
  116. subsys_initcall(wanrouter_init);
  117. module_exit(wanrouter_cleanup);
  118. /*
  119. * Kernel APIs
  120. */
  121. /*
  122. * Register WAN device.
  123. * o verify device credentials
  124. * o create an entry for the device in the /proc/net/router directory
  125. * o initialize internally maintained fields of the wan_device structure
  126. * o link device data space to a singly-linked list
  127. * o if it's the first device, then start kernel 'thread'
  128. * o increment module use count
  129. *
  130. * Return:
  131. * 0 Ok
  132. * < 0 error.
  133. *
  134. * Context: process
  135. */
  136. int register_wan_device(struct wan_device *wandev)
  137. {
  138. int err, namelen;
  139. if ((wandev == NULL) || (wandev->magic != ROUTER_MAGIC) ||
  140. (wandev->name == NULL))
  141. return -EINVAL;
  142. namelen = strlen(wandev->name);
  143. if (!namelen || (namelen > WAN_DRVNAME_SZ))
  144. return -EINVAL;
  145. if (wanrouter_find_device(wandev->name))
  146. return -EEXIST;
  147. #ifdef WANDEBUG
  148. printk(KERN_INFO "%s: registering WAN device %s\n",
  149. wanrouter_modname, wandev->name);
  150. #endif
  151. /*
  152. * Register /proc directory entry
  153. */
  154. err = wanrouter_proc_add(wandev);
  155. if (err) {
  156. printk(KERN_INFO
  157. "%s: can't create /proc/net/router/%s entry!\n",
  158. wanrouter_modname, wandev->name);
  159. return err;
  160. }
  161. /*
  162. * Initialize fields of the wan_device structure maintained by the
  163. * router and update local data.
  164. */
  165. wandev->ndev = 0;
  166. wandev->dev = NULL;
  167. wandev->next = wanrouter_router_devlist;
  168. wanrouter_router_devlist = wandev;
  169. return 0;
  170. }
  171. /*
  172. * Unregister WAN device.
  173. * o shut down device
  174. * o unlink device data space from the linked list
  175. * o delete device entry in the /proc/net/router directory
  176. * o decrement module use count
  177. *
  178. * Return: 0 Ok
  179. * <0 error.
  180. * Context: process
  181. */
  182. int unregister_wan_device(char *name)
  183. {
  184. struct wan_device *wandev, *prev;
  185. if (name == NULL)
  186. return -EINVAL;
  187. for (wandev = wanrouter_router_devlist, prev = NULL;
  188. wandev && strcmp(wandev->name, name);
  189. prev = wandev, wandev = wandev->next)
  190. ;
  191. if (wandev == NULL)
  192. return -ENODEV;
  193. #ifdef WANDEBUG
  194. printk(KERN_INFO "%s: unregistering WAN device %s\n",
  195. wanrouter_modname, name);
  196. #endif
  197. if (wandev->state != WAN_UNCONFIGURED)
  198. wanrouter_device_shutdown(wandev);
  199. if (prev)
  200. prev->next = wandev->next;
  201. else
  202. wanrouter_router_devlist = wandev->next;
  203. wanrouter_proc_delete(wandev);
  204. return 0;
  205. }
  206. #if 0
  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. skb_copy_to_linear_data_offset(skb, 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_reset_mac_header(skb);
  286. return ethertype;
  287. }
  288. #endif /* 0 */
  289. /*
  290. * WAN device IOCTL.
  291. * o find WAN device associated with this node
  292. * o execute requested action or pass command to the device driver
  293. */
  294. int wanrouter_ioctl(struct inode *inode, struct file *file,
  295. unsigned int cmd, unsigned long arg)
  296. {
  297. int err = 0;
  298. struct proc_dir_entry *dent;
  299. struct wan_device *wandev;
  300. void __user *data = (void __user *)arg;
  301. if (!capable(CAP_NET_ADMIN))
  302. return -EPERM;
  303. if ((cmd >> 8) != ROUTER_IOCTL)
  304. return -EINVAL;
  305. dent = PDE(inode);
  306. if ((dent == NULL) || (dent->data == NULL))
  307. return -EINVAL;
  308. wandev = dent->data;
  309. if (wandev->magic != ROUTER_MAGIC)
  310. return -EINVAL;
  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. return err;
  337. }
  338. /*
  339. * WAN Driver IOCTL Handlers
  340. */
  341. /*
  342. * Setup WAN link device.
  343. * o verify user address space
  344. * o allocate kernel memory and copy configuration data to kernel space
  345. * o if configuration data includes extension, copy it to kernel space too
  346. * o call driver's setup() entry point
  347. */
  348. static int wanrouter_device_setup(struct wan_device *wandev,
  349. wandev_conf_t __user *u_conf)
  350. {
  351. void *data = NULL;
  352. wandev_conf_t *conf;
  353. int err = -EINVAL;
  354. if (wandev->setup == NULL) { /* Nothing to do ? */
  355. printk(KERN_INFO "%s: ERROR, No setup script: wandev->setup()\n",
  356. wandev->name);
  357. return 0;
  358. }
  359. conf = kmalloc(sizeof(wandev_conf_t), GFP_KERNEL);
  360. if (conf == NULL){
  361. printk(KERN_INFO "%s: ERROR, Failed to allocate kernel memory !\n",
  362. wandev->name);
  363. return -ENOBUFS;
  364. }
  365. if (copy_from_user(conf, u_conf, sizeof(wandev_conf_t))) {
  366. printk(KERN_INFO "%s: Failed to copy user config data to kernel space!\n",
  367. wandev->name);
  368. kfree(conf);
  369. return -EFAULT;
  370. }
  371. if (conf->magic != ROUTER_MAGIC) {
  372. kfree(conf);
  373. printk(KERN_INFO "%s: ERROR, Invalid MAGIC Number\n",
  374. wandev->name);
  375. return -EINVAL;
  376. }
  377. if (conf->data_size && conf->data) {
  378. if (conf->data_size > 128000) {
  379. printk(KERN_INFO
  380. "%s: ERROR, Invalid firmware data size %i !\n",
  381. wandev->name, conf->data_size);
  382. kfree(conf);
  383. return -EINVAL;
  384. }
  385. data = vmalloc(conf->data_size);
  386. if (!data) {
  387. printk(KERN_INFO
  388. "%s: ERROR, Faild allocate kernel memory !\n",
  389. wandev->name);
  390. kfree(conf);
  391. return -ENOBUFS;
  392. }
  393. if (!copy_from_user(data, conf->data, conf->data_size)) {
  394. conf->data = data;
  395. err = wandev->setup(wandev, conf);
  396. } else {
  397. printk(KERN_INFO
  398. "%s: ERROR, Faild to copy from user data !\n",
  399. wandev->name);
  400. err = -EFAULT;
  401. }
  402. vfree(data);
  403. } else {
  404. printk(KERN_INFO
  405. "%s: ERROR, No firmware found ! Firmware size = %i !\n",
  406. wandev->name, conf->data_size);
  407. }
  408. kfree(conf);
  409. return err;
  410. }
  411. /*
  412. * Shutdown WAN device.
  413. * o delete all not opened logical channels for this device
  414. * o call driver's shutdown() entry point
  415. */
  416. static int wanrouter_device_shutdown(struct wan_device *wandev)
  417. {
  418. struct net_device *dev;
  419. int err=0;
  420. if (wandev->state == WAN_UNCONFIGURED)
  421. return 0;
  422. printk(KERN_INFO "\n%s: Shutting Down!\n",wandev->name);
  423. for (dev = wandev->dev; dev;) {
  424. err = wanrouter_delete_interface(wandev, dev->name);
  425. if (err)
  426. return err;
  427. /* The above function deallocates the current dev
  428. * structure. Therefore, we cannot use dev->priv
  429. * as the next element: wandev->dev points to the
  430. * next element */
  431. dev = wandev->dev;
  432. }
  433. if (wandev->ndev)
  434. return -EBUSY; /* there are opened interfaces */
  435. if (wandev->shutdown)
  436. err=wandev->shutdown(wandev);
  437. return err;
  438. }
  439. /*
  440. * Get WAN device status & statistics.
  441. */
  442. static int wanrouter_device_stat(struct wan_device *wandev,
  443. wandev_stat_t __user *u_stat)
  444. {
  445. wandev_stat_t stat;
  446. memset(&stat, 0, sizeof(stat));
  447. /* Ask device driver to update device statistics */
  448. if ((wandev->state != WAN_UNCONFIGURED) && wandev->update)
  449. wandev->update(wandev);
  450. /* Fill out structure */
  451. stat.ndev = wandev->ndev;
  452. stat.state = wandev->state;
  453. if (copy_to_user(u_stat, &stat, sizeof(stat)))
  454. return -EFAULT;
  455. return 0;
  456. }
  457. /*
  458. * Create new WAN interface.
  459. * o verify user address space
  460. * o copy configuration data to kernel address space
  461. * o allocate network interface data space
  462. * o call driver's new_if() entry point
  463. * o make sure there is no interface name conflict
  464. * o register network interface
  465. */
  466. static int wanrouter_device_new_if(struct wan_device *wandev,
  467. wanif_conf_t __user *u_conf)
  468. {
  469. wanif_conf_t *cnf;
  470. struct net_device *dev = NULL;
  471. #ifdef CONFIG_WANPIPE_MULTPPP
  472. struct ppp_device *pppdev=NULL;
  473. #endif
  474. int err;
  475. if ((wandev->state == WAN_UNCONFIGURED) || (wandev->new_if == NULL))
  476. return -ENODEV;
  477. cnf = kmalloc(sizeof(wanif_conf_t), GFP_KERNEL);
  478. if (!cnf)
  479. return -ENOBUFS;
  480. err = -EFAULT;
  481. if (copy_from_user(cnf, u_conf, sizeof(wanif_conf_t)))
  482. goto out;
  483. err = -EINVAL;
  484. if (cnf->magic != ROUTER_MAGIC)
  485. goto out;
  486. if (cnf->config_id == WANCONFIG_MPPP) {
  487. #ifdef CONFIG_WANPIPE_MULTPPP
  488. pppdev = kzalloc(sizeof(struct ppp_device), GFP_KERNEL);
  489. err = -ENOBUFS;
  490. if (pppdev == NULL)
  491. goto out;
  492. pppdev->dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  493. if (pppdev->dev == NULL) {
  494. kfree(pppdev);
  495. err = -ENOBUFS;
  496. goto out;
  497. }
  498. err = wandev->new_if(wandev, (struct net_device *)pppdev, cnf);
  499. dev = pppdev->dev;
  500. #else
  501. printk(KERN_INFO "%s: Wanpipe Mulit-Port PPP support has not been compiled in!\n",
  502. wandev->name);
  503. err = -EPROTONOSUPPORT;
  504. goto out;
  505. #endif
  506. } else {
  507. dev = kzalloc(sizeof(struct net_device), GFP_KERNEL);
  508. err = -ENOBUFS;
  509. if (dev == NULL)
  510. goto out;
  511. err = wandev->new_if(wandev, dev, cnf);
  512. }
  513. if (!err) {
  514. /* Register network interface. This will invoke init()
  515. * function supplied by the driver. If device registered
  516. * successfully, add it to the interface list.
  517. */
  518. if (dev->name == NULL) {
  519. err = -EINVAL;
  520. } else {
  521. #ifdef WANDEBUG
  522. printk(KERN_INFO "%s: registering interface %s...\n",
  523. wanrouter_modname, dev->name);
  524. #endif
  525. err = register_netdev(dev);
  526. if (!err) {
  527. struct net_device *slave = NULL;
  528. unsigned long smp_flags=0;
  529. lock_adapter_irq(&wandev->lock, &smp_flags);
  530. if (wandev->dev == NULL) {
  531. wandev->dev = dev;
  532. } else {
  533. for (slave=wandev->dev;
  534. *((struct net_device **)slave->priv);
  535. slave = *((struct net_device **)slave->priv));
  536. *((struct net_device **)slave->priv) = dev;
  537. }
  538. ++wandev->ndev;
  539. unlock_adapter_irq(&wandev->lock, &smp_flags);
  540. err = 0; /* done !!! */
  541. goto out;
  542. }
  543. }
  544. if (wandev->del_if)
  545. wandev->del_if(wandev, dev);
  546. }
  547. /* This code has moved from del_if() function */
  548. kfree(dev->priv);
  549. dev->priv = NULL;
  550. #ifdef CONFIG_WANPIPE_MULTPPP
  551. if (cnf->config_id == WANCONFIG_MPPP)
  552. kfree(pppdev);
  553. else
  554. kfree(dev);
  555. #else
  556. /* Sync PPP is disabled */
  557. if (cnf->config_id != WANCONFIG_MPPP)
  558. kfree(dev);
  559. #endif
  560. out:
  561. kfree(cnf);
  562. return err;
  563. }
  564. /*
  565. * Delete WAN logical channel.
  566. * o verify user address space
  567. * o copy configuration data to kernel address space
  568. */
  569. static int wanrouter_device_del_if(struct wan_device *wandev, char __user *u_name)
  570. {
  571. char name[WAN_IFNAME_SZ + 1];
  572. int err = 0;
  573. if (wandev->state == WAN_UNCONFIGURED)
  574. return -ENODEV;
  575. memset(name, 0, sizeof(name));
  576. if (copy_from_user(name, u_name, WAN_IFNAME_SZ))
  577. return -EFAULT;
  578. err = wanrouter_delete_interface(wandev, name);
  579. if (err)
  580. return err;
  581. /* If last interface being deleted, shutdown card
  582. * This helps with administration at leaf nodes
  583. * (You can tell if the person at the other end of the phone
  584. * has an interface configured) and avoids DoS vulnerabilities
  585. * in binary driver files - this fixes a problem with the current
  586. * Sangoma driver going into strange states when all the network
  587. * interfaces are deleted and the link irrecoverably disconnected.
  588. */
  589. if (!wandev->ndev && wandev->shutdown)
  590. err = wandev->shutdown(wandev);
  591. return err;
  592. }
  593. /*
  594. * Miscellaneous Functions
  595. */
  596. /*
  597. * Find WAN device by name.
  598. * Return pointer to the WAN device data space or NULL if device not found.
  599. */
  600. static struct wan_device *wanrouter_find_device(char *name)
  601. {
  602. struct wan_device *wandev;
  603. for (wandev = wanrouter_router_devlist;
  604. wandev && strcmp(wandev->name, name);
  605. wandev = wandev->next);
  606. return wandev;
  607. }
  608. /*
  609. * Delete WAN logical channel identified by its name.
  610. * o find logical channel by its name
  611. * o call driver's del_if() entry point
  612. * o unregister network interface
  613. * o unlink channel data space from linked list of channels
  614. * o release channel data space
  615. *
  616. * Return: 0 success
  617. * -ENODEV channel not found.
  618. * -EBUSY interface is open
  619. *
  620. * Note: If (force != 0), then device will be destroyed even if interface
  621. * associated with it is open. It's caller's responsibility to make
  622. * sure that opened interfaces are not removed!
  623. */
  624. static int wanrouter_delete_interface(struct wan_device *wandev, char *name)
  625. {
  626. struct net_device *dev = NULL, *prev = NULL;
  627. unsigned long smp_flags=0;
  628. lock_adapter_irq(&wandev->lock, &smp_flags);
  629. dev = wandev->dev;
  630. prev = NULL;
  631. while (dev && strcmp(name, dev->name)) {
  632. struct net_device **slave = dev->priv;
  633. prev = dev;
  634. dev = *slave;
  635. }
  636. unlock_adapter_irq(&wandev->lock, &smp_flags);
  637. if (dev == NULL)
  638. return -ENODEV; /* interface not found */
  639. if (netif_running(dev))
  640. return -EBUSY; /* interface in use */
  641. if (wandev->del_if)
  642. wandev->del_if(wandev, dev);
  643. lock_adapter_irq(&wandev->lock, &smp_flags);
  644. if (prev) {
  645. struct net_device **prev_slave = prev->priv;
  646. struct net_device **slave = dev->priv;
  647. *prev_slave = *slave;
  648. } else {
  649. struct net_device **slave = dev->priv;
  650. wandev->dev = *slave;
  651. }
  652. --wandev->ndev;
  653. unlock_adapter_irq(&wandev->lock, &smp_flags);
  654. printk(KERN_INFO "%s: unregistering '%s'\n", wandev->name, dev->name);
  655. /* Due to new interface linking method using dev->priv,
  656. * this code has moved from del_if() function.*/
  657. kfree(dev->priv);
  658. dev->priv=NULL;
  659. unregister_netdev(dev);
  660. free_netdev(dev);
  661. return 0;
  662. }
  663. static void lock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  664. {
  665. spin_lock_irqsave(lock, *smp_flags);
  666. }
  667. static void unlock_adapter_irq(spinlock_t *lock, unsigned long *smp_flags)
  668. {
  669. spin_unlock_irqrestore(lock, *smp_flags);
  670. }
  671. EXPORT_SYMBOL(register_wan_device);
  672. EXPORT_SYMBOL(unregister_wan_device);
  673. MODULE_LICENSE("GPL");
  674. /*
  675. * End
  676. */