irnet_ppp.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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 "irnet_ppp.h" /* Private header */
  16. /* Please put other headers in irnet.h - Thanks */
  17. /* Generic PPP callbacks (to call us) */
  18. static struct ppp_channel_ops irnet_ppp_ops = {
  19. .start_xmit = ppp_irnet_send,
  20. .ioctl = ppp_irnet_ioctl
  21. };
  22. /************************* CONTROL CHANNEL *************************/
  23. /*
  24. * When a pppd instance is not active on /dev/irnet, it acts as a control
  25. * channel.
  26. * Writing allow to set up the IrDA destination of the IrNET channel,
  27. * and any application may be read events happening in IrNET...
  28. */
  29. /*------------------------------------------------------------------*/
  30. /*
  31. * Write is used to send a command to configure a IrNET channel
  32. * before it is open by pppd. The syntax is : "command argument"
  33. * Currently there is only two defined commands :
  34. * o name : set the requested IrDA nickname of the IrNET peer.
  35. * o addr : set the requested IrDA address of the IrNET peer.
  36. * Note : the code is crude, but effective...
  37. */
  38. static inline ssize_t
  39. irnet_ctrl_write(irnet_socket * ap,
  40. const char __user *buf,
  41. size_t count)
  42. {
  43. char command[IRNET_MAX_COMMAND];
  44. char * start; /* Current command being processed */
  45. char * next; /* Next command to process */
  46. int length; /* Length of current command */
  47. DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
  48. /* Check for overflow... */
  49. DABORT(count >= IRNET_MAX_COMMAND, -ENOMEM,
  50. CTRL_ERROR, "Too much data !!!\n");
  51. /* Get the data in the driver */
  52. if(copy_from_user(command, buf, count))
  53. {
  54. DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
  55. return -EFAULT;
  56. }
  57. /* Safe terminate the string */
  58. command[count] = '\0';
  59. DEBUG(CTRL_INFO, "Command line received is ``%s'' (%Zd).\n",
  60. command, count);
  61. /* Check every commands in the command line */
  62. next = command;
  63. while(next != NULL)
  64. {
  65. /* Look at the next command */
  66. start = next;
  67. /* Scrap whitespaces before the command */
  68. while(isspace(*start))
  69. start++;
  70. /* ',' is our command separator */
  71. next = strchr(start, ',');
  72. if(next)
  73. {
  74. *next = '\0'; /* Terminate command */
  75. length = next - start; /* Length */
  76. next++; /* Skip the '\0' */
  77. }
  78. else
  79. length = strlen(start);
  80. DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);
  81. /* Check if we recognised one of the known command
  82. * We can't use "switch" with strings, so hack with "continue" */
  83. /* First command : name -> Requested IrDA nickname */
  84. if(!strncmp(start, "name", 4))
  85. {
  86. /* Copy the name only if is included and not "any" */
  87. if((length > 5) && (strcmp(start + 5, "any")))
  88. {
  89. /* Strip out trailing whitespaces */
  90. while(isspace(start[length - 1]))
  91. length--;
  92. /* Copy the name for later reuse */
  93. memcpy(ap->rname, start + 5, length - 5);
  94. ap->rname[length - 5] = '\0';
  95. }
  96. else
  97. ap->rname[0] = '\0';
  98. DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);
  99. /* Restart the loop */
  100. continue;
  101. }
  102. /* Second command : addr, daddr -> Requested IrDA destination address
  103. * Also process : saddr -> Requested IrDA source address */
  104. if((!strncmp(start, "addr", 4)) ||
  105. (!strncmp(start, "daddr", 5)) ||
  106. (!strncmp(start, "saddr", 5)))
  107. {
  108. __u32 addr = DEV_ADDR_ANY;
  109. /* Copy the address only if is included and not "any" */
  110. if((length > 5) && (strcmp(start + 5, "any")))
  111. {
  112. char * begp = start + 5;
  113. char * endp;
  114. /* Scrap whitespaces before the command */
  115. while(isspace(*begp))
  116. 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. /* Put our stuff where we will be able to find it later */
  448. file->private_data = ap;
  449. DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
  450. return 0;
  451. }
  452. /*------------------------------------------------------------------*/
  453. /*
  454. * Close : when somebody close /dev/irnet
  455. * Destroy the instance of /dev/irnet
  456. */
  457. static int
  458. dev_irnet_close(struct inode * inode,
  459. struct file * file)
  460. {
  461. irnet_socket * ap = (struct irnet_socket *) file->private_data;
  462. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
  463. file, ap);
  464. DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
  465. /* Detach ourselves */
  466. file->private_data = NULL;
  467. /* Close IrDA stuff */
  468. irda_irnet_destroy(ap);
  469. /* Disconnect from the generic PPP layer if not already done */
  470. if(ap->ppp_open)
  471. {
  472. DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
  473. ap->ppp_open = 0;
  474. ppp_unregister_channel(&ap->chan);
  475. }
  476. kfree(ap);
  477. DEXIT(FS_TRACE, "\n");
  478. return 0;
  479. }
  480. /*------------------------------------------------------------------*/
  481. /*
  482. * Write does nothing.
  483. * (we receive packet from ppp_generic through ppp_irnet_send())
  484. */
  485. static ssize_t
  486. dev_irnet_write(struct file * file,
  487. const char __user *buf,
  488. size_t count,
  489. loff_t * ppos)
  490. {
  491. irnet_socket * ap = (struct irnet_socket *) file->private_data;
  492. DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
  493. file, ap, count);
  494. DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
  495. /* If we are connected to ppp_generic, let it handle the job */
  496. if(ap->ppp_open)
  497. return -EAGAIN;
  498. else
  499. return irnet_ctrl_write(ap, buf, count);
  500. }
  501. /*------------------------------------------------------------------*/
  502. /*
  503. * Read doesn't do much either.
  504. * (pppd poll us, but ultimately reads through /dev/ppp)
  505. */
  506. static ssize_t
  507. dev_irnet_read(struct file * file,
  508. char __user * buf,
  509. size_t count,
  510. loff_t * ppos)
  511. {
  512. irnet_socket * ap = (struct irnet_socket *) file->private_data;
  513. DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
  514. file, ap, count);
  515. DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
  516. /* If we are connected to ppp_generic, let it handle the job */
  517. if(ap->ppp_open)
  518. return -EAGAIN;
  519. else
  520. return irnet_ctrl_read(ap, file, buf, count);
  521. }
  522. /*------------------------------------------------------------------*/
  523. /*
  524. * Poll : called when someone do a select on /dev/irnet
  525. */
  526. static unsigned int
  527. dev_irnet_poll(struct file * file,
  528. poll_table * wait)
  529. {
  530. irnet_socket * ap = (struct irnet_socket *) file->private_data;
  531. unsigned int mask;
  532. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
  533. file, ap);
  534. mask = POLLOUT | POLLWRNORM;
  535. DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
  536. /* If we are connected to ppp_generic, let it handle the job */
  537. if(!ap->ppp_open)
  538. mask |= irnet_ctrl_poll(ap, file, wait);
  539. DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
  540. return(mask);
  541. }
  542. /*------------------------------------------------------------------*/
  543. /*
  544. * IOCtl : Called when someone does some ioctls on /dev/irnet
  545. * This is the way pppd configure us and control us while the PPP
  546. * instance is active.
  547. */
  548. static int
  549. dev_irnet_ioctl(struct inode * inode,
  550. struct file * file,
  551. unsigned int cmd,
  552. unsigned long arg)
  553. {
  554. irnet_socket * ap = (struct irnet_socket *) file->private_data;
  555. int err;
  556. int val;
  557. void __user *argp = (void __user *)arg;
  558. DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
  559. file, ap, cmd);
  560. /* Basic checks... */
  561. DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
  562. #ifdef SECURE_DEVIRNET
  563. if(!capable(CAP_NET_ADMIN))
  564. return -EPERM;
  565. #endif /* SECURE_DEVIRNET */
  566. err = -EFAULT;
  567. switch(cmd)
  568. {
  569. /* Set discipline (should be N_SYNC_PPP or N_TTY) */
  570. case TIOCSETD:
  571. if(get_user(val, (int __user *)argp))
  572. break;
  573. if((val == N_SYNC_PPP) || (val == N_PPP))
  574. {
  575. DEBUG(FS_INFO, "Entering PPP discipline.\n");
  576. /* PPP channel setup (ap->chan in configued in dev_irnet_open())*/
  577. err = ppp_register_channel(&ap->chan);
  578. if(err == 0)
  579. {
  580. /* Our ppp side is active */
  581. ap->ppp_open = 1;
  582. DEBUG(FS_INFO, "Trying to establish a connection.\n");
  583. /* Setup the IrDA link now - may fail... */
  584. irda_irnet_connect(ap);
  585. }
  586. else
  587. DERROR(FS_ERROR, "Can't setup PPP channel...\n");
  588. }
  589. else
  590. {
  591. /* In theory, should be N_TTY */
  592. DEBUG(FS_INFO, "Exiting PPP discipline.\n");
  593. /* Disconnect from the generic PPP layer */
  594. if(ap->ppp_open)
  595. {
  596. ap->ppp_open = 0;
  597. ppp_unregister_channel(&ap->chan);
  598. }
  599. else
  600. DERROR(FS_ERROR, "Channel not registered !\n");
  601. err = 0;
  602. }
  603. break;
  604. /* Query PPP channel and unit number */
  605. case PPPIOCGCHAN:
  606. if(!ap->ppp_open)
  607. break;
  608. if(put_user(ppp_channel_index(&ap->chan), (int __user *)argp))
  609. break;
  610. DEBUG(FS_INFO, "Query channel.\n");
  611. err = 0;
  612. break;
  613. case PPPIOCGUNIT:
  614. if(!ap->ppp_open)
  615. break;
  616. if(put_user(ppp_unit_number(&ap->chan), (int __user *)argp))
  617. break;
  618. DEBUG(FS_INFO, "Query unit number.\n");
  619. err = 0;
  620. break;
  621. /* All these ioctls can be passed both directly and from ppp_generic,
  622. * so we just deal with them in one place...
  623. */
  624. case PPPIOCGFLAGS:
  625. case PPPIOCSFLAGS:
  626. case PPPIOCGASYNCMAP:
  627. case PPPIOCSASYNCMAP:
  628. case PPPIOCGRASYNCMAP:
  629. case PPPIOCSRASYNCMAP:
  630. case PPPIOCGXASYNCMAP:
  631. case PPPIOCSXASYNCMAP:
  632. case PPPIOCGMRU:
  633. case PPPIOCSMRU:
  634. DEBUG(FS_INFO, "Standard PPP ioctl.\n");
  635. if(!capable(CAP_NET_ADMIN))
  636. err = -EPERM;
  637. else
  638. err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
  639. break;
  640. /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
  641. /* Get termios */
  642. case TCGETS:
  643. DEBUG(FS_INFO, "Get termios.\n");
  644. #ifndef TCGETS2
  645. if(kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
  646. break;
  647. #else
  648. if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
  649. break;
  650. #endif
  651. err = 0;
  652. break;
  653. /* Set termios */
  654. case TCSETSF:
  655. DEBUG(FS_INFO, "Set termios.\n");
  656. #ifndef TCGETS2
  657. if(user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
  658. break;
  659. #else
  660. if(user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
  661. break;
  662. #endif
  663. err = 0;
  664. break;
  665. /* Set DTR/RTS */
  666. case TIOCMBIS:
  667. case TIOCMBIC:
  668. /* Set exclusive/non-exclusive mode */
  669. case TIOCEXCL:
  670. case TIOCNXCL:
  671. DEBUG(FS_INFO, "TTY compatibility.\n");
  672. err = 0;
  673. break;
  674. case TCGETA:
  675. DEBUG(FS_INFO, "TCGETA\n");
  676. break;
  677. case TCFLSH:
  678. DEBUG(FS_INFO, "TCFLSH\n");
  679. /* Note : this will flush buffers in PPP, so it *must* be done
  680. * We should also worry that we don't accept junk here and that
  681. * we get rid of our own buffers */
  682. #ifdef FLUSH_TO_PPP
  683. ppp_output_wakeup(&ap->chan);
  684. #endif /* FLUSH_TO_PPP */
  685. err = 0;
  686. break;
  687. case FIONREAD:
  688. DEBUG(FS_INFO, "FIONREAD\n");
  689. val = 0;
  690. if(put_user(val, (int __user *)argp))
  691. break;
  692. err = 0;
  693. break;
  694. default:
  695. DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
  696. err = -ENOIOCTLCMD;
  697. }
  698. DEXIT(FS_TRACE, " - err = 0x%X\n", err);
  699. return err;
  700. }
  701. /************************** PPP CALLBACKS **************************/
  702. /*
  703. * This are the functions that the generic PPP driver in the kernel
  704. * will call to communicate to us.
  705. */
  706. /*------------------------------------------------------------------*/
  707. /*
  708. * Prepare the ppp frame for transmission over the IrDA socket.
  709. * We make sure that the header space is enough, and we change ppp header
  710. * according to flags passed by pppd.
  711. * This is not a callback, but just a helper function used in ppp_irnet_send()
  712. */
  713. static inline struct sk_buff *
  714. irnet_prepare_skb(irnet_socket * ap,
  715. struct sk_buff * skb)
  716. {
  717. unsigned char * data;
  718. int proto; /* PPP protocol */
  719. int islcp; /* Protocol == LCP */
  720. int needaddr; /* Need PPP address */
  721. DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
  722. ap, skb);
  723. /* Extract PPP protocol from the frame */
  724. data = skb->data;
  725. proto = (data[0] << 8) + data[1];
  726. /* LCP packets with codes between 1 (configure-request)
  727. * and 7 (code-reject) must be sent as though no options
  728. * have been negotiated. */
  729. islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
  730. /* compress protocol field if option enabled */
  731. if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
  732. skb_pull(skb,1);
  733. /* Check if we need address/control fields */
  734. needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
  735. /* Is the skb headroom large enough to contain all IrDA-headers? */
  736. if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
  737. (skb_shared(skb)))
  738. {
  739. struct sk_buff * new_skb;
  740. DEBUG(PPP_INFO, "Reallocating skb\n");
  741. /* Create a new skb */
  742. new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
  743. /* We have to free the original skb anyway */
  744. dev_kfree_skb(skb);
  745. /* Did the realloc succeed ? */
  746. DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
  747. /* Use the new skb instead */
  748. skb = new_skb;
  749. }
  750. /* prepend address/control fields if necessary */
  751. if(needaddr)
  752. {
  753. skb_push(skb, 2);
  754. skb->data[0] = PPP_ALLSTATIONS;
  755. skb->data[1] = PPP_UI;
  756. }
  757. DEXIT(PPP_TRACE, "\n");
  758. return skb;
  759. }
  760. /*------------------------------------------------------------------*/
  761. /*
  762. * Send a packet to the peer over the IrTTP connection.
  763. * Returns 1 iff the packet was accepted.
  764. * Returns 0 iff packet was not consumed.
  765. * If the packet was not accepted, we will call ppp_output_wakeup
  766. * at some later time to reactivate flow control in ppp_generic.
  767. */
  768. static int
  769. ppp_irnet_send(struct ppp_channel * chan,
  770. struct sk_buff * skb)
  771. {
  772. irnet_socket * self = (struct irnet_socket *) chan->private;
  773. int ret;
  774. DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
  775. chan, self);
  776. /* Check if things are somewhat valid... */
  777. DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
  778. /* Check if we are connected */
  779. if(!(test_bit(0, &self->ttp_open)))
  780. {
  781. #ifdef CONNECT_IN_SEND
  782. /* Let's try to connect one more time... */
  783. /* Note : we won't be connected after this call, but we should be
  784. * ready for next packet... */
  785. /* If we are already connecting, this will fail */
  786. irda_irnet_connect(self);
  787. #endif /* CONNECT_IN_SEND */
  788. DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
  789. self->ttp_open, self->ttp_connect);
  790. /* Note : we can either drop the packet or block the packet.
  791. *
  792. * Blocking the packet allow us a better connection time,
  793. * because by calling ppp_output_wakeup() we can have
  794. * ppp_generic resending the LCP request immediately to us,
  795. * rather than waiting for one of pppd periodic transmission of
  796. * LCP request.
  797. *
  798. * On the other hand, if we block all packet, all those periodic
  799. * transmissions of pppd accumulate in ppp_generic, creating a
  800. * backlog of LCP request. When we eventually connect later on,
  801. * we have to transmit all this backlog before we can connect
  802. * proper (if we don't timeout before).
  803. *
  804. * The current strategy is as follow :
  805. * While we are attempting to connect, we block packets to get
  806. * a better connection time.
  807. * If we fail to connect, we drain the queue and start dropping packets
  808. */
  809. #ifdef BLOCK_WHEN_CONNECT
  810. /* If we are attempting to connect */
  811. if(test_bit(0, &self->ttp_connect))
  812. {
  813. /* Blocking packet, ppp_generic will retry later */
  814. return 0;
  815. }
  816. #endif /* BLOCK_WHEN_CONNECT */
  817. /* Dropping packet, pppd will retry later */
  818. dev_kfree_skb(skb);
  819. return 1;
  820. }
  821. /* Check if the queue can accept any packet, otherwise block */
  822. if(self->tx_flow != FLOW_START)
  823. DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
  824. skb_queue_len(&self->tsap->tx_queue));
  825. /* Prepare ppp frame for transmission */
  826. skb = irnet_prepare_skb(self, skb);
  827. DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
  828. /* Send the packet to IrTTP */
  829. ret = irttp_data_request(self->tsap, skb);
  830. if(ret < 0)
  831. {
  832. /*
  833. * > IrTTPs tx queue is full, so we just have to
  834. * > drop the frame! You might think that we should
  835. * > just return -1 and don't deallocate the frame,
  836. * > but that is dangerous since it's possible that
  837. * > we have replaced the original skb with a new
  838. * > one with larger headroom, and that would really
  839. * > confuse do_dev_queue_xmit() in dev.c! I have
  840. * > tried :-) DB
  841. * Correction : we verify the flow control above (self->tx_flow),
  842. * so we come here only if IrTTP doesn't like the packet (empty,
  843. * too large, IrTTP not connected). In those rare cases, it's ok
  844. * to drop it, we don't want to see it here again...
  845. * Jean II
  846. */
  847. DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
  848. /* irttp_data_request already free the packet */
  849. }
  850. DEXIT(PPP_TRACE, "\n");
  851. return 1; /* Packet has been consumed */
  852. }
  853. /*------------------------------------------------------------------*/
  854. /*
  855. * Take care of the ioctls that ppp_generic doesn't want to deal with...
  856. * Note : we are also called from dev_irnet_ioctl().
  857. */
  858. static int
  859. ppp_irnet_ioctl(struct ppp_channel * chan,
  860. unsigned int cmd,
  861. unsigned long arg)
  862. {
  863. irnet_socket * ap = (struct irnet_socket *) chan->private;
  864. int err;
  865. int val;
  866. u32 accm[8];
  867. void __user *argp = (void __user *)arg;
  868. DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
  869. chan, ap, cmd);
  870. /* Basic checks... */
  871. DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
  872. err = -EFAULT;
  873. switch(cmd)
  874. {
  875. /* PPP flags */
  876. case PPPIOCGFLAGS:
  877. val = ap->flags | ap->rbits;
  878. if(put_user(val, (int __user *) argp))
  879. break;
  880. err = 0;
  881. break;
  882. case PPPIOCSFLAGS:
  883. if(get_user(val, (int __user *) argp))
  884. break;
  885. ap->flags = val & ~SC_RCV_BITS;
  886. ap->rbits = val & SC_RCV_BITS;
  887. err = 0;
  888. break;
  889. /* Async map stuff - all dummy to please pppd */
  890. case PPPIOCGASYNCMAP:
  891. if(put_user(ap->xaccm[0], (u32 __user *) argp))
  892. break;
  893. err = 0;
  894. break;
  895. case PPPIOCSASYNCMAP:
  896. if(get_user(ap->xaccm[0], (u32 __user *) argp))
  897. break;
  898. err = 0;
  899. break;
  900. case PPPIOCGRASYNCMAP:
  901. if(put_user(ap->raccm, (u32 __user *) argp))
  902. break;
  903. err = 0;
  904. break;
  905. case PPPIOCSRASYNCMAP:
  906. if(get_user(ap->raccm, (u32 __user *) argp))
  907. break;
  908. err = 0;
  909. break;
  910. case PPPIOCGXASYNCMAP:
  911. if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
  912. break;
  913. err = 0;
  914. break;
  915. case PPPIOCSXASYNCMAP:
  916. if(copy_from_user(accm, argp, sizeof(accm)))
  917. break;
  918. accm[2] &= ~0x40000000U; /* can't escape 0x5e */
  919. accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
  920. memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
  921. err = 0;
  922. break;
  923. /* Max PPP frame size */
  924. case PPPIOCGMRU:
  925. if(put_user(ap->mru, (int __user *) argp))
  926. break;
  927. err = 0;
  928. break;
  929. case PPPIOCSMRU:
  930. if(get_user(val, (int __user *) argp))
  931. break;
  932. if(val < PPP_MRU)
  933. val = PPP_MRU;
  934. ap->mru = val;
  935. err = 0;
  936. break;
  937. default:
  938. DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
  939. err = -ENOIOCTLCMD;
  940. }
  941. DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
  942. return err;
  943. }
  944. /************************** INITIALISATION **************************/
  945. /*
  946. * Module initialisation and all that jazz...
  947. */
  948. /*------------------------------------------------------------------*/
  949. /*
  950. * Hook our device callbacks in the filesystem, to connect our code
  951. * to /dev/irnet
  952. */
  953. static inline int __init
  954. ppp_irnet_init(void)
  955. {
  956. int err = 0;
  957. DENTER(MODULE_TRACE, "()\n");
  958. /* Allocate ourselves as a minor in the misc range */
  959. err = misc_register(&irnet_misc_device);
  960. DEXIT(MODULE_TRACE, "\n");
  961. return err;
  962. }
  963. /*------------------------------------------------------------------*/
  964. /*
  965. * Cleanup at exit...
  966. */
  967. static inline void __exit
  968. ppp_irnet_cleanup(void)
  969. {
  970. DENTER(MODULE_TRACE, "()\n");
  971. /* De-allocate /dev/irnet minor in misc range */
  972. misc_deregister(&irnet_misc_device);
  973. DEXIT(MODULE_TRACE, "\n");
  974. }
  975. /*------------------------------------------------------------------*/
  976. /*
  977. * Module main entry point
  978. */
  979. static int __init
  980. irnet_init(void)
  981. {
  982. int err;
  983. /* Initialise both parts... */
  984. err = irda_irnet_init();
  985. if(!err)
  986. err = ppp_irnet_init();
  987. return err;
  988. }
  989. /*------------------------------------------------------------------*/
  990. /*
  991. * Module exit
  992. */
  993. static void __exit
  994. irnet_cleanup(void)
  995. {
  996. irda_irnet_cleanup();
  997. ppp_irnet_cleanup();
  998. }
  999. /*------------------------------------------------------------------*/
  1000. /*
  1001. * Module magic
  1002. */
  1003. module_init(irnet_init);
  1004. module_exit(irnet_cleanup);
  1005. MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
  1006. MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
  1007. MODULE_LICENSE("GPL");
  1008. MODULE_ALIAS_CHARDEV(10, 187);