irias_object.c 14 KB

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