~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/irda/iriap.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*********************************************************************
  2  *
  3  * Filename:      iriap.c
  4  * Version:       0.8
  5  * Description:   Information Access Protocol (IAP)
  6  * Status:        Experimental.
  7  * Author:        Dag Brattli <dagb@cs.uit.no>
  8  * Created at:    Thu Aug 21 00:02:07 1997
  9  * Modified at:   Sat Dec 25 16:42:42 1999
 10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
 11  *
 12  *     Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
 13  *     All Rights Reserved.
 14  *     Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
 15  *
 16  *     This program is free software; you can redistribute it and/or
 17  *     modify it under the terms of the GNU General Public License as
 18  *     published by the Free Software Foundation; either version 2 of
 19  *     the License, or (at your option) any later version.
 20  *
 21  *     Neither Dag Brattli nor University of Tromsø admit liability nor
 22  *     provide warranty for any of this software. This material is
 23  *     provided "AS-IS" and at no charge.
 24  *
 25  ********************************************************************/
 26 
 27 #include <linux/module.h>
 28 #include <linux/types.h>
 29 #include <linux/skbuff.h>
 30 #include <linux/fs.h>
 31 #include <linux/string.h>
 32 #include <linux/init.h>
 33 #include <linux/seq_file.h>
 34 #include <linux/slab.h>
 35 
 36 #include <asm/byteorder.h>
 37 #include <asm/unaligned.h>
 38 
 39 #include <net/irda/irda.h>
 40 #include <net/irda/irttp.h>
 41 #include <net/irda/irlmp.h>
 42 #include <net/irda/irias_object.h>
 43 #include <net/irda/iriap_event.h>
 44 #include <net/irda/iriap.h>
 45 
 46 #ifdef CONFIG_IRDA_DEBUG
 47 /* FIXME: This one should go in irlmp.c */
 48 static const char *const ias_charset_types[] = {
 49         "CS_ASCII",
 50         "CS_ISO_8859_1",
 51         "CS_ISO_8859_2",
 52         "CS_ISO_8859_3",
 53         "CS_ISO_8859_4",
 54         "CS_ISO_8859_5",
 55         "CS_ISO_8859_6",
 56         "CS_ISO_8859_7",
 57         "CS_ISO_8859_8",
 58         "CS_ISO_8859_9",
 59         "CS_UNICODE"
 60 };
 61 #endif  /* CONFIG_IRDA_DEBUG */
 62 
 63 static hashbin_t *iriap = NULL;
 64 static void *service_handle;
 65 
 66 static void __iriap_close(struct iriap_cb *self);
 67 static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode);
 68 static void iriap_disconnect_indication(void *instance, void *sap,
 69                                         LM_REASON reason, struct sk_buff *skb);
 70 static void iriap_connect_indication(void *instance, void *sap,
 71                                      struct qos_info *qos, __u32 max_sdu_size,
 72                                      __u8 max_header_size,
 73                                      struct sk_buff *skb);
 74 static void iriap_connect_confirm(void *instance, void *sap,
 75                                   struct qos_info *qos,
 76                                   __u32 max_sdu_size, __u8 max_header_size,
 77                                   struct sk_buff *skb);
 78 static int iriap_data_indication(void *instance, void *sap,
 79                                  struct sk_buff *skb);
 80 
 81 static void iriap_watchdog_timer_expired(void *data);
 82 
 83 static inline void iriap_start_watchdog_timer(struct iriap_cb *self,
 84                                               int timeout)
 85 {
 86         irda_start_timer(&self->watchdog_timer, timeout, self,
 87                          iriap_watchdog_timer_expired);
 88 }
 89 
 90 static struct lock_class_key irias_objects_key;
 91 
 92 /*
 93  * Function iriap_init (void)
 94  *
 95  *    Initializes the IrIAP layer, called by the module initialization code
 96  *    in irmod.c
 97  */
 98 int __init iriap_init(void)
 99 {
100         struct ias_object *obj;
101         struct iriap_cb *server;
102         __u8 oct_seq[6];
103         __u16 hints;
104 
105         /* Allocate master array */
106         iriap = hashbin_new(HB_LOCK);
107         if (!iriap)
108                 return -ENOMEM;
109 
110         /* Object repository - defined in irias_object.c */
111         irias_objects = hashbin_new(HB_LOCK);
112         if (!irias_objects) {
113                 IRDA_WARNING("%s: Can't allocate irias_objects hashbin!\n",
114                              __func__);
115                 hashbin_delete(iriap, NULL);
116                 return -ENOMEM;
117         }
118 
119         lockdep_set_class_and_name(&irias_objects->hb_spinlock, &irias_objects_key,
120                                    "irias_objects");
121 
122         /*
123          *  Register some default services for IrLMP
124          */
125         hints  = irlmp_service_to_hint(S_COMPUTER);
126         service_handle = irlmp_register_service(hints);
127 
128         /* Register the Device object with LM-IAS */
129         obj = irias_new_object("Device", IAS_DEVICE_ID);
130         irias_add_string_attrib(obj, "DeviceName", "Linux", IAS_KERNEL_ATTR);
131 
132         oct_seq[0] = 0x01;  /* Version 1 */
133         oct_seq[1] = 0x00;  /* IAS support bits */
134         oct_seq[2] = 0x00;  /* LM-MUX support bits */
135 #ifdef CONFIG_IRDA_ULTRA
136         oct_seq[2] |= 0x04; /* Connectionless Data support */
137 #endif
138         irias_add_octseq_attrib(obj, "IrLMPSupport", oct_seq, 3,
139                                 IAS_KERNEL_ATTR);
140         irias_insert_object(obj);
141 
142         /*
143          *  Register server support with IrLMP so we can accept incoming
144          *  connections
145          */
146         server = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
147         if (!server) {
148                 IRDA_DEBUG(0, "%s(), unable to open server\n", __func__);
149                 return -1;
150         }
151         iriap_register_lsap(server, LSAP_IAS, IAS_SERVER);
152 
153         return 0;
154 }
155 
156 /*
157  * Function iriap_cleanup (void)
158  *
159  *    Initializes the IrIAP layer, called by the module cleanup code in
160  *    irmod.c
161  */
162 void iriap_cleanup(void)
163 {
164         irlmp_unregister_service(service_handle);
165 
166         hashbin_delete(iriap, (FREE_FUNC) __iriap_close);
167         hashbin_delete(irias_objects, (FREE_FUNC) __irias_delete_object);
168 }
169 
170 /*
171  * Function iriap_open (void)
172  *
173  *    Opens an instance of the IrIAP layer, and registers with IrLMP
174  */
175 struct iriap_cb *iriap_open(__u8 slsap_sel, int mode, void *priv,
176                             CONFIRM_CALLBACK callback)
177 {
178         struct iriap_cb *self;
179 
180         IRDA_DEBUG(2, "%s()\n", __func__);
181 
182         self = kzalloc(sizeof(*self), GFP_ATOMIC);
183         if (!self) {
184                 IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
185                 return NULL;
186         }
187 
188         /*
189          *  Initialize instance
190          */
191 
192         self->magic = IAS_MAGIC;
193         self->mode = mode;
194         if (mode == IAS_CLIENT) {
195                 if (iriap_register_lsap(self, slsap_sel, mode)) {
196                         kfree(self);
197                         return NULL;
198                 }
199         }
200 
201         self->confirm = callback;
202         self->priv = priv;
203 
204         /* iriap_getvaluebyclass_request() will construct packets before
205          * we connect, so this must have a sane value... Jean II */
206         self->max_header_size = LMP_MAX_HEADER;
207 
208         init_timer(&self->watchdog_timer);
209 
210         hashbin_insert(iriap, (irda_queue_t *) self, (long) self, NULL);
211 
212         /* Initialize state machines */
213         iriap_next_client_state(self, S_DISCONNECT);
214         iriap_next_call_state(self, S_MAKE_CALL);
215         iriap_next_server_state(self, R_DISCONNECT);
216         iriap_next_r_connect_state(self, R_WAITING);
217 
218         return self;
219 }
220 EXPORT_SYMBOL(iriap_open);
221 
222 /*
223  * Function __iriap_close (self)
224  *
225  *    Removes (deallocates) the IrIAP instance
226  *
227  */
228 static void __iriap_close(struct iriap_cb *self)
229 {
230         IRDA_DEBUG(4, "%s()\n", __func__);
231 
232         IRDA_ASSERT(self != NULL, return;);
233         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
234 
235         del_timer(&self->watchdog_timer);
236 
237         if (self->request_skb)
238                 dev_kfree_skb(self->request_skb);
239 
240         self->magic = 0;
241 
242         kfree(self);
243 }
244 
245 /*
246  * Function iriap_close (void)
247  *
248  *    Closes IrIAP and deregisters with IrLMP
249  */
250 void iriap_close(struct iriap_cb *self)
251 {
252         struct iriap_cb *entry;
253 
254         IRDA_DEBUG(2, "%s()\n", __func__);
255 
256         IRDA_ASSERT(self != NULL, return;);
257         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
258 
259         if (self->lsap) {
260                 irlmp_close_lsap(self->lsap);
261                 self->lsap = NULL;
262         }
263 
264         entry = (struct iriap_cb *) hashbin_remove(iriap, (long) self, NULL);
265         IRDA_ASSERT(entry == self, return;);
266 
267         __iriap_close(self);
268 }
269 EXPORT_SYMBOL(iriap_close);
270 
271 static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode)
272 {
273         notify_t notify;
274 
275         IRDA_DEBUG(2, "%s()\n", __func__);
276 
277         irda_notify_init(&notify);
278         notify.connect_confirm       = iriap_connect_confirm;
279         notify.connect_indication    = iriap_connect_indication;
280         notify.disconnect_indication = iriap_disconnect_indication;
281         notify.data_indication       = iriap_data_indication;
282         notify.instance = self;
283         if (mode == IAS_CLIENT)
284                 strcpy(notify.name, "IrIAS cli");
285         else
286                 strcpy(notify.name, "IrIAS srv");
287 
288         self->lsap = irlmp_open_lsap(slsap_sel, &notify, 0);
289         if (self->lsap == NULL) {
290                 IRDA_ERROR("%s: Unable to allocated LSAP!\n", __func__);
291                 return -1;
292         }
293         self->slsap_sel = self->lsap->slsap_sel;
294 
295         return 0;
296 }
297 
298 /*
299  * Function iriap_disconnect_indication (handle, reason)
300  *
301  *    Got disconnect, so clean up everything associated with this connection
302  *
303  */
304 static void iriap_disconnect_indication(void *instance, void *sap,
305                                         LM_REASON reason,
306                                         struct sk_buff *skb)
307 {
308         struct iriap_cb *self;
309 
310         IRDA_DEBUG(4, "%s(), reason=%s [%d]\n", __func__,
311                    irlmp_reason_str(reason), reason);
312 
313         self = instance;
314 
315         IRDA_ASSERT(self != NULL, return;);
316         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
317 
318         IRDA_ASSERT(iriap != NULL, return;);
319 
320         del_timer(&self->watchdog_timer);
321 
322         /* Not needed */
323         if (skb)
324                 dev_kfree_skb(skb);
325 
326         if (self->mode == IAS_CLIENT) {
327                 IRDA_DEBUG(4, "%s(), disconnect as client\n", __func__);
328 
329 
330                 iriap_do_client_event(self, IAP_LM_DISCONNECT_INDICATION,
331                                       NULL);
332                 /*
333                  * Inform service user that the request failed by sending
334                  * it a NULL value. Warning, the client might close us, so
335                  * remember no to use self anymore after calling confirm
336                  */
337                 if (self->confirm)
338                         self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
339         } else {
340                 IRDA_DEBUG(4, "%s(), disconnect as server\n", __func__);
341                 iriap_do_server_event(self, IAP_LM_DISCONNECT_INDICATION,
342                                       NULL);
343                 iriap_close(self);
344         }
345 }
346 
347 /*
348  * Function iriap_disconnect_request (handle)
349  */
350 static void iriap_disconnect_request(struct iriap_cb *self)
351 {
352         struct sk_buff *tx_skb;
353 
354         IRDA_DEBUG(4, "%s()\n", __func__);
355 
356         IRDA_ASSERT(self != NULL, return;);
357         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
358 
359         tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
360         if (tx_skb == NULL) {
361                 IRDA_DEBUG(0,
362                            "%s(), Could not allocate an sk_buff of length %d\n",
363                            __func__, LMP_MAX_HEADER);
364                 return;
365         }
366 
367         /*
368          *  Reserve space for MUX control and LAP header
369          */
370         skb_reserve(tx_skb, LMP_MAX_HEADER);
371 
372         irlmp_disconnect_request(self->lsap, tx_skb);
373 }
374 
375 /*
376  * Function iriap_getvaluebyclass (addr, name, attr)
377  *
378  *    Retrieve all values from attribute in all objects with given class
379  *    name
380  */
381 int iriap_getvaluebyclass_request(struct iriap_cb *self,
382                                   __u32 saddr, __u32 daddr,
383                                   char *name, char *attr)
384 {
385         struct sk_buff *tx_skb;
386         int name_len, attr_len, skb_len;
387         __u8 *frame;
388 
389         IRDA_ASSERT(self != NULL, return -1;);
390         IRDA_ASSERT(self->magic == IAS_MAGIC, return -1;);
391 
392         /* Client must supply the destination device address */
393         if (!daddr)
394                 return -1;
395 
396         self->daddr = daddr;
397         self->saddr = saddr;
398 
399         /*
400          *  Save operation, so we know what the later indication is about
401          */
402         self->operation = GET_VALUE_BY_CLASS;
403 
404         /* Give ourselves 10 secs to finish this operation */
405         iriap_start_watchdog_timer(self, 10*HZ);
406 
407         name_len = strlen(name);        /* Up to IAS_MAX_CLASSNAME = 60 */
408         attr_len = strlen(attr);        /* Up to IAS_MAX_ATTRIBNAME = 60 */
409 
410         skb_len = self->max_header_size+2+name_len+1+attr_len+4;
411         tx_skb = alloc_skb(skb_len, GFP_ATOMIC);
412         if (!tx_skb)
413                 return -ENOMEM;
414 
415         /* Reserve space for MUX and LAP header */
416         skb_reserve(tx_skb, self->max_header_size);
417         skb_put(tx_skb, 3+name_len+attr_len);
418         frame = tx_skb->data;
419 
420         /* Build frame */
421         frame[0] = IAP_LST | GET_VALUE_BY_CLASS;
422         frame[1] = name_len;                       /* Insert length of name */
423         memcpy(frame+2, name, name_len);           /* Insert name */
424         frame[2+name_len] = attr_len;              /* Insert length of attr */
425         memcpy(frame+3+name_len, attr, attr_len);  /* Insert attr */
426 
427         iriap_do_client_event(self, IAP_CALL_REQUEST_GVBC, tx_skb);
428 
429         /* Drop reference count - see state_s_disconnect(). */
430         dev_kfree_skb(tx_skb);
431 
432         return 0;
433 }
434 EXPORT_SYMBOL(iriap_getvaluebyclass_request);
435 
436 /*
437  * Function iriap_getvaluebyclass_confirm (self, skb)
438  *
439  *    Got result from GetValueByClass command. Parse it and return result
440  *    to service user.
441  *
442  */
443 static void iriap_getvaluebyclass_confirm(struct iriap_cb *self,
444                                           struct sk_buff *skb)
445 {
446         struct ias_value *value;
447         int charset;
448         __u32 value_len;
449         __u32 tmp_cpu32;
450         __u16 obj_id;
451         __u16 len;
452         __u8  type;
453         __u8 *fp;
454         int n;
455 
456         IRDA_ASSERT(self != NULL, return;);
457         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
458         IRDA_ASSERT(skb != NULL, return;);
459 
460         /* Initialize variables */
461         fp = skb->data;
462         n = 2;
463 
464         /* Get length, MSB first */
465         len = get_unaligned_be16(fp + n);
466         n += 2;
467 
468         IRDA_DEBUG(4, "%s(), len=%d\n", __func__, len);
469 
470         /* Get object ID, MSB first */
471         obj_id = get_unaligned_be16(fp + n);
472         n += 2;
473 
474         type = fp[n++];
475         IRDA_DEBUG(4, "%s(), Value type = %d\n", __func__, type);
476 
477         switch (type) {
478         case IAS_INTEGER:
479                 memcpy(&tmp_cpu32, fp+n, 4); n += 4;
480                 be32_to_cpus(&tmp_cpu32);
481                 value = irias_new_integer_value(tmp_cpu32);
482 
483                 /*  Legal values restricted to 0x01-0x6f, page 15 irttp */
484                 IRDA_DEBUG(4, "%s(), lsap=%d\n", __func__, value->t.integer);
485                 break;
486         case IAS_STRING:
487                 charset = fp[n++];
488 
489                 switch (charset) {
490                 case CS_ASCII:
491                         break;
492 /*              case CS_ISO_8859_1: */
493 /*              case CS_ISO_8859_2: */
494 /*              case CS_ISO_8859_3: */
495 /*              case CS_ISO_8859_4: */
496 /*              case CS_ISO_8859_5: */
497 /*              case CS_ISO_8859_6: */
498 /*              case CS_ISO_8859_7: */
499 /*              case CS_ISO_8859_8: */
500 /*              case CS_ISO_8859_9: */
501 /*              case CS_UNICODE: */
502                 default:
503                         IRDA_DEBUG(0, "%s(), charset [%d] %s, not supported\n",
504                                    __func__, charset,
505                                    charset < ARRAY_SIZE(ias_charset_types) ?
506                                         ias_charset_types[charset] :
507                                         "(unknown)");
508 
509                         /* Aborting, close connection! */
510                         iriap_disconnect_request(self);
511                         return;
512                         /* break; */
513                 }
514                 value_len = fp[n++];
515                 IRDA_DEBUG(4, "%s(), strlen=%d\n", __func__, value_len);
516 
517                 /* Make sure the string is null-terminated */
518                 if (n + value_len < skb->len)
519                         fp[n + value_len] = 0x00;
520                 IRDA_DEBUG(4, "Got string %s\n", fp+n);
521 
522                 /* Will truncate to IAS_MAX_STRING bytes */
523                 value = irias_new_string_value(fp+n);
524                 break;
525         case IAS_OCT_SEQ:
526                 value_len = get_unaligned_be16(fp + n);
527                 n += 2;
528 
529                 /* Will truncate to IAS_MAX_OCTET_STRING bytes */
530                 value = irias_new_octseq_value(fp+n, value_len);
531                 break;
532         default:
533                 value = irias_new_missing_value();
534                 break;
535         }
536 
537         /* Finished, close connection! */
538         iriap_disconnect_request(self);
539 
540         /* Warning, the client might close us, so remember no to use self
541          * anymore after calling confirm
542          */
543         if (self->confirm)
544                 self->confirm(IAS_SUCCESS, obj_id, value, self->priv);
545         else {
546                 IRDA_DEBUG(0, "%s(), missing handler!\n", __func__);
547                 irias_delete_value(value);
548         }
549 }
550 
551 /*
552  * Function iriap_getvaluebyclass_response ()
553  *
554  *    Send answer back to remote LM-IAS
555  *
556  */
557 static void iriap_getvaluebyclass_response(struct iriap_cb *self,
558                                            __u16 obj_id,
559                                            __u8 ret_code,
560                                            struct ias_value *value)
561 {
562         struct sk_buff *tx_skb;
563         int n;
564         __be32 tmp_be32;
565         __be16 tmp_be16;
566         __u8 *fp;
567 
568         IRDA_DEBUG(4, "%s()\n", __func__);
569 
570         IRDA_ASSERT(self != NULL, return;);
571         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
572         IRDA_ASSERT(value != NULL, return;);
573         IRDA_ASSERT(value->len <= 1024, return;);
574 
575         /* Initialize variables */
576         n = 0;
577 
578         /*
579          *  We must adjust the size of the response after the length of the
580          *  value. We add 32 bytes because of the 6 bytes for the frame and
581          *  max 5 bytes for the value coding.
582          */
583         tx_skb = alloc_skb(value->len + self->max_header_size + 32,
584                            GFP_ATOMIC);
585         if (!tx_skb)
586                 return;
587 
588         /* Reserve space for MUX and LAP header */
589         skb_reserve(tx_skb, self->max_header_size);
590         skb_put(tx_skb, 6);
591 
592         fp = tx_skb->data;
593 
594         /* Build frame */
595         fp[n++] = GET_VALUE_BY_CLASS | IAP_LST;
596         fp[n++] = ret_code;
597 
598         /* Insert list length (MSB first) */
599         tmp_be16 = htons(0x0001);
600         memcpy(fp+n, &tmp_be16, 2);  n += 2;
601 
602         /* Insert object identifier ( MSB first) */
603         tmp_be16 = cpu_to_be16(obj_id);
604         memcpy(fp+n, &tmp_be16, 2); n += 2;
605 
606         switch (value->type) {
607         case IAS_STRING:
608                 skb_put(tx_skb, 3 + value->len);
609                 fp[n++] = value->type;
610                 fp[n++] = 0; /* ASCII */
611                 fp[n++] = (__u8) value->len;
612                 memcpy(fp+n, value->t.string, value->len); n+=value->len;
613                 break;
614         case IAS_INTEGER:
615                 skb_put(tx_skb, 5);
616                 fp[n++] = value->type;
617 
618                 tmp_be32 = cpu_to_be32(value->t.integer);
619                 memcpy(fp+n, &tmp_be32, 4); n += 4;
620                 break;
621         case IAS_OCT_SEQ:
622                 skb_put(tx_skb, 3 + value->len);
623                 fp[n++] = value->type;
624 
625                 tmp_be16 = cpu_to_be16(value->len);
626                 memcpy(fp+n, &tmp_be16, 2); n += 2;
627                 memcpy(fp+n, value->t.oct_seq, value->len); n+=value->len;
628                 break;
629         case IAS_MISSING:
630                 IRDA_DEBUG( 3, "%s: sending IAS_MISSING\n", __func__);
631                 skb_put(tx_skb, 1);
632                 fp[n++] = value->type;
633                 break;
634         default:
635                 IRDA_DEBUG(0, "%s(), type not implemented!\n", __func__);
636                 break;
637         }
638         iriap_do_r_connect_event(self, IAP_CALL_RESPONSE, tx_skb);
639 
640         /* Drop reference count - see state_r_execute(). */
641         dev_kfree_skb(tx_skb);
642 }
643 
644 /*
645  * Function iriap_getvaluebyclass_indication (self, skb)
646  *
647  *    getvaluebyclass is requested from peer LM-IAS
648  *
649  */
650 static void iriap_getvaluebyclass_indication(struct iriap_cb *self,
651                                              struct sk_buff *skb)
652 {
653         struct ias_object *obj;
654         struct ias_attrib *attrib;
655         int name_len;
656         int attr_len;
657         char name[IAS_MAX_CLASSNAME + 1];       /* 60 bytes */
658         char attr[IAS_MAX_ATTRIBNAME + 1];      /* 60 bytes */
659         __u8 *fp;
660         int n;
661 
662         IRDA_DEBUG(4, "%s()\n", __func__);
663 
664         IRDA_ASSERT(self != NULL, return;);
665         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
666         IRDA_ASSERT(skb != NULL, return;);
667 
668         fp = skb->data;
669         n = 1;
670 
671         name_len = fp[n++];
672 
673         IRDA_ASSERT(name_len < IAS_MAX_CLASSNAME + 1, return;);
674 
675         memcpy(name, fp+n, name_len); n+=name_len;
676         name[name_len] = '\0';
677 
678         attr_len = fp[n++];
679 
680         IRDA_ASSERT(attr_len < IAS_MAX_ATTRIBNAME + 1, return;);
681 
682         memcpy(attr, fp+n, attr_len); n+=attr_len;
683         attr[attr_len] = '\0';
684 
685         IRDA_DEBUG(4, "LM-IAS: Looking up %s: %s\n", name, attr);
686         obj = irias_find_object(name);
687 
688         if (obj == NULL) {
689                 IRDA_DEBUG(2, "LM-IAS: Object %s not found\n", name);
690                 iriap_getvaluebyclass_response(self, 0x1235, IAS_CLASS_UNKNOWN,
691                                                &irias_missing);
692                 return;
693         }
694         IRDA_DEBUG(4, "LM-IAS: found %s, id=%d\n", obj->name, obj->id);
695 
696         attrib = irias_find_attrib(obj, attr);
697         if (attrib == NULL) {
698                 IRDA_DEBUG(2, "LM-IAS: Attribute %s not found\n", attr);
699                 iriap_getvaluebyclass_response(self, obj->id,
700                                                IAS_ATTRIB_UNKNOWN,
701                                                &irias_missing);
702                 return;
703         }
704 
705         /* We have a match; send the value.  */
706         iriap_getvaluebyclass_response(self, obj->id, IAS_SUCCESS,
707                                        attrib->value);
708 }
709 
710 /*
711  * Function iriap_send_ack (void)
712  *
713  *    Currently not used
714  *
715  */
716 void iriap_send_ack(struct iriap_cb *self)
717 {
718         struct sk_buff *tx_skb;
719         __u8 *frame;
720 
721         IRDA_DEBUG(2, "%s()\n", __func__);
722 
723         IRDA_ASSERT(self != NULL, return;);
724         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
725 
726         tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
727         if (!tx_skb)
728                 return;
729 
730         /* Reserve space for MUX and LAP header */
731         skb_reserve(tx_skb, self->max_header_size);
732         skb_put(tx_skb, 1);
733         frame = tx_skb->data;
734 
735         /* Build frame */
736         frame[0] = IAP_LST | IAP_ACK | self->operation;
737 
738         irlmp_data_request(self->lsap, tx_skb);
739 }
740 
741 void iriap_connect_request(struct iriap_cb *self)
742 {
743         int ret;
744 
745         IRDA_ASSERT(self != NULL, return;);
746         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
747 
748         ret = irlmp_connect_request(self->lsap, LSAP_IAS,
749                                     self->saddr, self->daddr,
750                                     NULL, NULL);
751         if (ret < 0) {
752                 IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
753                 self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
754         }
755 }
756 
757 /*
758  * Function iriap_connect_confirm (handle, skb)
759  *
760  *    LSAP connection confirmed!
761  *
762  */
763 static void iriap_connect_confirm(void *instance, void *sap,
764                                   struct qos_info *qos, __u32 max_seg_size,
765                                   __u8 max_header_size,
766                                   struct sk_buff *skb)
767 {
768         struct iriap_cb *self;
769 
770         self = instance;
771 
772         IRDA_ASSERT(self != NULL, return;);
773         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
774         IRDA_ASSERT(skb != NULL, return;);
775 
776         self->max_data_size = max_seg_size;
777         self->max_header_size = max_header_size;
778 
779         del_timer(&self->watchdog_timer);
780 
781         iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
782 
783         /* Drop reference count - see state_s_make_call(). */
784         dev_kfree_skb(skb);
785 }
786 
787 /*
788  * Function iriap_connect_indication ( handle, skb)
789  *
790  *    Remote LM-IAS is requesting connection
791  *
792  */
793 static void iriap_connect_indication(void *instance, void *sap,
794                                      struct qos_info *qos, __u32 max_seg_size,
795                                      __u8 max_header_size,
796                                      struct sk_buff *skb)
797 {
798         struct iriap_cb *self, *new;
799 
800         IRDA_DEBUG(1, "%s()\n", __func__);
801 
802         self = instance;
803 
804         IRDA_ASSERT(skb != NULL, return;);
805         IRDA_ASSERT(self != NULL, goto out;);
806         IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
807 
808         /* Start new server */
809         new = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
810         if (!new) {
811                 IRDA_DEBUG(0, "%s(), open failed\n", __func__);
812                 goto out;
813         }
814 
815         /* Now attach up the new "socket" */
816         new->lsap = irlmp_dup(self->lsap, new);
817         if (!new->lsap) {
818                 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
819                 goto out;
820         }
821 
822         new->max_data_size = max_seg_size;
823         new->max_header_size = max_header_size;
824 
825         /* Clean up the original one to keep it in listen state */
826         irlmp_listen(self->lsap);
827 
828         iriap_do_server_event(new, IAP_LM_CONNECT_INDICATION, skb);
829 
830 out:
831         /* Drop reference count - see state_r_disconnect(). */
832         dev_kfree_skb(skb);
833 }
834 
835 /*
836  * Function iriap_data_indication (handle, skb)
837  *
838  *    Receives data from connection identified by handle from IrLMP
839  *
840  */
841 static int iriap_data_indication(void *instance, void *sap,
842                                  struct sk_buff *skb)
843 {
844         struct iriap_cb *self;
845         __u8  *frame;
846         __u8  opcode;
847 
848         IRDA_DEBUG(3, "%s()\n", __func__);
849 
850         self = instance;
851 
852         IRDA_ASSERT(skb != NULL, return 0;);
853         IRDA_ASSERT(self != NULL, goto out;);
854         IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
855 
856         frame = skb->data;
857 
858         if (self->mode == IAS_SERVER) {
859                 /* Call server */
860                 IRDA_DEBUG(4, "%s(), Calling server!\n", __func__);
861                 iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
862                 goto out;
863         }
864         opcode = frame[0];
865         if (~opcode & IAP_LST) {
866                 IRDA_WARNING("%s:, IrIAS multiframe commands or "
867                              "results is not implemented yet!\n",
868                              __func__);
869                 goto out;
870         }
871 
872         /* Check for ack frames since they don't contain any data */
873         if (opcode & IAP_ACK) {
874                 IRDA_DEBUG(0, "%s() Got ack frame!\n", __func__);
875                 goto out;
876         }
877 
878         opcode &= ~IAP_LST; /* Mask away LST bit */
879 
880         switch (opcode) {
881         case GET_INFO_BASE:
882                 IRDA_DEBUG(0, "IrLMP GetInfoBaseDetails not implemented!\n");
883                 break;
884         case GET_VALUE_BY_CLASS:
885                 iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
886 
887                 switch (frame[1]) {
888                 case IAS_SUCCESS:
889                         iriap_getvaluebyclass_confirm(self, skb);
890                         break;
891                 case IAS_CLASS_UNKNOWN:
892                         IRDA_DEBUG(1, "%s(), No such class!\n", __func__);
893                         /* Finished, close connection! */
894                         iriap_disconnect_request(self);
895 
896                         /*
897                          * Warning, the client might close us, so remember
898                          * no to use self anymore after calling confirm
899                          */
900                         if (self->confirm)
901                                 self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
902                                               self->priv);
903                         break;
904                 case IAS_ATTRIB_UNKNOWN:
905                         IRDA_DEBUG(1, "%s(), No such attribute!\n", __func__);
906                         /* Finished, close connection! */
907                         iriap_disconnect_request(self);
908 
909                         /*
910                          * Warning, the client might close us, so remember
911                          * no to use self anymore after calling confirm
912                          */
913                         if (self->confirm)
914                                 self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
915                                               self->priv);
916                         break;
917                 }
918                 break;
919         default:
920                 IRDA_DEBUG(0, "%s(), Unknown op-code: %02x\n", __func__,
921                            opcode);
922                 break;
923         }
924 
925 out:
926         /* Cleanup - sub-calls will have done skb_get() as needed. */
927         dev_kfree_skb(skb);
928         return 0;
929 }
930 
931 /*
932  * Function iriap_call_indication (self, skb)
933  *
934  *    Received call to server from peer LM-IAS
935  *
936  */
937 void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
938 {
939         __u8 *fp;
940         __u8 opcode;
941 
942         IRDA_DEBUG(4, "%s()\n", __func__);
943 
944         IRDA_ASSERT(self != NULL, return;);
945         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
946         IRDA_ASSERT(skb != NULL, return;);
947 
948         fp = skb->data;
949 
950         opcode = fp[0];
951         if (~opcode & 0x80) {
952                 IRDA_WARNING("%s: IrIAS multiframe commands or results "
953                              "is not implemented yet!\n", __func__);
954                 return;
955         }
956         opcode &= 0x7f; /* Mask away LST bit */
957 
958         switch (opcode) {
959         case GET_INFO_BASE:
960                 IRDA_WARNING("%s: GetInfoBaseDetails not implemented yet!\n",
961                              __func__);
962                 break;
963         case GET_VALUE_BY_CLASS:
964                 iriap_getvaluebyclass_indication(self, skb);
965                 break;
966         }
967         /* skb will be cleaned up in iriap_data_indication */
968 }
969 
970 /*
971  * Function iriap_watchdog_timer_expired (data)
972  *
973  *    Query has taken too long time, so abort
974  *
975  */
976 static void iriap_watchdog_timer_expired(void *data)
977 {
978         struct iriap_cb *self = (struct iriap_cb *) data;
979 
980         IRDA_ASSERT(self != NULL, return;);
981         IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
982 
983         /* iriap_close(self); */
984 }
985 
986 #ifdef CONFIG_PROC_FS
987 
988 static const char *const ias_value_types[] = {
989         "IAS_MISSING",
990         "IAS_INTEGER",
991         "IAS_OCT_SEQ",
992         "IAS_STRING"
993 };
994 
995 static inline struct ias_object *irias_seq_idx(loff_t pos)
996 {
997         struct ias_object *obj;
998 
999         for (obj = (struct ias_object *) hashbin_get_first(irias_objects);
1000              obj; obj = (struct ias_object *) hashbin_get_next(irias_objects)) {
1001                 if (pos-- == 0)
1002                         break;
1003         }
1004 
1005         return obj;
1006 }
1007 
1008 static void *irias_seq_start(struct seq_file *seq, loff_t *pos)
1009 {
1010         spin_lock_irq(&irias_objects->hb_spinlock);
1011 
1012         return *pos ? irias_seq_idx(*pos - 1) : SEQ_START_TOKEN;
1013 }
1014 
1015 static void *irias_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1016 {
1017         ++*pos;
1018 
1019         return (v == SEQ_START_TOKEN)
1020                 ? (void *) hashbin_get_first(irias_objects)
1021                 : (void *) hashbin_get_next(irias_objects);
1022 }
1023 
1024 static void irias_seq_stop(struct seq_file *seq, void *v)
1025 {
1026         spin_unlock_irq(&irias_objects->hb_spinlock);
1027 }
1028 
1029 static int irias_seq_show(struct seq_file *seq, void *v)
1030 {
1031         if (v == SEQ_START_TOKEN)
1032                 seq_puts(seq, "LM-IAS Objects:\n");
1033         else {
1034                 struct ias_object *obj = v;
1035                 struct ias_attrib *attrib;
1036 
1037                 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -EINVAL;);
1038 
1039                 seq_printf(seq, "name: %s, id=%d\n",
1040                            obj->name, obj->id);
1041 
1042                 /* Careful for priority inversions here !
1043                  * All other uses of attrib spinlock are independent of
1044                  * the object spinlock, so we are safe. Jean II */
1045                 spin_lock(&obj->attribs->hb_spinlock);
1046 
1047                 /* List all attributes for this object */
1048                 for (attrib = (struct ias_attrib *) hashbin_get_first(obj->attribs);
1049                      attrib != NULL;
1050                      attrib = (struct ias_attrib *) hashbin_get_next(obj->attribs)) {
1051 
1052                         IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC,
1053                                     goto outloop; );
1054 
1055                         seq_printf(seq, " - Attribute name: \"%s\", ",
1056                                    attrib->name);
1057                         seq_printf(seq, "value[%s]: ",
1058                                    ias_value_types[attrib->value->type]);
1059 
1060                         switch (attrib->value->type) {
1061                         case IAS_INTEGER:
1062                                 seq_printf(seq, "%d\n",
1063                                            attrib->value->t.integer);
1064                                 break;
1065                         case IAS_STRING:
1066                                 seq_printf(seq, "\"%s\"\n",
1067                                            attrib->value->t.string);
1068                                 break;
1069                         case IAS_OCT_SEQ:
1070                                 seq_printf(seq, "octet sequence (%d bytes)\n",
1071                                            attrib->value->len);
1072                                 break;
1073                         case IAS_MISSING:
1074                                 seq_puts(seq, "missing\n");
1075                                 break;
1076                         default:
1077                                 seq_printf(seq, "type %d?\n",
1078                                            attrib->value->type);
1079                         }
1080                         seq_putc(seq, '\n');
1081 
1082                 }
1083         IRDA_ASSERT_LABEL(outloop:)
1084                 spin_unlock(&obj->attribs->hb_spinlock);
1085         }
1086 
1087         return 0;
1088 }
1089 
1090 static const struct seq_operations irias_seq_ops = {
1091         .start  = irias_seq_start,
1092         .next   = irias_seq_next,
1093         .stop   = irias_seq_stop,
1094         .show   = irias_seq_show,
1095 };
1096 
1097 static int irias_seq_open(struct inode *inode, struct file *file)
1098 {
1099         IRDA_ASSERT( irias_objects != NULL, return -EINVAL;);
1100 
1101         return seq_open(file, &irias_seq_ops);
1102 }
1103 
1104 const struct file_operations irias_seq_fops = {
1105         .owner          = THIS_MODULE,
1106         .open           = irias_seq_open,
1107         .read           = seq_read,
1108         .llseek         = seq_lseek,
1109         .release        = seq_release,
1110 };
1111 
1112 #endif /* PROC_FS */
1113 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp