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 IRDA interface of IrNET.
7 * Basically, we sit on top of IrTTP. We set up IrTTP, IrIAS properly,
8 * and exchange frames with IrTTP.
9 */
10
11 #include "irnet_irda.h" /* Private header */
12
13 /************************* CONTROL CHANNEL *************************/
14 /*
15 * When ppp is not active, /dev/irnet act as a control channel.
16 * Writting allow to set up the IrDA destination of the IrNET channel,
17 * and any application may be read events happening on IrNET...
18 */
19
20 /*------------------------------------------------------------------*/
21 /*
22 * Post an event to the control channel...
23 * Put the event in the log, and then wait all process blocked on read
24 * so they can read the log...
25 */
26 static void
irnet_post_event(irnet_socket * ap,irnet_event event,__u32 saddr,__u32 daddr,char * name)27 irnet_post_event(irnet_socket * ap,
28 irnet_event event,
29 __u32 saddr,
30 __u32 daddr,
31 char * name)
32 {
33 unsigned long flags; /* For spinlock */
34 int index; /* In the log */
35
36 DENTER(CTRL_TRACE, "(ap=0x%X, event=%d, daddr=%08x, name=``%s'')\n",
37 (unsigned int) ap, event, daddr, name);
38
39 /* Protect this section via spinlock.
40 * Note : as we are the only event producer, we only need to exclude
41 * ourself when touching the log, which is nice and easy.
42 */
43 spin_lock_irqsave(&irnet_events.spinlock, flags);
44
45 /* Copy the event in the log */
46 index = irnet_events.index;
47 irnet_events.log[index].event = event;
48 irnet_events.log[index].daddr = daddr;
49 irnet_events.log[index].saddr = saddr;
50 /* Try to copy IrDA nickname */
51 if(name)
52 strcpy(irnet_events.log[index].name, name);
53 else
54 irnet_events.log[index].name[0] = '\0';
55 /* Try to get ppp unit number */
56 if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
57 irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
58 else
59 irnet_events.log[index].unit = -1;
60
61 /* Increment the index
62 * Note that we increment the index only after the event is written,
63 * to make sure that the readers don't get garbage... */
64 irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
65
66 DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
67
68 /* Spin lock end */
69 spin_unlock_irqrestore(&irnet_events.spinlock, flags);
70
71 /* Now : wake up everybody waiting for events... */
72 wake_up_interruptible_all(&irnet_events.rwait);
73
74 DEXIT(CTRL_TRACE, "\n");
75 }
76
77 /************************* IRDA SUBROUTINES *************************/
78 /*
79 * These are a bunch of subroutines called from other functions
80 * down there, mostly common code or to improve readability...
81 *
82 * Note : we duplicate quite heavily some routines of af_irda.c,
83 * because our input structure (self) is quite different
84 * (struct irnet instead of struct irda_sock), which make sharing
85 * the same code impossible (at least, without templates).
86 */
87
88 /*------------------------------------------------------------------*/
89 /*
90 * Function irda_open_tsap (self)
91 *
92 * Open local Transport Service Access Point (TSAP)
93 *
94 * Create a IrTTP instance for us and set all the IrTTP callbacks.
95 */
96 static inline int
irnet_open_tsap(irnet_socket * self)97 irnet_open_tsap(irnet_socket * self)
98 {
99 notify_t notify; /* Callback structure */
100
101 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
102
103 DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
104
105 /* Initialize IrTTP callbacks to be used by the IrDA stack */
106 irda_notify_init(¬ify);
107 notify.connect_confirm = irnet_connect_confirm;
108 notify.connect_indication = irnet_connect_indication;
109 notify.disconnect_indication = irnet_disconnect_indication;
110 notify.data_indication = irnet_data_indication;
111 /*notify.udata_indication = NULL;*/
112 notify.flow_indication = irnet_flow_indication;
113 notify.status_indication = irnet_status_indication;
114 notify.instance = self;
115 strncpy(notify.name, IRNET_NOTIFY_NAME, NOTIFY_MAX_NAME);
116
117 /* Open an IrTTP instance */
118 self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
119 ¬ify);
120 DABORT(self->tsap == NULL, -ENOMEM,
121 IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
122
123 /* Remember which TSAP selector we actually got */
124 self->stsap_sel = self->tsap->stsap_sel;
125
126 DEXIT(IRDA_SR_TRACE, " - tsap=0x%X, sel=0x%X\n",
127 (unsigned int) self->tsap, self->stsap_sel);
128 return 0;
129 }
130
131 /*------------------------------------------------------------------*/
132 /*
133 * Function irnet_ias_to_tsap (self, result, value)
134 *
135 * Examine an IAS object and extract TSAP
136 *
137 * We do an IAP query to find the TSAP associated with the IrNET service.
138 * When IrIAP pass us the result of the query, this function look at
139 * the return values to check for failures and extract the TSAP if
140 * possible.
141 * Also deallocate value
142 * The failure is in self->errno
143 * Return TSAP or -1
144 */
145 static inline __u8
irnet_ias_to_tsap(irnet_socket * self,int result,struct ias_value * value)146 irnet_ias_to_tsap(irnet_socket * self,
147 int result,
148 struct ias_value * value)
149 {
150 __u8 dtsap_sel = 0; /* TSAP we are looking for */
151
152 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
153
154 /* By default, no error */
155 self->errno = 0;
156
157 /* Check if request succeeded */
158 switch(result)
159 {
160 /* Standard errors : service not available */
161 case IAS_CLASS_UNKNOWN:
162 case IAS_ATTRIB_UNKNOWN:
163 DEBUG(IRDA_SR_INFO, "IAS object doesn't exist ! (%d)\n", result);
164 self->errno = -EADDRNOTAVAIL;
165 break;
166
167 /* Other errors, most likely IrDA stack failure */
168 default :
169 DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
170 self->errno = -EHOSTUNREACH;
171 break;
172
173 /* Success : we got what we wanted */
174 case IAS_SUCCESS:
175 break;
176 }
177
178 /* Check what was returned to us */
179 if(value != NULL)
180 {
181 /* What type of argument have we got ? */
182 switch(value->type)
183 {
184 case IAS_INTEGER:
185 DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
186 if(value->t.integer != -1)
187 /* Get the remote TSAP selector */
188 dtsap_sel = value->t.integer;
189 else
190 self->errno = -EADDRNOTAVAIL;
191 break;
192 default:
193 self->errno = -EADDRNOTAVAIL;
194 DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
195 break;
196 }
197
198 /* Cleanup */
199 irias_delete_value(value);
200 }
201 else /* value == NULL */
202 {
203 /* Nothing returned to us - usually result != SUCCESS */
204 if(!(self->errno))
205 {
206 DERROR(IRDA_SR_ERROR,
207 "IrDA bug : result == SUCCESS && value == NULL\n");
208 self->errno = -EHOSTUNREACH;
209 }
210 }
211 DEXIT(IRDA_SR_TRACE, "\n");
212
213 /* Return the TSAP */
214 return(dtsap_sel);
215 }
216
217 /*------------------------------------------------------------------*/
218 /*
219 * Function irnet_find_lsap_sel (self)
220 *
221 * Try to lookup LSAP selector in remote LM-IAS
222 *
223 * Basically, we start a IAP query, and then go to sleep. When the query
224 * return, irnet_getvalue_confirm will wake us up, and we can examine the
225 * result of the query...
226 * Note that in some case, the query fail even before we go to sleep,
227 * creating some races...
228 */
229 static inline int
irnet_find_lsap_sel(irnet_socket * self)230 irnet_find_lsap_sel(irnet_socket * self)
231 {
232 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
233
234 /* This should not happen */
235 DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
236
237 /* Create an IAP instance, will be closed in irnet_getvalue_confirm() */
238 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
239 irnet_getvalue_confirm);
240
241 /* Treat unexpected signals as disconnect */
242 self->errno = -EHOSTUNREACH;
243
244 /* Query remote LM-IAS */
245 iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
246 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
247
248 /* The above request is non-blocking.
249 * After a while, IrDA will call us back in irnet_getvalue_confirm()
250 * We will then call irnet_ias_to_tsap() and finish the
251 * connection procedure */
252
253 DEXIT(IRDA_SR_TRACE, "\n");
254 return 0;
255 }
256
257 /*------------------------------------------------------------------*/
258 /*
259 * Function irnet_connect_tsap (self)
260 *
261 * Initialise the TTP socket and initiate TTP connection
262 *
263 */
264 static inline int
irnet_connect_tsap(irnet_socket * self)265 irnet_connect_tsap(irnet_socket * self)
266 {
267 int err;
268
269 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
270
271 /* Open a local TSAP (an IrTTP instance) */
272 err = irnet_open_tsap(self);
273 if(err != 0)
274 {
275 clear_bit(0, &self->ttp_connect);
276 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
277 return(err);
278 }
279
280 /* Connect to remote device */
281 err = irttp_connect_request(self->tsap, self->dtsap_sel,
282 self->rsaddr, self->daddr, NULL,
283 self->max_sdu_size_rx, NULL);
284 if(err != 0)
285 {
286 clear_bit(0, &self->ttp_connect);
287 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
288 return(err);
289 }
290
291 /* The above call is non-blocking.
292 * After a while, the IrDA stack will either call us back in
293 * irnet_connect_confirm() or irnet_disconnect_indication()
294 * See you there ;-) */
295
296 DEXIT(IRDA_SR_TRACE, "\n");
297 return(err);
298 }
299
300 /*------------------------------------------------------------------*/
301 /*
302 * Function irnet_discover_next_daddr (self)
303 *
304 * Query the IrNET TSAP of the next device in the log.
305 *
306 * Used in the TSAP discovery procedure.
307 */
308 static inline int
irnet_discover_next_daddr(irnet_socket * self)309 irnet_discover_next_daddr(irnet_socket * self)
310 {
311 /* Close the last instance of IrIAP, and open a new one.
312 * We can't reuse the IrIAP instance in the IrIAP callback */
313 if(self->iriap)
314 {
315 iriap_close(self->iriap);
316 self->iriap = NULL;
317 }
318 /* Create a new IAP instance */
319 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
320 irnet_discovervalue_confirm);
321
322 /* Next discovery - before the call to avoid races */
323 self->disco_index++;
324
325 /* Check if we have one more address to try */
326 if(self->disco_index < self->disco_number)
327 {
328 /* Query remote LM-IAS */
329 iriap_getvaluebyclass_request(self->iriap,
330 self->discoveries[self->disco_index].saddr,
331 self->discoveries[self->disco_index].daddr,
332 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
333 /* The above request is non-blocking.
334 * After a while, IrDA will call us back in irnet_discovervalue_confirm()
335 * We will then call irnet_ias_to_tsap() and come back here again... */
336 return(0);
337 }
338 else
339 return(1);
340 }
341
342 /*------------------------------------------------------------------*/
343 /*
344 * Function irnet_discover_daddr_and_lsap_sel (self)
345 *
346 * This try to find a device with the requested service.
347 *
348 * Initiate a TSAP discovery procedure.
349 * It basically look into the discovery log. For each address in the list,
350 * it queries the LM-IAS of the device to find if this device offer
351 * the requested service.
352 * If there is more than one node supporting the service, we complain
353 * to the user (it should move devices around).
354 * If we find one node which have the requested TSAP, we connect to it.
355 *
356 * This function just start the whole procedure. It request the discovery
357 * log and submit the first IAS query.
358 * The bulk of the job is handled in irnet_discovervalue_confirm()
359 *
360 * Note : this procedure fails if there is more than one device in range
361 * on the same dongle, because IrLMP doesn't disconnect the LAP when the
362 * last LSAP is closed. Moreover, we would need to wait the LAP
363 * disconnection...
364 */
365 static inline int
irnet_discover_daddr_and_lsap_sel(irnet_socket * self)366 irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
367 {
368 int ret;
369
370 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
371
372 /* Ask lmp for the current discovery log */
373 self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
374 DISCOVERY_DEFAULT_SLOTS);
375
376 /* Check if the we got some results */
377 if(self->discoveries == NULL)
378 {
379 self->disco_number = -1;
380 clear_bit(0, &self->ttp_connect);
381 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
382 }
383 DEBUG(IRDA_SR_INFO, "Got the log (0x%X), size is %d\n",
384 (unsigned int) self->discoveries, self->disco_number);
385
386 /* Start with the first discovery */
387 self->disco_index = -1;
388 self->daddr = DEV_ADDR_ANY;
389
390 /* This will fail if the log is empty - this is non-blocking */
391 ret = irnet_discover_next_daddr(self);
392 if(ret)
393 {
394 /* Close IAP */
395 iriap_close(self->iriap);
396 self->iriap = NULL;
397
398 /* Cleanup our copy of the discovery log */
399 kfree(self->discoveries);
400 self->discoveries = NULL;
401
402 clear_bit(0, &self->ttp_connect);
403 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
404 }
405
406 /* Follow me in irnet_discovervalue_confirm() */
407
408 DEXIT(IRDA_SR_TRACE, "\n");
409 return(0);
410 }
411
412 /*------------------------------------------------------------------*/
413 /*
414 * Function irnet_dname_to_daddr (self)
415 *
416 * Convert an IrDA nickname to a valid IrDA address
417 *
418 * It basically look into the discovery log until there is a match.
419 */
420 static inline int
irnet_dname_to_daddr(irnet_socket * self)421 irnet_dname_to_daddr(irnet_socket * self)
422 {
423 struct irda_device_info *discoveries; /* Copy of the discovery log */
424 int number; /* Number of nodes in the log */
425 int i;
426
427 DENTER(IRDA_SR_TRACE, "(self=0x%X)\n", (unsigned int) self);
428
429 /* Ask lmp for the current discovery log */
430 discoveries = irlmp_get_discoveries(&number, 0xffff,
431 DISCOVERY_DEFAULT_SLOTS);
432 /* Check if the we got some results */
433 if(discoveries == NULL)
434 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
435
436 /*
437 * Now, check all discovered devices (if any), and connect
438 * client only about the services that the client is
439 * interested in...
440 */
441 for(i = 0; i < number; i++)
442 {
443 /* Does the name match ? */
444 if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
445 {
446 /* Yes !!! Get it.. */
447 self->daddr = discoveries[i].daddr;
448 DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
449 self->rname, self->daddr);
450 kfree(discoveries);
451 DEXIT(IRDA_SR_TRACE, "\n");
452 return 0;
453 }
454 }
455 /* No luck ! */
456 DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
457 kfree(discoveries);
458 return(-EADDRNOTAVAIL);
459 }
460
461
462 /************************* SOCKET ROUTINES *************************/
463 /*
464 * This are the main operations on IrNET sockets, basically to create
465 * and destroy IrNET sockets. These are called from the PPP part...
466 */
467
468 /*------------------------------------------------------------------*/
469 /*
470 * Create a IrNET instance : just initialise some parameters...
471 */
472 int
irda_irnet_create(irnet_socket * self)473 irda_irnet_create(irnet_socket * self)
474 {
475 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
476
477 self->magic = IRNET_MAGIC; /* Paranoia */
478
479 self->ttp_open = 0; /* Prevent higher layer from accessing IrTTP */
480 self->ttp_connect = 0; /* Not connecting yet */
481 self->rname[0] = '\0'; /* May be set via control channel */
482 self->rdaddr = DEV_ADDR_ANY; /* May be set via control channel */
483 self->rsaddr = DEV_ADDR_ANY; /* May be set via control channel */
484 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
485 self->saddr = DEV_ADDR_ANY; /* Until we get connected */
486 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
487
488 /* Register as a client with IrLMP */
489 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
490 #ifdef DISCOVERY_NOMASK
491 self->mask = 0xffff; /* For W2k compatibility */
492 #else /* DISCOVERY_NOMASK */
493 self->mask = irlmp_service_to_hint(S_LAN);
494 #endif /* DISCOVERY_NOMASK */
495 self->tx_flow = FLOW_START; /* Flow control from IrTTP */
496
497 DEXIT(IRDA_SOCK_TRACE, "\n");
498 return(0);
499 }
500
501 /*------------------------------------------------------------------*/
502 /*
503 * Connect to the other side :
504 * o convert device name to an address
505 * o find the socket number (dlsap)
506 * o Establish the connection
507 *
508 * Note : We no longer mimic af_irda. The IAS query for finding the TSAP
509 * is done asynchronously, like the TTP connection. This allow us to
510 * call this function from any context (not only process).
511 * The downside is that following what's happening in there is tricky
512 * because it involve various functions all over the place...
513 */
514 int
irda_irnet_connect(irnet_socket * self)515 irda_irnet_connect(irnet_socket * self)
516 {
517 int err;
518
519 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
520
521 /* Check if we are already trying to connect.
522 * Because irda_irnet_connect() can be called directly by pppd plus
523 * packet retries in ppp_generic and connect may take time, plus we may
524 * race with irnet_connect_indication(), we need to be careful there... */
525 if(test_and_set_bit(0, &self->ttp_connect))
526 DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
527 if((self->iriap != NULL) || (self->tsap != NULL))
528 DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
529
530 /* Insert ourselves in the hashbin so that the IrNET server can find us.
531 * Notes : 4th arg is string of 32 char max and must be null terminated
532 * When 4th arg is used (string), 3rd arg isn't (int)
533 * Can't re-insert (MUST remove first) so check for that... */
534 if((irnet_server.running) && (self->q.q_next == NULL))
535 {
536 unsigned long flags;
537 spin_lock_irqsave(&irnet_server.spinlock, flags);
538 hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
539 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
540 DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
541 }
542
543 /* If we don't have anything (no address, no name) */
544 if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
545 {
546 /* Try to find a suitable address */
547 if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
548 DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
549 /* In most cases, the call above is non-blocking */
550 }
551 else
552 {
553 /* If we have only the name (no address), try to get an address */
554 if(self->rdaddr == DEV_ADDR_ANY)
555 {
556 if((err = irnet_dname_to_daddr(self)) != 0)
557 DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
558 }
559 else
560 /* Use the requested destination address */
561 self->daddr = self->rdaddr;
562
563 /* Query remote LM-IAS to find LSAP selector */
564 irnet_find_lsap_sel(self);
565 /* The above call is non blocking */
566 }
567
568 /* At this point, we are waiting for the IrDA stack to call us back,
569 * or we have already failed.
570 * We will finish the connection procedure in irnet_connect_tsap().
571 */
572 DEXIT(IRDA_SOCK_TRACE, "\n");
573 return(0);
574 }
575
576 /*------------------------------------------------------------------*/
577 /*
578 * Function irda_irnet_destroy(self)
579 *
580 * Destroy irnet instance
581 *
582 * Note : this need to be called from a process context.
583 */
584 void
irda_irnet_destroy(irnet_socket * self)585 irda_irnet_destroy(irnet_socket * self)
586 {
587 DENTER(IRDA_SOCK_TRACE, "(self=0x%X)\n", (unsigned int) self);
588 if(self == NULL)
589 return;
590
591 /* Remove ourselves from hashbin (if we are queued in hashbin)
592 * Note : `irnet_server.running' protect us from calls in hashbin_delete() */
593 if((irnet_server.running) && (self->q.q_next != NULL))
594 {
595 struct irnet_socket * entry;
596 unsigned long flags;
597 DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
598 spin_lock_irqsave(&irnet_server.spinlock, flags);
599 entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
600 self->q.q_next = NULL;
601 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
602 DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
603 }
604
605 /* If we were connected, post a message */
606 if(test_bit(0, &self->ttp_open))
607 {
608 /* Note : as the disconnect comes from ppp_generic, the unit number
609 * doesn't exist anymore when we post the event, so we need to pass
610 * NULL as the first arg... */
611 irnet_post_event(NULL, IRNET_DISCONNECT_TO,
612 self->saddr, self->daddr, self->rname);
613 }
614
615 /* Prevent various IrDA callbacks from messing up things
616 * Need to be first */
617 clear_bit(0, &self->ttp_connect);
618
619 /* Prevent higher layer from accessing IrTTP */
620 clear_bit(0, &self->ttp_open);
621
622 /* Unregister with IrLMP */
623 irlmp_unregister_client(self->ckey);
624
625 /* Unregister with LM-IAS */
626 if(self->iriap)
627 {
628 iriap_close(self->iriap);
629 self->iriap = NULL;
630 }
631
632 /* Cleanup eventual discoveries from connection attempt */
633 if(self->discoveries != NULL)
634 {
635 /* Cleanup our copy of the discovery log */
636 kfree(self->discoveries);
637 self->discoveries = NULL;
638 }
639
640 /* Close our IrTTP connection */
641 if(self->tsap)
642 {
643 DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
644 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
645 irttp_close_tsap(self->tsap);
646 self->tsap = NULL;
647 }
648 self->stsap_sel = 0;
649
650 DEXIT(IRDA_SOCK_TRACE, "\n");
651 return;
652 }
653
654
655 /************************** SERVER SOCKET **************************/
656 /*
657 * The IrNET service is composed of one server socket and a variable
658 * number of regular IrNET sockets. The server socket is supposed to
659 * handle incoming connections and redirect them to one IrNET sockets.
660 * It's a superset of the regular IrNET socket, but has a very distinct
661 * behaviour...
662 */
663
664 /*------------------------------------------------------------------*/
665 /*
666 * Function irnet_daddr_to_dname (self)
667 *
668 * Convert an IrDA address to a IrDA nickname
669 *
670 * It basically look into the discovery log until there is a match.
671 */
672 static inline int
irnet_daddr_to_dname(irnet_socket * self)673 irnet_daddr_to_dname(irnet_socket * self)
674 {
675 struct irda_device_info *discoveries; /* Copy of the discovery log */
676 int number; /* Number of nodes in the log */
677 int i;
678
679 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
680
681 /* Ask lmp for the current discovery log */
682 discoveries = irlmp_get_discoveries(&number, 0xffff,
683 DISCOVERY_DEFAULT_SLOTS);
684 /* Check if the we got some results */
685 if (discoveries == NULL)
686 DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
687
688 /* Now, check all discovered devices (if any) */
689 for(i = 0; i < number; i++)
690 {
691 /* Does the name match ? */
692 if(discoveries[i].daddr == self->daddr)
693 {
694 /* Yes !!! Get it.. */
695 strncpy(self->rname, discoveries[i].info, NICKNAME_MAX_LEN);
696 self->rname[NICKNAME_MAX_LEN + 1] = '\0';
697 DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
698 self->daddr, self->rname);
699 kfree(discoveries);
700 DEXIT(IRDA_SERV_TRACE, "\n");
701 return 0;
702 }
703 }
704 /* No luck ! */
705 DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
706 kfree(discoveries);
707 return(-EADDRNOTAVAIL);
708 }
709
710 /*------------------------------------------------------------------*/
711 /*
712 * Function irda_find_socket (self)
713 *
714 * Find the correct IrNET socket
715 *
716 * Look into the list of IrNET sockets and finds one with the right
717 * properties...
718 */
719 static inline irnet_socket *
irnet_find_socket(irnet_socket * self)720 irnet_find_socket(irnet_socket * self)
721 {
722 irnet_socket * new = (irnet_socket *) NULL;
723 unsigned long flags;
724 int err;
725
726 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
727
728 /* Get the addresses of the requester */
729 self->daddr = irttp_get_daddr(self->tsap);
730 self->saddr = irttp_get_saddr(self->tsap);
731
732 /* Try to get the IrDA nickname of the requester */
733 err = irnet_daddr_to_dname(self);
734
735 /* Protect access to the instance list */
736 spin_lock_irqsave(&irnet_server.spinlock, flags);
737
738 /* So now, try to get an socket having specifically
739 * requested that nickname */
740 if(err == 0)
741 {
742 new = (irnet_socket *) hashbin_find(irnet_server.list,
743 0, self->rname);
744 if(new)
745 DEBUG(IRDA_SERV_INFO, "Socket 0x%X matches rname ``%s''.\n",
746 (unsigned int) new, new->rname);
747 }
748
749 /* If no name matches, try to find an socket by the destination address */
750 /* It can be either the requested destination address (set via the
751 * control channel), or the current destination address if the
752 * socket is in the middle of a connection request */
753 if(new == (irnet_socket *) NULL)
754 {
755 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
756 while(new !=(irnet_socket *) NULL)
757 {
758 /* Does it have the same address ? */
759 if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
760 {
761 /* Yes !!! Get it.. */
762 DEBUG(IRDA_SERV_INFO, "Socket 0x%X matches daddr %#08x.\n",
763 (unsigned int) new, self->daddr);
764 break;
765 }
766 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
767 }
768 }
769
770 /* If we don't have any socket, get the first unconnected socket */
771 if(new == (irnet_socket *) NULL)
772 {
773 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
774 while(new !=(irnet_socket *) NULL)
775 {
776 /* Is it available ? */
777 if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
778 (new->rname[0] == '\0') && (new->ppp_open))
779 {
780 /* Yes !!! Get it.. */
781 DEBUG(IRDA_SERV_INFO, "Socket 0x%X is free.\n",
782 (unsigned int) new);
783 break;
784 }
785 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
786 }
787 }
788
789 /* Spin lock end */
790 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
791
792 DEXIT(IRDA_SERV_TRACE, " - new = 0x%X\n", (unsigned int) new);
793 return new;
794 }
795
796 /*------------------------------------------------------------------*/
797 /*
798 * Function irda_connect_socket (self)
799 *
800 * Connect an incoming connection to the socket
801 *
802 */
803 static inline int
irnet_connect_socket(irnet_socket * server,irnet_socket * new,struct qos_info * qos,__u32 max_sdu_size,__u8 max_header_size)804 irnet_connect_socket(irnet_socket * server,
805 irnet_socket * new,
806 struct qos_info * qos,
807 __u32 max_sdu_size,
808 __u8 max_header_size)
809 {
810 DENTER(IRDA_SERV_TRACE, "(server=0x%X, new=0x%X)\n",
811 (unsigned int) server, (unsigned int) new);
812
813 /* Now attach up the new socket */
814 new->tsap = irttp_dup(server->tsap, new);
815 DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
816
817 /* Set up all the relevant parameters on the new socket */
818 new->stsap_sel = new->tsap->stsap_sel;
819 new->dtsap_sel = new->tsap->dtsap_sel;
820 new->saddr = irttp_get_saddr(new->tsap);
821 new->daddr = irttp_get_daddr(new->tsap);
822
823 new->max_header_size = max_header_size;
824 new->max_sdu_size_tx = max_sdu_size;
825 new->max_data_size = max_sdu_size;
826 #ifdef STREAM_COMPAT
827 /* If we want to receive "stream sockets" */
828 if(max_sdu_size == 0)
829 new->max_data_size = irttp_get_max_seg_size(new->tsap);
830 #endif /* STREAM_COMPAT */
831
832 /* Clean up the original one to keep it in listen state */
833 irttp_listen(server->tsap);
834
835 /* Send a connection response on the new socket */
836 irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
837
838 /* Allow PPP to send its junk over the new socket... */
839 set_bit(0, &new->ttp_open);
840
841 /* Not connecting anymore, and clean up last possible remains
842 * of connection attempts on the socket */
843 clear_bit(0, &new->ttp_connect);
844 if(new->iriap)
845 {
846 iriap_close(new->iriap);
847 new->iriap = NULL;
848 }
849 if(new->discoveries != NULL)
850 {
851 kfree(new->discoveries);
852 new->discoveries = NULL;
853 }
854
855 #ifdef CONNECT_INDIC_KICK
856 /* As currently we don't block packets in ppp_irnet_send() while passive,
857 * this is not really needed...
858 * Also, not doing it give IrDA a chance to finish the setup properly
859 * before beeing swamped with packets... */
860 ppp_output_wakeup(&new->chan);
861 #endif /* CONNECT_INDIC_KICK */
862
863 /* Notify the control channel */
864 irnet_post_event(new, IRNET_CONNECT_FROM,
865 new->saddr, new->daddr, server->rname);
866
867 DEXIT(IRDA_SERV_TRACE, "\n");
868 return 0;
869 }
870
871 /*------------------------------------------------------------------*/
872 /*
873 * Function irda_disconnect_server (self)
874 *
875 * Cleanup the server socket when the incoming connection abort
876 *
877 */
878 static inline void
irnet_disconnect_server(irnet_socket * self,struct sk_buff * skb)879 irnet_disconnect_server(irnet_socket * self,
880 struct sk_buff *skb)
881 {
882 DENTER(IRDA_SERV_TRACE, "(self=0x%X)\n", (unsigned int) self);
883
884 /* Put the received packet in the black hole */
885 kfree_skb(skb);
886
887 #ifdef FAIL_SEND_DISCONNECT
888 /* Tell the other party we don't want to be connected */
889 /* Hum... Is it the right thing to do ? And do we need to send
890 * a connect response before ? It looks ok without this... */
891 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
892 #endif /* FAIL_SEND_DISCONNECT */
893
894 /* Notify the control channel (see irnet_find_socket()) */
895 irnet_post_event(NULL, IRNET_REQUEST_FROM,
896 self->saddr, self->daddr, self->rname);
897
898 /* Clean up the server to keep it in listen state */
899 irttp_listen(self->tsap);
900
901 DEXIT(IRDA_SERV_TRACE, "\n");
902 return;
903 }
904
905 /*------------------------------------------------------------------*/
906 /*
907 * Function irda_setup_server (self)
908 *
909 * Create a IrTTP server and set it up...
910 *
911 * Register the IrLAN hint bit, create a IrTTP instance for us,
912 * set all the IrTTP callbacks and create an IrIAS entry...
913 */
914 static inline int
irnet_setup_server(void)915 irnet_setup_server(void)
916 {
917 __u16 hints;
918
919 DENTER(IRDA_SERV_TRACE, "()\n");
920
921 /* Initialise the regular socket part of the server */
922 irda_irnet_create(&irnet_server.s);
923
924 /* Open a local TSAP (an IrTTP instance) for the server */
925 irnet_open_tsap(&irnet_server.s);
926
927 /* PPP part setup */
928 irnet_server.s.ppp_open = 0;
929 irnet_server.s.chan.private = NULL;
930 irnet_server.s.file = NULL;
931
932 /* Get the hint bit corresponding to IrLAN */
933 /* Note : we overload the IrLAN hint bit. As it is only a "hint", and as
934 * we provide roughly the same functionality as IrLAN, this is ok.
935 * In fact, the situation is similar as JetSend overloading the Obex hint
936 */
937 hints = irlmp_service_to_hint(S_LAN);
938
939 #ifdef ADVERTISE_HINT
940 /* Register with IrLMP as a service (advertise our hint bit) */
941 irnet_server.skey = irlmp_register_service(hints);
942 #endif /* ADVERTISE_HINT */
943
944 /* Register with LM-IAS (so that people can connect to us) */
945 irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
946 irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
947 irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
948 irias_insert_object(irnet_server.ias_obj);
949
950 #ifdef DISCOVERY_EVENTS
951 /* Tell IrLMP we want to be notified of newly discovered nodes */
952 irlmp_update_client(irnet_server.s.ckey, hints,
953 irnet_discovery_indication, irnet_expiry_indication,
954 (void *) &irnet_server.s);
955 #endif
956
957 DEXIT(IRDA_SERV_TRACE, " - self=0x%X\n", (unsigned int) &irnet_server.s);
958 return 0;
959 }
960
961 /*------------------------------------------------------------------*/
962 /*
963 * Function irda_destroy_server (self)
964 *
965 * Destroy the IrTTP server...
966 *
967 * Reverse of the previous function...
968 */
969 static inline void
irnet_destroy_server(void)970 irnet_destroy_server(void)
971 {
972 DENTER(IRDA_SERV_TRACE, "()\n");
973
974 #ifdef ADVERTISE_HINT
975 /* Unregister with IrLMP */
976 irlmp_unregister_service(irnet_server.skey);
977 #endif /* ADVERTISE_HINT */
978
979 /* Unregister with LM-IAS */
980 if(irnet_server.ias_obj)
981 irias_delete_object(irnet_server.ias_obj);
982
983 /* Cleanup the socket part */
984 irda_irnet_destroy(&irnet_server.s);
985
986 DEXIT(IRDA_SERV_TRACE, "\n");
987 return;
988 }
989
990
991 /************************ IRDA-TTP CALLBACKS ************************/
992 /*
993 * When we create a IrTTP instance, we pass to it a set of callbacks
994 * that IrTTP will call in case of various events.
995 * We take care of those events here.
996 */
997
998 /*------------------------------------------------------------------*/
999 /*
1000 * Function irnet_data_indication (instance, sap, skb)
1001 *
1002 * Received some data from TinyTP. Just queue it on the receive queue
1003 *
1004 */
1005 static int
irnet_data_indication(void * instance,void * sap,struct sk_buff * skb)1006 irnet_data_indication(void * instance,
1007 void * sap,
1008 struct sk_buff *skb)
1009 {
1010 irnet_socket * ap = (irnet_socket *) instance;
1011 unsigned char * p;
1012 int code = 0;
1013
1014 DENTER(IRDA_TCB_TRACE, "(self/ap=0x%X, skb=0x%X)\n",
1015 (unsigned int) ap,(unsigned int) skb);
1016 DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
1017
1018 /* Check is ppp is ready to receive our packet */
1019 if(!ap->ppp_open)
1020 {
1021 DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
1022 /* When we return error, TTP will need to requeue the skb and
1023 * will stop the sender. IrTTP will stall until we send it a
1024 * flow control request... */
1025 return -ENOMEM;
1026 }
1027
1028 /* strip address/control field if present */
1029 p = skb->data;
1030 if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
1031 {
1032 /* chop off address/control */
1033 if(skb->len < 3)
1034 goto err_exit;
1035 p = skb_pull(skb, 2);
1036 }
1037
1038 /* decompress protocol field if compressed */
1039 if(p[0] & 1)
1040 {
1041 /* protocol is compressed */
1042 skb_push(skb, 1)[0] = 0;
1043 }
1044 else
1045 if(skb->len < 2)
1046 goto err_exit;
1047
1048 /* pass to generic ppp layer */
1049 /* Note : how do I know if ppp can accept or not the packet ? This is
1050 * essential if I want to manage flow control smoothly... */
1051 ppp_input(&ap->chan, skb);
1052
1053 DEXIT(IRDA_TCB_TRACE, "\n");
1054 return 0;
1055
1056 err_exit:
1057 DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
1058 kfree_skb(skb);
1059 ppp_input_error(&ap->chan, code);
1060 return 0; /* Don't return an error code, only for flow control... */
1061 }
1062
1063 /*------------------------------------------------------------------*/
1064 /*
1065 * Function irnet_disconnect_indication (instance, sap, reason, skb)
1066 *
1067 * Connection has been closed. Chech reason to find out why
1068 *
1069 * Note : there are many cases where we come here :
1070 * o attempted to connect, timeout
1071 * o connected, link is broken, LAP has timeout
1072 * o connected, other side close the link
1073 * o connection request on the server not handled
1074 */
1075 static void
irnet_disconnect_indication(void * instance,void * sap,LM_REASON reason,struct sk_buff * skb)1076 irnet_disconnect_indication(void * instance,
1077 void * sap,
1078 LM_REASON reason,
1079 struct sk_buff *skb)
1080 {
1081 irnet_socket * self = (irnet_socket *) instance;
1082 int test_open;
1083 int test_connect;
1084
1085 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1086 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1087
1088 /* Don't care about it, but let's not leak it */
1089 if(skb)
1090 dev_kfree_skb(skb);
1091
1092 /* Prevent higher layer from accessing IrTTP */
1093 test_open = test_and_clear_bit(0, &self->ttp_open);
1094 /* Not connecting anymore...
1095 * (note : TSAP is open, so IAP callbacks are no longer pending...) */
1096 test_connect = test_and_clear_bit(0, &self->ttp_connect);
1097
1098 /* If both self->ttp_open and self->ttp_connect are NULL, it mean that we
1099 * have a race condition with irda_irnet_destroy() or
1100 * irnet_connect_indication(), so don't mess up tsap...
1101 */
1102 if(!(test_open || test_connect))
1103 {
1104 DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
1105 return;
1106 }
1107
1108 /* If we were active, notify the control channel */
1109 if(test_open)
1110 irnet_post_event(self, IRNET_DISCONNECT_FROM,
1111 self->saddr, self->daddr, self->rname);
1112 else
1113 /* If we were trying to connect, notify the control channel */
1114 if((self->tsap) && (self != &irnet_server.s))
1115 irnet_post_event(self, IRNET_NOANSWER_FROM,
1116 self->saddr, self->daddr, self->rname);
1117
1118 /* Close our IrTTP connection, cleanup tsap */
1119 if((self->tsap) && (self != &irnet_server.s))
1120 {
1121 DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
1122 irttp_close_tsap(self->tsap);
1123 self->tsap = NULL;
1124 }
1125 /* Cleanup the socket in case we want to reconnect in ppp_output_wakeup() */
1126 self->stsap_sel = 0;
1127 self->daddr = DEV_ADDR_ANY;
1128 self->tx_flow = FLOW_START;
1129
1130 /* Deal with the ppp instance if it's still alive */
1131 if(self->ppp_open)
1132 {
1133 if(test_open)
1134 {
1135 /* If we were connected, cleanup & close the PPP channel,
1136 * which will kill pppd (hangup) and the rest */
1137 ppp_unregister_channel(&self->chan);
1138 self->ppp_open = 0;
1139 }
1140 else
1141 {
1142 /* If we were trying to connect, flush (drain) ppp_generic
1143 * Tx queue (most often we have blocked it), which will
1144 * trigger an other attempt to connect. If we are passive,
1145 * this will empty the Tx queue after last try. */
1146 ppp_output_wakeup(&self->chan);
1147 }
1148 }
1149
1150 DEXIT(IRDA_TCB_TRACE, "\n");
1151 }
1152
1153 /*------------------------------------------------------------------*/
1154 /*
1155 * Function irnet_connect_confirm (instance, sap, qos, max_sdu_size, skb)
1156 *
1157 * Connections has been confirmed by the remote device
1158 *
1159 */
1160 static void
irnet_connect_confirm(void * instance,void * sap,struct qos_info * qos,__u32 max_sdu_size,__u8 max_header_size,struct sk_buff * skb)1161 irnet_connect_confirm(void * instance,
1162 void * sap,
1163 struct qos_info *qos,
1164 __u32 max_sdu_size,
1165 __u8 max_header_size,
1166 struct sk_buff *skb)
1167 {
1168 irnet_socket * self = (irnet_socket *) instance;
1169
1170 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1171
1172 /* Check if socket is closing down (via irda_irnet_destroy()) */
1173 if(! test_bit(0, &self->ttp_connect))
1174 {
1175 DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
1176 return;
1177 }
1178
1179 /* How much header space do we need to reserve */
1180 self->max_header_size = max_header_size;
1181
1182 /* IrTTP max SDU size in transmit direction */
1183 self->max_sdu_size_tx = max_sdu_size;
1184 self->max_data_size = max_sdu_size;
1185 #ifdef STREAM_COMPAT
1186 if(max_sdu_size == 0)
1187 self->max_data_size = irttp_get_max_seg_size(self->tsap);
1188 #endif /* STREAM_COMPAT */
1189
1190 /* At this point, IrLMP has assigned our source address */
1191 self->saddr = irttp_get_saddr(self->tsap);
1192
1193 /* Allow higher layer to access IrTTP */
1194 set_bit(0, &self->ttp_open);
1195 clear_bit(0, &self->ttp_connect); /* Not racy, IrDA traffic is serial */
1196 /* Give a kick in the ass of ppp_generic so that he sends us some data */
1197 ppp_output_wakeup(&self->chan);
1198
1199 /* Check size of received packet */
1200 if(skb->len > 0)
1201 {
1202 #ifdef PASS_CONNECT_PACKETS
1203 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1204 /* Try to pass it to PPP */
1205 irnet_data_indication(instance, sap, skb);
1206 #else /* PASS_CONNECT_PACKETS */
1207 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1208 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1209 #endif /* PASS_CONNECT_PACKETS */
1210 }
1211 else
1212 kfree_skb(skb);
1213
1214 /* Notify the control channel */
1215 irnet_post_event(self, IRNET_CONNECT_TO,
1216 self->saddr, self->daddr, self->rname);
1217
1218 DEXIT(IRDA_TCB_TRACE, "\n");
1219 }
1220
1221 /*------------------------------------------------------------------*/
1222 /*
1223 * Function irnet_flow_indication (instance, sap, flow)
1224 *
1225 * Used by TinyTP to tell us if it can accept more data or not
1226 *
1227 */
1228 static void
irnet_flow_indication(void * instance,void * sap,LOCAL_FLOW flow)1229 irnet_flow_indication(void * instance,
1230 void * sap,
1231 LOCAL_FLOW flow)
1232 {
1233 irnet_socket * self = (irnet_socket *) instance;
1234 LOCAL_FLOW oldflow = self->tx_flow;
1235
1236 DENTER(IRDA_TCB_TRACE, "(self=0x%X, flow=%d)\n", (unsigned int) self, flow);
1237
1238 /* Update our state */
1239 self->tx_flow = flow;
1240
1241 /* Check what IrTTP want us to do... */
1242 switch(flow)
1243 {
1244 case FLOW_START:
1245 DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
1246 /* Check if we really need to wake up PPP */
1247 if(oldflow == FLOW_STOP)
1248 ppp_output_wakeup(&self->chan);
1249 else
1250 DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
1251 break;
1252 case FLOW_STOP:
1253 DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
1254 break;
1255 default:
1256 DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
1257 break;
1258 }
1259
1260 DEXIT(IRDA_TCB_TRACE, "\n");
1261 }
1262
1263 /*------------------------------------------------------------------*/
1264 /*
1265 * Function irnet_status_indication (instance, sap, reason, skb)
1266 *
1267 * Link (IrLAP) status report.
1268 *
1269 */
1270 static void
irnet_status_indication(void * instance,LINK_STATUS link,LOCK_STATUS lock)1271 irnet_status_indication(void * instance,
1272 LINK_STATUS link,
1273 LOCK_STATUS lock)
1274 {
1275 irnet_socket * self = (irnet_socket *) instance;
1276
1277 DENTER(IRDA_TCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1278 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1279
1280 /* We can only get this event if we are connected */
1281 switch(link)
1282 {
1283 case STATUS_NO_ACTIVITY:
1284 irnet_post_event(self, IRNET_BLOCKED_LINK,
1285 self->saddr, self->daddr, self->rname);
1286 break;
1287 default:
1288 DEBUG(IRDA_CB_INFO, "Unknown status...\n");
1289 }
1290
1291 DEXIT(IRDA_TCB_TRACE, "\n");
1292 }
1293
1294 /*------------------------------------------------------------------*/
1295 /*
1296 * Function irnet_connect_indication(instance, sap, qos, max_sdu_size, userdata)
1297 *
1298 * Incoming connection
1299 *
1300 * In theory, this function is called only on the server socket.
1301 * Some other node is attempting to connect to the IrNET service, and has
1302 * sent a connection request on our server socket.
1303 * We just redirect the connection to the relevant IrNET socket.
1304 *
1305 * Note : we also make sure that between 2 irnet nodes, there can
1306 * exist only one irnet connection.
1307 */
1308 static void
irnet_connect_indication(void * instance,void * sap,struct qos_info * qos,__u32 max_sdu_size,__u8 max_header_size,struct sk_buff * skb)1309 irnet_connect_indication(void * instance,
1310 void * sap,
1311 struct qos_info *qos,
1312 __u32 max_sdu_size,
1313 __u8 max_header_size,
1314 struct sk_buff *skb)
1315 {
1316 irnet_socket * server = &irnet_server.s;
1317 irnet_socket * new = (irnet_socket *) NULL;
1318
1319 DENTER(IRDA_TCB_TRACE, "(server=0x%X)\n", (unsigned int) server);
1320 DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
1321 "Invalid instance (0x%X) !!!\n", (unsigned int) instance);
1322 DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
1323
1324 /* Try to find the most appropriate IrNET socket */
1325 new = irnet_find_socket(server);
1326
1327 /* After all this hard work, do we have an socket ? */
1328 if(new == (irnet_socket *) NULL)
1329 {
1330 DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
1331 irnet_disconnect_server(server, skb);
1332 return;
1333 }
1334
1335 /* Is the socket already busy ? */
1336 if(test_bit(0, &new->ttp_open))
1337 {
1338 DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
1339 irnet_disconnect_server(server, skb);
1340 return;
1341 }
1342
1343 /* The following code is a bit tricky, so need comments ;-)
1344 */
1345 /* If ttp_connect is set, the socket is trying to connect to the other
1346 * end and may have sent a IrTTP connection request and is waiting for
1347 * a connection response (that may never come).
1348 * Now, the pain is that the socket may have opened a tsap and is
1349 * waiting on it, while the other end is trying to connect to it on
1350 * another tsap.
1351 * Because IrNET can be peer to peer, we need to workaround this.
1352 * Furthermore, the way the irnetd script is implemented, the
1353 * target will create a second IrNET connection back to the
1354 * originator and expect the originator to bind this new connection
1355 * to the original PPPD instance.
1356 * And of course, if we don't use irnetd, we can have a race when
1357 * both side try to connect simultaneously, which could leave both
1358 * connections half closed (yuck).
1359 * Conclusions :
1360 * 1) The "originator" must accept the new connection and get rid
1361 * of the old one so that irnetd works
1362 * 2) One side must deny the new connection to avoid races,
1363 * but both side must agree on which side it is...
1364 * Most often, the originator is primary at the LAP layer.
1365 * Jean II
1366 */
1367 /* Now, let's look at the way I wrote the test...
1368 * We need to clear up the ttp_connect flag atomically to prevent
1369 * irnet_disconnect_indication() to mess up the tsap we are going to close.
1370 * We want to clear the ttp_connect flag only if we close the tsap,
1371 * otherwise we will never close it, so we need to check for primary
1372 * *before* doing the test on the flag.
1373 * And of course, ALLOW_SIMULT_CONNECT can disable this entirely...
1374 * Jean II
1375 */
1376
1377 /* Socket already connecting ? On primary ? */
1378 if(0
1379 #ifdef ALLOW_SIMULT_CONNECT
1380 || ((irttp_is_primary(server->tsap) == 1) /* primary */
1381 && (test_and_clear_bit(0, &new->ttp_connect)))
1382 #endif /* ALLOW_SIMULT_CONNECT */
1383 )
1384 {
1385 DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
1386
1387 /* Cleanup the old TSAP if necessary - IrIAP will be cleaned up later */
1388 if(new->tsap != NULL)
1389 {
1390 /* Close the old connection the new socket was attempting,
1391 * so that we can hook it up to the new connection.
1392 * It's now safe to do it... */
1393 irttp_close_tsap(new->tsap);
1394 new->tsap = NULL;
1395 }
1396 }
1397 else
1398 {
1399 /* Three options :
1400 * 1) socket was not connecting or connected : ttp_connect should be 0.
1401 * 2) we don't want to connect the socket because we are secondary or
1402 * ALLOW_SIMULT_CONNECT is undefined. ttp_connect should be 1.
1403 * 3) we are half way in irnet_disconnect_indication(), and it's a
1404 * nice race condition... Fortunately, we can detect that by checking
1405 * if tsap is still alive. On the other hand, we can't be in
1406 * irda_irnet_destroy() otherwise we would not have found this
1407 * socket in the hashbin.
1408 * Jean II */
1409 if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
1410 {
1411 /* Don't mess this socket, somebody else in in charge... */
1412 DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
1413 irnet_disconnect_server(server, skb);
1414 return;
1415 }
1416 }
1417
1418 /* So : at this point, we have a socket, and it is idle. Good ! */
1419 irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
1420
1421 /* Check size of received packet */
1422 if(skb->len > 0)
1423 {
1424 #ifdef PASS_CONNECT_PACKETS
1425 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1426 /* Try to pass it to PPP */
1427 irnet_data_indication(new, new->tsap, skb);
1428 #else /* PASS_CONNECT_PACKETS */
1429 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1430 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1431 #endif /* PASS_CONNECT_PACKETS */
1432 }
1433 else
1434 kfree_skb(skb);
1435
1436 DEXIT(IRDA_TCB_TRACE, "\n");
1437 }
1438
1439
1440 /********************** IRDA-IAS/LMP CALLBACKS **********************/
1441 /*
1442 * These are the callbacks called by other layers of the IrDA stack,
1443 * mainly LMP for discovery and IAS for name queries.
1444 */
1445
1446 /*------------------------------------------------------------------*/
1447 /*
1448 * Function irnet_getvalue_confirm (result, obj_id, value, priv)
1449 *
1450 * Got answer from remote LM-IAS, just connect
1451 *
1452 * This is the reply to a IAS query we were doing to find the TSAP of
1453 * the device we want to connect to.
1454 * If we have found a valid TSAP, just initiate the TTP connection
1455 * on this TSAP.
1456 */
1457 static void
irnet_getvalue_confirm(int result,__u16 obj_id,struct ias_value * value,void * priv)1458 irnet_getvalue_confirm(int result,
1459 __u16 obj_id,
1460 struct ias_value *value,
1461 void * priv)
1462 {
1463 irnet_socket * self = (irnet_socket *) priv;
1464
1465 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1466 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1467
1468 /* Check if already connected (via irnet_connect_socket())
1469 * or socket is closing down (via irda_irnet_destroy()) */
1470 if(! test_bit(0, &self->ttp_connect))
1471 {
1472 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1473 return;
1474 }
1475
1476 /* We probably don't need to make any more queries */
1477 iriap_close(self->iriap);
1478 self->iriap = NULL;
1479
1480 /* Post process the IAS reply */
1481 self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
1482
1483 /* If error, just go out */
1484 if(self->errno)
1485 {
1486 clear_bit(0, &self->ttp_connect);
1487 DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
1488 return;
1489 }
1490
1491 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1492 self->daddr, self->dtsap_sel);
1493
1494 /* Start up TTP - non blocking */
1495 irnet_connect_tsap(self);
1496
1497 DEXIT(IRDA_OCB_TRACE, "\n");
1498 }
1499
1500 /*------------------------------------------------------------------*/
1501 /*
1502 * Function irnet_discovervalue_confirm (result, obj_id, value, priv)
1503 *
1504 * Handle the TSAP discovery procedure state machine.
1505 * Got answer from remote LM-IAS, try next device
1506 *
1507 * We are doing a TSAP discovery procedure, and we got an answer to
1508 * a IAS query we were doing to find the TSAP on one of the address
1509 * in the discovery log.
1510 *
1511 * If we have found a valid TSAP for the first time, save it. If it's
1512 * not the first time we found one, complain.
1513 *
1514 * If we have more addresses in the log, just initiate a new query.
1515 * Note that those query may fail (see irnet_discover_daddr_and_lsap_sel())
1516 *
1517 * Otherwise, wrap up the procedure (cleanup), check if we have found
1518 * any device and connect to it.
1519 */
1520 static void
irnet_discovervalue_confirm(int result,__u16 obj_id,struct ias_value * value,void * priv)1521 irnet_discovervalue_confirm(int result,
1522 __u16 obj_id,
1523 struct ias_value *value,
1524 void * priv)
1525 {
1526 irnet_socket * self = (irnet_socket *) priv;
1527 __u8 dtsap_sel; /* TSAP we are looking for */
1528
1529 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1530 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1531
1532 /* Check if already connected (via irnet_connect_socket())
1533 * or socket is closing down (via irda_irnet_destroy()) */
1534 if(! test_bit(0, &self->ttp_connect))
1535 {
1536 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1537 return;
1538 }
1539
1540 /* Post process the IAS reply */
1541 dtsap_sel = irnet_ias_to_tsap(self, result, value);
1542
1543 /* Have we got something ? */
1544 if(self->errno == 0)
1545 {
1546 /* We found the requested service */
1547 if(self->daddr != DEV_ADDR_ANY)
1548 {
1549 DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
1550 }
1551 else
1552 {
1553 /* First time we found that one, save it ! */
1554 self->daddr = self->discoveries[self->disco_index].daddr;
1555 self->dtsap_sel = dtsap_sel;
1556 }
1557 }
1558
1559 /* If no failure */
1560 if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
1561 {
1562 int ret;
1563
1564 /* Search the next node */
1565 ret = irnet_discover_next_daddr(self);
1566 if(!ret)
1567 {
1568 /* In this case, the above request was non-blocking.
1569 * We will return here after a while... */
1570 return;
1571 }
1572 /* In this case, we have processed the last discovery item */
1573 }
1574
1575 /* No more queries to be done (failure or last one) */
1576
1577 /* We probably don't need to make any more queries */
1578 iriap_close(self->iriap);
1579 self->iriap = NULL;
1580
1581 /* No more items : remove the log and signal termination */
1582 DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%X)\n",
1583 (unsigned int) self->discoveries);
1584 if(self->discoveries != NULL)
1585 {
1586 /* Cleanup our copy of the discovery log */
1587 kfree(self->discoveries);
1588 self->discoveries = NULL;
1589 }
1590 self->disco_number = -1;
1591
1592 /* Check out what we found */
1593 if(self->daddr == DEV_ADDR_ANY)
1594 {
1595 self->daddr = DEV_ADDR_ANY;
1596 clear_bit(0, &self->ttp_connect);
1597 DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
1598 return;
1599 }
1600
1601 /* We have a valid address - just connect */
1602
1603 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1604 self->daddr, self->dtsap_sel);
1605
1606 /* Start up TTP - non blocking */
1607 irnet_connect_tsap(self);
1608
1609 DEXIT(IRDA_OCB_TRACE, "\n");
1610 }
1611
1612 #ifdef DISCOVERY_EVENTS
1613 /*------------------------------------------------------------------*/
1614 /*
1615 * Function irnet_discovery_indication (discovery)
1616 *
1617 * Got a discovery indication from IrLMP, post an event
1618 *
1619 * Note : IrLMP take care of matching the hint mask for us, we only
1620 * check if it is a "new" node...
1621 *
1622 * As IrLMP filter on the IrLAN hint bit, we get both IrLAN and IrNET
1623 * nodes, so it's only at connection time that we will know if the
1624 * node support IrNET, IrLAN or both. The other solution is to check
1625 * in IAS the PNP ids and service name.
1626 * Note : even if a node support IrNET (or IrLAN), it's no guarantee
1627 * that we will be able to connect to it, the node might already be
1628 * busy...
1629 *
1630 * One last thing : in some case, this function will trigger duplicate
1631 * discovery events. On the other hand, we should catch all
1632 * discoveries properly (i.e. not miss one). Filtering duplicate here
1633 * is to messy, so we leave that to user space...
1634 */
1635 static void
irnet_discovery_indication(discovery_t * discovery,DISCOVERY_MODE mode,void * priv)1636 irnet_discovery_indication(discovery_t * discovery,
1637 DISCOVERY_MODE mode,
1638 void * priv)
1639 {
1640 irnet_socket * self = &irnet_server.s;
1641
1642 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1643 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1644 "Invalid instance (0x%X) !!!\n", (unsigned int) priv);
1645
1646 /* Check if node is discovered is a new one or an old one.
1647 * We check when how long ago this node was discovered, with a
1648 * coarse timeout (we may miss some discovery events or be delayed).
1649 */
1650 if((jiffies - discovery->first_timestamp) >= (sysctl_discovery_timeout * HZ))
1651 {
1652 return; /* Too old, not interesting -> goodbye */
1653 }
1654
1655 DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
1656 discovery->nickname);
1657
1658 /* Notify the control channel */
1659 irnet_post_event(NULL, IRNET_DISCOVER,
1660 discovery->saddr, discovery->daddr, discovery->nickname);
1661
1662 DEXIT(IRDA_OCB_TRACE, "\n");
1663 }
1664
1665 /*------------------------------------------------------------------*/
1666 /*
1667 * Function irnet_expiry_indication (expiry)
1668 *
1669 * Got a expiry indication from IrLMP, post an event
1670 *
1671 * Note : IrLMP take care of matching the hint mask for us, we only
1672 * check if it is a "new" node...
1673 */
1674 static void
irnet_expiry_indication(discovery_t * expiry,DISCOVERY_MODE mode,void * priv)1675 irnet_expiry_indication(discovery_t * expiry,
1676 DISCOVERY_MODE mode,
1677 void * priv)
1678 {
1679 irnet_socket * self = &irnet_server.s;
1680
1681 DENTER(IRDA_OCB_TRACE, "(self=0x%X)\n", (unsigned int) self);
1682 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1683 "Invalid instance (0x%X) !!!\n", (unsigned int) priv);
1684
1685 DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
1686 expiry->nickname);
1687
1688 /* Notify the control channel */
1689 irnet_post_event(NULL, IRNET_EXPIRE,
1690 expiry->saddr, expiry->daddr, expiry->nickname);
1691
1692 DEXIT(IRDA_OCB_TRACE, "\n");
1693 }
1694 #endif /* DISCOVERY_EVENTS */
1695
1696
1697 /*********************** PROC ENTRY CALLBACKS ***********************/
1698 /*
1699 * We create a instance in the /proc filesystem, and here we take care
1700 * of that...
1701 */
1702
1703 #ifdef CONFIG_PROC_FS
1704 /*------------------------------------------------------------------*/
1705 /*
1706 * Function irnet_proc_read (buf, start, offset, len, unused)
1707 *
1708 * Give some info to the /proc file system
1709 */
1710 static int
irnet_proc_read(char * buf,char ** start,off_t offset,int len)1711 irnet_proc_read(char * buf,
1712 char ** start,
1713 off_t offset,
1714 int len)
1715 {
1716 irnet_socket * self;
1717 char * state;
1718 unsigned long flags;
1719 int i = 0;
1720
1721 len = 0;
1722
1723 /* Get the IrNET server information... */
1724 len += sprintf(buf+len, "IrNET server - ");
1725 len += sprintf(buf+len, "IrDA state: %s, ",
1726 (irnet_server.running ? "running" : "dead"));
1727 len += sprintf(buf+len, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
1728 len += sprintf(buf+len, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
1729
1730 /* Do we need to continue ? */
1731 if(!irnet_server.running)
1732 return len;
1733
1734 /* Protect access to the instance list */
1735 spin_lock_irqsave(&irnet_server.spinlock, flags);
1736
1737 /* Get the sockets one by one... */
1738 self = (irnet_socket *) hashbin_get_first(irnet_server.list);
1739 while(self != NULL)
1740 {
1741 /* Start printing info about the socket. */
1742 len += sprintf(buf+len, "\nIrNET socket %d - ", i++);
1743
1744 /* First, get the requested configuration */
1745 len += sprintf(buf+len, "Requested IrDA name: \"%s\", ", self->rname);
1746 len += sprintf(buf+len, "daddr: %08x, ", self->rdaddr);
1747 len += sprintf(buf+len, "saddr: %08x\n", self->rsaddr);
1748
1749 /* Second, get all the PPP info */
1750 len += sprintf(buf+len, " PPP state: %s",
1751 (self->ppp_open ? "registered" : "unregistered"));
1752 if(self->ppp_open)
1753 {
1754 len += sprintf(buf+len, ", unit: ppp%d",
1755 ppp_unit_number(&self->chan));
1756 len += sprintf(buf+len, ", channel: %d",
1757 ppp_channel_index(&self->chan));
1758 len += sprintf(buf+len, ", mru: %d",
1759 self->mru);
1760 /* Maybe add self->flags ? Later... */
1761 }
1762
1763 /* Then, get all the IrDA specific info... */
1764 if(self->ttp_open)
1765 state = "connected";
1766 else
1767 if(self->tsap != NULL)
1768 state = "connecting";
1769 else
1770 if(self->iriap != NULL)
1771 state = "searching";
1772 else
1773 if(self->ttp_connect)
1774 state = "weird";
1775 else
1776 state = "idle";
1777 len += sprintf(buf+len, "\n IrDA state: %s, ", state);
1778 len += sprintf(buf+len, "daddr: %08x, ", self->daddr);
1779 len += sprintf(buf+len, "stsap_sel: %02x, ", self->stsap_sel);
1780 len += sprintf(buf+len, "dtsap_sel: %02x\n", self->dtsap_sel);
1781
1782 /* Next socket, please... */
1783 self = (irnet_socket *) hashbin_get_next(irnet_server.list);
1784 }
1785
1786 /* Spin lock end */
1787 spin_unlock_irqrestore(&irnet_server.spinlock, flags);
1788
1789 return len;
1790 }
1791 #endif /* PROC_FS */
1792
1793
1794 /********************** CONFIGURATION/CLEANUP **********************/
1795 /*
1796 * Initialisation and teardown of the IrDA part, called at module
1797 * insertion and removal...
1798 */
1799
1800 /*------------------------------------------------------------------*/
1801 /*
1802 * Prepare the IrNET layer for operation...
1803 */
1804 int
irda_irnet_init(void)1805 irda_irnet_init(void)
1806 {
1807 int err = 0;
1808
1809 DENTER(MODULE_TRACE, "()\n");
1810
1811 /* Pure paranoia - should be redundant */
1812 memset(&irnet_server, 0, sizeof(struct irnet_root));
1813
1814 /* Setup start of irnet instance list */
1815 irnet_server.list = hashbin_new(HB_NOLOCK);
1816 DABORT(irnet_server.list == NULL, -ENOMEM,
1817 MODULE_ERROR, "Can't allocate hashbin!\n");
1818 /* Init spinlock for instance list */
1819 spin_lock_init(&irnet_server.spinlock);
1820
1821 /* Initialise control channel */
1822 init_waitqueue_head(&irnet_events.rwait);
1823 irnet_events.index = 0;
1824 /* Init spinlock for event logging */
1825 spin_lock_init(&irnet_events.spinlock);
1826
1827 #ifdef CONFIG_PROC_FS
1828 /* Add a /proc file for irnet infos */
1829 create_proc_info_entry("irnet", 0, proc_irda, irnet_proc_read);
1830 #endif /* CONFIG_PROC_FS */
1831
1832 /* Setup the IrNET server */
1833 err = irnet_setup_server();
1834
1835 if(!err)
1836 /* We are no longer functional... */
1837 irnet_server.running = 1;
1838
1839 DEXIT(MODULE_TRACE, "\n");
1840 return err;
1841 }
1842
1843 /*------------------------------------------------------------------*/
1844 /*
1845 * Cleanup at exit...
1846 */
1847 void
irda_irnet_cleanup(void)1848 irda_irnet_cleanup(void)
1849 {
1850 DENTER(MODULE_TRACE, "()\n");
1851
1852 /* We are no longer there... */
1853 irnet_server.running = 0;
1854
1855 #ifdef CONFIG_PROC_FS
1856 /* Remove our /proc file */
1857 remove_proc_entry("irnet", proc_irda);
1858 #endif /* CONFIG_PROC_FS */
1859
1860 /* Remove our IrNET server from existence */
1861 irnet_destroy_server();
1862
1863 /* Remove all instances of IrNET socket still present */
1864 hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
1865
1866 DEXIT(MODULE_TRACE, "\n");
1867 }
1868