wanmain.c 22 KB

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