irias_object.c 14 KB

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