irias_object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. EXPORT_SYMBOL(irias_find_attrib);
  217. /*
  218. * Function irias_add_attribute (obj, attrib)
  219. *
  220. * Add attribute to object
  221. *
  222. */
  223. static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
  224. int owner)
  225. {
  226. IRDA_ASSERT(obj != NULL, return;);
  227. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  228. IRDA_ASSERT(attrib != NULL, return;);
  229. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
  230. /* Set if attrib is owned by kernel or user space */
  231. attrib->value->owner = owner;
  232. hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
  233. }
  234. /*
  235. * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
  236. *
  237. * Change the value of an objects attribute.
  238. *
  239. */
  240. int irias_object_change_attribute(char *obj_name, char *attrib_name,
  241. struct ias_value *new_value)
  242. {
  243. struct ias_object *obj;
  244. struct ias_attrib *attrib;
  245. unsigned long flags;
  246. /* Find object */
  247. obj = hashbin_lock_find(irias_objects, 0, obj_name);
  248. if (obj == NULL) {
  249. IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
  250. obj_name);
  251. return -1;
  252. }
  253. /* Slightly unsafe (obj might get removed under us) */
  254. spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
  255. /* Find attribute */
  256. attrib = hashbin_find(obj->attribs, 0, attrib_name);
  257. if (attrib == NULL) {
  258. IRDA_WARNING("%s: Unable to find attribute: %s\n",
  259. __FUNCTION__, attrib_name);
  260. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  261. return -1;
  262. }
  263. if ( attrib->value->type != new_value->type) {
  264. IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
  265. __FUNCTION__);
  266. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  267. return -1;
  268. }
  269. /* Delete old value */
  270. irias_delete_value(attrib->value);
  271. /* Insert new value */
  272. attrib->value = new_value;
  273. /* Success */
  274. spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
  275. return 0;
  276. }
  277. EXPORT_SYMBOL(irias_object_change_attribute);
  278. /*
  279. * Function irias_object_add_integer_attrib (obj, name, value)
  280. *
  281. * Add an integer attribute to an LM-IAS object
  282. *
  283. */
  284. void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
  285. int owner)
  286. {
  287. struct ias_attrib *attrib;
  288. IRDA_ASSERT(obj != NULL, return;);
  289. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  290. IRDA_ASSERT(name != NULL, return;);
  291. attrib = kmalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
  292. if (attrib == NULL) {
  293. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  294. __FUNCTION__);
  295. return;
  296. }
  297. memset(attrib, 0, sizeof( struct ias_attrib));
  298. attrib->magic = IAS_ATTRIB_MAGIC;
  299. attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
  300. /* Insert value */
  301. attrib->value = irias_new_integer_value(value);
  302. irias_add_attrib(obj, attrib, owner);
  303. }
  304. EXPORT_SYMBOL(irias_add_integer_attrib);
  305. /*
  306. * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
  307. *
  308. * Add a octet sequence attribute to an LM-IAS object
  309. *
  310. */
  311. void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
  312. int len, int owner)
  313. {
  314. struct ias_attrib *attrib;
  315. IRDA_ASSERT(obj != NULL, return;);
  316. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  317. IRDA_ASSERT(name != NULL, return;);
  318. IRDA_ASSERT(octets != NULL, return;);
  319. attrib = kmalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
  320. if (attrib == NULL) {
  321. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  322. __FUNCTION__);
  323. return;
  324. }
  325. memset(attrib, 0, sizeof( struct ias_attrib));
  326. attrib->magic = IAS_ATTRIB_MAGIC;
  327. attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
  328. attrib->value = irias_new_octseq_value( octets, len);
  329. irias_add_attrib(obj, attrib, owner);
  330. }
  331. EXPORT_SYMBOL(irias_add_octseq_attrib);
  332. /*
  333. * Function irias_object_add_string_attrib (obj, string)
  334. *
  335. * Add a string attribute to an LM-IAS object
  336. *
  337. */
  338. void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
  339. int owner)
  340. {
  341. struct ias_attrib *attrib;
  342. IRDA_ASSERT(obj != NULL, return;);
  343. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
  344. IRDA_ASSERT(name != NULL, return;);
  345. IRDA_ASSERT(value != NULL, return;);
  346. attrib = kmalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
  347. if (attrib == NULL) {
  348. IRDA_WARNING("%s: Unable to allocate attribute!\n",
  349. __FUNCTION__);
  350. return;
  351. }
  352. memset(attrib, 0, sizeof( struct ias_attrib));
  353. attrib->magic = IAS_ATTRIB_MAGIC;
  354. attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
  355. attrib->value = irias_new_string_value(value);
  356. irias_add_attrib(obj, attrib, owner);
  357. }
  358. EXPORT_SYMBOL(irias_add_string_attrib);
  359. /*
  360. * Function irias_new_integer_value (integer)
  361. *
  362. * Create new IAS integer value
  363. *
  364. */
  365. struct ias_value *irias_new_integer_value(int integer)
  366. {
  367. struct ias_value *value;
  368. value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
  369. if (value == NULL) {
  370. IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
  371. return NULL;
  372. }
  373. memset(value, 0, sizeof(struct ias_value));
  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 = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
  391. if (value == NULL) {
  392. IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
  393. return NULL;
  394. }
  395. memset( value, 0, sizeof( struct ias_value));
  396. value->type = IAS_STRING;
  397. value->charset = CS_ASCII;
  398. value->t.string = strndup(string, IAS_MAX_STRING);
  399. value->len = strlen(value->t.string);
  400. return value;
  401. }
  402. EXPORT_SYMBOL(irias_new_string_value);
  403. /*
  404. * Function irias_new_octseq_value (octets, len)
  405. *
  406. * Create new IAS octet-sequence value
  407. *
  408. * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
  409. */
  410. struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
  411. {
  412. struct ias_value *value;
  413. value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
  414. if (value == NULL) {
  415. IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
  416. return NULL;
  417. }
  418. memset(value, 0, sizeof(struct ias_value));
  419. value->type = IAS_OCT_SEQ;
  420. /* Check length */
  421. if(len > IAS_MAX_OCTET_STRING)
  422. len = IAS_MAX_OCTET_STRING;
  423. value->len = len;
  424. value->t.oct_seq = kmalloc(len, GFP_ATOMIC);
  425. if (value->t.oct_seq == NULL){
  426. IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
  427. kfree(value);
  428. return NULL;
  429. }
  430. memcpy(value->t.oct_seq, octseq , len);
  431. return value;
  432. }
  433. EXPORT_SYMBOL(irias_new_octseq_value);
  434. struct ias_value *irias_new_missing_value(void)
  435. {
  436. struct ias_value *value;
  437. value = kmalloc(sizeof(struct ias_value), GFP_ATOMIC);
  438. if (value == NULL) {
  439. IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
  440. return NULL;
  441. }
  442. memset(value, 0, sizeof(struct ias_value));
  443. value->type = IAS_MISSING;
  444. value->len = 0;
  445. return value;
  446. }
  447. /*
  448. * Function irias_delete_value (value)
  449. *
  450. * Delete IAS value
  451. *
  452. */
  453. void irias_delete_value(struct ias_value *value)
  454. {
  455. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  456. IRDA_ASSERT(value != NULL, return;);
  457. switch (value->type) {
  458. case IAS_INTEGER: /* Fallthrough */
  459. case IAS_MISSING:
  460. /* No need to deallocate */
  461. break;
  462. case IAS_STRING:
  463. /* Deallocate string */
  464. kfree(value->t.string);
  465. break;
  466. case IAS_OCT_SEQ:
  467. /* Deallocate byte stream */
  468. kfree(value->t.oct_seq);
  469. break;
  470. default:
  471. IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
  472. break;
  473. }
  474. kfree(value);
  475. }
  476. EXPORT_SYMBOL(irias_delete_value);