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