1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <linux/slab.h>
26#include <linux/string.h>
27#include <linux/socket.h>
28#include <linux/module.h>
29
30#include <net/irda/irda.h>
31#include <net/irda/irias_object.h>
32
33hashbin_t *irias_objects;
34
35
36
37
38struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}};
39
40
41
42
43
44
45
46
47struct ias_object *irias_new_object( char *name, int id)
48{
49 struct ias_object *obj;
50
51 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
52 if (obj == NULL) {
53 net_warn_ratelimited("%s(), Unable to allocate object!\n",
54 __func__);
55 return NULL;
56 }
57
58 obj->magic = IAS_OBJECT_MAGIC;
59 obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC);
60 if (!obj->name) {
61 net_warn_ratelimited("%s(), Unable to allocate name!\n",
62 __func__);
63 kfree(obj);
64 return NULL;
65 }
66 obj->id = id;
67
68
69
70
71 obj->attribs = hashbin_new(HB_LOCK);
72
73 if (obj->attribs == NULL) {
74 net_warn_ratelimited("%s(), Unable to allocate attribs!\n",
75 __func__);
76 kfree(obj->name);
77 kfree(obj);
78 return NULL;
79 }
80
81 return obj;
82}
83EXPORT_SYMBOL(irias_new_object);
84
85
86
87
88
89
90
91static void __irias_delete_attrib(struct ias_attrib *attrib)
92{
93 IRDA_ASSERT(attrib != NULL, return;);
94 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
95
96 kfree(attrib->name);
97
98 irias_delete_value(attrib->value);
99 attrib->magic = ~IAS_ATTRIB_MAGIC;
100
101 kfree(attrib);
102}
103
104void __irias_delete_object(struct ias_object *obj)
105{
106 IRDA_ASSERT(obj != NULL, return;);
107 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
108
109 kfree(obj->name);
110
111 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
112
113 obj->magic = ~IAS_OBJECT_MAGIC;
114
115 kfree(obj);
116}
117
118
119
120
121
122
123
124
125int irias_delete_object(struct ias_object *obj)
126{
127 struct ias_object *node;
128
129 IRDA_ASSERT(obj != NULL, return -1;);
130 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
131
132
133 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
134 if (!node)
135 pr_debug("%s(), object already removed!\n",
136 __func__);
137
138
139 __irias_delete_object(obj);
140
141 return 0;
142}
143EXPORT_SYMBOL(irias_delete_object);
144
145
146
147
148
149
150
151
152int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
153 int cleanobject)
154{
155 struct ias_attrib *node;
156
157 IRDA_ASSERT(obj != NULL, return -1;);
158 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
159 IRDA_ASSERT(attrib != NULL, return -1;);
160
161
162 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
163 if (!node)
164 return 0;
165
166
167 __irias_delete_attrib(node);
168
169
170
171
172
173
174 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
175 if (cleanobject && !node)
176 irias_delete_object(obj);
177
178 return 0;
179}
180
181
182
183
184
185
186
187void irias_insert_object(struct ias_object *obj)
188{
189 IRDA_ASSERT(obj != NULL, return;);
190 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
191
192 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
193}
194EXPORT_SYMBOL(irias_insert_object);
195
196
197
198
199
200
201
202struct ias_object *irias_find_object(char *name)
203{
204 IRDA_ASSERT(name != NULL, return NULL;);
205
206
207 return hashbin_lock_find(irias_objects, 0, name);
208}
209EXPORT_SYMBOL(irias_find_object);
210
211
212
213
214
215
216
217struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
218{
219 struct ias_attrib *attrib;
220
221 IRDA_ASSERT(obj != NULL, return NULL;);
222 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
223 IRDA_ASSERT(name != NULL, return NULL;);
224
225 attrib = hashbin_lock_find(obj->attribs, 0, name);
226 if (attrib == NULL)
227 return NULL;
228
229
230 return attrib;
231}
232
233
234
235
236
237
238
239static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
240 int owner)
241{
242 IRDA_ASSERT(obj != NULL, return;);
243 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
244
245 IRDA_ASSERT(attrib != NULL, return;);
246 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
247
248
249 attrib->value->owner = owner;
250
251 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
252}
253
254
255
256
257
258
259
260int irias_object_change_attribute(char *obj_name, char *attrib_name,
261 struct ias_value *new_value)
262{
263 struct ias_object *obj;
264 struct ias_attrib *attrib;
265 unsigned long flags;
266
267
268 obj = hashbin_lock_find(irias_objects, 0, obj_name);
269 if (obj == NULL) {
270 net_warn_ratelimited("%s: Unable to find object: %s\n",
271 __func__, obj_name);
272 return -1;
273 }
274
275
276 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
277
278
279 attrib = hashbin_find(obj->attribs, 0, attrib_name);
280 if (attrib == NULL) {
281 net_warn_ratelimited("%s: Unable to find attribute: %s\n",
282 __func__, attrib_name);
283 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
284 return -1;
285 }
286
287 if ( attrib->value->type != new_value->type) {
288 pr_debug("%s(), changing value type not allowed!\n",
289 __func__);
290 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
291 return -1;
292 }
293
294
295 irias_delete_value(attrib->value);
296
297
298 attrib->value = new_value;
299
300
301 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
302 return 0;
303}
304EXPORT_SYMBOL(irias_object_change_attribute);
305
306
307
308
309
310
311
312void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
313 int owner)
314{
315 struct ias_attrib *attrib;
316
317 IRDA_ASSERT(obj != NULL, return;);
318 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
319 IRDA_ASSERT(name != NULL, return;);
320
321 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
322 if (attrib == NULL) {
323 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
324 __func__);
325 return;
326 }
327
328 attrib->magic = IAS_ATTRIB_MAGIC;
329 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
330
331
332 attrib->value = irias_new_integer_value(value);
333 if (!attrib->name || !attrib->value) {
334 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
335 __func__);
336 if (attrib->value)
337 irias_delete_value(attrib->value);
338 kfree(attrib->name);
339 kfree(attrib);
340 return;
341 }
342
343 irias_add_attrib(obj, attrib, owner);
344}
345EXPORT_SYMBOL(irias_add_integer_attrib);
346
347
348
349
350
351
352
353
354void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
355 int len, int owner)
356{
357 struct ias_attrib *attrib;
358
359 IRDA_ASSERT(obj != NULL, return;);
360 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
361
362 IRDA_ASSERT(name != NULL, return;);
363 IRDA_ASSERT(octets != NULL, return;);
364
365 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
366 if (attrib == NULL) {
367 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
368 __func__);
369 return;
370 }
371
372 attrib->magic = IAS_ATTRIB_MAGIC;
373 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
374
375 attrib->value = irias_new_octseq_value( octets, len);
376 if (!attrib->name || !attrib->value) {
377 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
378 __func__);
379 if (attrib->value)
380 irias_delete_value(attrib->value);
381 kfree(attrib->name);
382 kfree(attrib);
383 return;
384 }
385
386 irias_add_attrib(obj, attrib, owner);
387}
388EXPORT_SYMBOL(irias_add_octseq_attrib);
389
390
391
392
393
394
395
396void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
397 int owner)
398{
399 struct ias_attrib *attrib;
400
401 IRDA_ASSERT(obj != NULL, return;);
402 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
403
404 IRDA_ASSERT(name != NULL, return;);
405 IRDA_ASSERT(value != NULL, return;);
406
407 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
408 if (attrib == NULL) {
409 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
410 __func__);
411 return;
412 }
413
414 attrib->magic = IAS_ATTRIB_MAGIC;
415 attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
416
417 attrib->value = irias_new_string_value(value);
418 if (!attrib->name || !attrib->value) {
419 net_warn_ratelimited("%s: Unable to allocate attribute!\n",
420 __func__);
421 if (attrib->value)
422 irias_delete_value(attrib->value);
423 kfree(attrib->name);
424 kfree(attrib);
425 return;
426 }
427
428 irias_add_attrib(obj, attrib, owner);
429}
430EXPORT_SYMBOL(irias_add_string_attrib);
431
432
433
434
435
436
437
438struct ias_value *irias_new_integer_value(int integer)
439{
440 struct ias_value *value;
441
442 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
443 if (value == NULL)
444 return NULL;
445
446 value->type = IAS_INTEGER;
447 value->len = 4;
448 value->t.integer = integer;
449
450 return value;
451}
452EXPORT_SYMBOL(irias_new_integer_value);
453
454
455
456
457
458
459
460
461struct ias_value *irias_new_string_value(char *string)
462{
463 struct ias_value *value;
464
465 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
466 if (value == NULL)
467 return NULL;
468
469 value->type = IAS_STRING;
470 value->charset = CS_ASCII;
471 value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC);
472 if (!value->t.string) {
473 net_warn_ratelimited("%s: Unable to kmalloc!\n", __func__);
474 kfree(value);
475 return NULL;
476 }
477
478 value->len = strlen(value->t.string);
479
480 return value;
481}
482
483
484
485
486
487
488
489
490struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
491{
492 struct ias_value *value;
493
494 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
495 if (value == NULL)
496 return NULL;
497
498 value->type = IAS_OCT_SEQ;
499
500 if(len > IAS_MAX_OCTET_STRING)
501 len = IAS_MAX_OCTET_STRING;
502 value->len = len;
503
504 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
505 if (value->t.oct_seq == NULL){
506 net_warn_ratelimited("%s: Unable to kmalloc!\n", __func__);
507 kfree(value);
508 return NULL;
509 }
510 return value;
511}
512
513struct ias_value *irias_new_missing_value(void)
514{
515 struct ias_value *value;
516
517 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
518 if (value == NULL)
519 return NULL;
520
521 value->type = IAS_MISSING;
522
523 return value;
524}
525
526
527
528
529
530
531
532void irias_delete_value(struct ias_value *value)
533{
534 IRDA_ASSERT(value != NULL, return;);
535
536 switch (value->type) {
537 case IAS_INTEGER:
538 case IAS_MISSING:
539
540 break;
541 case IAS_STRING:
542
543 kfree(value->t.string);
544 break;
545 case IAS_OCT_SEQ:
546
547 kfree(value->t.oct_seq);
548 break;
549 default:
550 pr_debug("%s(), Unknown value type!\n", __func__);
551 break;
552 }
553 kfree(value);
554}
555EXPORT_SYMBOL(irias_delete_value);
556