irias_object.c 13 KB

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