iriap.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. fp[n+value_len] = 0x00;
  419. IRDA_DEBUG(4, "Got string %s\n", fp+n);
  420. /* Will truncate to IAS_MAX_STRING bytes */
  421. value = irias_new_string_value(fp+n);
  422. break;
  423. case IAS_OCT_SEQ:
  424. value_len = get_unaligned_be16(fp + n);
  425. n += 2;
  426. /* Will truncate to IAS_MAX_OCTET_STRING bytes */
  427. value = irias_new_octseq_value(fp+n, value_len);
  428. break;
  429. default:
  430. value = irias_new_missing_value();
  431. break;
  432. }
  433. /* Finished, close connection! */
  434. iriap_disconnect_request(self);
  435. /* Warning, the client might close us, so remember no to use self
  436. * anymore after calling confirm
  437. */
  438. if (self->confirm)
  439. self->confirm(IAS_SUCCESS, obj_id, value, self->priv);
  440. else {
  441. IRDA_DEBUG(0, "%s(), missing handler!\n", __func__);
  442. irias_delete_value(value);
  443. }
  444. }
  445. /*
  446. * Function iriap_getvaluebyclass_response ()
  447. *
  448. * Send answer back to remote LM-IAS
  449. *
  450. */
  451. static void iriap_getvaluebyclass_response(struct iriap_cb *self,
  452. __u16 obj_id,
  453. __u8 ret_code,
  454. struct ias_value *value)
  455. {
  456. struct sk_buff *tx_skb;
  457. int n;
  458. __be32 tmp_be32;
  459. __be16 tmp_be16;
  460. __u8 *fp;
  461. IRDA_DEBUG(4, "%s()\n", __func__);
  462. IRDA_ASSERT(self != NULL, return;);
  463. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  464. IRDA_ASSERT(value != NULL, return;);
  465. IRDA_ASSERT(value->len <= 1024, return;);
  466. /* Initialize variables */
  467. n = 0;
  468. /*
  469. * We must adjust the size of the response after the length of the
  470. * value. We add 32 bytes because of the 6 bytes for the frame and
  471. * max 5 bytes for the value coding.
  472. */
  473. tx_skb = alloc_skb(value->len + self->max_header_size + 32,
  474. GFP_ATOMIC);
  475. if (!tx_skb)
  476. return;
  477. /* Reserve space for MUX and LAP header */
  478. skb_reserve(tx_skb, self->max_header_size);
  479. skb_put(tx_skb, 6);
  480. fp = tx_skb->data;
  481. /* Build frame */
  482. fp[n++] = GET_VALUE_BY_CLASS | IAP_LST;
  483. fp[n++] = ret_code;
  484. /* Insert list length (MSB first) */
  485. tmp_be16 = htons(0x0001);
  486. memcpy(fp+n, &tmp_be16, 2); n += 2;
  487. /* Insert object identifier ( MSB first) */
  488. tmp_be16 = cpu_to_be16(obj_id);
  489. memcpy(fp+n, &tmp_be16, 2); n += 2;
  490. switch (value->type) {
  491. case IAS_STRING:
  492. skb_put(tx_skb, 3 + value->len);
  493. fp[n++] = value->type;
  494. fp[n++] = 0; /* ASCII */
  495. fp[n++] = (__u8) value->len;
  496. memcpy(fp+n, value->t.string, value->len); n+=value->len;
  497. break;
  498. case IAS_INTEGER:
  499. skb_put(tx_skb, 5);
  500. fp[n++] = value->type;
  501. tmp_be32 = cpu_to_be32(value->t.integer);
  502. memcpy(fp+n, &tmp_be32, 4); n += 4;
  503. break;
  504. case IAS_OCT_SEQ:
  505. skb_put(tx_skb, 3 + value->len);
  506. fp[n++] = value->type;
  507. tmp_be16 = cpu_to_be16(value->len);
  508. memcpy(fp+n, &tmp_be16, 2); n += 2;
  509. memcpy(fp+n, value->t.oct_seq, value->len); n+=value->len;
  510. break;
  511. case IAS_MISSING:
  512. IRDA_DEBUG( 3, "%s: sending IAS_MISSING\n", __func__);
  513. skb_put(tx_skb, 1);
  514. fp[n++] = value->type;
  515. break;
  516. default:
  517. IRDA_DEBUG(0, "%s(), type not implemented!\n", __func__);
  518. break;
  519. }
  520. iriap_do_r_connect_event(self, IAP_CALL_RESPONSE, tx_skb);
  521. /* Drop reference count - see state_r_execute(). */
  522. dev_kfree_skb(tx_skb);
  523. }
  524. /*
  525. * Function iriap_getvaluebyclass_indication (self, skb)
  526. *
  527. * getvaluebyclass is requested from peer LM-IAS
  528. *
  529. */
  530. static void iriap_getvaluebyclass_indication(struct iriap_cb *self,
  531. struct sk_buff *skb)
  532. {
  533. struct ias_object *obj;
  534. struct ias_attrib *attrib;
  535. int name_len;
  536. int attr_len;
  537. char name[IAS_MAX_CLASSNAME + 1]; /* 60 bytes */
  538. char attr[IAS_MAX_ATTRIBNAME + 1]; /* 60 bytes */
  539. __u8 *fp;
  540. int n;
  541. IRDA_DEBUG(4, "%s()\n", __func__);
  542. IRDA_ASSERT(self != NULL, return;);
  543. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  544. IRDA_ASSERT(skb != NULL, return;);
  545. fp = skb->data;
  546. n = 1;
  547. name_len = fp[n++];
  548. memcpy(name, fp+n, name_len); n+=name_len;
  549. name[name_len] = '\0';
  550. attr_len = fp[n++];
  551. memcpy(attr, fp+n, attr_len); n+=attr_len;
  552. attr[attr_len] = '\0';
  553. IRDA_DEBUG(4, "LM-IAS: Looking up %s: %s\n", name, attr);
  554. obj = irias_find_object(name);
  555. if (obj == NULL) {
  556. IRDA_DEBUG(2, "LM-IAS: Object %s not found\n", name);
  557. iriap_getvaluebyclass_response(self, 0x1235, IAS_CLASS_UNKNOWN,
  558. &irias_missing);
  559. return;
  560. }
  561. IRDA_DEBUG(4, "LM-IAS: found %s, id=%d\n", obj->name, obj->id);
  562. attrib = irias_find_attrib(obj, attr);
  563. if (attrib == NULL) {
  564. IRDA_DEBUG(2, "LM-IAS: Attribute %s not found\n", attr);
  565. iriap_getvaluebyclass_response(self, obj->id,
  566. IAS_ATTRIB_UNKNOWN,
  567. &irias_missing);
  568. return;
  569. }
  570. /* We have a match; send the value. */
  571. iriap_getvaluebyclass_response(self, obj->id, IAS_SUCCESS,
  572. attrib->value);
  573. }
  574. /*
  575. * Function iriap_send_ack (void)
  576. *
  577. * Currently not used
  578. *
  579. */
  580. void iriap_send_ack(struct iriap_cb *self)
  581. {
  582. struct sk_buff *tx_skb;
  583. __u8 *frame;
  584. IRDA_DEBUG(2, "%s()\n", __func__);
  585. IRDA_ASSERT(self != NULL, return;);
  586. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  587. tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
  588. if (!tx_skb)
  589. return;
  590. /* Reserve space for MUX and LAP header */
  591. skb_reserve(tx_skb, self->max_header_size);
  592. skb_put(tx_skb, 1);
  593. frame = tx_skb->data;
  594. /* Build frame */
  595. frame[0] = IAP_LST | IAP_ACK | self->operation;
  596. irlmp_data_request(self->lsap, tx_skb);
  597. }
  598. void iriap_connect_request(struct iriap_cb *self)
  599. {
  600. int ret;
  601. IRDA_ASSERT(self != NULL, return;);
  602. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  603. ret = irlmp_connect_request(self->lsap, LSAP_IAS,
  604. self->saddr, self->daddr,
  605. NULL, NULL);
  606. if (ret < 0) {
  607. IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
  608. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  609. }
  610. }
  611. /*
  612. * Function iriap_connect_confirm (handle, skb)
  613. *
  614. * LSAP connection confirmed!
  615. *
  616. */
  617. static void iriap_connect_confirm(void *instance, void *sap,
  618. struct qos_info *qos, __u32 max_seg_size,
  619. __u8 max_header_size,
  620. struct sk_buff *skb)
  621. {
  622. struct iriap_cb *self;
  623. self = (struct iriap_cb *) instance;
  624. IRDA_ASSERT(self != NULL, return;);
  625. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  626. IRDA_ASSERT(skb != NULL, return;);
  627. self->max_data_size = max_seg_size;
  628. self->max_header_size = max_header_size;
  629. del_timer(&self->watchdog_timer);
  630. iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
  631. /* Drop reference count - see state_s_make_call(). */
  632. dev_kfree_skb(skb);
  633. }
  634. /*
  635. * Function iriap_connect_indication ( handle, skb)
  636. *
  637. * Remote LM-IAS is requesting connection
  638. *
  639. */
  640. static void iriap_connect_indication(void *instance, void *sap,
  641. struct qos_info *qos, __u32 max_seg_size,
  642. __u8 max_header_size,
  643. struct sk_buff *skb)
  644. {
  645. struct iriap_cb *self, *new;
  646. IRDA_DEBUG(1, "%s()\n", __func__);
  647. self = (struct iriap_cb *) instance;
  648. IRDA_ASSERT(skb != NULL, return;);
  649. IRDA_ASSERT(self != NULL, goto out;);
  650. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  651. /* Start new server */
  652. new = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
  653. if (!new) {
  654. IRDA_DEBUG(0, "%s(), open failed\n", __func__);
  655. goto out;
  656. }
  657. /* Now attach up the new "socket" */
  658. new->lsap = irlmp_dup(self->lsap, new);
  659. if (!new->lsap) {
  660. IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
  661. goto out;
  662. }
  663. new->max_data_size = max_seg_size;
  664. new->max_header_size = max_header_size;
  665. /* Clean up the original one to keep it in listen state */
  666. irlmp_listen(self->lsap);
  667. iriap_do_server_event(new, IAP_LM_CONNECT_INDICATION, skb);
  668. out:
  669. /* Drop reference count - see state_r_disconnect(). */
  670. dev_kfree_skb(skb);
  671. }
  672. /*
  673. * Function iriap_data_indication (handle, skb)
  674. *
  675. * Receives data from connection identified by handle from IrLMP
  676. *
  677. */
  678. static int iriap_data_indication(void *instance, void *sap,
  679. struct sk_buff *skb)
  680. {
  681. struct iriap_cb *self;
  682. __u8 *frame;
  683. __u8 opcode;
  684. IRDA_DEBUG(3, "%s()\n", __func__);
  685. self = (struct iriap_cb *) instance;
  686. IRDA_ASSERT(skb != NULL, return 0;);
  687. IRDA_ASSERT(self != NULL, goto out;);
  688. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  689. frame = skb->data;
  690. if (self->mode == IAS_SERVER) {
  691. /* Call server */
  692. IRDA_DEBUG(4, "%s(), Calling server!\n", __func__);
  693. iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
  694. goto out;
  695. }
  696. opcode = frame[0];
  697. if (~opcode & IAP_LST) {
  698. IRDA_WARNING("%s:, IrIAS multiframe commands or "
  699. "results is not implemented yet!\n",
  700. __func__);
  701. goto out;
  702. }
  703. /* Check for ack frames since they don't contain any data */
  704. if (opcode & IAP_ACK) {
  705. IRDA_DEBUG(0, "%s() Got ack frame!\n", __func__);
  706. goto out;
  707. }
  708. opcode &= ~IAP_LST; /* Mask away LST bit */
  709. switch (opcode) {
  710. case GET_INFO_BASE:
  711. IRDA_DEBUG(0, "IrLMP GetInfoBaseDetails not implemented!\n");
  712. break;
  713. case GET_VALUE_BY_CLASS:
  714. iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
  715. switch (frame[1]) {
  716. case IAS_SUCCESS:
  717. iriap_getvaluebyclass_confirm(self, skb);
  718. break;
  719. case IAS_CLASS_UNKNOWN:
  720. IRDA_DEBUG(1, "%s(), No such class!\n", __func__);
  721. /* Finished, close connection! */
  722. iriap_disconnect_request(self);
  723. /*
  724. * Warning, the client might close us, so remember
  725. * no to use self anymore after calling confirm
  726. */
  727. if (self->confirm)
  728. self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
  729. self->priv);
  730. break;
  731. case IAS_ATTRIB_UNKNOWN:
  732. IRDA_DEBUG(1, "%s(), No such attribute!\n", __func__);
  733. /* Finished, close connection! */
  734. iriap_disconnect_request(self);
  735. /*
  736. * Warning, the client might close us, so remember
  737. * no to use self anymore after calling confirm
  738. */
  739. if (self->confirm)
  740. self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
  741. self->priv);
  742. break;
  743. }
  744. break;
  745. default:
  746. IRDA_DEBUG(0, "%s(), Unknown op-code: %02x\n", __func__,
  747. opcode);
  748. break;
  749. }
  750. out:
  751. /* Cleanup - sub-calls will have done skb_get() as needed. */
  752. dev_kfree_skb(skb);
  753. return 0;
  754. }
  755. /*
  756. * Function iriap_call_indication (self, skb)
  757. *
  758. * Received call to server from peer LM-IAS
  759. *
  760. */
  761. void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
  762. {
  763. __u8 *fp;
  764. __u8 opcode;
  765. IRDA_DEBUG(4, "%s()\n", __func__);
  766. IRDA_ASSERT(self != NULL, return;);
  767. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  768. IRDA_ASSERT(skb != NULL, return;);
  769. fp = skb->data;
  770. opcode = fp[0];
  771. if (~opcode & 0x80) {
  772. IRDA_WARNING("%s: IrIAS multiframe commands or results "
  773. "is not implemented yet!\n", __func__);
  774. return;
  775. }
  776. opcode &= 0x7f; /* Mask away LST bit */
  777. switch (opcode) {
  778. case GET_INFO_BASE:
  779. IRDA_WARNING("%s: GetInfoBaseDetails not implemented yet!\n",
  780. __func__);
  781. break;
  782. case GET_VALUE_BY_CLASS:
  783. iriap_getvaluebyclass_indication(self, skb);
  784. break;
  785. }
  786. /* skb will be cleaned up in iriap_data_indication */
  787. }
  788. /*
  789. * Function iriap_watchdog_timer_expired (data)
  790. *
  791. * Query has taken too long time, so abort
  792. *
  793. */
  794. static void iriap_watchdog_timer_expired(void *data)
  795. {
  796. struct iriap_cb *self = (struct iriap_cb *) data;
  797. IRDA_ASSERT(self != NULL, return;);
  798. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  799. /* iriap_close(self); */
  800. }
  801. #ifdef CONFIG_PROC_FS
  802. static const char *const ias_value_types[] = {
  803. "IAS_MISSING",
  804. "IAS_INTEGER",
  805. "IAS_OCT_SEQ",
  806. "IAS_STRING"
  807. };
  808. static inline struct ias_object *irias_seq_idx(loff_t pos)
  809. {
  810. struct ias_object *obj;
  811. for (obj = (struct ias_object *) hashbin_get_first(irias_objects);
  812. obj; obj = (struct ias_object *) hashbin_get_next(irias_objects)) {
  813. if (pos-- == 0)
  814. break;
  815. }
  816. return obj;
  817. }
  818. static void *irias_seq_start(struct seq_file *seq, loff_t *pos)
  819. {
  820. spin_lock_irq(&irias_objects->hb_spinlock);
  821. return *pos ? irias_seq_idx(*pos - 1) : SEQ_START_TOKEN;
  822. }
  823. static void *irias_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  824. {
  825. ++*pos;
  826. return (v == SEQ_START_TOKEN)
  827. ? (void *) hashbin_get_first(irias_objects)
  828. : (void *) hashbin_get_next(irias_objects);
  829. }
  830. static void irias_seq_stop(struct seq_file *seq, void *v)
  831. {
  832. spin_unlock_irq(&irias_objects->hb_spinlock);
  833. }
  834. static int irias_seq_show(struct seq_file *seq, void *v)
  835. {
  836. if (v == SEQ_START_TOKEN)
  837. seq_puts(seq, "LM-IAS Objects:\n");
  838. else {
  839. struct ias_object *obj = v;
  840. struct ias_attrib *attrib;
  841. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -EINVAL;);
  842. seq_printf(seq, "name: %s, id=%d\n",
  843. obj->name, obj->id);
  844. /* Careful for priority inversions here !
  845. * All other uses of attrib spinlock are independent of
  846. * the object spinlock, so we are safe. Jean II */
  847. spin_lock(&obj->attribs->hb_spinlock);
  848. /* List all attributes for this object */
  849. for (attrib = (struct ias_attrib *) hashbin_get_first(obj->attribs);
  850. attrib != NULL;
  851. attrib = (struct ias_attrib *) hashbin_get_next(obj->attribs)) {
  852. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC,
  853. goto outloop; );
  854. seq_printf(seq, " - Attribute name: \"%s\", ",
  855. attrib->name);
  856. seq_printf(seq, "value[%s]: ",
  857. ias_value_types[attrib->value->type]);
  858. switch (attrib->value->type) {
  859. case IAS_INTEGER:
  860. seq_printf(seq, "%d\n",
  861. attrib->value->t.integer);
  862. break;
  863. case IAS_STRING:
  864. seq_printf(seq, "\"%s\"\n",
  865. attrib->value->t.string);
  866. break;
  867. case IAS_OCT_SEQ:
  868. seq_printf(seq, "octet sequence (%d bytes)\n",
  869. attrib->value->len);
  870. break;
  871. case IAS_MISSING:
  872. seq_puts(seq, "missing\n");
  873. break;
  874. default:
  875. seq_printf(seq, "type %d?\n",
  876. attrib->value->type);
  877. }
  878. seq_putc(seq, '\n');
  879. }
  880. IRDA_ASSERT_LABEL(outloop:)
  881. spin_unlock(&obj->attribs->hb_spinlock);
  882. }
  883. return 0;
  884. }
  885. static const struct seq_operations irias_seq_ops = {
  886. .start = irias_seq_start,
  887. .next = irias_seq_next,
  888. .stop = irias_seq_stop,
  889. .show = irias_seq_show,
  890. };
  891. static int irias_seq_open(struct inode *inode, struct file *file)
  892. {
  893. IRDA_ASSERT( irias_objects != NULL, return -EINVAL;);
  894. return seq_open(file, &irias_seq_ops);
  895. }
  896. const struct file_operations irias_seq_fops = {
  897. .owner = THIS_MODULE,
  898. .open = irias_seq_open,
  899. .read = seq_read,
  900. .llseek = seq_lseek,
  901. .release = seq_release,
  902. };
  903. #endif /* PROC_FS */