linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / irda / irias_object.c
1 /*********************************************************************
2  *
3  * Filename:      irias_object.c
4  * Version:       0.3
5  * Description:   IAS object database and functions
6  * Status:        Experimental.
7  * Author:        Dag Brattli <dagb@cs.uit.no>
8  * Created at:    Thu Oct  1 22:50:04 1998
9  * Modified at:   Wed Dec 15 11:23:16 1999
10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
11  *
12  *     Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
13  *
14  *     This program is free software; you can redistribute it and/or
15  *     modify it under the terms of the GNU General Public License as
16  *     published by the Free Software Foundation; either version 2 of
17  *     the License, or (at your option) any later version.
18  *
19  *     Neither Dag Brattli nor University of Tromsø admit liability nor
20  *     provide warranty for any of this software. This material is
21  *     provided "AS-IS" and at no charge.
22  *
23  ********************************************************************/
24
25 #include <linux/string.h>
26 #include <linux/socket.h>
27 #include <linux/module.h>
28
29 #include <net/irda/irda.h>
30 #include <net/irda/irias_object.h>
31
32 hashbin_t *irias_objects;
33
34 /*
35  *  Used when a missing value needs to be returned
36  */
37 struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}};
38
39 /*
40  * Function strndup (str, max)
41  *
42  *    My own kernel version of strndup!
43  *
44  * Faster, check boundary... Jean II
45  */
46 static char *strndup(char *str, int max)
47 {
48         char *new_str;
49         int len;
50
51         /* Check string */
52         if (str == NULL)
53                 return NULL;
54         /* Check length, truncate */
55         len = strlen(str);
56         if(len > max)
57                 len = max;
58
59         /* Allocate new string */
60         new_str = kmalloc(len + 1, GFP_ATOMIC);
61         if (new_str == NULL) {
62                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
63                 return NULL;
64         }
65
66         /* Copy and truncate */
67         memcpy(new_str, str, len);
68         new_str[len] = '\0';
69
70         return new_str;
71 }
72
73 /*
74  * Function ias_new_object (name, id)
75  *
76  *    Create a new IAS object
77  *
78  */
79 struct ias_object *irias_new_object( char *name, int id)
80 {
81         struct ias_object *obj;
82
83         IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
84
85         obj = kmalloc(sizeof(struct ias_object), GFP_ATOMIC);
86         if (obj == NULL) {
87                 IRDA_WARNING("%s(), Unable to allocate object!\n",
88                              __FUNCTION__);
89                 return NULL;
90         }
91         memset(obj, 0, sizeof( struct ias_object));
92
93         obj->magic = IAS_OBJECT_MAGIC;
94         obj->name = strndup(name, IAS_MAX_CLASSNAME);
95         obj->id = id;
96
97         /* Locking notes : the attrib spinlock has lower precendence
98          * than the objects spinlock. Never grap the objects spinlock
99          * while holding any attrib spinlock (risk of deadlock). Jean II */
100         obj->attribs = hashbin_new(HB_LOCK);
101
102         if (obj->attribs == NULL) {
103                 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
104                              __FUNCTION__);
105                 kfree(obj);
106                 return NULL;
107         }
108
109         return obj;
110 }
111 EXPORT_SYMBOL(irias_new_object);
112
113 /*
114  * Function irias_delete_attrib (attrib)
115  *
116  *    Delete given attribute and deallocate all its memory
117  *
118  */
119 static void __irias_delete_attrib(struct ias_attrib *attrib)
120 {
121         IRDA_ASSERT(attrib != NULL, return;);
122         IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
123
124         kfree(attrib->name);
125
126         irias_delete_value(attrib->value);
127         attrib->magic = ~IAS_ATTRIB_MAGIC;
128
129         kfree(attrib);
130 }
131
132 void __irias_delete_object(struct ias_object *obj)
133 {
134         IRDA_ASSERT(obj != NULL, return;);
135         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
136
137         kfree(obj->name);
138
139         hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
140
141         obj->magic = ~IAS_OBJECT_MAGIC;
142
143         kfree(obj);
144 }
145
146 /*
147  * Function irias_delete_object (obj)
148  *
149  *    Remove object from hashbin and deallocate all attributes associated with
150  *    with this object and the object itself
151  *
152  */
153 int irias_delete_object(struct ias_object *obj)
154 {
155         struct ias_object *node;
156
157         IRDA_ASSERT(obj != NULL, return -1;);
158         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
159
160         /* Remove from list */
161         node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
162         if (!node)
163                 IRDA_DEBUG( 0, "%s(), object already removed!\n",
164                             __FUNCTION__);
165
166         /* Destroy */
167         __irias_delete_object(obj);
168
169         return 0;
170 }
171 EXPORT_SYMBOL(irias_delete_object);
172
173 /*
174  * Function irias_delete_attrib (obj)
175  *
176  *    Remove attribute from hashbin and, if it was the last attribute of
177  *    the object, remove the object as well.
178  *
179  */
180 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
181                         int cleanobject)
182 {
183         struct ias_attrib *node;
184
185         IRDA_ASSERT(obj != NULL, return -1;);
186         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
187         IRDA_ASSERT(attrib != NULL, return -1;);
188
189         /* Remove attribute from object */
190         node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
191         if (!node)
192                 return 0; /* Already removed or non-existent */
193
194         /* Deallocate attribute */
195         __irias_delete_attrib(node);
196
197         /* Check if object has still some attributes, destroy it if none.
198          * At first glance, this look dangerous, as the kernel reference
199          * various IAS objects. However, we only use this function on
200          * user attributes, not kernel attributes, so there is no risk
201          * of deleting a kernel object this way. Jean II */
202         node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
203         if (cleanobject && !node)
204                 irias_delete_object(obj);
205
206         return 0;
207 }
208
209 /*
210  * Function irias_insert_object (obj)
211  *
212  *    Insert an object into the LM-IAS database
213  *
214  */
215 void irias_insert_object(struct ias_object *obj)
216 {
217         IRDA_ASSERT(obj != NULL, return;);
218         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
219
220         hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
221 }
222 EXPORT_SYMBOL(irias_insert_object);
223
224 /*
225  * Function irias_find_object (name)
226  *
227  *    Find object with given name
228  *
229  */
230 struct ias_object *irias_find_object(char *name)
231 {
232         IRDA_ASSERT(name != NULL, return NULL;);
233
234         /* Unsafe (locking), object might change */
235         return hashbin_lock_find(irias_objects, 0, name);
236 }
237 EXPORT_SYMBOL(irias_find_object);
238
239 /*
240  * Function irias_find_attrib (obj, name)
241  *
242  *    Find named attribute in object
243  *
244  */
245 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
246 {
247         struct ias_attrib *attrib;
248
249         IRDA_ASSERT(obj != NULL, return NULL;);
250         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
251         IRDA_ASSERT(name != NULL, return NULL;);
252
253         attrib = hashbin_lock_find(obj->attribs, 0, name);
254         if (attrib == NULL)
255                 return NULL;
256
257         /* Unsafe (locking), attrib might change */
258         return attrib;
259 }
260 EXPORT_SYMBOL(irias_find_attrib);
261
262 /*
263  * Function irias_add_attribute (obj, attrib)
264  *
265  *    Add attribute to object
266  *
267  */
268 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
269                              int owner)
270 {
271         IRDA_ASSERT(obj != NULL, return;);
272         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
273
274         IRDA_ASSERT(attrib != NULL, return;);
275         IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
276
277         /* Set if attrib is owned by kernel or user space */
278         attrib->value->owner = owner;
279
280         hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
281 }
282
283 /*
284  * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
285  *
286  *    Change the value of an objects attribute.
287  *
288  */
289 int irias_object_change_attribute(char *obj_name, char *attrib_name,
290                                   struct ias_value *new_value)
291 {
292         struct ias_object *obj;
293         struct ias_attrib *attrib;
294         unsigned long flags;
295
296         /* Find object */
297         obj = hashbin_lock_find(irias_objects, 0, obj_name);
298         if (obj == NULL) {
299                 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
300                              obj_name);
301                 return -1;
302         }
303
304         /* Slightly unsafe (obj might get removed under us) */
305         spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
306
307         /* Find attribute */
308         attrib = hashbin_find(obj->attribs, 0, attrib_name);
309         if (attrib == NULL) {
310                 IRDA_WARNING("%s: Unable to find attribute: %s\n",
311                              __FUNCTION__, attrib_name);
312                 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
313                 return -1;
314         }
315
316         if ( attrib->value->type != new_value->type) {
317                 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
318                             __FUNCTION__);
319                 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
320                 return -1;
321         }
322
323         /* Delete old value */
324         irias_delete_value(attrib->value);
325
326         /* Insert new value */
327         attrib->value = new_value;
328
329         /* Success */
330         spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
331         return 0;
332 }
333 EXPORT_SYMBOL(irias_object_change_attribute);
334
335 /*
336  * Function irias_object_add_integer_attrib (obj, name, value)
337  *
338  *    Add an integer attribute to an LM-IAS object
339  *
340  */
341 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
342                               int owner)
343 {
344         struct ias_attrib *attrib;
345
346         IRDA_ASSERT(obj != NULL, return;);
347         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
348         IRDA_ASSERT(name != NULL, return;);
349
350         attrib = kmalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
351         if (attrib == NULL) {
352                 IRDA_WARNING("%s: Unable to allocate attribute!\n",
353                              __FUNCTION__);
354                 return;
355         }
356         memset(attrib, 0, sizeof( struct ias_attrib));
357
358         attrib->magic = IAS_ATTRIB_MAGIC;
359         attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
360
361         /* Insert value */
362         attrib->value = irias_new_integer_value(value);
363
364         irias_add_attrib(obj, attrib, owner);
365 }
366 EXPORT_SYMBOL(irias_add_integer_attrib);
367
368  /*
369  * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
370  *
371  *    Add a octet sequence attribute to an LM-IAS object
372  *
373  */
374
375 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
376                              int len, int owner)
377 {
378         struct ias_attrib *attrib;
379
380         IRDA_ASSERT(obj != NULL, return;);
381         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
382
383         IRDA_ASSERT(name != NULL, return;);
384         IRDA_ASSERT(octets != NULL, return;);
385
386         attrib = kmalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
387         if (attrib == NULL) {
388                 IRDA_WARNING("%s: Unable to allocate attribute!\n",
389                              __FUNCTION__);
390                 return;
391         }
392         memset(attrib, 0, sizeof( struct ias_attrib));
393
394         attrib->magic = IAS_ATTRIB_MAGIC;
395         attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
396
397         attrib->value = irias_new_octseq_value( octets, len);
398
399         irias_add_attrib(obj, attrib, owner);
400 }
401 EXPORT_SYMBOL(irias_add_octseq_attrib);
402
403 /*
404  * Function irias_object_add_string_attrib (obj, string)
405  *
406  *    Add a string attribute to an LM-IAS object
407  *
408  */
409 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
410                              int owner)
411 {
412         struct ias_attrib *attrib;
413
414         IRDA_ASSERT(obj != NULL, return;);
415         IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
416
417         IRDA_ASSERT(name != NULL, return;);
418         IRDA_ASSERT(value != NULL, return;);
419
420         attrib = kmalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
421         if (attrib == NULL) {
422                 IRDA_WARNING("%s: Unable to allocate attribute!\n",
423                              __FUNCTION__);
424                 return;
425         }
426         memset(attrib, 0, sizeof( struct ias_attrib));
427
428         attrib->magic = IAS_ATTRIB_MAGIC;
429         attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
430
431         attrib->value = irias_new_string_value(value);
432
433         irias_add_attrib(obj, attrib, owner);
434 }
435 EXPORT_SYMBOL(irias_add_string_attrib);
436
437 /*
438  * Function irias_new_integer_value (integer)
439  *
440  *    Create new IAS integer value
441  *
442  */
443 struct ias_value *irias_new_integer_value(int integer)
444 {
445         struct ias_value *value;
446
447         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
448         if (value == NULL) {
449                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
450                 return NULL;
451         }
452         memset(value, 0, sizeof(struct ias_value));
453
454         value->type = IAS_INTEGER;
455         value->len = 4;
456         value->t.integer = integer;
457
458         return value;
459 }
460 EXPORT_SYMBOL(irias_new_integer_value);
461
462 /*
463  * Function irias_new_string_value (string)
464  *
465  *    Create new IAS string value
466  *
467  * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
468  */
469 struct ias_value *irias_new_string_value(char *string)
470 {
471         struct ias_value *value;
472
473         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
474         if (value == NULL) {
475                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
476                 return NULL;
477         }
478         memset( value, 0, sizeof( struct ias_value));
479
480         value->type = IAS_STRING;
481         value->charset = CS_ASCII;
482         value->t.string = strndup(string, IAS_MAX_STRING);
483         value->len = strlen(value->t.string);
484
485         return value;
486 }
487 EXPORT_SYMBOL(irias_new_string_value);
488
489 /*
490  * Function irias_new_octseq_value (octets, len)
491  *
492  *    Create new IAS octet-sequence value
493  *
494  * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
495  */
496 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
497 {
498         struct ias_value *value;
499
500         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
501         if (value == NULL) {
502                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
503                 return NULL;
504         }
505         memset(value, 0, sizeof(struct ias_value));
506
507         value->type = IAS_OCT_SEQ;
508         /* Check length */
509         if(len > IAS_MAX_OCTET_STRING)
510                 len = IAS_MAX_OCTET_STRING;
511         value->len = len;
512
513         value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
514         if (value->t.oct_seq == NULL){
515                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
516                 kfree(value);
517                 return NULL;
518         }
519         memcpy(value->t.oct_seq, octseq , len);
520         return value;
521 }
522 EXPORT_SYMBOL(irias_new_octseq_value);
523
524 struct ias_value *irias_new_missing_value(void)
525 {
526         struct ias_value *value;
527
528         value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
529         if (value == NULL) {
530                 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
531                 return NULL;
532         }
533         memset(value, 0, sizeof(struct ias_value));
534
535         value->type = IAS_MISSING;
536         value->len = 0;
537
538         return value;
539 }
540
541 /*
542  * Function irias_delete_value (value)
543  *
544  *    Delete IAS value
545  *
546  */
547 void irias_delete_value(struct ias_value *value)
548 {
549         IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
550
551         IRDA_ASSERT(value != NULL, return;);
552
553         switch (value->type) {
554         case IAS_INTEGER: /* Fallthrough */
555         case IAS_MISSING:
556                 /* No need to deallocate */
557                 break;
558         case IAS_STRING:
559                 /* Deallocate string */
560                 kfree(value->t.string);
561                 break;
562         case IAS_OCT_SEQ:
563                 /* Deallocate byte stream */
564                  kfree(value->t.oct_seq);
565                  break;
566         default:
567                 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
568                 break;
569         }
570         kfree(value);
571 }
572 EXPORT_SYMBOL(irias_delete_value);