blob: a154b1d71c0f8d9af8e0f0c429886c5cdbe8b44c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
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
32hashbin_t *irias_objects;
33
34/*
35 * Used when a missing value needs to be returned
36 */
37struct 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 */
46static 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 */
79struct ias_object *irias_new_object( char *name, int id)
80{
81 struct ias_object *obj;
82
83 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
84
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070085 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 if (obj == NULL) {
87 IRDA_WARNING("%s(), Unable to allocate object!\n",
88 __FUNCTION__);
89 return NULL;
90 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 obj->magic = IAS_OBJECT_MAGIC;
93 obj->name = strndup(name, IAS_MAX_CLASSNAME);
94 obj->id = id;
95
96 /* Locking notes : the attrib spinlock has lower precendence
97 * than the objects spinlock. Never grap the objects spinlock
98 * while holding any attrib spinlock (risk of deadlock). Jean II */
99 obj->attribs = hashbin_new(HB_LOCK);
100
101 if (obj->attribs == NULL) {
102 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
103 __FUNCTION__);
104 kfree(obj);
105 return NULL;
106 }
107
108 return obj;
109}
110EXPORT_SYMBOL(irias_new_object);
111
112/*
113 * Function irias_delete_attrib (attrib)
114 *
115 * Delete given attribute and deallocate all its memory
116 *
117 */
118static void __irias_delete_attrib(struct ias_attrib *attrib)
119{
120 IRDA_ASSERT(attrib != NULL, return;);
121 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
122
Jesper Juhla51482b2005-11-08 09:41:34 -0800123 kfree(attrib->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 irias_delete_value(attrib->value);
126 attrib->magic = ~IAS_ATTRIB_MAGIC;
127
128 kfree(attrib);
129}
130
131void __irias_delete_object(struct ias_object *obj)
132{
133 IRDA_ASSERT(obj != NULL, return;);
134 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
135
Jesper Juhla51482b2005-11-08 09:41:34 -0800136 kfree(obj->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
139
140 obj->magic = ~IAS_OBJECT_MAGIC;
141
142 kfree(obj);
143}
144
145/*
146 * Function irias_delete_object (obj)
147 *
148 * Remove object from hashbin and deallocate all attributes associated with
149 * with this object and the object itself
150 *
151 */
152int irias_delete_object(struct ias_object *obj)
153{
154 struct ias_object *node;
155
156 IRDA_ASSERT(obj != NULL, return -1;);
157 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
158
159 /* Remove from list */
160 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
161 if (!node)
162 IRDA_DEBUG( 0, "%s(), object already removed!\n",
163 __FUNCTION__);
164
165 /* Destroy */
166 __irias_delete_object(obj);
167
168 return 0;
169}
170EXPORT_SYMBOL(irias_delete_object);
171
172/*
173 * Function irias_delete_attrib (obj)
174 *
175 * Remove attribute from hashbin and, if it was the last attribute of
176 * the object, remove the object as well.
177 *
178 */
179int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
180 int cleanobject)
181{
182 struct ias_attrib *node;
183
184 IRDA_ASSERT(obj != NULL, return -1;);
185 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
186 IRDA_ASSERT(attrib != NULL, return -1;);
187
188 /* Remove attribute from object */
189 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
190 if (!node)
191 return 0; /* Already removed or non-existent */
192
193 /* Deallocate attribute */
194 __irias_delete_attrib(node);
195
196 /* Check if object has still some attributes, destroy it if none.
197 * At first glance, this look dangerous, as the kernel reference
198 * various IAS objects. However, we only use this function on
199 * user attributes, not kernel attributes, so there is no risk
200 * of deleting a kernel object this way. Jean II */
201 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
202 if (cleanobject && !node)
203 irias_delete_object(obj);
204
205 return 0;
206}
207
208/*
209 * Function irias_insert_object (obj)
210 *
211 * Insert an object into the LM-IAS database
212 *
213 */
214void irias_insert_object(struct ias_object *obj)
215{
216 IRDA_ASSERT(obj != NULL, return;);
217 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
218
219 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
220}
221EXPORT_SYMBOL(irias_insert_object);
222
223/*
224 * Function irias_find_object (name)
225 *
226 * Find object with given name
227 *
228 */
229struct ias_object *irias_find_object(char *name)
230{
231 IRDA_ASSERT(name != NULL, return NULL;);
232
233 /* Unsafe (locking), object might change */
234 return hashbin_lock_find(irias_objects, 0, name);
235}
236EXPORT_SYMBOL(irias_find_object);
237
238/*
239 * Function irias_find_attrib (obj, name)
240 *
241 * Find named attribute in object
242 *
243 */
244struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
245{
246 struct ias_attrib *attrib;
247
248 IRDA_ASSERT(obj != NULL, return NULL;);
249 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
250 IRDA_ASSERT(name != NULL, return NULL;);
251
252 attrib = hashbin_lock_find(obj->attribs, 0, name);
253 if (attrib == NULL)
254 return NULL;
255
256 /* Unsafe (locking), attrib might change */
257 return attrib;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/*
261 * Function irias_add_attribute (obj, attrib)
262 *
263 * Add attribute to object
264 *
265 */
266static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
267 int owner)
268{
269 IRDA_ASSERT(obj != NULL, return;);
270 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
271
272 IRDA_ASSERT(attrib != NULL, return;);
273 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
274
275 /* Set if attrib is owned by kernel or user space */
276 attrib->value->owner = owner;
277
278 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
279}
280
281/*
282 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
283 *
284 * Change the value of an objects attribute.
285 *
286 */
287int irias_object_change_attribute(char *obj_name, char *attrib_name,
288 struct ias_value *new_value)
289{
290 struct ias_object *obj;
291 struct ias_attrib *attrib;
292 unsigned long flags;
293
294 /* Find object */
295 obj = hashbin_lock_find(irias_objects, 0, obj_name);
296 if (obj == NULL) {
297 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
298 obj_name);
299 return -1;
300 }
301
302 /* Slightly unsafe (obj might get removed under us) */
303 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
304
305 /* Find attribute */
306 attrib = hashbin_find(obj->attribs, 0, attrib_name);
307 if (attrib == NULL) {
308 IRDA_WARNING("%s: Unable to find attribute: %s\n",
309 __FUNCTION__, attrib_name);
310 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
311 return -1;
312 }
313
314 if ( attrib->value->type != new_value->type) {
315 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
316 __FUNCTION__);
317 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
318 return -1;
319 }
320
321 /* Delete old value */
322 irias_delete_value(attrib->value);
323
324 /* Insert new value */
325 attrib->value = new_value;
326
327 /* Success */
328 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
329 return 0;
330}
331EXPORT_SYMBOL(irias_object_change_attribute);
332
333/*
334 * Function irias_object_add_integer_attrib (obj, name, value)
335 *
336 * Add an integer attribute to an LM-IAS object
337 *
338 */
339void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
340 int owner)
341{
342 struct ias_attrib *attrib;
343
344 IRDA_ASSERT(obj != NULL, return;);
345 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
346 IRDA_ASSERT(name != NULL, return;);
347
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700348 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (attrib == NULL) {
350 IRDA_WARNING("%s: Unable to allocate attribute!\n",
351 __FUNCTION__);
352 return;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 attrib->magic = IAS_ATTRIB_MAGIC;
356 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
357
358 /* Insert value */
359 attrib->value = irias_new_integer_value(value);
360
361 irias_add_attrib(obj, attrib, owner);
362}
363EXPORT_SYMBOL(irias_add_integer_attrib);
364
365 /*
366 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
367 *
368 * Add a octet sequence attribute to an LM-IAS object
369 *
370 */
371
372void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
373 int len, int owner)
374{
375 struct ias_attrib *attrib;
376
377 IRDA_ASSERT(obj != NULL, return;);
378 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
379
380 IRDA_ASSERT(name != NULL, return;);
381 IRDA_ASSERT(octets != NULL, return;);
382
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700383 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (attrib == NULL) {
385 IRDA_WARNING("%s: Unable to allocate attribute!\n",
386 __FUNCTION__);
387 return;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 attrib->magic = IAS_ATTRIB_MAGIC;
391 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
392
393 attrib->value = irias_new_octseq_value( octets, len);
394
395 irias_add_attrib(obj, attrib, owner);
396}
397EXPORT_SYMBOL(irias_add_octseq_attrib);
398
399/*
400 * Function irias_object_add_string_attrib (obj, string)
401 *
402 * Add a string attribute to an LM-IAS object
403 *
404 */
405void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
406 int owner)
407{
408 struct ias_attrib *attrib;
409
410 IRDA_ASSERT(obj != NULL, return;);
411 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
412
413 IRDA_ASSERT(name != NULL, return;);
414 IRDA_ASSERT(value != NULL, return;);
415
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700416 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (attrib == NULL) {
418 IRDA_WARNING("%s: Unable to allocate attribute!\n",
419 __FUNCTION__);
420 return;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 attrib->magic = IAS_ATTRIB_MAGIC;
424 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
425
426 attrib->value = irias_new_string_value(value);
427
428 irias_add_attrib(obj, attrib, owner);
429}
430EXPORT_SYMBOL(irias_add_string_attrib);
431
432/*
433 * Function irias_new_integer_value (integer)
434 *
435 * Create new IAS integer value
436 *
437 */
438struct ias_value *irias_new_integer_value(int integer)
439{
440 struct ias_value *value;
441
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700442 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (value == NULL) {
444 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
445 return NULL;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 value->type = IAS_INTEGER;
449 value->len = 4;
450 value->t.integer = integer;
451
452 return value;
453}
454EXPORT_SYMBOL(irias_new_integer_value);
455
456/*
457 * Function irias_new_string_value (string)
458 *
459 * Create new IAS string value
460 *
461 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
462 */
463struct ias_value *irias_new_string_value(char *string)
464{
465 struct ias_value *value;
466
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700467 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (value == NULL) {
469 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
470 return NULL;
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 value->type = IAS_STRING;
474 value->charset = CS_ASCII;
475 value->t.string = strndup(string, IAS_MAX_STRING);
476 value->len = strlen(value->t.string);
477
478 return value;
479}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/*
482 * Function irias_new_octseq_value (octets, len)
483 *
484 * Create new IAS octet-sequence value
485 *
486 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
487 */
488struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
489{
490 struct ias_value *value;
491
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700492 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (value == NULL) {
494 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
495 return NULL;
496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 value->type = IAS_OCT_SEQ;
499 /* Check length */
500 if(len > IAS_MAX_OCTET_STRING)
501 len = IAS_MAX_OCTET_STRING;
502 value->len = len;
503
504 value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
505 if (value->t.oct_seq == NULL){
506 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
507 kfree(value);
508 return NULL;
509 }
510 memcpy(value->t.oct_seq, octseq , len);
511 return value;
512}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514struct ias_value *irias_new_missing_value(void)
515{
516 struct ias_value *value;
517
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700518 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (value == NULL) {
520 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
521 return NULL;
522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 value->type = IAS_MISSING;
525 value->len = 0;
526
527 return value;
528}
529
530/*
531 * Function irias_delete_value (value)
532 *
533 * Delete IAS value
534 *
535 */
536void irias_delete_value(struct ias_value *value)
537{
538 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
539
540 IRDA_ASSERT(value != NULL, return;);
541
542 switch (value->type) {
543 case IAS_INTEGER: /* Fallthrough */
544 case IAS_MISSING:
545 /* No need to deallocate */
546 break;
547 case IAS_STRING:
Jesper Juhla51482b2005-11-08 09:41:34 -0800548 /* Deallocate string */
549 kfree(value->t.string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 case IAS_OCT_SEQ:
Jesper Juhla51482b2005-11-08 09:41:34 -0800552 /* Deallocate byte stream */
553 kfree(value->t.oct_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555 default:
556 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
557 break;
558 }
559 kfree(value);
560}
561EXPORT_SYMBOL(irias_delete_value);