iriap.c 26 KB

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