iriap.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  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. return;
  574. }
  575. /*
  576. * Function iriap_send_ack (void)
  577. *
  578. * Currently not used
  579. *
  580. */
  581. void iriap_send_ack(struct iriap_cb *self)
  582. {
  583. struct sk_buff *tx_skb;
  584. __u8 *frame;
  585. IRDA_DEBUG(2, "%s()\n", __func__);
  586. IRDA_ASSERT(self != NULL, return;);
  587. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  588. tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
  589. if (!tx_skb)
  590. return;
  591. /* Reserve space for MUX and LAP header */
  592. skb_reserve(tx_skb, self->max_header_size);
  593. skb_put(tx_skb, 1);
  594. frame = tx_skb->data;
  595. /* Build frame */
  596. frame[0] = IAP_LST | IAP_ACK | self->operation;
  597. irlmp_data_request(self->lsap, tx_skb);
  598. }
  599. void iriap_connect_request(struct iriap_cb *self)
  600. {
  601. int ret;
  602. IRDA_ASSERT(self != NULL, return;);
  603. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  604. ret = irlmp_connect_request(self->lsap, LSAP_IAS,
  605. self->saddr, self->daddr,
  606. NULL, NULL);
  607. if (ret < 0) {
  608. IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
  609. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  610. }
  611. }
  612. /*
  613. * Function iriap_connect_confirm (handle, skb)
  614. *
  615. * LSAP connection confirmed!
  616. *
  617. */
  618. static void iriap_connect_confirm(void *instance, void *sap,
  619. struct qos_info *qos, __u32 max_seg_size,
  620. __u8 max_header_size,
  621. struct sk_buff *skb)
  622. {
  623. struct iriap_cb *self;
  624. self = (struct iriap_cb *) instance;
  625. IRDA_ASSERT(self != NULL, return;);
  626. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  627. IRDA_ASSERT(skb != NULL, return;);
  628. self->max_data_size = max_seg_size;
  629. self->max_header_size = max_header_size;
  630. del_timer(&self->watchdog_timer);
  631. iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
  632. /* Drop reference count - see state_s_make_call(). */
  633. dev_kfree_skb(skb);
  634. }
  635. /*
  636. * Function iriap_connect_indication ( handle, skb)
  637. *
  638. * Remote LM-IAS is requesting connection
  639. *
  640. */
  641. static void iriap_connect_indication(void *instance, void *sap,
  642. struct qos_info *qos, __u32 max_seg_size,
  643. __u8 max_header_size,
  644. struct sk_buff *skb)
  645. {
  646. struct iriap_cb *self, *new;
  647. IRDA_DEBUG(1, "%s()\n", __func__);
  648. self = (struct iriap_cb *) instance;
  649. IRDA_ASSERT(skb != NULL, return;);
  650. IRDA_ASSERT(self != NULL, goto out;);
  651. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  652. /* Start new server */
  653. new = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
  654. if (!new) {
  655. IRDA_DEBUG(0, "%s(), open failed\n", __func__);
  656. goto out;
  657. }
  658. /* Now attach up the new "socket" */
  659. new->lsap = irlmp_dup(self->lsap, new);
  660. if (!new->lsap) {
  661. IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
  662. goto out;
  663. }
  664. new->max_data_size = max_seg_size;
  665. new->max_header_size = max_header_size;
  666. /* Clean up the original one to keep it in listen state */
  667. irlmp_listen(self->lsap);
  668. iriap_do_server_event(new, IAP_LM_CONNECT_INDICATION, skb);
  669. out:
  670. /* Drop reference count - see state_r_disconnect(). */
  671. dev_kfree_skb(skb);
  672. }
  673. /*
  674. * Function iriap_data_indication (handle, skb)
  675. *
  676. * Receives data from connection identified by handle from IrLMP
  677. *
  678. */
  679. static int iriap_data_indication(void *instance, void *sap,
  680. struct sk_buff *skb)
  681. {
  682. struct iriap_cb *self;
  683. __u8 *frame;
  684. __u8 opcode;
  685. IRDA_DEBUG(3, "%s()\n", __func__);
  686. self = (struct iriap_cb *) instance;
  687. IRDA_ASSERT(skb != NULL, return 0;);
  688. IRDA_ASSERT(self != NULL, goto out;);
  689. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  690. frame = skb->data;
  691. if (self->mode == IAS_SERVER) {
  692. /* Call server */
  693. IRDA_DEBUG(4, "%s(), Calling server!\n", __func__);
  694. iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
  695. goto out;
  696. }
  697. opcode = frame[0];
  698. if (~opcode & IAP_LST) {
  699. IRDA_WARNING("%s:, IrIAS multiframe commands or "
  700. "results is not implemented yet!\n",
  701. __func__);
  702. goto out;
  703. }
  704. /* Check for ack frames since they don't contain any data */
  705. if (opcode & IAP_ACK) {
  706. IRDA_DEBUG(0, "%s() Got ack frame!\n", __func__);
  707. goto out;
  708. }
  709. opcode &= ~IAP_LST; /* Mask away LST bit */
  710. switch (opcode) {
  711. case GET_INFO_BASE:
  712. IRDA_DEBUG(0, "IrLMP GetInfoBaseDetails not implemented!\n");
  713. break;
  714. case GET_VALUE_BY_CLASS:
  715. iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
  716. switch (frame[1]) {
  717. case IAS_SUCCESS:
  718. iriap_getvaluebyclass_confirm(self, skb);
  719. break;
  720. case IAS_CLASS_UNKNOWN:
  721. IRDA_DEBUG(1, "%s(), No such class!\n", __func__);
  722. /* Finished, close connection! */
  723. iriap_disconnect_request(self);
  724. /*
  725. * Warning, the client might close us, so remember
  726. * no to use self anymore after calling confirm
  727. */
  728. if (self->confirm)
  729. self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
  730. self->priv);
  731. break;
  732. case IAS_ATTRIB_UNKNOWN:
  733. IRDA_DEBUG(1, "%s(), No such attribute!\n", __func__);
  734. /* Finished, close connection! */
  735. iriap_disconnect_request(self);
  736. /*
  737. * Warning, the client might close us, so remember
  738. * no to use self anymore after calling confirm
  739. */
  740. if (self->confirm)
  741. self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
  742. self->priv);
  743. break;
  744. }
  745. break;
  746. default:
  747. IRDA_DEBUG(0, "%s(), Unknown op-code: %02x\n", __func__,
  748. opcode);
  749. break;
  750. }
  751. out:
  752. /* Cleanup - sub-calls will have done skb_get() as needed. */
  753. dev_kfree_skb(skb);
  754. return 0;
  755. }
  756. /*
  757. * Function iriap_call_indication (self, skb)
  758. *
  759. * Received call to server from peer LM-IAS
  760. *
  761. */
  762. void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
  763. {
  764. __u8 *fp;
  765. __u8 opcode;
  766. IRDA_DEBUG(4, "%s()\n", __func__);
  767. IRDA_ASSERT(self != NULL, return;);
  768. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  769. IRDA_ASSERT(skb != NULL, return;);
  770. fp = skb->data;
  771. opcode = fp[0];
  772. if (~opcode & 0x80) {
  773. IRDA_WARNING("%s: IrIAS multiframe commands or results "
  774. "is not implemented yet!\n", __func__);
  775. return;
  776. }
  777. opcode &= 0x7f; /* Mask away LST bit */
  778. switch (opcode) {
  779. case GET_INFO_BASE:
  780. IRDA_WARNING("%s: GetInfoBaseDetails not implemented yet!\n",
  781. __func__);
  782. break;
  783. case GET_VALUE_BY_CLASS:
  784. iriap_getvaluebyclass_indication(self, skb);
  785. break;
  786. }
  787. /* skb will be cleaned up in iriap_data_indication */
  788. }
  789. /*
  790. * Function iriap_watchdog_timer_expired (data)
  791. *
  792. * Query has taken too long time, so abort
  793. *
  794. */
  795. static void iriap_watchdog_timer_expired(void *data)
  796. {
  797. struct iriap_cb *self = (struct iriap_cb *) data;
  798. IRDA_ASSERT(self != NULL, return;);
  799. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  800. /* iriap_close(self); */
  801. }
  802. #ifdef CONFIG_PROC_FS
  803. static const char *const ias_value_types[] = {
  804. "IAS_MISSING",
  805. "IAS_INTEGER",
  806. "IAS_OCT_SEQ",
  807. "IAS_STRING"
  808. };
  809. static inline struct ias_object *irias_seq_idx(loff_t pos)
  810. {
  811. struct ias_object *obj;
  812. for (obj = (struct ias_object *) hashbin_get_first(irias_objects);
  813. obj; obj = (struct ias_object *) hashbin_get_next(irias_objects)) {
  814. if (pos-- == 0)
  815. break;
  816. }
  817. return obj;
  818. }
  819. static void *irias_seq_start(struct seq_file *seq, loff_t *pos)
  820. {
  821. spin_lock_irq(&irias_objects->hb_spinlock);
  822. return *pos ? irias_seq_idx(*pos - 1) : SEQ_START_TOKEN;
  823. }
  824. static void *irias_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  825. {
  826. ++*pos;
  827. return (v == SEQ_START_TOKEN)
  828. ? (void *) hashbin_get_first(irias_objects)
  829. : (void *) hashbin_get_next(irias_objects);
  830. }
  831. static void irias_seq_stop(struct seq_file *seq, void *v)
  832. {
  833. spin_unlock_irq(&irias_objects->hb_spinlock);
  834. }
  835. static int irias_seq_show(struct seq_file *seq, void *v)
  836. {
  837. if (v == SEQ_START_TOKEN)
  838. seq_puts(seq, "LM-IAS Objects:\n");
  839. else {
  840. struct ias_object *obj = v;
  841. struct ias_attrib *attrib;
  842. IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -EINVAL;);
  843. seq_printf(seq, "name: %s, id=%d\n",
  844. obj->name, obj->id);
  845. /* Careful for priority inversions here !
  846. * All other uses of attrib spinlock are independent of
  847. * the object spinlock, so we are safe. Jean II */
  848. spin_lock(&obj->attribs->hb_spinlock);
  849. /* List all attributes for this object */
  850. for (attrib = (struct ias_attrib *) hashbin_get_first(obj->attribs);
  851. attrib != NULL;
  852. attrib = (struct ias_attrib *) hashbin_get_next(obj->attribs)) {
  853. IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC,
  854. goto outloop; );
  855. seq_printf(seq, " - Attribute name: \"%s\", ",
  856. attrib->name);
  857. seq_printf(seq, "value[%s]: ",
  858. ias_value_types[attrib->value->type]);
  859. switch (attrib->value->type) {
  860. case IAS_INTEGER:
  861. seq_printf(seq, "%d\n",
  862. attrib->value->t.integer);
  863. break;
  864. case IAS_STRING:
  865. seq_printf(seq, "\"%s\"\n",
  866. attrib->value->t.string);
  867. break;
  868. case IAS_OCT_SEQ:
  869. seq_printf(seq, "octet sequence (%d bytes)\n",
  870. attrib->value->len);
  871. break;
  872. case IAS_MISSING:
  873. seq_puts(seq, "missing\n");
  874. break;
  875. default:
  876. seq_printf(seq, "type %d?\n",
  877. attrib->value->type);
  878. }
  879. seq_putc(seq, '\n');
  880. }
  881. IRDA_ASSERT_LABEL(outloop:)
  882. spin_unlock(&obj->attribs->hb_spinlock);
  883. }
  884. return 0;
  885. }
  886. static const struct seq_operations irias_seq_ops = {
  887. .start = irias_seq_start,
  888. .next = irias_seq_next,
  889. .stop = irias_seq_stop,
  890. .show = irias_seq_show,
  891. };
  892. static int irias_seq_open(struct inode *inode, struct file *file)
  893. {
  894. IRDA_ASSERT( irias_objects != NULL, return -EINVAL;);
  895. return seq_open(file, &irias_seq_ops);
  896. }
  897. const struct file_operations irias_seq_fops = {
  898. .owner = THIS_MODULE,
  899. .open = irias_seq_open,
  900. .read = seq_read,
  901. .llseek = seq_lseek,
  902. .release = seq_release,
  903. };
  904. #endif /* PROC_FS */