wanmain.c 22 KB

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