irias_object.c 13 KB

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