iriap.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*********************************************************************
  2. *
  3. * Filename: iriap.c
  4. * Version: 0.8
  5. * Description: Information Access Protocol (IAP)
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Thu Aug 21 00:02:07 1997
  9. * Modified at: Sat Dec 25 16:42:42 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
  13. * All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * Neither Dag Brattli nor University of Tromsø admit liability nor
  22. * provide warranty for any of this software. This material is
  23. * provided "AS-IS" and at no charge.
  24. *
  25. ********************************************************************/
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/fs.h>
  30. #include <linux/string.h>
  31. #include <linux/init.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/slab.h>
  34. #include <asm/byteorder.h>
  35. #include <asm/unaligned.h>
  36. #include <net/irda/irda.h>
  37. #include <net/irda/irttp.h>
  38. #include <net/irda/irlmp.h>
  39. #include <net/irda/irias_object.h>
  40. #include <net/irda/iriap_event.h>
  41. #include <net/irda/iriap.h>
  42. #ifdef CONFIG_IRDA_DEBUG
  43. /* FIXME: This one should go in irlmp.c */
  44. static const char *const ias_charset_types[] = {
  45. "CS_ASCII",
  46. "CS_ISO_8859_1",
  47. "CS_ISO_8859_2",
  48. "CS_ISO_8859_3",
  49. "CS_ISO_8859_4",
  50. "CS_ISO_8859_5",
  51. "CS_ISO_8859_6",
  52. "CS_ISO_8859_7",
  53. "CS_ISO_8859_8",
  54. "CS_ISO_8859_9",
  55. "CS_UNICODE"
  56. };
  57. #endif /* CONFIG_IRDA_DEBUG */
  58. static hashbin_t *iriap = NULL;
  59. static void *service_handle;
  60. static void __iriap_close(struct iriap_cb *self);
  61. static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode);
  62. static void iriap_disconnect_indication(void *instance, void *sap,
  63. LM_REASON reason, struct sk_buff *skb);
  64. static void iriap_connect_indication(void *instance, void *sap,
  65. struct qos_info *qos, __u32 max_sdu_size,
  66. __u8 max_header_size,
  67. struct sk_buff *skb);
  68. static void iriap_connect_confirm(void *instance, void *sap,
  69. struct qos_info *qos,
  70. __u32 max_sdu_size, __u8 max_header_size,
  71. struct sk_buff *skb);
  72. static int iriap_data_indication(void *instance, void *sap,
  73. struct sk_buff *skb);
  74. static void iriap_watchdog_timer_expired(void *data);
  75. static inline void iriap_start_watchdog_timer(struct iriap_cb *self,
  76. int timeout)
  77. {
  78. irda_start_timer(&self->watchdog_timer, timeout, self,
  79. iriap_watchdog_timer_expired);
  80. }
  81. static struct lock_class_key irias_objects_key;
  82. /*
  83. * Function iriap_init (void)
  84. *
  85. * Initializes the IrIAP layer, called by the module initialization code
  86. * in irmod.c
  87. */
  88. int __init iriap_init(void)
  89. {
  90. struct ias_object *obj;
  91. struct iriap_cb *server;
  92. __u8 oct_seq[6];
  93. __u16 hints;
  94. /* Allocate master array */
  95. iriap = hashbin_new(HB_LOCK);
  96. if (!iriap)
  97. return -ENOMEM;
  98. /* Object repository - defined in irias_object.c */
  99. irias_objects = hashbin_new(HB_LOCK);
  100. if (!irias_objects) {
  101. IRDA_WARNING("%s: Can't allocate irias_objects hashbin!\n",
  102. __func__);
  103. hashbin_delete(iriap, NULL);
  104. return -ENOMEM;
  105. }
  106. lockdep_set_class_and_name(&irias_objects->hb_spinlock, &irias_objects_key,
  107. "irias_objects");
  108. /*
  109. * Register some default services for IrLMP
  110. */
  111. hints = irlmp_service_to_hint(S_COMPUTER);
  112. service_handle = irlmp_register_service(hints);
  113. /* Register the Device object with LM-IAS */
  114. obj = irias_new_object("Device", IAS_DEVICE_ID);
  115. irias_add_string_attrib(obj, "DeviceName", "Linux", IAS_KERNEL_ATTR);
  116. oct_seq[0] = 0x01; /* Version 1 */
  117. oct_seq[1] = 0x00; /* IAS support bits */
  118. oct_seq[2] = 0x00; /* LM-MUX support bits */
  119. #ifdef CONFIG_IRDA_ULTRA
  120. oct_seq[2] |= 0x04; /* Connectionless Data support */
  121. #endif
  122. irias_add_octseq_attrib(obj, "IrLMPSupport", oct_seq, 3,
  123. IAS_KERNEL_ATTR);
  124. irias_insert_object(obj);
  125. /*
  126. * Register server support with IrLMP so we can accept incoming
  127. * connections
  128. */
  129. server = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
  130. if (!server) {
  131. IRDA_DEBUG(0, "%s(), unable to open server\n", __func__);
  132. return -1;
  133. }
  134. iriap_register_lsap(server, LSAP_IAS, IAS_SERVER);
  135. return 0;
  136. }
  137. /*
  138. * Function iriap_cleanup (void)
  139. *
  140. * Initializes the IrIAP layer, called by the module cleanup code in
  141. * irmod.c
  142. */
  143. void iriap_cleanup(void)
  144. {
  145. irlmp_unregister_service(service_handle);
  146. hashbin_delete(iriap, (FREE_FUNC) __iriap_close);
  147. hashbin_delete(irias_objects, (FREE_FUNC) __irias_delete_object);
  148. }
  149. /*
  150. * Function iriap_open (void)
  151. *
  152. * Opens an instance of the IrIAP layer, and registers with IrLMP
  153. */
  154. struct iriap_cb *iriap_open(__u8 slsap_sel, int mode, void *priv,
  155. CONFIRM_CALLBACK callback)
  156. {
  157. struct iriap_cb *self;
  158. IRDA_DEBUG(2, "%s()\n", __func__);
  159. self = kzalloc(sizeof(*self), GFP_ATOMIC);
  160. if (!self) {
  161. IRDA_WARNING("%s: Unable to kmalloc!\n", __func__);
  162. return NULL;
  163. }
  164. /*
  165. * Initialize instance
  166. */
  167. self->magic = IAS_MAGIC;
  168. self->mode = mode;
  169. if (mode == IAS_CLIENT)
  170. iriap_register_lsap(self, slsap_sel, mode);
  171. self->confirm = callback;
  172. self->priv = priv;
  173. /* iriap_getvaluebyclass_request() will construct packets before
  174. * we connect, so this must have a sane value... Jean II */
  175. self->max_header_size = LMP_MAX_HEADER;
  176. init_timer(&self->watchdog_timer);
  177. hashbin_insert(iriap, (irda_queue_t *) self, (long) self, NULL);
  178. /* Initialize state machines */
  179. iriap_next_client_state(self, S_DISCONNECT);
  180. iriap_next_call_state(self, S_MAKE_CALL);
  181. iriap_next_server_state(self, R_DISCONNECT);
  182. iriap_next_r_connect_state(self, R_WAITING);
  183. return self;
  184. }
  185. EXPORT_SYMBOL(iriap_open);
  186. /*
  187. * Function __iriap_close (self)
  188. *
  189. * Removes (deallocates) the IrIAP instance
  190. *
  191. */
  192. static void __iriap_close(struct iriap_cb *self)
  193. {
  194. IRDA_DEBUG(4, "%s()\n", __func__);
  195. IRDA_ASSERT(self != NULL, return;);
  196. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  197. del_timer(&self->watchdog_timer);
  198. if (self->request_skb)
  199. dev_kfree_skb(self->request_skb);
  200. self->magic = 0;
  201. kfree(self);
  202. }
  203. /*
  204. * Function iriap_close (void)
  205. *
  206. * Closes IrIAP and deregisters with IrLMP
  207. */
  208. void iriap_close(struct iriap_cb *self)
  209. {
  210. struct iriap_cb *entry;
  211. IRDA_DEBUG(2, "%s()\n", __func__);
  212. IRDA_ASSERT(self != NULL, return;);
  213. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  214. if (self->lsap) {
  215. irlmp_close_lsap(self->lsap);
  216. self->lsap = NULL;
  217. }
  218. entry = (struct iriap_cb *) hashbin_remove(iriap, (long) self, NULL);
  219. IRDA_ASSERT(entry == self, return;);
  220. __iriap_close(self);
  221. }
  222. EXPORT_SYMBOL(iriap_close);
  223. static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode)
  224. {
  225. notify_t notify;
  226. IRDA_DEBUG(2, "%s()\n", __func__);
  227. irda_notify_init(&notify);
  228. notify.connect_confirm = iriap_connect_confirm;
  229. notify.connect_indication = iriap_connect_indication;
  230. notify.disconnect_indication = iriap_disconnect_indication;
  231. notify.data_indication = iriap_data_indication;
  232. notify.instance = self;
  233. if (mode == IAS_CLIENT)
  234. strcpy(notify.name, "IrIAS cli");
  235. else
  236. strcpy(notify.name, "IrIAS srv");
  237. self->lsap = irlmp_open_lsap(slsap_sel, &notify, 0);
  238. if (self->lsap == NULL) {
  239. IRDA_ERROR("%s: Unable to allocated LSAP!\n", __func__);
  240. return -1;
  241. }
  242. self->slsap_sel = self->lsap->slsap_sel;
  243. return 0;
  244. }
  245. /*
  246. * Function iriap_disconnect_indication (handle, reason)
  247. *
  248. * Got disconnect, so clean up everything associated with this connection
  249. *
  250. */
  251. static void iriap_disconnect_indication(void *instance, void *sap,
  252. LM_REASON reason,
  253. struct sk_buff *skb)
  254. {
  255. struct iriap_cb *self;
  256. IRDA_DEBUG(4, "%s(), reason=%s [%d]\n", __func__,
  257. irlmp_reason_str(reason), reason);
  258. self = instance;
  259. IRDA_ASSERT(self != NULL, return;);
  260. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  261. IRDA_ASSERT(iriap != NULL, return;);
  262. del_timer(&self->watchdog_timer);
  263. /* Not needed */
  264. if (skb)
  265. dev_kfree_skb(skb);
  266. if (self->mode == IAS_CLIENT) {
  267. IRDA_DEBUG(4, "%s(), disconnect as client\n", __func__);
  268. iriap_do_client_event(self, IAP_LM_DISCONNECT_INDICATION,
  269. NULL);
  270. /*
  271. * Inform service user that the request failed by sending
  272. * it a NULL value. Warning, the client might close us, so
  273. * remember no to use self anymore after calling confirm
  274. */
  275. if (self->confirm)
  276. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  277. } else {
  278. IRDA_DEBUG(4, "%s(), disconnect as server\n", __func__);
  279. iriap_do_server_event(self, IAP_LM_DISCONNECT_INDICATION,
  280. NULL);
  281. iriap_close(self);
  282. }
  283. }
  284. /*
  285. * Function iriap_disconnect_request (handle)
  286. */
  287. static void iriap_disconnect_request(struct iriap_cb *self)
  288. {
  289. struct sk_buff *tx_skb;
  290. IRDA_DEBUG(4, "%s()\n", __func__);
  291. IRDA_ASSERT(self != NULL, return;);
  292. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  293. tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
  294. if (tx_skb == NULL) {
  295. IRDA_DEBUG(0,
  296. "%s(), Could not allocate an sk_buff of length %d\n",
  297. __func__, LMP_MAX_HEADER);
  298. return;
  299. }
  300. /*
  301. * Reserve space for MUX control and LAP header
  302. */
  303. skb_reserve(tx_skb, LMP_MAX_HEADER);
  304. irlmp_disconnect_request(self->lsap, tx_skb);
  305. }
  306. /*
  307. * Function iriap_getvaluebyclass (addr, name, attr)
  308. *
  309. * Retrieve all values from attribute in all objects with given class
  310. * name
  311. */
  312. int iriap_getvaluebyclass_request(struct iriap_cb *self,
  313. __u32 saddr, __u32 daddr,
  314. char *name, char *attr)
  315. {
  316. struct sk_buff *tx_skb;
  317. int name_len, attr_len, skb_len;
  318. __u8 *frame;
  319. IRDA_ASSERT(self != NULL, return -1;);
  320. IRDA_ASSERT(self->magic == IAS_MAGIC, return -1;);
  321. /* Client must supply the destination device address */
  322. if (!daddr)
  323. return -1;
  324. self->daddr = daddr;
  325. self->saddr = saddr;
  326. /*
  327. * Save operation, so we know what the later indication is about
  328. */
  329. self->operation = GET_VALUE_BY_CLASS;
  330. /* Give ourselves 10 secs to finish this operation */
  331. iriap_start_watchdog_timer(self, 10*HZ);
  332. name_len = strlen(name); /* Up to IAS_MAX_CLASSNAME = 60 */
  333. attr_len = strlen(attr); /* Up to IAS_MAX_ATTRIBNAME = 60 */
  334. skb_len = self->max_header_size+2+name_len+1+attr_len+4;
  335. tx_skb = alloc_skb(skb_len, GFP_ATOMIC);
  336. if (!tx_skb)
  337. return -ENOMEM;
  338. /* Reserve space for MUX and LAP header */
  339. skb_reserve(tx_skb, self->max_header_size);
  340. skb_put(tx_skb, 3+name_len+attr_len);
  341. frame = tx_skb->data;
  342. /* Build frame */
  343. frame[0] = IAP_LST | GET_VALUE_BY_CLASS;
  344. frame[1] = name_len; /* Insert length of name */
  345. memcpy(frame+2, name, name_len); /* Insert name */
  346. frame[2+name_len] = attr_len; /* Insert length of attr */
  347. memcpy(frame+3+name_len, attr, attr_len); /* Insert attr */
  348. iriap_do_client_event(self, IAP_CALL_REQUEST_GVBC, tx_skb);
  349. /* Drop reference count - see state_s_disconnect(). */
  350. dev_kfree_skb(tx_skb);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(iriap_getvaluebyclass_request);
  354. /*
  355. * Function iriap_getvaluebyclass_confirm (self, skb)
  356. *
  357. * Got result from GetValueByClass command. Parse it and return result
  358. * to service user.
  359. *
  360. */
  361. static void iriap_getvaluebyclass_confirm(struct iriap_cb *self,
  362. struct sk_buff *skb)
  363. {
  364. struct ias_value *value;
  365. int charset;
  366. __u32 value_len;
  367. __u32 tmp_cpu32;
  368. __u16 obj_id;
  369. __u16 len;
  370. __u8 type;
  371. __u8 *fp;
  372. int n;
  373. IRDA_ASSERT(self != NULL, return;);
  374. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  375. IRDA_ASSERT(skb != NULL, return;);
  376. /* Initialize variables */
  377. fp = skb->data;
  378. n = 2;
  379. /* Get length, MSB first */
  380. len = get_unaligned_be16(fp + n);
  381. n += 2;
  382. IRDA_DEBUG(4, "%s(), len=%d\n", __func__, len);
  383. /* Get object ID, MSB first */
  384. obj_id = get_unaligned_be16(fp + n);
  385. n += 2;
  386. type = fp[n++];
  387. IRDA_DEBUG(4, "%s(), Value type = %d\n", __func__, type);
  388. switch (type) {
  389. case IAS_INTEGER:
  390. memcpy(&tmp_cpu32, fp+n, 4); n += 4;
  391. be32_to_cpus(&tmp_cpu32);
  392. value = irias_new_integer_value(tmp_cpu32);
  393. /* Legal values restricted to 0x01-0x6f, page 15 irttp */
  394. IRDA_DEBUG(4, "%s(), lsap=%d\n", __func__, value->t.integer);
  395. break;
  396. case IAS_STRING:
  397. charset = fp[n++];
  398. switch (charset) {
  399. case CS_ASCII:
  400. break;
  401. /* case CS_ISO_8859_1: */
  402. /* case CS_ISO_8859_2: */
  403. /* case CS_ISO_8859_3: */
  404. /* case CS_ISO_8859_4: */
  405. /* case CS_ISO_8859_5: */
  406. /* case CS_ISO_8859_6: */
  407. /* case CS_ISO_8859_7: */
  408. /* case CS_ISO_8859_8: */
  409. /* case CS_ISO_8859_9: */
  410. /* case CS_UNICODE: */
  411. default:
  412. IRDA_DEBUG(0, "%s(), charset [%d] %s, not supported\n",
  413. __func__, charset,
  414. charset < ARRAY_SIZE(ias_charset_types) ?
  415. ias_charset_types[charset] :
  416. "(unknown)");
  417. /* Aborting, close connection! */
  418. iriap_disconnect_request(self);
  419. return;
  420. /* break; */
  421. }
  422. value_len = fp[n++];
  423. IRDA_DEBUG(4, "%s(), strlen=%d\n", __func__, value_len);
  424. /* Make sure the string is null-terminated */
  425. if (n + value_len < skb->len)
  426. fp[n + value_len] = 0x00;
  427. IRDA_DEBUG(4, "Got string %s\n", fp+n);
  428. /* Will truncate to IAS_MAX_STRING bytes */
  429. value = irias_new_string_value(fp+n);
  430. break;
  431. case IAS_OCT_SEQ:
  432. value_len = get_unaligned_be16(fp + n);
  433. n += 2;
  434. /* Will truncate to IAS_MAX_OCTET_STRING bytes */
  435. value = irias_new_octseq_value(fp+n, value_len);
  436. break;
  437. default:
  438. value = irias_new_missing_value();
  439. break;
  440. }
  441. /* Finished, close connection! */
  442. iriap_disconnect_request(self);
  443. /* Warning, the client might close us, so remember no to use self
  444. * anymore after calling confirm
  445. */
  446. if (self->confirm)
  447. self->confirm(IAS_SUCCESS, obj_id, value, self->priv);
  448. else {
  449. IRDA_DEBUG(0, "%s(), missing handler!\n", __func__);
  450. irias_delete_value(value);
  451. }
  452. }
  453. /*
  454. * Function iriap_getvaluebyclass_response ()
  455. *
  456. * Send answer back to remote LM-IAS
  457. *
  458. */
  459. static void iriap_getvaluebyclass_response(struct iriap_cb *self,
  460. __u16 obj_id,
  461. __u8 ret_code,
  462. struct ias_value *value)
  463. {
  464. struct sk_buff *tx_skb;
  465. int n;
  466. __be32 tmp_be32;
  467. __be16 tmp_be16;
  468. __u8 *fp;
  469. IRDA_DEBUG(4, "%s()\n", __func__);
  470. IRDA_ASSERT(self != NULL, return;);
  471. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  472. IRDA_ASSERT(value != NULL, return;);
  473. IRDA_ASSERT(value->len <= 1024, return;);
  474. /* Initialize variables */
  475. n = 0;
  476. /*
  477. * We must adjust the size of the response after the length of the
  478. * value. We add 32 bytes because of the 6 bytes for the frame and
  479. * max 5 bytes for the value coding.
  480. */
  481. tx_skb = alloc_skb(value->len + self->max_header_size + 32,
  482. GFP_ATOMIC);
  483. if (!tx_skb)
  484. return;
  485. /* Reserve space for MUX and LAP header */
  486. skb_reserve(tx_skb, self->max_header_size);
  487. skb_put(tx_skb, 6);
  488. fp = tx_skb->data;
  489. /* Build frame */
  490. fp[n++] = GET_VALUE_BY_CLASS | IAP_LST;
  491. fp[n++] = ret_code;
  492. /* Insert list length (MSB first) */
  493. tmp_be16 = htons(0x0001);
  494. memcpy(fp+n, &tmp_be16, 2); n += 2;
  495. /* Insert object identifier ( MSB first) */
  496. tmp_be16 = cpu_to_be16(obj_id);
  497. memcpy(fp+n, &tmp_be16, 2); n += 2;
  498. switch (value->type) {
  499. case IAS_STRING:
  500. skb_put(tx_skb, 3 + value->len);
  501. fp[n++] = value->type;
  502. fp[n++] = 0; /* ASCII */
  503. fp[n++] = (__u8) value->len;
  504. memcpy(fp+n, value->t.string, value->len); n+=value->len;
  505. break;
  506. case IAS_INTEGER:
  507. skb_put(tx_skb, 5);
  508. fp[n++] = value->type;
  509. tmp_be32 = cpu_to_be32(value->t.integer);
  510. memcpy(fp+n, &tmp_be32, 4); n += 4;
  511. break;
  512. case IAS_OCT_SEQ:
  513. skb_put(tx_skb, 3 + value->len);
  514. fp[n++] = value->type;
  515. tmp_be16 = cpu_to_be16(value->len);
  516. memcpy(fp+n, &tmp_be16, 2); n += 2;
  517. memcpy(fp+n, value->t.oct_seq, value->len); n+=value->len;
  518. break;
  519. case IAS_MISSING:
  520. IRDA_DEBUG( 3, "%s: sending IAS_MISSING\n", __func__);
  521. skb_put(tx_skb, 1);
  522. fp[n++] = value->type;
  523. break;
  524. default:
  525. IRDA_DEBUG(0, "%s(), type not implemented!\n", __func__);
  526. break;
  527. }
  528. iriap_do_r_connect_event(self, IAP_CALL_RESPONSE, tx_skb);
  529. /* Drop reference count - see state_r_execute(). */
  530. dev_kfree_skb(tx_skb);
  531. }
  532. /*
  533. * Function iriap_getvaluebyclass_indication (self, skb)
  534. *
  535. * getvaluebyclass is requested from peer LM-IAS
  536. *
  537. */
  538. static void iriap_getvaluebyclass_indication(struct iriap_cb *self,
  539. struct sk_buff *skb)
  540. {
  541. struct ias_object *obj;
  542. struct ias_attrib *attrib;
  543. int name_len;
  544. int attr_len;
  545. char name[IAS_MAX_CLASSNAME + 1]; /* 60 bytes */
  546. char attr[IAS_MAX_ATTRIBNAME + 1]; /* 60 bytes */
  547. __u8 *fp;
  548. int n;
  549. IRDA_DEBUG(4, "%s()\n", __func__);
  550. IRDA_ASSERT(self != NULL, return;);
  551. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  552. IRDA_ASSERT(skb != NULL, return;);
  553. fp = skb->data;
  554. n = 1;
  555. name_len = fp[n++];
  556. IRDA_ASSERT(name_len < IAS_MAX_CLASSNAME + 1, return;);
  557. memcpy(name, fp+n, name_len); n+=name_len;
  558. name[name_len] = '\0';
  559. attr_len = fp[n++];
  560. IRDA_ASSERT(attr_len < IAS_MAX_ATTRIBNAME + 1, return;);
  561. memcpy(attr, fp+n, attr_len); n+=attr_len;
  562. attr[attr_len] = '\0';
  563. IRDA_DEBUG(4, "LM-IAS: Looking up %s: %s\n", name, attr);
  564. obj = irias_find_object(name);
  565. if (obj == NULL) {
  566. IRDA_DEBUG(2, "LM-IAS: Object %s not found\n", name);
  567. iriap_getvaluebyclass_response(self, 0x1235, IAS_CLASS_UNKNOWN,
  568. &irias_missing);
  569. return;
  570. }
  571. IRDA_DEBUG(4, "LM-IAS: found %s, id=%d\n", obj->name, obj->id);
  572. attrib = irias_find_attrib(obj, attr);
  573. if (attrib == NULL) {
  574. IRDA_DEBUG(2, "LM-IAS: Attribute %s not found\n", attr);
  575. iriap_getvaluebyclass_response(self, obj->id,
  576. IAS_ATTRIB_UNKNOWN,
  577. &irias_missing);
  578. return;
  579. }
  580. /* We have a match; send the value. */
  581. iriap_getvaluebyclass_response(self, obj->id, IAS_SUCCESS,
  582. attrib->value);
  583. }
  584. /*
  585. * Function iriap_send_ack (void)
  586. *
  587. * Currently not used
  588. *
  589. */
  590. void iriap_send_ack(struct iriap_cb *self)
  591. {
  592. struct sk_buff *tx_skb;
  593. __u8 *frame;
  594. IRDA_DEBUG(2, "%s()\n", __func__);
  595. IRDA_ASSERT(self != NULL, return;);
  596. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  597. tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
  598. if (!tx_skb)
  599. return;
  600. /* Reserve space for MUX and LAP header */
  601. skb_reserve(tx_skb, self->max_header_size);
  602. skb_put(tx_skb, 1);
  603. frame = tx_skb->data;
  604. /* Build frame */
  605. frame[0] = IAP_LST | IAP_ACK | self->operation;
  606. irlmp_data_request(self->lsap, tx_skb);
  607. }
  608. void iriap_connect_request(struct iriap_cb *self)
  609. {
  610. int ret;
  611. IRDA_ASSERT(self != NULL, return;);
  612. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  613. ret = irlmp_connect_request(self->lsap, LSAP_IAS,
  614. self->saddr, self->daddr,
  615. NULL, NULL);
  616. if (ret < 0) {
  617. IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
  618. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  619. }
  620. }
  621. /*
  622. * Function iriap_connect_confirm (handle, skb)
  623. *
  624. * LSAP connection confirmed!
  625. *
  626. */
  627. static void iriap_connect_confirm(void *instance, void *sap,
  628. struct qos_info *qos, __u32 max_seg_size,
  629. __u8 max_header_size,
  630. struct sk_buff *skb)
  631. {
  632. struct iriap_cb *self;
  633. self = instance;
  634. IRDA_ASSERT(self != NULL, return;);
  635. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  636. IRDA_ASSERT(skb != NULL, return;);
  637. self->max_data_size = max_seg_size;
  638. self->max_header_size = max_header_size;
  639. del_timer(&self->watchdog_timer);
  640. iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
  641. /* Drop reference count - see state_s_make_call(). */
  642. dev_kfree_skb(skb);
  643. }
  644. /*
  645. * Function iriap_connect_indication ( handle, skb)
  646. *
  647. * Remote LM-IAS is requesting connection
  648. *
  649. */
  650. static void iriap_connect_indication(void *instance, void *sap,
  651. struct qos_info *qos, __u32 max_seg_size,
  652. __u8 max_header_size,
  653. struct sk_buff *skb)
  654. {
  655. struct iriap_cb *self, *new;
  656. IRDA_DEBUG(1, "%s()\n", __func__);
  657. self = instance;
  658. IRDA_ASSERT(skb != NULL, return;);
  659. IRDA_ASSERT(self != NULL, goto out;);
  660. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  661. /* Start new server */
  662. new = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
  663. if (!new) {
  664. IRDA_DEBUG(0, "%s(), open failed\n", __func__);
  665. goto out;
  666. }
  667. /* Now attach up the new "socket" */
  668. new->lsap = irlmp_dup(self->lsap, new);
  669. if (!new->lsap) {
  670. IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
  671. goto out;
  672. }
  673. new->max_data_size = max_seg_size;
  674. new->max_header_size = max_header_size;
  675. /* Clean up the original one to keep it in listen state */
  676. irlmp_listen(self->lsap);
  677. iriap_do_server_event(new, IAP_LM_CONNECT_INDICATION, skb);
  678. out:
  679. /* Drop reference count - see state_r_disconnect(). */
  680. dev_kfree_skb(skb);
  681. }
  682. /*
  683. * Function iriap_data_indication (handle, skb)
  684. *
  685. * Receives data from connection identified by handle from IrLMP
  686. *
  687. */
  688. static int iriap_data_indication(void *instance, void *sap,
  689. struct sk_buff *skb)
  690. {
  691. struct iriap_cb *self;
  692. __u8 *frame;
  693. __u8 opcode;
  694. IRDA_DEBUG(3, "%s()\n", __func__);
  695. self = instance;
  696. IRDA_ASSERT(skb != NULL, return 0;);
  697. IRDA_ASSERT(self != NULL, goto out;);
  698. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  699. frame = skb->data;
  700. if (self->mode == IAS_SERVER) {
  701. /* Call server */
  702. IRDA_DEBUG(4, "%s(), Calling server!\n", __func__);
  703. iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
  704. goto out;
  705. }
  706. opcode = frame[0];
  707. if (~opcode & IAP_LST) {
  708. IRDA_WARNING("%s:, IrIAS multiframe commands or "
  709. "results is not implemented yet!\n",
  710. __func__);
  711. goto out;
  712. }
  713. /* Check for ack frames since they don't contain any data */
  714. if (opcode & IAP_ACK) {
  715. IRDA_DEBUG(0, "%s() Got ack frame!\n", __func__);
  716. goto out;
  717. }
  718. opcode &= ~IAP_LST; /* Mask away LST bit */
  719. switch (opcode) {
  720. case GET_INFO_BASE:
  721. IRDA_DEBUG(0, "IrLMP GetInfoBaseDetails not implemented!\n");
  722. break;
  723. case GET_VALUE_BY_CLASS:
  724. iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
  725. switch (frame[1]) {
  726. case IAS_SUCCESS:
  727. iriap_getvaluebyclass_confirm(self, skb);
  728. break;
  729. case IAS_CLASS_UNKNOWN:
  730. IRDA_DEBUG(1, "%s(), No such class!\n", __func__);
  731. /* Finished, close connection! */
  732. iriap_disconnect_request(self);
  733. /*
  734. * Warning, the client might close us, so remember
  735. * no to use self anymore after calling confirm
  736. */
  737. if (self->confirm)
  738. self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
  739. self->priv);
  740. break;
  741. case IAS_ATTRIB_UNKNOWN:
  742. IRDA_DEBUG(1, "%s(), No such attribute!\n", __func__);
  743. /* Finished, close connection! */
  744. iriap_disconnect_request(self);
  745. /*
  746. * Warning, the client might close us, so remember
  747. * no to use self anymore after calling confirm
  748. */
  749. if (self->confirm)
  750. self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
  751. self->priv);
  752. break;
  753. }
  754. break;
  755. default:
  756. IRDA_DEBUG(0, "%s(), Unknown op-code: %02x\n", __func__,
  757. opcode);
  758. break;
  759. }
  760. out:
  761. /* Cleanup - sub-calls will have done skb_get() as needed. */
  762. dev_kfree_skb(skb);
  763. return 0;
  764. }
  765. /*
  766. * Function iriap_call_indication (self, skb)
  767. *
  768. * Received call to server from peer LM-IAS
  769. *
  770. */
  771. void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
  772. {
  773. __u8 *fp;
  774. __u8 opcode;
  775. IRDA_DEBUG(4, "%s()\n", __func__);
  776. IRDA_ASSERT(self != NULL, return;);
  777. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  778. IRDA_ASSERT(skb != NULL, return;);
  779. fp = skb->data;
  780. opcode = fp[0];
  781. if (~opcode & 0x80) {
  782. IRDA_WARNING("%s: IrIAS multiframe commands or results "
  783. "is not implemented yet!\n", __func__);
  784. return;
  785. }
  786. opcode &= 0x7f; /* Mask away LST bit */
  787. switch (opcode) {
  788. case GET_INFO_BASE:
  789. IRDA_WARNING("%s: GetInfoBaseDetails not implemented yet!\n",
  790. __func__);
  791. break;
  792. case GET_VALUE_BY_CLASS:
  793. iriap_getvaluebyclass_indication(self, skb);
  794. break;
  795. }
  796. /* skb will be cleaned up in iriap_data_indication */
  797. }
  798. /*
  799. * Function iriap_watchdog_timer_expired (data)
  800. *
  801. * Query has taken too long time, so abort
  802. *
  803. */
  804. static void iriap_watchdog_timer_expired(void *data)
  805. {
  806. struct iriap_cb *self = (struct iriap_cb *) data;
  807. IRDA_ASSERT(self != NULL, return;);
  808. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  809. /* iriap_close(self); */
  810. }
  811. #ifdef CONFIG_PROC_FS
  812. static const char *const ias_value_types[] = {
  813. "IAS_MISSING",
  814. "IAS_INTEGER",
  815. "IAS_OCT_SEQ",
  816. "IAS_STRING"
  817. };
  818. static inline struct ias_object *irias_seq_idx(loff_t pos)
  819. {
  820. struct ias_object *obj;
  821. for (obj = (struct ias_object *) hashbin_get_first(irias_objects);
  822. obj; obj = (struct ias_object *) hashbin_get_next(irias_objects)) {
  823. if (pos-- == 0)
  824. break;
  825. }
  826. return obj;
  827. }
  828. static void *irias_seq_start(struct seq_file *seq, loff_t *pos)
  829. {
  830. spin_lock_irq(&irias_objects->hb_spinlock);
  831. return *pos ? irias_seq_idx(*pos - 1) : SEQ_START_TOKEN;
  832. }
  833. static void *irias_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  834. {
  835. ++*pos;
  836. return (v == SEQ_START_TOKEN)
  837. ? (void *) hashbin_get_first(irias_objects)
  838. : (void *) hashbin_get_next(irias_objects);
  839. }
  840. static void irias_seq_stop(struct seq_file *seq, void *v)
  841. {
  842. spin_unlock_irq(&irias_objects->hb_spinlock);
  843. }
  844. static int irias_seq_show(struct seq_file *seq, void *v)
  845. {
  846. if (v == SEQ_START_TOKEN)
  847. seq_puts(seq, "LM-IAS Objects:\n");
  848. else {
  849. struct ias_object *obj = v;
  850. struct ias_attrib *attrib;
  851. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -EINVAL;);
  852. seq_printf(seq, "name: %s, id=%d\n",
  853. obj->name, obj->id);
  854. /* Careful for priority inversions here !
  855. * All other uses of attrib spinlock are independent of
  856. * the object spinlock, so we are safe. Jean II */
  857. spin_lock(&obj->attribs->hb_spinlock);
  858. /* List all attributes for this object */
  859. for (attrib = (struct ias_attrib *) hashbin_get_first(obj->attribs);
  860. attrib != NULL;
  861. attrib = (struct ias_attrib *) hashbin_get_next(obj->attribs)) {
  862. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC,
  863. goto outloop; );
  864. seq_printf(seq, " - Attribute name: \"%s\", ",
  865. attrib->name);
  866. seq_printf(seq, "value[%s]: ",
  867. ias_value_types[attrib->value->type]);
  868. switch (attrib->value->type) {
  869. case IAS_INTEGER:
  870. seq_printf(seq, "%d\n",
  871. attrib->value->t.integer);
  872. break;
  873. case IAS_STRING:
  874. seq_printf(seq, "\"%s\"\n",
  875. attrib->value->t.string);
  876. break;
  877. case IAS_OCT_SEQ:
  878. seq_printf(seq, "octet sequence (%d bytes)\n",
  879. attrib->value->len);
  880. break;
  881. case IAS_MISSING:
  882. seq_puts(seq, "missing\n");
  883. break;
  884. default:
  885. seq_printf(seq, "type %d?\n",
  886. attrib->value->type);
  887. }
  888. seq_putc(seq, '\n');
  889. }
  890. IRDA_ASSERT_LABEL(outloop:)
  891. spin_unlock(&obj->attribs->hb_spinlock);
  892. }
  893. return 0;
  894. }
  895. static const struct seq_operations irias_seq_ops = {
  896. .start = irias_seq_start,
  897. .next = irias_seq_next,
  898. .stop = irias_seq_stop,
  899. .show = irias_seq_show,
  900. };
  901. static int irias_seq_open(struct inode *inode, struct file *file)
  902. {
  903. IRDA_ASSERT( irias_objects != NULL, return -EINVAL;);
  904. return seq_open(file, &irias_seq_ops);
  905. }
  906. const struct file_operations irias_seq_fops = {
  907. .owner = THIS_MODULE,
  908. .open = irias_seq_open,
  909. .read = seq_read,
  910. .llseek = seq_lseek,
  911. .release = seq_release,
  912. };
  913. #endif /* PROC_FS */