irias_object.c 13 KB

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