]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/irda/irnet/irnet_ppp.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[mirror_ubuntu-bionic-kernel.git] / net / irda / irnet / irnet_ppp.c
1 /*
2 * IrNET protocol module : Synchronous PPP over an IrDA socket.
3 *
4 * Jean II - HPL `00 - <jt@hpl.hp.com>
5 *
6 * This file implement the PPP interface and /dev/irnet character device.
7 * The PPP interface hook to the ppp_generic module, handle all our
8 * relationship to the PPP code in the kernel (and by extension to pppd),
9 * and exchange PPP frames with this module (send/receive).
10 * The /dev/irnet device is used primarily for 2 functions :
11 * 1) as a stub for pppd (the ppp daemon), so that we can appropriately
12 * generate PPP sessions (we pretend we are a tty).
13 * 2) as a control channel (write commands, read events)
14 */
15
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include "irnet_ppp.h" /* Private header */
19 /* Please put other headers in irnet.h - Thanks */
20
21 /* Generic PPP callbacks (to call us) */
22 static const struct ppp_channel_ops irnet_ppp_ops = {
23 .start_xmit = ppp_irnet_send,
24 .ioctl = ppp_irnet_ioctl
25 };
26
27 /************************* CONTROL CHANNEL *************************/
28 /*
29 * When a pppd instance is not active on /dev/irnet, it acts as a control
30 * channel.
31 * Writing allow to set up the IrDA destination of the IrNET channel,
32 * and any application may be read events happening in IrNET...
33 */
34
35 /*------------------------------------------------------------------*/
36 /*
37 * Write is used to send a command to configure a IrNET channel
38 * before it is open by pppd. The syntax is : "command argument"
39 * Currently there is only two defined commands :
40 * o name : set the requested IrDA nickname of the IrNET peer.
41 * o addr : set the requested IrDA address of the IrNET peer.
42 * Note : the code is crude, but effective...
43 */
44 static inline ssize_t
45 irnet_ctrl_write(irnet_socket * ap,
46 const char __user *buf,
47 size_t count)
48 {
49 char command[IRNET_MAX_COMMAND];
50 char * start; /* Current command being processed */
51 char * next; /* Next command to process */
52 int length; /* Length of current command */
53
54 DENTER(CTRL_TRACE, "(ap=0x%p, count=%Zd)\n", ap, count);
55
56 /* Check for overflow... */
57 DABORT(count >= IRNET_MAX_COMMAND, -ENOMEM,
58 CTRL_ERROR, "Too much data !!!\n");
59
60 /* Get the data in the driver */
61 if(copy_from_user(command, buf, count))
62 {
63 DERROR(CTRL_ERROR, "Invalid user space pointer.\n");
64 return -EFAULT;
65 }
66
67 /* Safe terminate the string */
68 command[count] = '\0';
69 DEBUG(CTRL_INFO, "Command line received is ``%s'' (%Zd).\n",
70 command, count);
71
72 /* Check every commands in the command line */
73 next = command;
74 while(next != NULL)
75 {
76 /* Look at the next command */
77 start = next;
78
79 /* Scrap whitespaces before the command */
80 start = skip_spaces(start);
81
82 /* ',' is our command separator */
83 next = strchr(start, ',');
84 if(next)
85 {
86 *next = '\0'; /* Terminate command */
87 length = next - start; /* Length */
88 next++; /* Skip the '\0' */
89 }
90 else
91 length = strlen(start);
92
93 DEBUG(CTRL_INFO, "Found command ``%s'' (%d).\n", start, length);
94
95 /* Check if we recognised one of the known command
96 * We can't use "switch" with strings, so hack with "continue" */
97
98 /* First command : name -> Requested IrDA nickname */
99 if(!strncmp(start, "name", 4))
100 {
101 /* Copy the name only if is included and not "any" */
102 if((length > 5) && (strcmp(start + 5, "any")))
103 {
104 /* Strip out trailing whitespaces */
105 while(isspace(start[length - 1]))
106 length--;
107
108 /* Copy the name for later reuse */
109 memcpy(ap->rname, start + 5, length - 5);
110 ap->rname[length - 5] = '\0';
111 }
112 else
113 ap->rname[0] = '\0';
114 DEBUG(CTRL_INFO, "Got rname = ``%s''\n", ap->rname);
115
116 /* Restart the loop */
117 continue;
118 }
119
120 /* Second command : addr, daddr -> Requested IrDA destination address
121 * Also process : saddr -> Requested IrDA source address */
122 if((!strncmp(start, "addr", 4)) ||
123 (!strncmp(start, "daddr", 5)) ||
124 (!strncmp(start, "saddr", 5)))
125 {
126 __u32 addr = DEV_ADDR_ANY;
127
128 /* Copy the address only if is included and not "any" */
129 if((length > 5) && (strcmp(start + 5, "any")))
130 {
131 char * begp = start + 5;
132 char * endp;
133
134 /* Scrap whitespaces before the command */
135 begp = skip_spaces(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 */
181 static void
182 irnet_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 */
213 static inline int
214 irnet_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 */
275 static inline ssize_t
276 irnet_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 */
419 static inline unsigned int
420 irnet_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 */
463 static int
464 dev_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 */
479 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
480 DABORT(ap == NULL, -ENOMEM, FS_ERROR, "Can't allocate struct irnet...\n");
481
482 /* initialize the irnet structure */
483 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
504 return err;
505 }
506
507 /* For the control channel */
508 ap->event_index = irnet_events.index; /* Cancel all past events */
509
510 mutex_init(&ap->lock);
511
512 /* Put our stuff where we will be able to find it later */
513 file->private_data = ap;
514
515 DEXIT(FS_TRACE, " - ap=0x%p\n", ap);
516
517 return 0;
518 }
519
520
521 /*------------------------------------------------------------------*/
522 /*
523 * Close : when somebody close /dev/irnet
524 * Destroy the instance of /dev/irnet
525 */
526 static int
527 dev_irnet_close(struct inode * inode,
528 struct file * file)
529 {
530 irnet_socket * ap = file->private_data;
531
532 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
533 file, ap);
534 DABORT(ap == NULL, 0, FS_ERROR, "ap is NULL !!!\n");
535
536 /* Detach ourselves */
537 file->private_data = NULL;
538
539 /* Close IrDA stuff */
540 irda_irnet_destroy(ap);
541
542 /* Disconnect from the generic PPP layer if not already done */
543 if(ap->ppp_open)
544 {
545 DERROR(FS_ERROR, "Channel still registered - deregistering !\n");
546 ap->ppp_open = 0;
547 ppp_unregister_channel(&ap->chan);
548 }
549
550 kfree(ap);
551
552 DEXIT(FS_TRACE, "\n");
553 return 0;
554 }
555
556 /*------------------------------------------------------------------*/
557 /*
558 * Write does nothing.
559 * (we receive packet from ppp_generic through ppp_irnet_send())
560 */
561 static ssize_t
562 dev_irnet_write(struct file * file,
563 const char __user *buf,
564 size_t count,
565 loff_t * ppos)
566 {
567 irnet_socket * ap = file->private_data;
568
569 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
570 file, ap, count);
571 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
572
573 /* If we are connected to ppp_generic, let it handle the job */
574 if(ap->ppp_open)
575 return -EAGAIN;
576 else
577 return irnet_ctrl_write(ap, buf, count);
578 }
579
580 /*------------------------------------------------------------------*/
581 /*
582 * Read doesn't do much either.
583 * (pppd poll us, but ultimately reads through /dev/ppp)
584 */
585 static ssize_t
586 dev_irnet_read(struct file * file,
587 char __user * buf,
588 size_t count,
589 loff_t * ppos)
590 {
591 irnet_socket * ap = file->private_data;
592
593 DPASS(FS_TRACE, "(file=0x%p, ap=0x%p, count=%Zd)\n",
594 file, ap, count);
595 DABORT(ap == NULL, -ENXIO, FS_ERROR, "ap is NULL !!!\n");
596
597 /* If we are connected to ppp_generic, let it handle the job */
598 if(ap->ppp_open)
599 return -EAGAIN;
600 else
601 return irnet_ctrl_read(ap, file, buf, count);
602 }
603
604 /*------------------------------------------------------------------*/
605 /*
606 * Poll : called when someone do a select on /dev/irnet
607 */
608 static unsigned int
609 dev_irnet_poll(struct file * file,
610 poll_table * wait)
611 {
612 irnet_socket * ap = file->private_data;
613 unsigned int mask;
614
615 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p)\n",
616 file, ap);
617
618 mask = POLLOUT | POLLWRNORM;
619 DABORT(ap == NULL, mask, FS_ERROR, "ap is NULL !!!\n");
620
621 /* If we are connected to ppp_generic, let it handle the job */
622 if(!ap->ppp_open)
623 mask |= irnet_ctrl_poll(ap, file, wait);
624
625 DEXIT(FS_TRACE, " - mask=0x%X\n", mask);
626 return mask;
627 }
628
629 /*------------------------------------------------------------------*/
630 /*
631 * IOCtl : Called when someone does some ioctls on /dev/irnet
632 * This is the way pppd configure us and control us while the PPP
633 * instance is active.
634 */
635 static long
636 dev_irnet_ioctl(
637 struct file * file,
638 unsigned int cmd,
639 unsigned long arg)
640 {
641 irnet_socket * ap = file->private_data;
642 int err;
643 int val;
644 void __user *argp = (void __user *)arg;
645
646 DENTER(FS_TRACE, "(file=0x%p, ap=0x%p, cmd=0x%X)\n",
647 file, ap, cmd);
648
649 /* Basic checks... */
650 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
651 #ifdef SECURE_DEVIRNET
652 if(!capable(CAP_NET_ADMIN))
653 return -EPERM;
654 #endif /* SECURE_DEVIRNET */
655
656 err = -EFAULT;
657 switch(cmd)
658 {
659 /* Set discipline (should be N_SYNC_PPP or N_TTY) */
660 case TIOCSETD:
661 if(get_user(val, (int __user *)argp))
662 break;
663 if((val == N_SYNC_PPP) || (val == N_PPP))
664 {
665 DEBUG(FS_INFO, "Entering PPP discipline.\n");
666 /* PPP channel setup (ap->chan in configured in dev_irnet_open())*/
667 if (mutex_lock_interruptible(&ap->lock))
668 return -EINTR;
669
670 err = ppp_register_channel(&ap->chan);
671 if(err == 0)
672 {
673 /* Our ppp side is active */
674 ap->ppp_open = 1;
675
676 DEBUG(FS_INFO, "Trying to establish a connection.\n");
677 /* Setup the IrDA link now - may fail... */
678 irda_irnet_connect(ap);
679 }
680 else
681 DERROR(FS_ERROR, "Can't setup PPP channel...\n");
682
683 mutex_unlock(&ap->lock);
684 }
685 else
686 {
687 /* In theory, should be N_TTY */
688 DEBUG(FS_INFO, "Exiting PPP discipline.\n");
689 /* Disconnect from the generic PPP layer */
690 if (mutex_lock_interruptible(&ap->lock))
691 return -EINTR;
692
693 if(ap->ppp_open)
694 {
695 ap->ppp_open = 0;
696 ppp_unregister_channel(&ap->chan);
697 }
698 else
699 DERROR(FS_ERROR, "Channel not registered !\n");
700 err = 0;
701
702 mutex_unlock(&ap->lock);
703 }
704 break;
705
706 /* Query PPP channel and unit number */
707 case PPPIOCGCHAN:
708 if (mutex_lock_interruptible(&ap->lock))
709 return -EINTR;
710
711 if(ap->ppp_open && !put_user(ppp_channel_index(&ap->chan),
712 (int __user *)argp))
713 err = 0;
714
715 mutex_unlock(&ap->lock);
716 break;
717 case PPPIOCGUNIT:
718 if (mutex_lock_interruptible(&ap->lock))
719 return -EINTR;
720
721 if(ap->ppp_open && !put_user(ppp_unit_number(&ap->chan),
722 (int __user *)argp))
723 err = 0;
724
725 mutex_unlock(&ap->lock);
726 break;
727
728 /* All these ioctls can be passed both directly and from ppp_generic,
729 * so we just deal with them in one place...
730 */
731 case PPPIOCGFLAGS:
732 case PPPIOCSFLAGS:
733 case PPPIOCGASYNCMAP:
734 case PPPIOCSASYNCMAP:
735 case PPPIOCGRASYNCMAP:
736 case PPPIOCSRASYNCMAP:
737 case PPPIOCGXASYNCMAP:
738 case PPPIOCSXASYNCMAP:
739 case PPPIOCGMRU:
740 case PPPIOCSMRU:
741 DEBUG(FS_INFO, "Standard PPP ioctl.\n");
742 if(!capable(CAP_NET_ADMIN))
743 err = -EPERM;
744 else {
745 if (mutex_lock_interruptible(&ap->lock))
746 return -EINTR;
747
748 err = ppp_irnet_ioctl(&ap->chan, cmd, arg);
749
750 mutex_unlock(&ap->lock);
751 }
752 break;
753
754 /* TTY IOCTLs : Pretend that we are a tty, to keep pppd happy */
755 /* Get termios */
756 case TCGETS:
757 DEBUG(FS_INFO, "Get termios.\n");
758 if (mutex_lock_interruptible(&ap->lock))
759 return -EINTR;
760
761 #ifndef TCGETS2
762 if(!kernel_termios_to_user_termios((struct termios __user *)argp, &ap->termios))
763 err = 0;
764 #else
765 if(kernel_termios_to_user_termios_1((struct termios __user *)argp, &ap->termios))
766 err = 0;
767 #endif
768
769 mutex_unlock(&ap->lock);
770 break;
771 /* Set termios */
772 case TCSETSF:
773 DEBUG(FS_INFO, "Set termios.\n");
774 if (mutex_lock_interruptible(&ap->lock))
775 return -EINTR;
776
777 #ifndef TCGETS2
778 if(!user_termios_to_kernel_termios(&ap->termios, (struct termios __user *)argp))
779 err = 0;
780 #else
781 if(!user_termios_to_kernel_termios_1(&ap->termios, (struct termios __user *)argp))
782 err = 0;
783 #endif
784
785 mutex_unlock(&ap->lock);
786 break;
787
788 /* Set DTR/RTS */
789 case TIOCMBIS:
790 case TIOCMBIC:
791 /* Set exclusive/non-exclusive mode */
792 case TIOCEXCL:
793 case TIOCNXCL:
794 DEBUG(FS_INFO, "TTY compatibility.\n");
795 err = 0;
796 break;
797
798 case TCGETA:
799 DEBUG(FS_INFO, "TCGETA\n");
800 break;
801
802 case TCFLSH:
803 DEBUG(FS_INFO, "TCFLSH\n");
804 /* Note : this will flush buffers in PPP, so it *must* be done
805 * We should also worry that we don't accept junk here and that
806 * we get rid of our own buffers */
807 #ifdef FLUSH_TO_PPP
808 if (mutex_lock_interruptible(&ap->lock))
809 return -EINTR;
810 ppp_output_wakeup(&ap->chan);
811 mutex_unlock(&ap->lock);
812 #endif /* FLUSH_TO_PPP */
813 err = 0;
814 break;
815
816 case FIONREAD:
817 DEBUG(FS_INFO, "FIONREAD\n");
818 val = 0;
819 if(put_user(val, (int __user *)argp))
820 break;
821 err = 0;
822 break;
823
824 default:
825 DERROR(FS_ERROR, "Unsupported ioctl (0x%X)\n", cmd);
826 err = -ENOTTY;
827 }
828
829 DEXIT(FS_TRACE, " - err = 0x%X\n", err);
830 return err;
831 }
832
833 /************************** PPP CALLBACKS **************************/
834 /*
835 * This are the functions that the generic PPP driver in the kernel
836 * will call to communicate to us.
837 */
838
839 /*------------------------------------------------------------------*/
840 /*
841 * Prepare the ppp frame for transmission over the IrDA socket.
842 * We make sure that the header space is enough, and we change ppp header
843 * according to flags passed by pppd.
844 * This is not a callback, but just a helper function used in ppp_irnet_send()
845 */
846 static inline struct sk_buff *
847 irnet_prepare_skb(irnet_socket * ap,
848 struct sk_buff * skb)
849 {
850 unsigned char * data;
851 int proto; /* PPP protocol */
852 int islcp; /* Protocol == LCP */
853 int needaddr; /* Need PPP address */
854
855 DENTER(PPP_TRACE, "(ap=0x%p, skb=0x%p)\n",
856 ap, skb);
857
858 /* Extract PPP protocol from the frame */
859 data = skb->data;
860 proto = (data[0] << 8) + data[1];
861
862 /* LCP packets with codes between 1 (configure-request)
863 * and 7 (code-reject) must be sent as though no options
864 * have been negotiated. */
865 islcp = (proto == PPP_LCP) && (1 <= data[2]) && (data[2] <= 7);
866
867 /* compress protocol field if option enabled */
868 if((data[0] == 0) && (ap->flags & SC_COMP_PROT) && (!islcp))
869 skb_pull(skb,1);
870
871 /* Check if we need address/control fields */
872 needaddr = 2*((ap->flags & SC_COMP_AC) == 0 || islcp);
873
874 /* Is the skb headroom large enough to contain all IrDA-headers? */
875 if((skb_headroom(skb) < (ap->max_header_size + needaddr)) ||
876 (skb_shared(skb)))
877 {
878 struct sk_buff * new_skb;
879
880 DEBUG(PPP_INFO, "Reallocating skb\n");
881
882 /* Create a new skb */
883 new_skb = skb_realloc_headroom(skb, ap->max_header_size + needaddr);
884
885 /* We have to free the original skb anyway */
886 dev_kfree_skb(skb);
887
888 /* Did the realloc succeed ? */
889 DABORT(new_skb == NULL, NULL, PPP_ERROR, "Could not realloc skb\n");
890
891 /* Use the new skb instead */
892 skb = new_skb;
893 }
894
895 /* prepend address/control fields if necessary */
896 if(needaddr)
897 {
898 skb_push(skb, 2);
899 skb->data[0] = PPP_ALLSTATIONS;
900 skb->data[1] = PPP_UI;
901 }
902
903 DEXIT(PPP_TRACE, "\n");
904
905 return skb;
906 }
907
908 /*------------------------------------------------------------------*/
909 /*
910 * Send a packet to the peer over the IrTTP connection.
911 * Returns 1 iff the packet was accepted.
912 * Returns 0 iff packet was not consumed.
913 * If the packet was not accepted, we will call ppp_output_wakeup
914 * at some later time to reactivate flow control in ppp_generic.
915 */
916 static int
917 ppp_irnet_send(struct ppp_channel * chan,
918 struct sk_buff * skb)
919 {
920 irnet_socket * self = (struct irnet_socket *) chan->private;
921 int ret;
922
923 DENTER(PPP_TRACE, "(channel=0x%p, ap/self=0x%p)\n",
924 chan, self);
925
926 /* Check if things are somewhat valid... */
927 DASSERT(self != NULL, 0, PPP_ERROR, "Self is NULL !!!\n");
928
929 /* Check if we are connected */
930 if(!(test_bit(0, &self->ttp_open)))
931 {
932 #ifdef CONNECT_IN_SEND
933 /* Let's try to connect one more time... */
934 /* Note : we won't be connected after this call, but we should be
935 * ready for next packet... */
936 /* If we are already connecting, this will fail */
937 irda_irnet_connect(self);
938 #endif /* CONNECT_IN_SEND */
939
940 DEBUG(PPP_INFO, "IrTTP not ready ! (%ld-%ld)\n",
941 self->ttp_open, self->ttp_connect);
942
943 /* Note : we can either drop the packet or block the packet.
944 *
945 * Blocking the packet allow us a better connection time,
946 * because by calling ppp_output_wakeup() we can have
947 * ppp_generic resending the LCP request immediately to us,
948 * rather than waiting for one of pppd periodic transmission of
949 * LCP request.
950 *
951 * On the other hand, if we block all packet, all those periodic
952 * transmissions of pppd accumulate in ppp_generic, creating a
953 * backlog of LCP request. When we eventually connect later on,
954 * we have to transmit all this backlog before we can connect
955 * proper (if we don't timeout before).
956 *
957 * The current strategy is as follow :
958 * While we are attempting to connect, we block packets to get
959 * a better connection time.
960 * If we fail to connect, we drain the queue and start dropping packets
961 */
962 #ifdef BLOCK_WHEN_CONNECT
963 /* If we are attempting to connect */
964 if(test_bit(0, &self->ttp_connect))
965 {
966 /* Blocking packet, ppp_generic will retry later */
967 return 0;
968 }
969 #endif /* BLOCK_WHEN_CONNECT */
970
971 /* Dropping packet, pppd will retry later */
972 dev_kfree_skb(skb);
973 return 1;
974 }
975
976 /* Check if the queue can accept any packet, otherwise block */
977 if(self->tx_flow != FLOW_START)
978 DRETURN(0, PPP_INFO, "IrTTP queue full (%d skbs)...\n",
979 skb_queue_len(&self->tsap->tx_queue));
980
981 /* Prepare ppp frame for transmission */
982 skb = irnet_prepare_skb(self, skb);
983 DABORT(skb == NULL, 1, PPP_ERROR, "Prepare skb for Tx failed.\n");
984
985 /* Send the packet to IrTTP */
986 ret = irttp_data_request(self->tsap, skb);
987 if(ret < 0)
988 {
989 /*
990 * > IrTTPs tx queue is full, so we just have to
991 * > drop the frame! You might think that we should
992 * > just return -1 and don't deallocate the frame,
993 * > but that is dangerous since it's possible that
994 * > we have replaced the original skb with a new
995 * > one with larger headroom, and that would really
996 * > confuse do_dev_queue_xmit() in dev.c! I have
997 * > tried :-) DB
998 * Correction : we verify the flow control above (self->tx_flow),
999 * so we come here only if IrTTP doesn't like the packet (empty,
1000 * too large, IrTTP not connected). In those rare cases, it's ok
1001 * to drop it, we don't want to see it here again...
1002 * Jean II
1003 */
1004 DERROR(PPP_ERROR, "IrTTP doesn't like this packet !!! (0x%X)\n", ret);
1005 /* irttp_data_request already free the packet */
1006 }
1007
1008 DEXIT(PPP_TRACE, "\n");
1009 return 1; /* Packet has been consumed */
1010 }
1011
1012 /*------------------------------------------------------------------*/
1013 /*
1014 * Take care of the ioctls that ppp_generic doesn't want to deal with...
1015 * Note : we are also called from dev_irnet_ioctl().
1016 */
1017 static int
1018 ppp_irnet_ioctl(struct ppp_channel * chan,
1019 unsigned int cmd,
1020 unsigned long arg)
1021 {
1022 irnet_socket * ap = (struct irnet_socket *) chan->private;
1023 int err;
1024 int val;
1025 u32 accm[8];
1026 void __user *argp = (void __user *)arg;
1027
1028 DENTER(PPP_TRACE, "(channel=0x%p, ap=0x%p, cmd=0x%X)\n",
1029 chan, ap, cmd);
1030
1031 /* Basic checks... */
1032 DASSERT(ap != NULL, -ENXIO, PPP_ERROR, "ap is NULL...\n");
1033
1034 err = -EFAULT;
1035 switch(cmd)
1036 {
1037 /* PPP flags */
1038 case PPPIOCGFLAGS:
1039 val = ap->flags | ap->rbits;
1040 if(put_user(val, (int __user *) argp))
1041 break;
1042 err = 0;
1043 break;
1044 case PPPIOCSFLAGS:
1045 if(get_user(val, (int __user *) argp))
1046 break;
1047 ap->flags = val & ~SC_RCV_BITS;
1048 ap->rbits = val & SC_RCV_BITS;
1049 err = 0;
1050 break;
1051
1052 /* Async map stuff - all dummy to please pppd */
1053 case PPPIOCGASYNCMAP:
1054 if(put_user(ap->xaccm[0], (u32 __user *) argp))
1055 break;
1056 err = 0;
1057 break;
1058 case PPPIOCSASYNCMAP:
1059 if(get_user(ap->xaccm[0], (u32 __user *) argp))
1060 break;
1061 err = 0;
1062 break;
1063 case PPPIOCGRASYNCMAP:
1064 if(put_user(ap->raccm, (u32 __user *) argp))
1065 break;
1066 err = 0;
1067 break;
1068 case PPPIOCSRASYNCMAP:
1069 if(get_user(ap->raccm, (u32 __user *) argp))
1070 break;
1071 err = 0;
1072 break;
1073 case PPPIOCGXASYNCMAP:
1074 if(copy_to_user(argp, ap->xaccm, sizeof(ap->xaccm)))
1075 break;
1076 err = 0;
1077 break;
1078 case PPPIOCSXASYNCMAP:
1079 if(copy_from_user(accm, argp, sizeof(accm)))
1080 break;
1081 accm[2] &= ~0x40000000U; /* can't escape 0x5e */
1082 accm[3] |= 0x60000000U; /* must escape 0x7d, 0x7e */
1083 memcpy(ap->xaccm, accm, sizeof(ap->xaccm));
1084 err = 0;
1085 break;
1086
1087 /* Max PPP frame size */
1088 case PPPIOCGMRU:
1089 if(put_user(ap->mru, (int __user *) argp))
1090 break;
1091 err = 0;
1092 break;
1093 case PPPIOCSMRU:
1094 if(get_user(val, (int __user *) argp))
1095 break;
1096 if(val < PPP_MRU)
1097 val = PPP_MRU;
1098 ap->mru = val;
1099 err = 0;
1100 break;
1101
1102 default:
1103 DEBUG(PPP_INFO, "Unsupported ioctl (0x%X)\n", cmd);
1104 err = -ENOIOCTLCMD;
1105 }
1106
1107 DEXIT(PPP_TRACE, " - err = 0x%X\n", err);
1108 return err;
1109 }
1110
1111 /************************** INITIALISATION **************************/
1112 /*
1113 * Module initialisation and all that jazz...
1114 */
1115
1116 /*------------------------------------------------------------------*/
1117 /*
1118 * Hook our device callbacks in the filesystem, to connect our code
1119 * to /dev/irnet
1120 */
1121 static inline int __init
1122 ppp_irnet_init(void)
1123 {
1124 int err = 0;
1125
1126 DENTER(MODULE_TRACE, "()\n");
1127
1128 /* Allocate ourselves as a minor in the misc range */
1129 err = misc_register(&irnet_misc_device);
1130
1131 DEXIT(MODULE_TRACE, "\n");
1132 return err;
1133 }
1134
1135 /*------------------------------------------------------------------*/
1136 /*
1137 * Cleanup at exit...
1138 */
1139 static inline void __exit
1140 ppp_irnet_cleanup(void)
1141 {
1142 DENTER(MODULE_TRACE, "()\n");
1143
1144 /* De-allocate /dev/irnet minor in misc range */
1145 misc_deregister(&irnet_misc_device);
1146
1147 DEXIT(MODULE_TRACE, "\n");
1148 }
1149
1150 /*------------------------------------------------------------------*/
1151 /*
1152 * Module main entry point
1153 */
1154 static int __init
1155 irnet_init(void)
1156 {
1157 int err;
1158
1159 /* Initialise both parts... */
1160 err = irda_irnet_init();
1161 if(!err)
1162 err = ppp_irnet_init();
1163 return err;
1164 }
1165
1166 /*------------------------------------------------------------------*/
1167 /*
1168 * Module exit
1169 */
1170 static void __exit
1171 irnet_cleanup(void)
1172 {
1173 irda_irnet_cleanup();
1174 ppp_irnet_cleanup();
1175 }
1176
1177 /*------------------------------------------------------------------*/
1178 /*
1179 * Module magic
1180 */
1181 module_init(irnet_init);
1182 module_exit(irnet_cleanup);
1183 MODULE_AUTHOR("Jean Tourrilhes <jt@hpl.hp.com>");
1184 MODULE_DESCRIPTION("IrNET : Synchronous PPP over IrDA");
1185 MODULE_LICENSE("GPL");
1186 MODULE_ALIAS_CHARDEV(10, 187);