irnet_ppp.c 32 KB

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