irnet_ppp.c 32 KB

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