irnet_ppp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. /*
  2. * IrNET protocol module : Synchronous PPP over an IrDA socket.
  3. *
  4. * Jean II - HPL `00 - <jt@hpl.hp.com>
  5. *
  6. * This file implement the PPP interface and /dev/irnet character device.
  7. * The PPP interface hook to the ppp_generic module, handle all our
  8. * relationship to the PPP code in the kernel (and by extension to pppd),
  9. * and exchange PPP frames with this module (send/receive).
  10. * The /dev/irnet device is used primarily for 2 functions :
  11. * 1) as a stub for pppd (the ppp daemon), so that we can appropriately
  12. * generate PPP sessions (we pretend we are a tty).
  13. * 2) as a control channel (write commands, read events)
  14. */
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include "irnet_ppp.h" /* Private header */
  18. /* Please put other headers in irnet.h - Thanks */
  19. /* Generic PPP callbacks (to call us) */
  20. static const struct ppp_channel_ops irnet_ppp_ops = {
  21. .start_xmit = ppp_irnet_send,
  22. .ioctl = ppp_irnet_ioctl
  23. };
  24. /************************* CONTROL CHANNEL *************************/
  25. /*
  26. * When a pppd instance is not active on /dev/irnet, it acts as a control
  27. * channel.
  28. * Writing allow to set up the IrDA destination of the IrNET channel,
  29. * and any application may be read events happening in IrNET...
  30. */
  31. /*------------------------------------------------------------------*/
  32. /*
  33. * Write is used to send a command to configure a IrNET channel
  34. * before it is open by pppd. The syntax is : "command argument"
  35. * Currently there is only two defined commands :
  36. * o name : set the requested IrDA nickname of the IrNET peer.
  37. * o addr : set the requested IrDA address of the IrNET peer.
  38. * Note : the code is crude, but effective...
  39. */
  40. static inline ssize_t
  41. irnet_ctrl_write(irnet_socket * ap,
  42. const char __user *buf,
  43. size_t count)
  44. {
  45. char command[IRNET_MAX_COMMAND];
  46. char * start; /* Current command being processed */
  47. char * next; /* Next command to process */
  48. int length; /* Length of current command */
  49. DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
  50. /* Check for overflow... */
  51. DABORT(count >= IRNET_MAX_COMMAND, -ENOMEM,
  52. CTRL_ERROR, "Too much data !!!\n");
  53. /* Get the data in the driver */
  54. if(copy_from_user(command, buf, count))
  55. {
  56. DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
  57. return -EFAULT;
  58. }
  59. /* Safe terminate the string */
  60. command[count] = '\0';
  61. DEBUG(CTRL_INFO, "Command line received is ``%s'' (%Zd).\n",
  62. command, count);
  63. /* Check every commands in the command line */
  64. next = command;
  65. while(next != NULL)
  66. {
  67. /* Look at the next command */
  68. start = next;
  69. /* Scrap whitespaces before the command */
  70. start = skip_spaces(start);
  71. /* ',' is our command separator */
  72. next = strchr(start, ',');
  73. if(next)
  74. {
  75. *next = '\0'; /* Terminate command */
  76. length = next - start; /* Length */
  77. next++; /* Skip the '\0' */
  78. }
  79. else
  80. length = strlen(start);
  81. DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);
  82. /* Check if we recognised one of the known command
  83. * We can't use "switch" with strings, so hack with "continue" */
  84. /* First command : name -> Requested IrDA nickname */
  85. if(!strncmp(start, "name", 4))
  86. {
  87. /* Copy the name only if is included and not "any" */
  88. if((length > 5) && (strcmp(start + 5, "any")))
  89. {
  90. /* Strip out trailing whitespaces */
  91. while(isspace(start[length - 1]))
  92. length--;
  93. /* Copy the name for later reuse */
  94. memcpy(ap->rname, start + 5, length - 5);
  95. ap->rname[length - 5] = '\0';
  96. }
  97. else
  98. ap->rname[0] = '\0';
  99. DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);
  100. /* Restart the loop */
  101. continue;
  102. }
  103. /* Second command : addr, daddr -> Requested IrDA destination address
  104. * Also process : saddr -> Requested IrDA source address */
  105. if((!strncmp(start, "addr", 4)) ||
  106. (!strncmp(start, "daddr", 5)) ||
  107. (!strncmp(start, "saddr", 5)))
  108. {
  109. __u32 addr = DEV_ADDR_ANY;
  110. /* Copy the address only if is included and not "any" */
  111. if((length > 5) && (strcmp(start + 5, "any")))
  112. {
  113. char * begp = start + 5;
  114. char * endp;
  115. /* Scrap whitespaces before the command */
  116. begp = skip_spaces(begp);
  117. /* Convert argument to a number (last arg is the base) */
  118. addr = simple_strtoul(begp, &endp, 16);
  119. /* Has it worked ? (endp should be start + length) */
  120. DABORT(endp <= (start + 5), -EINVAL,
  121. CTRL_ERROR, "Invalid address.\n");
  122. }
  123. /* Which type of address ? */
  124. if(start[0] == 's')
  125. {
  126. /* Save it */
  127. ap->rsaddr = addr;
  128. DEBUG(CTRL_INFO, "Got rsaddr = %08x\n", ap->rsaddr);
  129. }
  130. else
  131. {
  132. /* Save it */
  133. ap->rdaddr = addr;
  134. DEBUG(CTRL_INFO, "Got rdaddr = %08x\n", ap->rdaddr);
  135. }
  136. /* Restart the loop */
  137. continue;
  138. }
  139. /* Other possible command : connect N (number of retries) */
  140. /* No command matched -> Failed... */
  141. DABORT(1, -EINVAL, CTRL_ERROR, "Not a recognised IrNET command.\n");
  142. }
  143. /* Success : we have parsed all commands successfully */
  144. return count;
  145. }
  146. #ifdef INITIAL_DISCOVERY
  147. /*------------------------------------------------------------------*/
  148. /*
  149. * Function irnet_get_discovery_log (self)
  150. *
  151. * Query the content on the discovery log if not done
  152. *
  153. * This function query the current content of the discovery log
  154. * at the startup of the event channel and save it in the internal struct.
  155. */
  156. static void
  157. irnet_get_discovery_log(irnet_socket * ap)
  158. {
  159. __u16 mask = irlmp_service_to_hint(S_LAN);
  160. /* Ask IrLMP for the current discovery log */
  161. ap->discoveries = irlmp_get_discoveries(&ap->disco_number, mask,
  162. DISCOVERY_DEFAULT_SLOTS);
  163. /* Check if the we got some results */
  164. if(ap->discoveries == NULL)
  165. ap->disco_number = -1;
  166. DEBUG(CTRL_INFO, "Got the log (0x%p), size is %d\n",
  167. ap->discoveries, ap->disco_number);
  168. }
  169. /*------------------------------------------------------------------*/
  170. /*
  171. * Function irnet_read_discovery_log (self, event)
  172. *
  173. * Read the content on the discovery log
  174. *
  175. * This function dump the current content of the discovery log
  176. * at the startup of the event channel.
  177. * Return 1 if wrote an event on the control channel...
  178. *
  179. * State of the ap->disco_XXX variables :
  180. * Socket creation : discoveries = NULL ; disco_index = 0 ; disco_number = 0
  181. * While reading : discoveries = ptr ; disco_index = X ; disco_number = Y
  182. * After reading : discoveries = NULL ; disco_index = Y ; disco_number = -1
  183. */
  184. static inline int
  185. irnet_read_discovery_log(irnet_socket * ap,
  186. char * event)
  187. {
  188. int done_event = 0;
  189. DENTER(CTRL_TRACE, "(ap=0x%p, event=0x%p)\n",
  190. ap, event);
  191. /* Test if we have some work to do or we have already finished */
  192. if(ap->disco_number == -1)
  193. {
  194. DEBUG(CTRL_INFO, "Already done\n");
  195. return 0;
  196. }
  197. /* Test if it's the first time and therefore we need to get the log */
  198. if(ap->discoveries == NULL)
  199. irnet_get_discovery_log(ap);
  200. /* Check if we have more item to dump */
  201. if(ap->disco_index < ap->disco_number)
  202. {
  203. /* Write an event */
  204. sprintf(event, "Found %08x (%s) behind %08x {hints %02X-%02X}\n",
  205. ap->discoveries[ap->disco_index].daddr,
  206. ap->discoveries[ap->disco_index].info,
  207. ap->discoveries[ap->disco_index].saddr,
  208. ap->discoveries[ap->disco_index].hints[0],
  209. ap->discoveries[ap->disco_index].hints[1]);
  210. DEBUG(CTRL_INFO, "Writing discovery %d : %s\n",
  211. ap->disco_index, ap->discoveries[ap->disco_index].info);
  212. /* We have an event */
  213. done_event = 1;
  214. /* Next discovery */
  215. ap->disco_index++;
  216. }
  217. /* Check if we have done the last item */
  218. if(ap->disco_index >= ap->disco_number)
  219. {
  220. /* No more items : remove the log and signal termination */
  221. DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",
  222. ap->discoveries);
  223. if(ap->discoveries != NULL)
  224. {
  225. /* Cleanup our copy of the discovery log */
  226. kfree(ap->discoveries);
  227. ap->discoveries = NULL;
  228. }
  229. ap->disco_number = -1;
  230. }
  231. return done_event;
  232. }
  233. #endif /* INITIAL_DISCOVERY */
  234. /*------------------------------------------------------------------*/
  235. /*
  236. * Read is used to get IrNET events
  237. */
  238. static inline ssize_t
  239. irnet_ctrl_read(irnet_socket * ap,
  240. struct file * file,
  241. char __user * buf,
  242. size_t count)
  243. {
  244. DECLARE_WAITQUEUE(wait, current);
  245. char event[64]; /* Max event is 61 char */
  246. ssize_t ret = 0;
  247. DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
  248. /* Check if we can write an event out in one go */
  249. DABORT(count < sizeof(event), -EOVERFLOW, CTRL_ERROR, "Buffer to small.\n");
  250. #ifdef INITIAL_DISCOVERY
  251. /* Check if we have read the log */
  252. if(irnet_read_discovery_log(ap, event))
  253. {
  254. /* We have an event !!! Copy it to the user */
  255. if(copy_to_user(buf, event, strlen(event)))
  256. {
  257. DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
  258. return -EFAULT;
  259. }
  260. DEXIT(CTRL_TRACE, "\n");
  261. return strlen(event);
  262. }
  263. #endif /* INITIAL_DISCOVERY */
  264. /* Put ourselves on the wait queue to be woken up */
  265. add_wait_queue(&irnet_events.rwait, &wait);
  266. current->state = TASK_INTERRUPTIBLE;
  267. for(;;)
  268. {
  269. /* If there is unread events */
  270. ret = 0;
  271. if(ap->event_index != irnet_events.index)
  272. break;
  273. ret = -EAGAIN;
  274. if(file->f_flags & O_NONBLOCK)
  275. break;
  276. ret = -ERESTARTSYS;
  277. if(signal_pending(current))
  278. break;
  279. /* Yield and wait to be woken up */
  280. schedule();
  281. }
  282. current->state = TASK_RUNNING;
  283. remove_wait_queue(&irnet_events.rwait, &wait);
  284. /* Did we got it ? */
  285. if(ret != 0)
  286. {
  287. /* No, return the error code */
  288. DEXIT(CTRL_TRACE, " - ret %Zd\n", ret);
  289. return ret;
  290. }
  291. /* Which event is it ? */
  292. switch(irnet_events.log[ap->event_index].event)
  293. {
  294. case IRNET_DISCOVER:
  295. sprintf(event, "Discovered %08x (%s) behind %08x {hints %02X-%02X}\n",
  296. irnet_events.log[ap->event_index].daddr,
  297. irnet_events.log[ap->event_index].name,
  298. irnet_events.log[ap->event_index].saddr,
  299. irnet_events.log[ap->event_index].hints.byte[0],
  300. irnet_events.log[ap->event_index].hints.byte[1]);
  301. break;
  302. case IRNET_EXPIRE:
  303. sprintf(event, "Expired %08x (%s) behind %08x {hints %02X-%02X}\n",
  304. irnet_events.log[ap->event_index].daddr,
  305. irnet_events.log[ap->event_index].name,
  306. irnet_events.log[ap->event_index].saddr,
  307. irnet_events.log[ap->event_index].hints.byte[0],
  308. irnet_events.log[ap->event_index].hints.byte[1]);
  309. break;
  310. case IRNET_CONNECT_TO:
  311. sprintf(event, "Connected to %08x (%s) on ppp%d\n",
  312. irnet_events.log[ap->event_index].daddr,
  313. irnet_events.log[ap->event_index].name,
  314. irnet_events.log[ap->event_index].unit);
  315. break;
  316. case IRNET_CONNECT_FROM:
  317. sprintf(event, "Connection from %08x (%s) on ppp%d\n",
  318. irnet_events.log[ap->event_index].daddr,
  319. irnet_events.log[ap->event_index].name,
  320. irnet_events.log[ap->event_index].unit);
  321. break;
  322. case IRNET_REQUEST_FROM:
  323. sprintf(event, "Request from %08x (%s) behind %08x\n",
  324. irnet_events.log[ap->event_index].daddr,
  325. irnet_events.log[ap->event_index].name,
  326. irnet_events.log[ap->event_index].saddr);
  327. break;
  328. case IRNET_NOANSWER_FROM:
  329. sprintf(event, "No-answer from %08x (%s) on ppp%d\n",
  330. irnet_events.log[ap->event_index].daddr,
  331. irnet_events.log[ap->event_index].name,
  332. irnet_events.log[ap->event_index].unit);
  333. break;
  334. case IRNET_BLOCKED_LINK:
  335. sprintf(event, "Blocked link with %08x (%s) on ppp%d\n",
  336. irnet_events.log[ap->event_index].daddr,
  337. irnet_events.log[ap->event_index].name,
  338. irnet_events.log[ap->event_index].unit);
  339. break;
  340. case IRNET_DISCONNECT_FROM:
  341. sprintf(event, "Disconnection from %08x (%s) on ppp%d\n",
  342. irnet_events.log[ap->event_index].daddr,
  343. irnet_events.log[ap->event_index].name,
  344. irnet_events.log[ap->event_index].unit);
  345. break;
  346. case IRNET_DISCONNECT_TO:
  347. sprintf(event, "Disconnected to %08x (%s)\n",
  348. irnet_events.log[ap->event_index].daddr,
  349. irnet_events.log[ap->event_index].name);
  350. break;
  351. default:
  352. sprintf(event, "Bug\n");
  353. }
  354. /* Increment our event index */
  355. ap->event_index = (ap->event_index + 1) % IRNET_MAX_EVENTS;
  356. DEBUG(CTRL_INFO, "Event is :%s", event);
  357. /* Copy it to the user */
  358. if(copy_to_user(buf, event, strlen(event)))
  359. {
  360. DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
  361. return -EFAULT;
  362. }
  363. DEXIT(CTRL_TRACE, "\n");
  364. return strlen(event);
  365. }
  366. /*------------------------------------------------------------------*/
  367. /*
  368. * Poll : called when someone do a select on /dev/irnet.
  369. * Just check if there are new events...
  370. */
  371. static inline unsigned int
  372. irnet_ctrl_poll(irnet_socket * ap,
  373. struct file * file,
  374. poll_table * wait)
  375. {
  376. unsigned int mask;
  377. DENTER(CTRL_TRACE, "(ap=0x%p)\n", ap);
  378. poll_wait(file, &irnet_events.rwait, wait);
  379. mask = POLLOUT | POLLWRNORM;
  380. /* If there is unread events */
  381. if(ap->event_index != irnet_events.index)
  382. mask |= POLLIN | POLLRDNORM;
  383. #ifdef INITIAL_DISCOVERY
  384. if(ap->disco_number != -1)
  385. {
  386. /* Test if it's the first time and therefore we need to get the log */
  387. if(ap->discoveries == NULL)
  388. irnet_get_discovery_log(ap);
  389. /* Recheck */
  390. if(ap->disco_number != -1)
  391. mask |= POLLIN | POLLRDNORM;
  392. }
  393. #endif /* INITIAL_DISCOVERY */
  394. DEXIT(CTRL_TRACE, " - mask=0x%X\n", mask);
  395. return mask;
  396. }
  397. /*********************** FILESYSTEM CALLBACKS ***********************/
  398. /*
  399. * Implement the usual open, read, write functions that will be called
  400. * by the file system when some action is performed on /dev/irnet.
  401. * Most of those actions will in fact be performed by "pppd" or
  402. * the control channel, we just act as a redirector...
  403. */
  404. /*------------------------------------------------------------------*/
  405. /*
  406. * Open : when somebody open /dev/irnet
  407. * We basically create a new instance of irnet and initialise it.
  408. */
  409. static int
  410. dev_irnet_open(struct inode * inode,
  411. struct file * file)
  412. {
  413. struct irnet_socket * ap;
  414. int err;
  415. DENTER(FS_TRACE, "(file=0x%p)\n", file);
  416. #ifdef SECURE_DEVIRNET
  417. /* This could (should?) be enforced by the permissions on /dev/irnet. */
  418. if(!capable(CAP_NET_ADMIN))
  419. return -EPERM;
  420. #endif /* SECURE_DEVIRNET */
  421. /* Allocate a private structure for this IrNET instance */
  422. ap = kzalloc(sizeof(*ap), GFP_KERNEL);
  423. DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");
  424. /* initialize the irnet structure */
  425. ap->file = file;
  426. /* PPP channel setup */
  427. ap->ppp_open = 0;
  428. ap->chan.private = ap;
  429. ap->chan.ops = &irnet_ppp_ops;
  430. ap->chan.mtu = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
  431. ap->chan.hdrlen = 2 + TTP_MAX_HEADER; /* for A/C + Max IrDA hdr */
  432. /* PPP parameters */
  433. ap->mru = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
  434. ap->xaccm[0] = ~0U;
  435. ap->xaccm[3] = 0x60000000U;
  436. ap->raccm = ~0U;
  437. /* Setup the IrDA part... */
  438. err = irda_irnet_create(ap);
  439. if(err)
  440. {
  441. DERROR(FS_ERROR, "Can't setup IrDA link...\n");
  442. kfree(ap);
  443. return err;
  444. }
  445. /* For the control channel */
  446. ap->event_index = irnet_events.index; /* Cancel all past events */
  447. mutex_init(&ap->lock);
  448. /* Put our stuff where we will be able to find it later */
  449. file->private_data = ap;
  450. DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
  451. return 0;
  452. }
  453. /*------------------------------------------------------------------*/
  454. /*
  455. * Close : when somebody close /dev/irnet
  456. * Destroy the instance of /dev/irnet
  457. */
  458. static int
  459. dev_irnet_close(struct inode * inode,
  460. struct file * file)
  461. {
  462. irnet_socket * ap = file->private_data;
  463. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
  464. file, ap);
  465. DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
  466. /* Detach ourselves */
  467. file->private_data = NULL;
  468. /* Close IrDA stuff */
  469. irda_irnet_destroy(ap);
  470. /* Disconnect from the generic PPP layer if not already done */
  471. if(ap->ppp_open)
  472. {
  473. DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
  474. ap->ppp_open = 0;
  475. ppp_unregister_channel(&ap->chan);
  476. }
  477. kfree(ap);
  478. DEXIT(FS_TRACE, "\n");
  479. return 0;
  480. }
  481. /*------------------------------------------------------------------*/
  482. /*
  483. * Write does nothing.
  484. * (we receive packet from ppp_generic through ppp_irnet_send())
  485. */
  486. static ssize_t
  487. dev_irnet_write(struct file * file,
  488. const char __user *buf,
  489. size_t count,
  490. loff_t * ppos)
  491. {
  492. irnet_socket * ap = file->private_data;
  493. DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
  494. file, ap, count);
  495. DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
  496. /* If we are connected to ppp_generic, let it handle the job */
  497. if(ap->ppp_open)
  498. return -EAGAIN;
  499. else
  500. return irnet_ctrl_write(ap, buf, count);
  501. }
  502. /*------------------------------------------------------------------*/
  503. /*
  504. * Read doesn't do much either.
  505. * (pppd poll us, but ultimately reads through /dev/ppp)
  506. */
  507. static ssize_t
  508. dev_irnet_read(struct file * file,
  509. char __user * buf,
  510. size_t count,
  511. loff_t * ppos)
  512. {
  513. irnet_socket * ap = file->private_data;
  514. DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
  515. file, ap, count);
  516. DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
  517. /* If we are connected to ppp_generic, let it handle the job */
  518. if(ap->ppp_open)
  519. return -EAGAIN;
  520. else
  521. return irnet_ctrl_read(ap, file, buf, count);
  522. }
  523. /*------------------------------------------------------------------*/
  524. /*
  525. * Poll : called when someone do a select on /dev/irnet
  526. */
  527. static unsigned int
  528. dev_irnet_poll(struct file * file,
  529. poll_table * wait)
  530. {
  531. irnet_socket * ap = file->private_data;
  532. unsigned int mask;
  533. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
  534. file, ap);
  535. mask = POLLOUT | POLLWRNORM;
  536. DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
  537. /* If we are connected to ppp_generic, let it handle the job */
  538. if(!ap->ppp_open)
  539. mask |= irnet_ctrl_poll(ap, file, wait);
  540. DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
  541. return mask;
  542. }
  543. /*------------------------------------------------------------------*/
  544. /*
  545. * IOCtl : Called when someone does some ioctls on /dev/irnet
  546. * This is the way pppd configure us and control us while the PPP
  547. * instance is active.
  548. */
  549. static long
  550. dev_irnet_ioctl(
  551. struct file * file,
  552. unsigned int cmd,
  553. unsigned long arg)
  554. {
  555. irnet_socket * ap = file->private_data;
  556. int err;
  557. int val;
  558. void __user *argp = (void __user *)arg;
  559. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
  560. file, ap, cmd);
  561. /* Basic checks... */
  562. DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
  563. #ifdef SECURE_DEVIRNET
  564. if(!capable(CAP_NET_ADMIN))
  565. return -EPERM;
  566. #endif /* SECURE_DEVIRNET */
  567. err = -EFAULT;
  568. switch(cmd)
  569. {
  570. /* Set discipline (should be N_SYNC_PPP or N_TTY) */
  571. case TIOCSETD:
  572. if(get_user(val, (int __user *)argp))
  573. break;
  574. if((val == N_SYNC_PPP) || (val == N_PPP))
  575. {
  576. DEBUG(FS_INFO, "Entering PPP discipline.\n");
  577. /* PPP channel setup (ap->chan in configured in dev_irnet_open())*/
  578. if (mutex_lock_interruptible(&ap->lock))
  579. return -EINTR;
  580. err = ppp_register_channel(&ap->chan);
  581. if(err == 0)
  582. {
  583. /* Our ppp side is active */
  584. ap->ppp_open = 1;
  585. DEBUG(FS_INFO, "Trying to establish a connection.\n");
  586. /* Setup the IrDA link now - may fail... */
  587. irda_irnet_connect(ap);
  588. }
  589. else
  590. DERROR(FS_ERROR, "Can't setup PPP channel...\n");
  591. mutex_unlock(&ap->lock);
  592. }
  593. else
  594. {
  595. /* In theory, should be N_TTY */
  596. DEBUG(FS_INFO, "Exiting PPP discipline.\n");
  597. /* Disconnect from the generic PPP layer */
  598. if (mutex_lock_interruptible(&ap->lock))
  599. return -EINTR;
  600. if(ap->ppp_open)
  601. {
  602. ap->ppp_open = 0;
  603. ppp_unregister_channel(&ap->chan);
  604. }
  605. else
  606. DERROR(FS_ERROR, "Channel not registered !\n");
  607. err = 0;
  608. mutex_unlock(&ap->lock);
  609. }
  610. break;
  611. /* Query PPP channel and unit number */
  612. case PPPIOCGCHAN:
  613. if (mutex_lock_interruptible(&ap->lock))
  614. return -EINTR;
  615. if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),
  616. (int __user *)argp))
  617. err = 0;
  618. mutex_unlock(&ap->lock);
  619. break;
  620. case PPPIOCGUNIT:
  621. if (mutex_lock_interruptible(&ap->lock))
  622. return -EINTR;
  623. if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),
  624. (int __user *)argp))
  625. err = 0;
  626. mutex_unlock(&ap->lock);
  627. break;
  628. /* All these ioctls can be passed both directly and from ppp_generic,
  629. * so we just deal with them in one place...
  630. */
  631. case PPPIOCGFLAGS:
  632. case PPPIOCSFLAGS:
  633. case PPPIOCGASYNCMAP:
  634. case PPPIOCSASYNCMAP:
  635. case PPPIOCGRASYNCMAP:
  636. case PPPIOCSRASYNCMAP:
  637. case PPPIOCGXASYNCMAP:
  638. case PPPIOCSXASYNCMAP:
  639. case PPPIOCGMRU:
  640. case PPPIOCSMRU:
  641. DEBUG(FS_INFO, "Standard PPP ioctl.\n");
  642. if(!capable(CAP_NET_ADMIN))
  643. err = -EPERM;
  644. else {
  645. if (mutex_lock_interruptible(&ap->lock))
  646. return -EINTR;
  647. err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
  648. mutex_unlock(&ap->lock);
  649. }
  650. break;
  651. /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
  652. /* Get termios */
  653. case TCGETS:
  654. DEBUG(FS_INFO, "Get termios.\n");
  655. if (mutex_lock_interruptible(&ap->lock))
  656. return -EINTR;
  657. #ifndef TCGETS2
  658. if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
  659. err = 0;
  660. #else
  661. if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
  662. err = 0;
  663. #endif
  664. mutex_unlock(&ap->lock);
  665. break;
  666. /* Set termios */
  667. case TCSETSF:
  668. DEBUG(FS_INFO, "Set termios.\n");
  669. if (mutex_lock_interruptible(&ap->lock))
  670. return -EINTR;
  671. #ifndef TCGETS2
  672. if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
  673. err = 0;
  674. #else
  675. if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
  676. err = 0;
  677. #endif
  678. mutex_unlock(&ap->lock);
  679. break;
  680. /* Set DTR/RTS */
  681. case TIOCMBIS:
  682. case TIOCMBIC:
  683. /* Set exclusive/non-exclusive mode */
  684. case TIOCEXCL:
  685. case TIOCNXCL:
  686. DEBUG(FS_INFO, "TTY compatibility.\n");
  687. err = 0;
  688. break;
  689. case TCGETA:
  690. DEBUG(FS_INFO, "TCGETA\n");
  691. break;
  692. case TCFLSH:
  693. DEBUG(FS_INFO, "TCFLSH\n");
  694. /* Note : this will flush buffers in PPP, so it *must* be done
  695. * We should also worry that we don't accept junk here and that
  696. * we get rid of our own buffers */
  697. #ifdef FLUSH_TO_PPP
  698. if (mutex_lock_interruptible(&ap->lock))
  699. return -EINTR;
  700. ppp_output_wakeup(&ap->chan);
  701. mutex_unlock(&ap->lock);
  702. #endif /* FLUSH_TO_PPP */
  703. err = 0;
  704. break;
  705. case FIONREAD:
  706. DEBUG(FS_INFO, "FIONREAD\n");
  707. val = 0;
  708. if(put_user(val, (int __user *)argp))
  709. break;
  710. err = 0;
  711. break;
  712. default:
  713. DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
  714. err = -ENOTTY;
  715. }
  716. DEXIT(FS_TRACE, " - err = 0x%X\n", err);
  717. return err;
  718. }
  719. /************************** PPP CALLBACKS **************************/
  720. /*
  721. * This are the functions that the generic PPP driver in the kernel
  722. * will call to communicate to us.
  723. */
  724. /*------------------------------------------------------------------*/
  725. /*
  726. * Prepare the ppp frame for transmission over the IrDA socket.
  727. * We make sure that the header space is enough, and we change ppp header
  728. * according to flags passed by pppd.
  729. * This is not a callback, but just a helper function used in ppp_irnet_send()
  730. */
  731. static inline struct sk_buff *
  732. irnet_prepare_skb(irnet_socket * ap,
  733. struct sk_buff * skb)
  734. {
  735. unsigned char * data;
  736. int proto; /* PPP protocol */
  737. int islcp; /* Protocol == LCP */
  738. int needaddr; /* Need PPP address */
  739. DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
  740. ap, skb);
  741. /* Extract PPP protocol from the frame */
  742. data = skb->data;
  743. proto = (data[0] << 8) + data[1];
  744. /* LCP packets with codes between 1 (configure-request)
  745. * and 7 (code-reject) must be sent as though no options
  746. * have been negotiated. */
  747. islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
  748. /* compress protocol field if option enabled */
  749. if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
  750. skb_pull(skb,1);
  751. /* Check if we need address/control fields */
  752. needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
  753. /* Is the skb headroom large enough to contain all IrDA-headers? */
  754. if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
  755. (skb_shared(skb)))
  756. {
  757. struct sk_buff * new_skb;
  758. DEBUG(PPP_INFO, "Reallocating skb\n");
  759. /* Create a new skb */
  760. new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
  761. /* We have to free the original skb anyway */
  762. dev_kfree_skb(skb);
  763. /* Did the realloc succeed ? */
  764. DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
  765. /* Use the new skb instead */
  766. skb = new_skb;
  767. }
  768. /* prepend address/control fields if necessary */
  769. if(needaddr)
  770. {
  771. skb_push(skb, 2);
  772. skb->data[0] = PPP_ALLSTATIONS;
  773. skb->data[1] = PPP_UI;
  774. }
  775. DEXIT(PPP_TRACE, "\n");
  776. return skb;
  777. }
  778. /*------------------------------------------------------------------*/
  779. /*
  780. * Send a packet to the peer over the IrTTP connection.
  781. * Returns 1 iff the packet was accepted.
  782. * Returns 0 iff packet was not consumed.
  783. * If the packet was not accepted, we will call ppp_output_wakeup
  784. * at some later time to reactivate flow control in ppp_generic.
  785. */
  786. static int
  787. ppp_irnet_send(struct ppp_channel * chan,
  788. struct sk_buff * skb)
  789. {
  790. irnet_socket * self = (struct irnet_socket *) chan->private;
  791. int ret;
  792. DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
  793. chan, self);
  794. /* Check if things are somewhat valid... */
  795. DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
  796. /* Check if we are connected */
  797. if(!(test_bit(0, &self->ttp_open)))
  798. {
  799. #ifdef CONNECT_IN_SEND
  800. /* Let's try to connect one more time... */
  801. /* Note : we won't be connected after this call, but we should be
  802. * ready for next packet... */
  803. /* If we are already connecting, this will fail */
  804. irda_irnet_connect(self);
  805. #endif /* CONNECT_IN_SEND */
  806. DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
  807. self->ttp_open, self->ttp_connect);
  808. /* Note : we can either drop the packet or block the packet.
  809. *
  810. * Blocking the packet allow us a better connection time,
  811. * because by calling ppp_output_wakeup() we can have
  812. * ppp_generic resending the LCP request immediately to us,
  813. * rather than waiting for one of pppd periodic transmission of
  814. * LCP request.
  815. *
  816. * On the other hand, if we block all packet, all those periodic
  817. * transmissions of pppd accumulate in ppp_generic, creating a
  818. * backlog of LCP request. When we eventually connect later on,
  819. * we have to transmit all this backlog before we can connect
  820. * proper (if we don't timeout before).
  821. *
  822. * The current strategy is as follow :
  823. * While we are attempting to connect, we block packets to get
  824. * a better connection time.
  825. * If we fail to connect, we drain the queue and start dropping packets
  826. */
  827. #ifdef BLOCK_WHEN_CONNECT
  828. /* If we are attempting to connect */
  829. if(test_bit(0, &self->ttp_connect))
  830. {
  831. /* Blocking packet, ppp_generic will retry later */
  832. return 0;
  833. }
  834. #endif /* BLOCK_WHEN_CONNECT */
  835. /* Dropping packet, pppd will retry later */
  836. dev_kfree_skb(skb);
  837. return 1;
  838. }
  839. /* Check if the queue can accept any packet, otherwise block */
  840. if(self->tx_flow != FLOW_START)
  841. DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
  842. skb_queue_len(&self->tsap->tx_queue));
  843. /* Prepare ppp frame for transmission */
  844. skb = irnet_prepare_skb(self, skb);
  845. DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
  846. /* Send the packet to IrTTP */
  847. ret = irttp_data_request(self->tsap, skb);
  848. if(ret < 0)
  849. {
  850. /*
  851. * > IrTTPs tx queue is full, so we just have to
  852. * > drop the frame! You might think that we should
  853. * > just return -1 and don't deallocate the frame,
  854. * > but that is dangerous since it's possible that
  855. * > we have replaced the original skb with a new
  856. * > one with larger headroom, and that would really
  857. * > confuse do_dev_queue_xmit() in dev.c! I have
  858. * > tried :-) DB
  859. * Correction : we verify the flow control above (self->tx_flow),
  860. * so we come here only if IrTTP doesn't like the packet (empty,
  861. * too large, IrTTP not connected). In those rare cases, it's ok
  862. * to drop it, we don't want to see it here again...
  863. * Jean II
  864. */
  865. DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
  866. /* irttp_data_request already free the packet */
  867. }
  868. DEXIT(PPP_TRACE, "\n");
  869. return 1; /* Packet has been consumed */
  870. }
  871. /*------------------------------------------------------------------*/
  872. /*
  873. * Take care of the ioctls that ppp_generic doesn't want to deal with...
  874. * Note : we are also called from dev_irnet_ioctl().
  875. */
  876. static int
  877. ppp_irnet_ioctl(struct ppp_channel * chan,
  878. unsigned int cmd,
  879. unsigned long arg)
  880. {
  881. irnet_socket * ap = (struct irnet_socket *) chan->private;
  882. int err;
  883. int val;
  884. u32 accm[8];
  885. void __user *argp = (void __user *)arg;
  886. DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
  887. chan, ap, cmd);
  888. /* Basic checks... */
  889. DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
  890. err = -EFAULT;
  891. switch(cmd)
  892. {
  893. /* PPP flags */
  894. case PPPIOCGFLAGS:
  895. val = ap->flags | ap->rbits;
  896. if(put_user(val, (int __user *) argp))
  897. break;
  898. err = 0;
  899. break;
  900. case PPPIOCSFLAGS:
  901. if(get_user(val, (int __user *) argp))
  902. break;
  903. ap->flags = val & ~SC_RCV_BITS;
  904. ap->rbits = val & SC_RCV_BITS;
  905. err = 0;
  906. break;
  907. /* Async map stuff - all dummy to please pppd */
  908. case PPPIOCGASYNCMAP:
  909. if(put_user(ap->xaccm[0], (u32 __user *) argp))
  910. break;
  911. err = 0;
  912. break;
  913. case PPPIOCSASYNCMAP:
  914. if(get_user(ap->xaccm[0], (u32 __user *) argp))
  915. break;
  916. err = 0;
  917. break;
  918. case PPPIOCGRASYNCMAP:
  919. if(put_user(ap->raccm, (u32 __user *) argp))
  920. break;
  921. err = 0;
  922. break;
  923. case PPPIOCSRASYNCMAP:
  924. if(get_user(ap->raccm, (u32 __user *) argp))
  925. break;
  926. err = 0;
  927. break;
  928. case PPPIOCGXASYNCMAP:
  929. if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  930. break;
  931. err = 0;
  932. break;
  933. case PPPIOCSXASYNCMAP:
  934. if(copy_from_user(accm, argp, sizeof(accm)))
  935. break;
  936. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  937. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  938. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  939. err = 0;
  940. break;
  941. /* Max PPP frame size */
  942. case PPPIOCGMRU:
  943. if(put_user(ap->mru, (int __user *) argp))
  944. break;
  945. err = 0;
  946. break;
  947. case PPPIOCSMRU:
  948. if(get_user(val, (int __user *) argp))
  949. break;
  950. if(val < PPP_MRU)
  951. val = PPP_MRU;
  952. ap->mru = val;
  953. err = 0;
  954. break;
  955. default:
  956. DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
  957. err = -ENOIOCTLCMD;
  958. }
  959. DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
  960. return err;
  961. }
  962. /************************** INITIALISATION **************************/
  963. /*
  964. * Module initialisation and all that jazz...
  965. */
  966. /*------------------------------------------------------------------*/
  967. /*
  968. * Hook our device callbacks in the filesystem, to connect our code
  969. * to /dev/irnet
  970. */
  971. static inline int __init
  972. ppp_irnet_init(void)
  973. {
  974. int err = 0;
  975. DENTER(MODULE_TRACE, "()\n");
  976. /* Allocate ourselves as a minor in the misc range */
  977. err = misc_register(&irnet_misc_device);
  978. DEXIT(MODULE_TRACE, "\n");
  979. return err;
  980. }
  981. /*------------------------------------------------------------------*/
  982. /*
  983. * Cleanup at exit...
  984. */
  985. static inline void __exit
  986. ppp_irnet_cleanup(void)
  987. {
  988. DENTER(MODULE_TRACE, "()\n");
  989. /* De-allocate /dev/irnet minor in misc range */
  990. misc_deregister(&irnet_misc_device);
  991. DEXIT(MODULE_TRACE, "\n");
  992. }
  993. /*------------------------------------------------------------------*/
  994. /*
  995. * Module main entry point
  996. */
  997. static int __init
  998. irnet_init(void)
  999. {
  1000. int err;
  1001. /* Initialise both parts... */
  1002. err = irda_irnet_init();
  1003. if(!err)
  1004. err = ppp_irnet_init();
  1005. return err;
  1006. }
  1007. /*------------------------------------------------------------------*/
  1008. /*
  1009. * Module exit
  1010. */
  1011. static void __exit
  1012. irnet_cleanup(void)
  1013. {
  1014. irda_irnet_cleanup();
  1015. ppp_irnet_cleanup();
  1016. }
  1017. /*------------------------------------------------------------------*/
  1018. /*
  1019. * Module magic
  1020. */
  1021. module_init(irnet_init);
  1022. module_exit(irnet_cleanup);
  1023. MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
  1024. MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
  1025. MODULE_LICENSE("GPL");
  1026. MODULE_ALIAS_CHARDEV(10, 187);