iriap.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. /* FIXME: This one should go in irlmp.c */
  43. static const char *const ias_charset_types[] __maybe_unused = {
  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. static hashbin_t *iriap = NULL;
  57. static void *service_handle;
  58. static void __iriap_close(struct iriap_cb *self);
  59. static int iriap_register_lsap(struct iriap_cb *self, __u8 slsap_sel, int mode);
  60. static void iriap_disconnect_indication(void *instance, void *sap,
  61. LM_REASON reason, struct sk_buff *skb);
  62. static void iriap_connect_indication(void *instance, void *sap,
  63. struct qos_info *qos, __u32 max_sdu_size,
  64. __u8 max_header_size,
  65. struct sk_buff *skb);
  66. static void iriap_connect_confirm(void *instance, void *sap,
  67. struct qos_info *qos,
  68. __u32 max_sdu_size, __u8 max_header_size,
  69. struct sk_buff *skb);
  70. static int iriap_data_indication(void *instance, void *sap,
  71. struct sk_buff *skb);
  72. static void iriap_watchdog_timer_expired(void *data);
  73. static inline void iriap_start_watchdog_timer(struct iriap_cb *self,
  74. int timeout)
  75. {
  76. irda_start_timer(&self->watchdog_timer, timeout, self,
  77. iriap_watchdog_timer_expired);
  78. }
  79. static struct lock_class_key irias_objects_key;
  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. net_warn_ratelimited("%s: Can't allocate irias_objects hashbin!\n",
  100. __func__);
  101. hashbin_delete(iriap, NULL);
  102. return -ENOMEM;
  103. }
  104. lockdep_set_class_and_name(&irias_objects->hb_spinlock, &irias_objects_key,
  105. "irias_objects");
  106. /*
  107. * Register some default services for IrLMP
  108. */
  109. hints = irlmp_service_to_hint(S_COMPUTER);
  110. service_handle = irlmp_register_service(hints);
  111. /* Register the Device object with LM-IAS */
  112. obj = irias_new_object("Device", IAS_DEVICE_ID);
  113. irias_add_string_attrib(obj, "DeviceName", "Linux", IAS_KERNEL_ATTR);
  114. oct_seq[0] = 0x01; /* Version 1 */
  115. oct_seq[1] = 0x00; /* IAS support bits */
  116. oct_seq[2] = 0x00; /* LM-MUX support bits */
  117. #ifdef CONFIG_IRDA_ULTRA
  118. oct_seq[2] |= 0x04; /* Connectionless Data support */
  119. #endif
  120. irias_add_octseq_attrib(obj, "IrLMPSupport", oct_seq, 3,
  121. IAS_KERNEL_ATTR);
  122. irias_insert_object(obj);
  123. /*
  124. * Register server support with IrLMP so we can accept incoming
  125. * connections
  126. */
  127. server = iriap_open(LSAP_IAS, IAS_SERVER, NULL, NULL);
  128. if (!server) {
  129. pr_debug("%s(), unable to open server\n", __func__);
  130. return -1;
  131. }
  132. iriap_register_lsap(server, LSAP_IAS, IAS_SERVER);
  133. return 0;
  134. }
  135. /*
  136. * Function iriap_cleanup (void)
  137. *
  138. * Initializes the IrIAP layer, called by the module cleanup code in
  139. * irmod.c
  140. */
  141. void iriap_cleanup(void)
  142. {
  143. irlmp_unregister_service(service_handle);
  144. hashbin_delete(iriap, (FREE_FUNC) __iriap_close);
  145. hashbin_delete(irias_objects, (FREE_FUNC) __irias_delete_object);
  146. }
  147. /*
  148. * Function iriap_open (void)
  149. *
  150. * Opens an instance of the IrIAP layer, and registers with IrLMP
  151. */
  152. struct iriap_cb *iriap_open(__u8 slsap_sel, int mode, void *priv,
  153. CONFIRM_CALLBACK callback)
  154. {
  155. struct iriap_cb *self;
  156. self = kzalloc(sizeof(*self), GFP_ATOMIC);
  157. if (!self)
  158. return NULL;
  159. /*
  160. * Initialize instance
  161. */
  162. self->magic = IAS_MAGIC;
  163. self->mode = mode;
  164. if (mode == IAS_CLIENT) {
  165. if (iriap_register_lsap(self, slsap_sel, mode)) {
  166. kfree(self);
  167. return NULL;
  168. }
  169. }
  170. self->confirm = callback;
  171. self->priv = priv;
  172. /* iriap_getvaluebyclass_request() will construct packets before
  173. * we connect, so this must have a sane value... Jean II */
  174. self->max_header_size = LMP_MAX_HEADER;
  175. init_timer(&self->watchdog_timer);
  176. hashbin_insert(iriap, (irda_queue_t *) self, (long) self, NULL);
  177. /* Initialize state machines */
  178. iriap_next_client_state(self, S_DISCONNECT);
  179. iriap_next_call_state(self, S_MAKE_CALL);
  180. iriap_next_server_state(self, R_DISCONNECT);
  181. iriap_next_r_connect_state(self, R_WAITING);
  182. return self;
  183. }
  184. EXPORT_SYMBOL(iriap_open);
  185. /*
  186. * Function __iriap_close (self)
  187. *
  188. * Removes (deallocates) the IrIAP instance
  189. *
  190. */
  191. static void __iriap_close(struct iriap_cb *self)
  192. {
  193. IRDA_ASSERT(self != NULL, return;);
  194. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  195. del_timer(&self->watchdog_timer);
  196. if (self->request_skb)
  197. dev_kfree_skb(self->request_skb);
  198. self->magic = 0;
  199. kfree(self);
  200. }
  201. /*
  202. * Function iriap_close (void)
  203. *
  204. * Closes IrIAP and deregisters with IrLMP
  205. */
  206. void iriap_close(struct iriap_cb *self)
  207. {
  208. struct iriap_cb *entry;
  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_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. net_err_ratelimited("%s: Unable to allocated LSAP!\n",
  236. __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. pr_debug("%s(), reason=%s [%d]\n", __func__,
  254. irlmp_reason_str(reason), reason);
  255. self = instance;
  256. IRDA_ASSERT(self != NULL, return;);
  257. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  258. IRDA_ASSERT(iriap != NULL, return;);
  259. del_timer(&self->watchdog_timer);
  260. /* Not needed */
  261. if (skb)
  262. dev_kfree_skb(skb);
  263. if (self->mode == IAS_CLIENT) {
  264. pr_debug("%s(), disconnect as client\n", __func__);
  265. iriap_do_client_event(self, IAP_LM_DISCONNECT_INDICATION,
  266. NULL);
  267. /*
  268. * Inform service user that the request failed by sending
  269. * it a NULL value. Warning, the client might close us, so
  270. * remember no to use self anymore after calling confirm
  271. */
  272. if (self->confirm)
  273. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  274. } else {
  275. pr_debug("%s(), disconnect as server\n", __func__);
  276. iriap_do_server_event(self, IAP_LM_DISCONNECT_INDICATION,
  277. NULL);
  278. iriap_close(self);
  279. }
  280. }
  281. /*
  282. * Function iriap_disconnect_request (handle)
  283. */
  284. static void iriap_disconnect_request(struct iriap_cb *self)
  285. {
  286. struct sk_buff *tx_skb;
  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. pr_debug("%s(), Could not allocate an sk_buff of length %d\n",
  292. __func__, 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 = get_unaligned_be16(fp + n);
  376. n += 2;
  377. pr_debug("%s(), len=%d\n", __func__, len);
  378. /* Get object ID, MSB first */
  379. obj_id = get_unaligned_be16(fp + n);
  380. n += 2;
  381. type = fp[n++];
  382. pr_debug("%s(), Value type = %d\n", __func__, type);
  383. switch (type) {
  384. case IAS_INTEGER:
  385. memcpy(&tmp_cpu32, fp+n, 4); n += 4;
  386. be32_to_cpus(&tmp_cpu32);
  387. value = irias_new_integer_value(tmp_cpu32);
  388. /* Legal values restricted to 0x01-0x6f, page 15 irttp */
  389. pr_debug("%s(), lsap=%d\n", __func__, value->t.integer);
  390. break;
  391. case IAS_STRING:
  392. charset = fp[n++];
  393. switch (charset) {
  394. case CS_ASCII:
  395. break;
  396. /* case CS_ISO_8859_1: */
  397. /* case CS_ISO_8859_2: */
  398. /* case CS_ISO_8859_3: */
  399. /* case CS_ISO_8859_4: */
  400. /* case CS_ISO_8859_5: */
  401. /* case CS_ISO_8859_6: */
  402. /* case CS_ISO_8859_7: */
  403. /* case CS_ISO_8859_8: */
  404. /* case CS_ISO_8859_9: */
  405. /* case CS_UNICODE: */
  406. default:
  407. pr_debug("%s(), charset [%d] %s, not supported\n",
  408. __func__, charset,
  409. charset < ARRAY_SIZE(ias_charset_types) ?
  410. ias_charset_types[charset] :
  411. "(unknown)");
  412. /* Aborting, close connection! */
  413. iriap_disconnect_request(self);
  414. return;
  415. /* break; */
  416. }
  417. value_len = fp[n++];
  418. pr_debug("%s(), strlen=%d\n", __func__, value_len);
  419. /* Make sure the string is null-terminated */
  420. if (n + value_len < skb->len)
  421. fp[n + value_len] = 0x00;
  422. pr_debug("Got string %s\n", fp+n);
  423. /* Will truncate to IAS_MAX_STRING bytes */
  424. value = irias_new_string_value(fp+n);
  425. break;
  426. case IAS_OCT_SEQ:
  427. value_len = get_unaligned_be16(fp + n);
  428. n += 2;
  429. /* Will truncate to IAS_MAX_OCTET_STRING bytes */
  430. value = irias_new_octseq_value(fp+n, value_len);
  431. break;
  432. default:
  433. value = irias_new_missing_value();
  434. break;
  435. }
  436. /* Finished, close connection! */
  437. iriap_disconnect_request(self);
  438. /* Warning, the client might close us, so remember no to use self
  439. * anymore after calling confirm
  440. */
  441. if (self->confirm)
  442. self->confirm(IAS_SUCCESS, obj_id, value, self->priv);
  443. else {
  444. pr_debug("%s(), missing handler!\n", __func__);
  445. irias_delete_value(value);
  446. }
  447. }
  448. /*
  449. * Function iriap_getvaluebyclass_response ()
  450. *
  451. * Send answer back to remote LM-IAS
  452. *
  453. */
  454. static void iriap_getvaluebyclass_response(struct iriap_cb *self,
  455. __u16 obj_id,
  456. __u8 ret_code,
  457. struct ias_value *value)
  458. {
  459. struct sk_buff *tx_skb;
  460. int n;
  461. __be32 tmp_be32;
  462. __be16 tmp_be16;
  463. __u8 *fp;
  464. IRDA_ASSERT(self != NULL, return;);
  465. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  466. IRDA_ASSERT(value != NULL, return;);
  467. IRDA_ASSERT(value->len <= 1024, return;);
  468. /* Initialize variables */
  469. n = 0;
  470. /*
  471. * We must adjust the size of the response after the length of the
  472. * value. We add 32 bytes because of the 6 bytes for the frame and
  473. * max 5 bytes for the value coding.
  474. */
  475. tx_skb = alloc_skb(value->len + self->max_header_size + 32,
  476. GFP_ATOMIC);
  477. if (!tx_skb)
  478. return;
  479. /* Reserve space for MUX and LAP header */
  480. skb_reserve(tx_skb, self->max_header_size);
  481. skb_put(tx_skb, 6);
  482. fp = tx_skb->data;
  483. /* Build frame */
  484. fp[n++] = GET_VALUE_BY_CLASS | IAP_LST;
  485. fp[n++] = ret_code;
  486. /* Insert list length (MSB first) */
  487. tmp_be16 = htons(0x0001);
  488. memcpy(fp+n, &tmp_be16, 2); n += 2;
  489. /* Insert object identifier ( MSB first) */
  490. tmp_be16 = cpu_to_be16(obj_id);
  491. memcpy(fp+n, &tmp_be16, 2); n += 2;
  492. switch (value->type) {
  493. case IAS_STRING:
  494. skb_put(tx_skb, 3 + value->len);
  495. fp[n++] = value->type;
  496. fp[n++] = 0; /* ASCII */
  497. fp[n++] = (__u8) value->len;
  498. memcpy(fp+n, value->t.string, value->len); n+=value->len;
  499. break;
  500. case IAS_INTEGER:
  501. skb_put(tx_skb, 5);
  502. fp[n++] = value->type;
  503. tmp_be32 = cpu_to_be32(value->t.integer);
  504. memcpy(fp+n, &tmp_be32, 4); n += 4;
  505. break;
  506. case IAS_OCT_SEQ:
  507. skb_put(tx_skb, 3 + value->len);
  508. fp[n++] = value->type;
  509. tmp_be16 = cpu_to_be16(value->len);
  510. memcpy(fp+n, &tmp_be16, 2); n += 2;
  511. memcpy(fp+n, value->t.oct_seq, value->len); n+=value->len;
  512. break;
  513. case IAS_MISSING:
  514. pr_debug("%s: sending IAS_MISSING\n", __func__);
  515. skb_put(tx_skb, 1);
  516. fp[n++] = value->type;
  517. break;
  518. default:
  519. pr_debug("%s(), type not implemented!\n", __func__);
  520. break;
  521. }
  522. iriap_do_r_connect_event(self, IAP_CALL_RESPONSE, tx_skb);
  523. /* Drop reference count - see state_r_execute(). */
  524. dev_kfree_skb(tx_skb);
  525. }
  526. /*
  527. * Function iriap_getvaluebyclass_indication (self, skb)
  528. *
  529. * getvaluebyclass is requested from peer LM-IAS
  530. *
  531. */
  532. static void iriap_getvaluebyclass_indication(struct iriap_cb *self,
  533. struct sk_buff *skb)
  534. {
  535. struct ias_object *obj;
  536. struct ias_attrib *attrib;
  537. int name_len;
  538. int attr_len;
  539. char name[IAS_MAX_CLASSNAME + 1]; /* 60 bytes */
  540. char attr[IAS_MAX_ATTRIBNAME + 1]; /* 60 bytes */
  541. __u8 *fp;
  542. int n;
  543. IRDA_ASSERT(self != NULL, return;);
  544. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  545. IRDA_ASSERT(skb != NULL, return;);
  546. fp = skb->data;
  547. n = 1;
  548. name_len = fp[n++];
  549. IRDA_ASSERT(name_len < IAS_MAX_CLASSNAME + 1, return;);
  550. memcpy(name, fp+n, name_len); n+=name_len;
  551. name[name_len] = '\0';
  552. attr_len = fp[n++];
  553. IRDA_ASSERT(attr_len < IAS_MAX_ATTRIBNAME + 1, return;);
  554. memcpy(attr, fp+n, attr_len); n+=attr_len;
  555. attr[attr_len] = '\0';
  556. pr_debug("LM-IAS: Looking up %s: %s\n", name, attr);
  557. obj = irias_find_object(name);
  558. if (obj == NULL) {
  559. pr_debug("LM-IAS: Object %s not found\n", name);
  560. iriap_getvaluebyclass_response(self, 0x1235, IAS_CLASS_UNKNOWN,
  561. &irias_missing);
  562. return;
  563. }
  564. pr_debug("LM-IAS: found %s, id=%d\n", obj->name, obj->id);
  565. attrib = irias_find_attrib(obj, attr);
  566. if (attrib == NULL) {
  567. pr_debug("LM-IAS: Attribute %s not found\n", attr);
  568. iriap_getvaluebyclass_response(self, obj->id,
  569. IAS_ATTRIB_UNKNOWN,
  570. &irias_missing);
  571. return;
  572. }
  573. /* We have a match; send the value. */
  574. iriap_getvaluebyclass_response(self, obj->id, IAS_SUCCESS,
  575. attrib->value);
  576. }
  577. /*
  578. * Function iriap_send_ack (void)
  579. *
  580. * Currently not used
  581. *
  582. */
  583. void iriap_send_ack(struct iriap_cb *self)
  584. {
  585. struct sk_buff *tx_skb;
  586. __u8 *frame;
  587. IRDA_ASSERT(self != NULL, return;);
  588. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  589. tx_skb = alloc_skb(LMP_MAX_HEADER + 1, GFP_ATOMIC);
  590. if (!tx_skb)
  591. return;
  592. /* Reserve space for MUX and LAP header */
  593. skb_reserve(tx_skb, self->max_header_size);
  594. skb_put(tx_skb, 1);
  595. frame = tx_skb->data;
  596. /* Build frame */
  597. frame[0] = IAP_LST | IAP_ACK | self->operation;
  598. irlmp_data_request(self->lsap, tx_skb);
  599. }
  600. void iriap_connect_request(struct iriap_cb *self)
  601. {
  602. int ret;
  603. IRDA_ASSERT(self != NULL, return;);
  604. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  605. ret = irlmp_connect_request(self->lsap, LSAP_IAS,
  606. self->saddr, self->daddr,
  607. NULL, NULL);
  608. if (ret < 0) {
  609. pr_debug("%s(), connect failed!\n", __func__);
  610. self->confirm(IAS_DISCONNECT, 0, NULL, self->priv);
  611. }
  612. }
  613. /*
  614. * Function iriap_connect_confirm (handle, skb)
  615. *
  616. * LSAP connection confirmed!
  617. *
  618. */
  619. static void iriap_connect_confirm(void *instance, void *sap,
  620. struct qos_info *qos, __u32 max_seg_size,
  621. __u8 max_header_size,
  622. struct sk_buff *skb)
  623. {
  624. struct iriap_cb *self;
  625. self = instance;
  626. IRDA_ASSERT(self != NULL, return;);
  627. IRDA_ASSERT(self->magic == IAS_MAGIC, return;);
  628. IRDA_ASSERT(skb != NULL, return;);
  629. self->max_data_size = max_seg_size;
  630. self->max_header_size = max_header_size;
  631. del_timer(&self->watchdog_timer);
  632. iriap_do_client_event(self, IAP_LM_CONNECT_CONFIRM, skb);
  633. /* Drop reference count - see state_s_make_call(). */
  634. dev_kfree_skb(skb);
  635. }
  636. /*
  637. * Function iriap_connect_indication ( handle, skb)
  638. *
  639. * Remote LM-IAS is requesting connection
  640. *
  641. */
  642. static void iriap_connect_indication(void *instance, void *sap,
  643. struct qos_info *qos, __u32 max_seg_size,
  644. __u8 max_header_size,
  645. struct sk_buff *skb)
  646. {
  647. struct iriap_cb *self, *new;
  648. self = 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. pr_debug("%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. pr_debug("%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. self = instance;
  686. IRDA_ASSERT(skb != NULL, return 0;);
  687. IRDA_ASSERT(self != NULL, goto out;);
  688. IRDA_ASSERT(self->magic == IAS_MAGIC, goto out;);
  689. frame = skb->data;
  690. if (self->mode == IAS_SERVER) {
  691. /* Call server */
  692. pr_debug("%s(), Calling server!\n", __func__);
  693. iriap_do_r_connect_event(self, IAP_RECV_F_LST, skb);
  694. goto out;
  695. }
  696. opcode = frame[0];
  697. if (~opcode & IAP_LST) {
  698. net_warn_ratelimited("%s:, IrIAS multiframe commands or results is not implemented yet!\n",
  699. __func__);
  700. goto out;
  701. }
  702. /* Check for ack frames since they don't contain any data */
  703. if (opcode & IAP_ACK) {
  704. pr_debug("%s() Got ack frame!\n", __func__);
  705. goto out;
  706. }
  707. opcode &= ~IAP_LST; /* Mask away LST bit */
  708. switch (opcode) {
  709. case GET_INFO_BASE:
  710. pr_debug("IrLMP GetInfoBaseDetails not implemented!\n");
  711. break;
  712. case GET_VALUE_BY_CLASS:
  713. iriap_do_call_event(self, IAP_RECV_F_LST, NULL);
  714. switch (frame[1]) {
  715. case IAS_SUCCESS:
  716. iriap_getvaluebyclass_confirm(self, skb);
  717. break;
  718. case IAS_CLASS_UNKNOWN:
  719. pr_debug("%s(), No such class!\n", __func__);
  720. /* Finished, close connection! */
  721. iriap_disconnect_request(self);
  722. /*
  723. * Warning, the client might close us, so remember
  724. * no to use self anymore after calling confirm
  725. */
  726. if (self->confirm)
  727. self->confirm(IAS_CLASS_UNKNOWN, 0, NULL,
  728. self->priv);
  729. break;
  730. case IAS_ATTRIB_UNKNOWN:
  731. pr_debug("%s(), No such attribute!\n", __func__);
  732. /* Finished, close connection! */
  733. iriap_disconnect_request(self);
  734. /*
  735. * Warning, the client might close us, so remember
  736. * no to use self anymore after calling confirm
  737. */
  738. if (self->confirm)
  739. self->confirm(IAS_ATTRIB_UNKNOWN, 0, NULL,
  740. self->priv);
  741. break;
  742. }
  743. break;
  744. default:
  745. pr_debug("%s(), Unknown op-code: %02x\n", __func__,
  746. opcode);
  747. break;
  748. }
  749. out:
  750. /* Cleanup - sub-calls will have done skb_get() as needed. */
  751. dev_kfree_skb(skb);
  752. return 0;
  753. }
  754. /*
  755. * Function iriap_call_indication (self, skb)
  756. *
  757. * Received call to server from peer LM-IAS
  758. *
  759. */
  760. void iriap_call_indication(struct iriap_cb *self, struct sk_buff *skb)
  761. {
  762. __u8 *fp;
  763. __u8 opcode;
  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. net_warn_ratelimited("%s: IrIAS multiframe commands or results is not implemented yet!\n",
  771. __func__);
  772. return;
  773. }
  774. opcode &= 0x7f; /* Mask away LST bit */
  775. switch (opcode) {
  776. case GET_INFO_BASE:
  777. net_warn_ratelimited("%s: GetInfoBaseDetails not implemented yet!\n",
  778. __func__);
  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 *const 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 */