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