iriap.c 26 KB

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