irnet_ppp.c 33 KB

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