iriap.c 26 KB

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