blob: f6e54fa97f47286ad43e247909b15a4b22339db9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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) */
20static 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 */
42static inline ssize_t
43irnet_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" */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +090096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* 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_get_discovery_log (self)
175 *
176 * Query the content on the discovery log if not done
177 *
178 * This function query the current content of the discovery log
179 * at the startup of the event channel and save it in the internal struct.
180 */
181static void
182irnet_get_discovery_log(irnet_socket * ap)
183{
184 __u16 mask = irlmp_service_to_hint(S_LAN);
185
186 /* Ask IrLMP for the current discovery log */
187 ap->discoveries = irlmp_get_discoveries(&ap->disco_number, mask,
188 DISCOVERY_DEFAULT_SLOTS);
189
190 /* Check if the we got some results */
191 if(ap->discoveries == NULL)
192 ap->disco_number = -1;
193
194 DEBUG(CTRL_INFO, "Got the log (0x%p), size is %d\n",
195 ap->discoveries, ap->disco_number);
196}
197
198/*------------------------------------------------------------------*/
199/*
200 * Function irnet_read_discovery_log (self, event)
201 *
202 * Read the content on the discovery log
203 *
204 * This function dump the current content of the discovery log
205 * at the startup of the event channel.
206 * Return 1 if wrote an event on the control channel...
207 *
208 * State of the ap->disco_XXX variables :
209 * Socket creation : discoveries = NULL ; disco_index = 0 ; disco_number = 0
210 * While reading : discoveries = ptr ; disco_index = X ; disco_number = Y
211 * After reading : discoveries = NULL ; disco_index = Y ; disco_number = -1
212 */
213static inline int
214irnet_read_discovery_log(irnet_socket * ap,
215 char * event)
216{
217 int done_event = 0;
218
219 DENTER(CTRL_TRACE, "(ap=0x%p, event=0x%p)\n",
220 ap, event);
221
222 /* Test if we have some work to do or we have already finished */
223 if(ap->disco_number == -1)
224 {
225 DEBUG(CTRL_INFO, "Already done\n");
226 return 0;
227 }
228
229 /* Test if it's the first time and therefore we need to get the log */
230 if(ap->discoveries == NULL)
231 irnet_get_discovery_log(ap);
232
233 /* Check if we have more item to dump */
234 if(ap->disco_index < ap->disco_number)
235 {
236 /* Write an event */
237 sprintf(event, "Found %08x (%s) behind %08x {hints %02X-%02X}\n",
238 ap->discoveries[ap->disco_index].daddr,
239 ap->discoveries[ap->disco_index].info,
240 ap->discoveries[ap->disco_index].saddr,
241 ap->discoveries[ap->disco_index].hints[0],
242 ap->discoveries[ap->disco_index].hints[1]);
243 DEBUG(CTRL_INFO, "Writing discovery %d : %s\n",
244 ap->disco_index, ap->discoveries[ap->disco_index].info);
245
246 /* We have an event */
247 done_event = 1;
248 /* Next discovery */
249 ap->disco_index++;
250 }
251
252 /* Check if we have done the last item */
253 if(ap->disco_index >= ap->disco_number)
254 {
255 /* No more items : remove the log and signal termination */
256 DEBUG(CTRL_INFO, "Cleaning up log (0x%p)\n",
257 ap->discoveries);
258 if(ap->discoveries != NULL)
259 {
260 /* Cleanup our copy of the discovery log */
261 kfree(ap->discoveries);
262 ap->discoveries = NULL;
263 }
264 ap->disco_number = -1;
265 }
266
267 return done_event;
268}
269#endif /* INITIAL_DISCOVERY */
270
271/*------------------------------------------------------------------*/
272/*
273 * Read is used to get IrNET events
274 */
275static inline ssize_t
276irnet_ctrl_read(irnet_socket * ap,
277 struct file * file,
278 char __user * buf,
279 size_t count)
280{
281 DECLARE_WAITQUEUE(wait, current);
282 char event[64]; /* Max event is 61 char */
283 ssize_t ret = 0;
284
285 DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
286
287 /* Check if we can write an event out in one go */
288 DABORT(count < sizeof(event), -EOVERFLOW, CTRL_ERROR, "Buffer to small.\n");
289
290#ifdef INITIAL_DISCOVERY
291 /* Check if we have read the log */
292 if(irnet_read_discovery_log(ap, event))
293 {
294 /* We have an event !!! Copy it to the user */
295 if(copy_to_user(buf, event, strlen(event)))
296 {
297 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
298 return -EFAULT;
299 }
300
301 DEXIT(CTRL_TRACE, "\n");
302 return(strlen(event));
303 }
304#endif /* INITIAL_DISCOVERY */
305
306 /* Put ourselves on the wait queue to be woken up */
307 add_wait_queue(&irnet_events.rwait, &wait);
308 current->state = TASK_INTERRUPTIBLE;
309 for(;;)
310 {
311 /* If there is unread events */
312 ret = 0;
313 if(ap->event_index != irnet_events.index)
314 break;
315 ret = -EAGAIN;
316 if(file->f_flags & O_NONBLOCK)
317 break;
318 ret = -ERESTARTSYS;
319 if(signal_pending(current))
320 break;
321 /* Yield and wait to be woken up */
322 schedule();
323 }
324 current->state = TASK_RUNNING;
325 remove_wait_queue(&irnet_events.rwait, &wait);
326
327 /* Did we got it ? */
328 if(ret != 0)
329 {
330 /* No, return the error code */
331 DEXIT(CTRL_TRACE, " - ret %Zd\n", ret);
332 return ret;
333 }
334
335 /* Which event is it ? */
336 switch(irnet_events.log[ap->event_index].event)
337 {
338 case IRNET_DISCOVER:
339 sprintf(event, "Discovered %08x (%s) behind %08x {hints %02X-%02X}\n",
340 irnet_events.log[ap->event_index].daddr,
341 irnet_events.log[ap->event_index].name,
342 irnet_events.log[ap->event_index].saddr,
343 irnet_events.log[ap->event_index].hints.byte[0],
344 irnet_events.log[ap->event_index].hints.byte[1]);
345 break;
346 case IRNET_EXPIRE:
347 sprintf(event, "Expired %08x (%s) behind %08x {hints %02X-%02X}\n",
348 irnet_events.log[ap->event_index].daddr,
349 irnet_events.log[ap->event_index].name,
350 irnet_events.log[ap->event_index].saddr,
351 irnet_events.log[ap->event_index].hints.byte[0],
352 irnet_events.log[ap->event_index].hints.byte[1]);
353 break;
354 case IRNET_CONNECT_TO:
355 sprintf(event, "Connected to %08x (%s) on ppp%d\n",
356 irnet_events.log[ap->event_index].daddr,
357 irnet_events.log[ap->event_index].name,
358 irnet_events.log[ap->event_index].unit);
359 break;
360 case IRNET_CONNECT_FROM:
361 sprintf(event, "Connection from %08x (%s) on ppp%d\n",
362 irnet_events.log[ap->event_index].daddr,
363 irnet_events.log[ap->event_index].name,
364 irnet_events.log[ap->event_index].unit);
365 break;
366 case IRNET_REQUEST_FROM:
367 sprintf(event, "Request from %08x (%s) behind %08x\n",
368 irnet_events.log[ap->event_index].daddr,
369 irnet_events.log[ap->event_index].name,
370 irnet_events.log[ap->event_index].saddr);
371 break;
372 case IRNET_NOANSWER_FROM:
373 sprintf(event, "No-answer from %08x (%s) on ppp%d\n",
374 irnet_events.log[ap->event_index].daddr,
375 irnet_events.log[ap->event_index].name,
376 irnet_events.log[ap->event_index].unit);
377 break;
378 case IRNET_BLOCKED_LINK:
379 sprintf(event, "Blocked link with %08x (%s) on ppp%d\n",
380 irnet_events.log[ap->event_index].daddr,
381 irnet_events.log[ap->event_index].name,
382 irnet_events.log[ap->event_index].unit);
383 break;
384 case IRNET_DISCONNECT_FROM:
385 sprintf(event, "Disconnection from %08x (%s) on ppp%d\n",
386 irnet_events.log[ap->event_index].daddr,
387 irnet_events.log[ap->event_index].name,
388 irnet_events.log[ap->event_index].unit);
389 break;
390 case IRNET_DISCONNECT_TO:
391 sprintf(event, "Disconnected to %08x (%s)\n",
392 irnet_events.log[ap->event_index].daddr,
393 irnet_events.log[ap->event_index].name);
394 break;
395 default:
396 sprintf(event, "Bug\n");
397 }
398 /* Increment our event index */
399 ap->event_index = (ap->event_index + 1) % IRNET_MAX_EVENTS;
400
401 DEBUG(CTRL_INFO, "Event is :%s", event);
402
403 /* Copy it to the user */
404 if(copy_to_user(buf, event, strlen(event)))
405 {
406 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
407 return -EFAULT;
408 }
409
410 DEXIT(CTRL_TRACE, "\n");
411 return(strlen(event));
412}
413
414/*------------------------------------------------------------------*/
415/*
416 * Poll : called when someone do a select on /dev/irnet.
417 * Just check if there are new events...
418 */
419static inline unsigned int
420irnet_ctrl_poll(irnet_socket * ap,
421 struct file * file,
422 poll_table * wait)
423{
424 unsigned int mask;
425
426 DENTER(CTRL_TRACE, "(ap=0x%p)\n", ap);
427
428 poll_wait(file, &irnet_events.rwait, wait);
429 mask = POLLOUT | POLLWRNORM;
430 /* If there is unread events */
431 if(ap->event_index != irnet_events.index)
432 mask |= POLLIN | POLLRDNORM;
433#ifdef INITIAL_DISCOVERY
434 if(ap->disco_number != -1)
435 {
436 /* Test if it's the first time and therefore we need to get the log */
437 if(ap->discoveries == NULL)
438 irnet_get_discovery_log(ap);
439 /* Recheck */
440 if(ap->disco_number != -1)
441 mask |= POLLIN | POLLRDNORM;
442 }
443#endif /* INITIAL_DISCOVERY */
444
445 DEXIT(CTRL_TRACE, " - mask=0x%X\n", mask);
446 return mask;
447}
448
449
450/*********************** FILESYSTEM CALLBACKS ***********************/
451/*
452 * Implement the usual open, read, write functions that will be called
453 * by the file system when some action is performed on /dev/irnet.
454 * Most of those actions will in fact be performed by "pppd" or
455 * the control channel, we just act as a redirector...
456 */
457
458/*------------------------------------------------------------------*/
459/*
460 * Open : when somebody open /dev/irnet
461 * We basically create a new instance of irnet and initialise it.
462 */
463static int
464dev_irnet_open(struct inode * inode,
465 struct file * file)
466{
467 struct irnet_socket * ap;
468 int err;
469
470 DENTER(FS_TRACE, "(file=0x%p)\n", file);
471
472#ifdef SECURE_DEVIRNET
473 /* This could (should?) be enforced by the permissions on /dev/irnet. */
474 if(!capable(CAP_NET_ADMIN))
475 return -EPERM;
476#endif /* SECURE_DEVIRNET */
477
478 /* Allocate a private structure for this IrNET instance */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700479 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");
481
482 /* initialize the irnet structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ap->file = file;
484
485 /* PPP channel setup */
486 ap->ppp_open = 0;
487 ap->chan.private = ap;
488 ap->chan.ops = &irnet_ppp_ops;
489 ap->chan.mtu = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
490 ap->chan.hdrlen = 2 + TTP_MAX_HEADER; /* for A/C + Max IrDA hdr */
491 /* PPP parameters */
492 ap->mru = (2048 - TTP_MAX_HEADER - 2 - PPP_HDRLEN);
493 ap->xaccm[0] = ~0U;
494 ap->xaccm[3] = 0x60000000U;
495 ap->raccm = ~0U;
496
497 /* Setup the IrDA part... */
498 err = irda_irnet_create(ap);
499 if(err)
500 {
501 DERROR(FS_ERROR, "Can't setup IrDA link...\n");
502 kfree(ap);
503 return err;
504 }
505
506 /* For the control channel */
507 ap->event_index = irnet_events.index; /* Cancel all past events */
508
509 /* Put our stuff where we will be able to find it later */
510 file->private_data = ap;
511
512 DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
513 return 0;
514}
515
516
517/*------------------------------------------------------------------*/
518/*
519 * Close : when somebody close /dev/irnet
520 * Destroy the instance of /dev/irnet
521 */
522static int
523dev_irnet_close(struct inode * inode,
524 struct file * file)
525{
526 irnet_socket * ap = (struct irnet_socket *) file->private_data;
527
528 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
529 file, ap);
530 DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
531
532 /* Detach ourselves */
533 file->private_data = NULL;
534
535 /* Close IrDA stuff */
536 irda_irnet_destroy(ap);
537
538 /* Disconnect from the generic PPP layer if not already done */
539 if(ap->ppp_open)
540 {
541 DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
542 ap->ppp_open = 0;
543 ppp_unregister_channel(&ap->chan);
544 }
545
546 kfree(ap);
547
548 DEXIT(FS_TRACE, "\n");
549 return 0;
550}
551
552/*------------------------------------------------------------------*/
553/*
554 * Write does nothing.
555 * (we receive packet from ppp_generic through ppp_irnet_send())
556 */
557static ssize_t
558dev_irnet_write(struct file * file,
559 const char __user *buf,
560 size_t count,
561 loff_t * ppos)
562{
563 irnet_socket * ap = (struct irnet_socket *) file->private_data;
564
565 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
566 file, ap, count);
567 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
568
569 /* If we are connected to ppp_generic, let it handle the job */
570 if(ap->ppp_open)
571 return -EAGAIN;
572 else
573 return irnet_ctrl_write(ap, buf, count);
574}
575
576/*------------------------------------------------------------------*/
577/*
578 * Read doesn't do much either.
579 * (pppd poll us, but ultimately reads through /dev/ppp)
580 */
581static ssize_t
582dev_irnet_read(struct file * file,
583 char __user * buf,
584 size_t count,
585 loff_t * ppos)
586{
587 irnet_socket * ap = (struct irnet_socket *) file->private_data;
588
589 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
590 file, ap, count);
591 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
592
593 /* If we are connected to ppp_generic, let it handle the job */
594 if(ap->ppp_open)
595 return -EAGAIN;
596 else
597 return irnet_ctrl_read(ap, file, buf, count);
598}
599
600/*------------------------------------------------------------------*/
601/*
602 * Poll : called when someone do a select on /dev/irnet
603 */
604static unsigned int
605dev_irnet_poll(struct file * file,
606 poll_table * wait)
607{
608 irnet_socket * ap = (struct irnet_socket *) file->private_data;
609 unsigned int mask;
610
611 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
612 file, ap);
613
614 mask = POLLOUT | POLLWRNORM;
615 DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
616
617 /* If we are connected to ppp_generic, let it handle the job */
618 if(!ap->ppp_open)
619 mask |= irnet_ctrl_poll(ap, file, wait);
620
621 DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
622 return(mask);
623}
624
625/*------------------------------------------------------------------*/
626/*
627 * IOCtl : Called when someone does some ioctls on /dev/irnet
628 * This is the way pppd configure us and control us while the PPP
629 * instance is active.
630 */
Alan Cox54064602008-05-25 23:43:11 -0700631static long
632dev_irnet_ioctl(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct file * file,
634 unsigned int cmd,
635 unsigned long arg)
636{
637 irnet_socket * ap = (struct irnet_socket *) file->private_data;
638 int err;
639 int val;
640 void __user *argp = (void __user *)arg;
641
642 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
643 file, ap, cmd);
644
645 /* Basic checks... */
646 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
647#ifdef SECURE_DEVIRNET
648 if(!capable(CAP_NET_ADMIN))
649 return -EPERM;
650#endif /* SECURE_DEVIRNET */
651
652 err = -EFAULT;
653 switch(cmd)
654 {
655 /* Set discipline (should be N_SYNC_PPP or N_TTY) */
656 case TIOCSETD:
657 if(get_user(val, (int __user *)argp))
658 break;
659 if((val == N_SYNC_PPP) || (val == N_PPP))
660 {
661 DEBUG(FS_INFO, "Entering PPP discipline.\n");
662 /* PPP channel setup (ap->chan in configued in dev_irnet_open())*/
Alan Cox54064602008-05-25 23:43:11 -0700663 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 err = ppp_register_channel(&ap->chan);
665 if(err == 0)
666 {
667 /* Our ppp side is active */
668 ap->ppp_open = 1;
669
670 DEBUG(FS_INFO, "Trying to establish a connection.\n");
671 /* Setup the IrDA link now - may fail... */
672 irda_irnet_connect(ap);
673 }
674 else
675 DERROR(FS_ERROR, "Can't setup PPP channel...\n");
Alan Cox54064602008-05-25 23:43:11 -0700676 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 else
679 {
680 /* In theory, should be N_TTY */
681 DEBUG(FS_INFO, "Exiting PPP discipline.\n");
682 /* Disconnect from the generic PPP layer */
Alan Cox54064602008-05-25 23:43:11 -0700683 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if(ap->ppp_open)
685 {
686 ap->ppp_open = 0;
687 ppp_unregister_channel(&ap->chan);
688 }
689 else
690 DERROR(FS_ERROR, "Channel not registered !\n");
691 err = 0;
Alan Cox54064602008-05-25 23:43:11 -0700692 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 break;
695
696 /* Query PPP channel and unit number */
697 case PPPIOCGCHAN:
Alan Cox54064602008-05-25 23:43:11 -0700698 if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),
699 (int __user *)argp))
700 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 case PPPIOCGUNIT:
Alan Cox54064602008-05-25 23:43:11 -0700703 lock_kernel();
704 if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),
705 (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 err = 0;
707 break;
708
709 /* All these ioctls can be passed both directly and from ppp_generic,
710 * so we just deal with them in one place...
711 */
712 case PPPIOCGFLAGS:
713 case PPPIOCSFLAGS:
714 case PPPIOCGASYNCMAP:
715 case PPPIOCSASYNCMAP:
716 case PPPIOCGRASYNCMAP:
717 case PPPIOCSRASYNCMAP:
718 case PPPIOCGXASYNCMAP:
719 case PPPIOCSXASYNCMAP:
720 case PPPIOCGMRU:
721 case PPPIOCSMRU:
722 DEBUG(FS_INFO, "Standard PPP ioctl.\n");
723 if(!capable(CAP_NET_ADMIN))
724 err = -EPERM;
Alan Cox54064602008-05-25 23:43:11 -0700725 else {
726 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
Alan Cox54064602008-05-25 23:43:11 -0700728 unlock_kernel();
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 break;
731
732 /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
733 /* Get termios */
734 case TCGETS:
735 DEBUG(FS_INFO, "Get termios.\n");
Alan Cox54064602008-05-25 23:43:11 -0700736 lock_kernel();
David S. Miller49259d32007-11-01 02:26:38 -0700737#ifndef TCGETS2
Alan Cox54064602008-05-25 23:43:11 -0700738 if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
739 err = 0;
David S. Miller49259d32007-11-01 02:26:38 -0700740#else
741 if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
Alan Cox54064602008-05-25 23:43:11 -0700742 err = 0;
David S. Miller49259d32007-11-01 02:26:38 -0700743#endif
Alan Cox54064602008-05-25 23:43:11 -0700744 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 break;
746 /* Set termios */
747 case TCSETSF:
748 DEBUG(FS_INFO, "Set termios.\n");
Alan Cox54064602008-05-25 23:43:11 -0700749 lock_kernel();
David S. Miller49259d32007-11-01 02:26:38 -0700750#ifndef TCGETS2
Alan Cox54064602008-05-25 23:43:11 -0700751 if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
752 err = 0;
David S. Miller49259d32007-11-01 02:26:38 -0700753#else
Alan Cox54064602008-05-25 23:43:11 -0700754 if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
755 err = 0;
David S. Miller49259d32007-11-01 02:26:38 -0700756#endif
Alan Cox54064602008-05-25 23:43:11 -0700757 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 break;
759
760 /* Set DTR/RTS */
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900761 case TIOCMBIS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 case TIOCMBIC:
763 /* Set exclusive/non-exclusive mode */
764 case TIOCEXCL:
765 case TIOCNXCL:
766 DEBUG(FS_INFO, "TTY compatibility.\n");
767 err = 0;
768 break;
769
770 case TCGETA:
771 DEBUG(FS_INFO, "TCGETA\n");
772 break;
773
774 case TCFLSH:
775 DEBUG(FS_INFO, "TCFLSH\n");
776 /* Note : this will flush buffers in PPP, so it *must* be done
777 * We should also worry that we don't accept junk here and that
778 * we get rid of our own buffers */
779#ifdef FLUSH_TO_PPP
Alan Cox54064602008-05-25 23:43:11 -0700780 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 ppp_output_wakeup(&ap->chan);
Alan Cox54064602008-05-25 23:43:11 -0700782 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#endif /* FLUSH_TO_PPP */
784 err = 0;
785 break;
786
787 case FIONREAD:
788 DEBUG(FS_INFO, "FIONREAD\n");
789 val = 0;
790 if(put_user(val, (int __user *)argp))
791 break;
792 err = 0;
793 break;
794
795 default:
796 DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
Alan Cox54064602008-05-25 23:43:11 -0700797 err = -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 DEXIT(FS_TRACE, " - err = 0x%X\n", err);
801 return err;
802}
803
804/************************** PPP CALLBACKS **************************/
805/*
806 * This are the functions that the generic PPP driver in the kernel
807 * will call to communicate to us.
808 */
809
810/*------------------------------------------------------------------*/
811/*
812 * Prepare the ppp frame for transmission over the IrDA socket.
813 * We make sure that the header space is enough, and we change ppp header
814 * according to flags passed by pppd.
815 * This is not a callback, but just a helper function used in ppp_irnet_send()
816 */
817static inline struct sk_buff *
818irnet_prepare_skb(irnet_socket * ap,
819 struct sk_buff * skb)
820{
821 unsigned char * data;
822 int proto; /* PPP protocol */
823 int islcp; /* Protocol == LCP */
824 int needaddr; /* Need PPP address */
825
826 DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
827 ap, skb);
828
829 /* Extract PPP protocol from the frame */
830 data = skb->data;
831 proto = (data[0] << 8) + data[1];
832
833 /* LCP packets with codes between 1 (configure-request)
834 * and 7 (code-reject) must be sent as though no options
835 * have been negotiated. */
836 islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
837
838 /* compress protocol field if option enabled */
839 if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
840 skb_pull(skb,1);
841
842 /* Check if we need address/control fields */
843 needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
844
845 /* Is the skb headroom large enough to contain all IrDA-headers? */
846 if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
847 (skb_shared(skb)))
848 {
849 struct sk_buff * new_skb;
850
851 DEBUG(PPP_INFO, "Reallocating skb\n");
852
853 /* Create a new skb */
854 new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
855
856 /* We have to free the original skb anyway */
857 dev_kfree_skb(skb);
858
859 /* Did the realloc succeed ? */
860 DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
861
862 /* Use the new skb instead */
863 skb = new_skb;
864 }
865
866 /* prepend address/control fields if necessary */
867 if(needaddr)
868 {
869 skb_push(skb, 2);
870 skb->data[0] = PPP_ALLSTATIONS;
871 skb->data[1] = PPP_UI;
872 }
873
874 DEXIT(PPP_TRACE, "\n");
875
876 return skb;
877}
878
879/*------------------------------------------------------------------*/
880/*
881 * Send a packet to the peer over the IrTTP connection.
882 * Returns 1 iff the packet was accepted.
883 * Returns 0 iff packet was not consumed.
884 * If the packet was not accepted, we will call ppp_output_wakeup
885 * at some later time to reactivate flow control in ppp_generic.
886 */
887static int
888ppp_irnet_send(struct ppp_channel * chan,
889 struct sk_buff * skb)
890{
891 irnet_socket * self = (struct irnet_socket *) chan->private;
892 int ret;
893
894 DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
895 chan, self);
896
897 /* Check if things are somewhat valid... */
898 DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
899
900 /* Check if we are connected */
901 if(!(test_bit(0, &self->ttp_open)))
902 {
903#ifdef CONNECT_IN_SEND
904 /* Let's try to connect one more time... */
905 /* Note : we won't be connected after this call, but we should be
906 * ready for next packet... */
907 /* If we are already connecting, this will fail */
908 irda_irnet_connect(self);
909#endif /* CONNECT_IN_SEND */
910
911 DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
912 self->ttp_open, self->ttp_connect);
913
914 /* Note : we can either drop the packet or block the packet.
915 *
916 * Blocking the packet allow us a better connection time,
917 * because by calling ppp_output_wakeup() we can have
918 * ppp_generic resending the LCP request immediately to us,
919 * rather than waiting for one of pppd periodic transmission of
920 * LCP request.
921 *
922 * On the other hand, if we block all packet, all those periodic
923 * transmissions of pppd accumulate in ppp_generic, creating a
924 * backlog of LCP request. When we eventually connect later on,
925 * we have to transmit all this backlog before we can connect
926 * proper (if we don't timeout before).
927 *
928 * The current strategy is as follow :
929 * While we are attempting to connect, we block packets to get
930 * a better connection time.
931 * If we fail to connect, we drain the queue and start dropping packets
932 */
933#ifdef BLOCK_WHEN_CONNECT
934 /* If we are attempting to connect */
935 if(test_bit(0, &self->ttp_connect))
936 {
937 /* Blocking packet, ppp_generic will retry later */
938 return 0;
939 }
940#endif /* BLOCK_WHEN_CONNECT */
941
942 /* Dropping packet, pppd will retry later */
943 dev_kfree_skb(skb);
944 return 1;
945 }
946
947 /* Check if the queue can accept any packet, otherwise block */
948 if(self->tx_flow != FLOW_START)
949 DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
950 skb_queue_len(&self->tsap->tx_queue));
951
952 /* Prepare ppp frame for transmission */
953 skb = irnet_prepare_skb(self, skb);
954 DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
955
956 /* Send the packet to IrTTP */
957 ret = irttp_data_request(self->tsap, skb);
958 if(ret < 0)
959 {
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900960 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 * > IrTTPs tx queue is full, so we just have to
962 * > drop the frame! You might think that we should
963 * > just return -1 and don't deallocate the frame,
964 * > but that is dangerous since it's possible that
965 * > we have replaced the original skb with a new
966 * > one with larger headroom, and that would really
967 * > confuse do_dev_queue_xmit() in dev.c! I have
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +0900968 * > tried :-) DB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 * Correction : we verify the flow control above (self->tx_flow),
970 * so we come here only if IrTTP doesn't like the packet (empty,
971 * too large, IrTTP not connected). In those rare cases, it's ok
972 * to drop it, we don't want to see it here again...
973 * Jean II
974 */
975 DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
976 /* irttp_data_request already free the packet */
977 }
978
979 DEXIT(PPP_TRACE, "\n");
980 return 1; /* Packet has been consumed */
981}
982
983/*------------------------------------------------------------------*/
984/*
985 * Take care of the ioctls that ppp_generic doesn't want to deal with...
986 * Note : we are also called from dev_irnet_ioctl().
987 */
988static int
989ppp_irnet_ioctl(struct ppp_channel * chan,
990 unsigned int cmd,
991 unsigned long arg)
992{
993 irnet_socket * ap = (struct irnet_socket *) chan->private;
994 int err;
995 int val;
996 u32 accm[8];
997 void __user *argp = (void __user *)arg;
998
999 DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
1000 chan, ap, cmd);
1001
1002 /* Basic checks... */
1003 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
1004
1005 err = -EFAULT;
1006 switch(cmd)
1007 {
1008 /* PPP flags */
1009 case PPPIOCGFLAGS:
1010 val = ap->flags | ap->rbits;
1011 if(put_user(val, (int __user *) argp))
1012 break;
1013 err = 0;
1014 break;
1015 case PPPIOCSFLAGS:
1016 if(get_user(val, (int __user *) argp))
1017 break;
1018 ap->flags = val & ~SC_RCV_BITS;
1019 ap->rbits = val & SC_RCV_BITS;
1020 err = 0;
1021 break;
1022
1023 /* Async map stuff - all dummy to please pppd */
1024 case PPPIOCGASYNCMAP:
1025 if(put_user(ap->xaccm[0], (u32 __user *) argp))
1026 break;
1027 err = 0;
1028 break;
1029 case PPPIOCSASYNCMAP:
1030 if(get_user(ap->xaccm[0], (u32 __user *) argp))
1031 break;
1032 err = 0;
1033 break;
1034 case PPPIOCGRASYNCMAP:
1035 if(put_user(ap->raccm, (u32 __user *) argp))
1036 break;
1037 err = 0;
1038 break;
1039 case PPPIOCSRASYNCMAP:
1040 if(get_user(ap->raccm, (u32 __user *) argp))
1041 break;
1042 err = 0;
1043 break;
1044 case PPPIOCGXASYNCMAP:
1045 if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
1046 break;
1047 err = 0;
1048 break;
1049 case PPPIOCSXASYNCMAP:
1050 if(copy_from_user(accm, argp, sizeof(accm)))
1051 break;
1052 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
1053 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
1054 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
1055 err = 0;
1056 break;
1057
1058 /* Max PPP frame size */
1059 case PPPIOCGMRU:
1060 if(put_user(ap->mru, (int __user *) argp))
1061 break;
1062 err = 0;
1063 break;
1064 case PPPIOCSMRU:
1065 if(get_user(val, (int __user *) argp))
1066 break;
1067 if(val < PPP_MRU)
1068 val = PPP_MRU;
1069 ap->mru = val;
1070 err = 0;
1071 break;
1072
1073 default:
1074 DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
1075 err = -ENOIOCTLCMD;
1076 }
1077
1078 DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
1079 return err;
1080}
1081
1082/************************** INITIALISATION **************************/
1083/*
1084 * Module initialisation and all that jazz...
1085 */
1086
1087/*------------------------------------------------------------------*/
1088/*
1089 * Hook our device callbacks in the filesystem, to connect our code
1090 * to /dev/irnet
1091 */
1092static inline int __init
1093ppp_irnet_init(void)
1094{
1095 int err = 0;
1096
1097 DENTER(MODULE_TRACE, "()\n");
1098
1099 /* Allocate ourselves as a minor in the misc range */
1100 err = misc_register(&irnet_misc_device);
1101
1102 DEXIT(MODULE_TRACE, "\n");
1103 return err;
1104}
1105
1106/*------------------------------------------------------------------*/
1107/*
1108 * Cleanup at exit...
1109 */
1110static inline void __exit
1111ppp_irnet_cleanup(void)
1112{
1113 DENTER(MODULE_TRACE, "()\n");
1114
1115 /* De-allocate /dev/irnet minor in misc range */
1116 misc_deregister(&irnet_misc_device);
1117
1118 DEXIT(MODULE_TRACE, "\n");
1119}
1120
1121/*------------------------------------------------------------------*/
1122/*
1123 * Module main entry point
1124 */
Adrian Bunkbf73d1c2005-08-16 20:45:45 -07001125static int __init
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126irnet_init(void)
1127{
1128 int err;
1129
1130 /* Initialise both parts... */
1131 err = irda_irnet_init();
1132 if(!err)
1133 err = ppp_irnet_init();
1134 return err;
1135}
1136
1137/*------------------------------------------------------------------*/
1138/*
1139 * Module exit
1140 */
1141static void __exit
1142irnet_cleanup(void)
1143{
1144 irda_irnet_cleanup();
1145 ppp_irnet_cleanup();
1146}
1147
1148/*------------------------------------------------------------------*/
1149/*
1150 * Module magic
1151 */
1152module_init(irnet_init);
1153module_exit(irnet_cleanup);
1154MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
YOSHIFUJI Hideaki6819bc22007-02-09 23:24:53 +09001155MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156MODULE_LICENSE("GPL");
1157MODULE_ALIAS_CHARDEV(10, 187);