irnet_ppp.c 32 KB

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