irias_object.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/socket.h>
  27. #include <linux/module.h>
  28. #include <net/irda/irda.h>
  29. #include <net/irda/irias_object.h>
  30. hashbin_t *irias_objects;
  31. /*
  32. * Used when a missing value needs to be returned
  33. */
  34. struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}};
  35. /*
  36. * Function ias_new_object (name, id)
  37. *
  38. * Create a new IAS object
  39. *
  40. */
  41. struct ias_object *irias_new_object( char *name, int id)
  42. {
  43. struct ias_object *obj;
  44. IRDA_DEBUG( 4, "%s()\n", __func__);
  45. obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
  46. if (obj == NULL) {
  47. IRDA_WARNING("%s(), Unable to allocate object!\n",
  48. __func__);
  49. return NULL;
  50. }
  51. obj->magic = IAS_OBJECT_MAGIC;
  52. obj->name = kstrndup(name, IAS_MAX_CLASSNAME, GFP_ATOMIC);
  53. if (!obj->name) {
  54. IRDA_WARNING("%s(), Unable to allocate name!\n",
  55. __func__);
  56. kfree(obj);
  57. return NULL;
  58. }
  59. obj->id = id;
  60. /* Locking notes : the attrib spinlock has lower precendence
  61. * than the objects spinlock. Never grap the objects spinlock
  62. * while holding any attrib spinlock (risk of deadlock). Jean II */
  63. obj->attribs = hashbin_new(HB_LOCK);
  64. if (obj->attribs == NULL) {
  65. IRDA_WARNING("%s(), Unable to allocate attribs!\n",
  66. __func__);
  67. kfree(obj->name);
  68. kfree(obj);
  69. return NULL;
  70. }
  71. return obj;
  72. }
  73. EXPORT_SYMBOL(irias_new_object);
  74. /*
  75. * Function irias_delete_attrib (attrib)
  76. *
  77. * Delete given attribute and deallocate all its memory
  78. *
  79. */
  80. static void __irias_delete_attrib(struct ias_attrib *attrib)
  81. {
  82. IRDA_ASSERT(attrib != NULL, return;);
  83. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
  84. kfree(attrib->name);
  85. irias_delete_value(attrib->value);
  86. attrib->magic = ~IAS_ATTRIB_MAGIC;
  87. kfree(attrib);
  88. }
  89. void __irias_delete_object(struct ias_object *obj)
  90. {
  91. IRDA_ASSERT(obj != NULL, return;);
  92. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  93. kfree(obj->name);
  94. hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
  95. obj->magic = ~IAS_OBJECT_MAGIC;
  96. kfree(obj);
  97. }
  98. /*
  99. * Function irias_delete_object (obj)
  100. *
  101. * Remove object from hashbin and deallocate all attributes associated with
  102. * with this object and the object itself
  103. *
  104. */
  105. int irias_delete_object(struct ias_object *obj)
  106. {
  107. struct ias_object *node;
  108. IRDA_ASSERT(obj != NULL, return -1;);
  109. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
  110. /* Remove from list */
  111. node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
  112. if (!node)
  113. IRDA_DEBUG( 0, "%s(), object already removed!\n",
  114. __func__);
  115. /* Destroy */
  116. __irias_delete_object(obj);
  117. return 0;
  118. }
  119. EXPORT_SYMBOL(irias_delete_object);
  120. /*
  121. * Function irias_delete_attrib (obj)
  122. *
  123. * Remove attribute from hashbin and, if it was the last attribute of
  124. * the object, remove the object as well.
  125. *
  126. */
  127. int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
  128. int cleanobject)
  129. {
  130. struct ias_attrib *node;
  131. IRDA_ASSERT(obj != NULL, return -1;);
  132. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
  133. IRDA_ASSERT(attrib != NULL, return -1;);
  134. /* Remove attribute from object */
  135. node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
  136. if (!node)
  137. return 0; /* Already removed or non-existent */
  138. /* Deallocate attribute */
  139. __irias_delete_attrib(node);
  140. /* Check if object has still some attributes, destroy it if none.
  141. * At first glance, this look dangerous, as the kernel reference
  142. * various IAS objects. However, we only use this function on
  143. * user attributes, not kernel attributes, so there is no risk
  144. * of deleting a kernel object this way. Jean II */
  145. node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
  146. if (cleanobject && !node)
  147. irias_delete_object(obj);
  148. return 0;
  149. }
  150. /*
  151. * Function irias_insert_object (obj)
  152. *
  153. * Insert an object into the LM-IAS database
  154. *
  155. */
  156. void irias_insert_object(struct ias_object *obj)
  157. {
  158. IRDA_ASSERT(obj != NULL, return;);
  159. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  160. hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
  161. }
  162. EXPORT_SYMBOL(irias_insert_object);
  163. /*
  164. * Function irias_find_object (name)
  165. *
  166. * Find object with given name
  167. *
  168. */
  169. struct ias_object *irias_find_object(char *name)
  170. {
  171. IRDA_ASSERT(name != NULL, return NULL;);
  172. /* Unsafe (locking), object might change */
  173. return hashbin_lock_find(irias_objects, 0, name);
  174. }
  175. EXPORT_SYMBOL(irias_find_object);
  176. /*
  177. * Function irias_find_attrib (obj, name)
  178. *
  179. * Find named attribute in object
  180. *
  181. */
  182. struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
  183. {
  184. struct ias_attrib *attrib;
  185. IRDA_ASSERT(obj != NULL, return NULL;);
  186. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
  187. IRDA_ASSERT(name != NULL, return NULL;);
  188. attrib = hashbin_lock_find(obj->attribs, 0, name);
  189. if (attrib == NULL)
  190. return NULL;
  191. /* Unsafe (locking), attrib might change */
  192. return attrib;
  193. }
  194. /*
  195. * Function irias_add_attribute (obj, attrib)
  196. *
  197. * Add attribute to object
  198. *
  199. */
  200. static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
  201. int owner)
  202. {
  203. IRDA_ASSERT(obj != NULL, return;);
  204. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  205. IRDA_ASSERT(attrib != NULL, return;);
  206. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
  207. /* Set if attrib is owned by kernel or user space */
  208. attrib->value->owner = owner;
  209. hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
  210. }
  211. /*
  212. * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
  213. *
  214. * Change the value of an objects attribute.
  215. *
  216. */
  217. int irias_object_change_attribute(char *obj_name, char *attrib_name,
  218. struct ias_value *new_value)
  219. {
  220. struct ias_object *obj;
  221. struct ias_attrib *attrib;
  222. unsigned long flags;
  223. /* Find object */
  224. obj = hashbin_lock_find(irias_objects, 0, obj_name);
  225. if (obj == NULL) {
  226. IRDA_WARNING("%s: Unable to find object: %s\n", __func__,
  227. obj_name);
  228. return -1;
  229. }
  230. /* Slightly unsafe (obj might get removed under us) */
  231. spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
  232. /* Find attribute */
  233. attrib = hashbin_find(obj->attribs, 0, attrib_name);
  234. if (attrib == NULL) {
  235. IRDA_WARNING("%s: Unable to find attribute: %s\n",
  236. __func__, attrib_name);
  237. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  238. return -1;
  239. }
  240. if ( attrib->value->type != new_value->type) {
  241. IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
  242. __func__);
  243. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  244. return -1;
  245. }
  246. /* Delete old value */
  247. irias_delete_value(attrib->value);
  248. /* Insert new value */
  249. attrib->value = new_value;
  250. /* Success */
  251. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  252. return 0;
  253. }
  254. EXPORT_SYMBOL(irias_object_change_attribute);
  255. /*
  256. * Function irias_object_add_integer_attrib (obj, name, value)
  257. *
  258. * Add an integer attribute to an LM-IAS object
  259. *
  260. */
  261. void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
  262. int owner)
  263. {
  264. struct ias_attrib *attrib;
  265. IRDA_ASSERT(obj != NULL, return;);
  266. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  267. IRDA_ASSERT(name != NULL, return;);
  268. attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
  269. if (attrib == NULL) {
  270. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  271. __func__);
  272. return;
  273. }
  274. attrib->magic = IAS_ATTRIB_MAGIC;
  275. attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
  276. /* Insert value */
  277. attrib->value = irias_new_integer_value(value);
  278. if (!attrib->name || !attrib->value) {
  279. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  280. __func__);
  281. if (attrib->value)
  282. irias_delete_value(attrib->value);
  283. kfree(attrib->name);
  284. kfree(attrib);
  285. return;
  286. }
  287. irias_add_attrib(obj, attrib, owner);
  288. }
  289. EXPORT_SYMBOL(irias_add_integer_attrib);
  290. /*
  291. * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
  292. *
  293. * Add a octet sequence attribute to an LM-IAS object
  294. *
  295. */
  296. void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
  297. int len, int owner)
  298. {
  299. struct ias_attrib *attrib;
  300. IRDA_ASSERT(obj != NULL, return;);
  301. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  302. IRDA_ASSERT(name != NULL, return;);
  303. IRDA_ASSERT(octets != NULL, return;);
  304. attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
  305. if (attrib == NULL) {
  306. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  307. __func__);
  308. return;
  309. }
  310. attrib->magic = IAS_ATTRIB_MAGIC;
  311. attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
  312. attrib->value = irias_new_octseq_value( octets, len);
  313. if (!attrib->name || !attrib->value) {
  314. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  315. __func__);
  316. if (attrib->value)
  317. irias_delete_value(attrib->value);
  318. kfree(attrib->name);
  319. kfree(attrib);
  320. return;
  321. }
  322. irias_add_attrib(obj, attrib, owner);
  323. }
  324. EXPORT_SYMBOL(irias_add_octseq_attrib);
  325. /*
  326. * Function irias_object_add_string_attrib (obj, string)
  327. *
  328. * Add a string attribute to an LM-IAS object
  329. *
  330. */
  331. void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
  332. int owner)
  333. {
  334. struct ias_attrib *attrib;
  335. IRDA_ASSERT(obj != NULL, return;);
  336. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  337. IRDA_ASSERT(name != NULL, return;);
  338. IRDA_ASSERT(value != NULL, return;);
  339. attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
  340. if (attrib == NULL) {
  341. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  342. __func__);
  343. return;
  344. }
  345. attrib->magic = IAS_ATTRIB_MAGIC;
  346. attrib->name = kstrndup(name, IAS_MAX_ATTRIBNAME, GFP_ATOMIC);
  347. attrib->value = irias_new_string_value(value);
  348. if (!attrib->name || !attrib->value) {
  349. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  350. __func__);
  351. if (attrib->value)
  352. irias_delete_value(attrib->value);
  353. kfree(attrib->name);
  354. kfree(attrib);
  355. return;
  356. }
  357. irias_add_attrib(obj, attrib, owner);
  358. }
  359. EXPORT_SYMBOL(irias_add_string_attrib);
  360. /*
  361. * Function irias_new_integer_value (integer)
  362. *
  363. * Create new IAS integer value
  364. *
  365. */
  366. struct ias_value *irias_new_integer_value(int integer)
  367. {
  368. struct ias_value *value;
  369. value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
  370. if (value == NULL) {
  371. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  372. return NULL;
  373. }
  374. value->type = IAS_INTEGER;
  375. value->len = 4;
  376. value->t.integer = integer;
  377. return value;
  378. }
  379. EXPORT_SYMBOL(irias_new_integer_value);
  380. /*
  381. * Function irias_new_string_value (string)
  382. *
  383. * Create new IAS string value
  384. *
  385. * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
  386. */
  387. struct ias_value *irias_new_string_value(char *string)
  388. {
  389. struct ias_value *value;
  390. value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
  391. if (value == NULL) {
  392. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  393. return NULL;
  394. }
  395. value->type = IAS_STRING;
  396. value->charset = CS_ASCII;
  397. value->t.string = kstrndup(string, IAS_MAX_STRING, GFP_ATOMIC);
  398. if (!value->t.string) {
  399. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  400. kfree(value);
  401. return NULL;
  402. }
  403. value->len = strlen(value->t.string);
  404. return value;
  405. }
  406. /*
  407. * Function irias_new_octseq_value (octets, len)
  408. *
  409. * Create new IAS octet-sequence value
  410. *
  411. * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
  412. */
  413. struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
  414. {
  415. struct ias_value *value;
  416. value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
  417. if (value == NULL) {
  418. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  419. return NULL;
  420. }
  421. value->type = IAS_OCT_SEQ;
  422. /* Check length */
  423. if(len > IAS_MAX_OCTET_STRING)
  424. len = IAS_MAX_OCTET_STRING;
  425. value->len = len;
  426. value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
  427. if (value->t.oct_seq == NULL){
  428. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  429. kfree(value);
  430. return NULL;
  431. }
  432. return value;
  433. }
  434. struct ias_value *irias_new_missing_value(void)
  435. {
  436. struct ias_value *value;
  437. value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
  438. if (value == NULL) {
  439. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  440. return NULL;
  441. }
  442. value->type = IAS_MISSING;
  443. return value;
  444. }
  445. /*
  446. * Function irias_delete_value (value)
  447. *
  448. * Delete IAS value
  449. *
  450. */
  451. void irias_delete_value(struct ias_value *value)
  452. {
  453. IRDA_DEBUG(4, "%s()\n", __func__);
  454. IRDA_ASSERT(value != NULL, return;);
  455. switch (value->type) {
  456. case IAS_INTEGER: /* Fallthrough */
  457. case IAS_MISSING:
  458. /* No need to deallocate */
  459. break;
  460. case IAS_STRING:
  461. /* Deallocate string */
  462. kfree(value->t.string);
  463. break;
  464. case IAS_OCT_SEQ:
  465. /* Deallocate byte stream */
  466. kfree(value->t.oct_seq);
  467. break;
  468. default:
  469. IRDA_DEBUG(0, "%s(), Unknown value type!\n", __func__);
  470. break;
  471. }
  472. kfree(value);
  473. }
  474. EXPORT_SYMBOL(irias_delete_value);