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