iriap.c 26 KB

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