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