irias_object.c 14 KB

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