irlan_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*********************************************************************
  2. *
  3. * Filename: irlan_client.c
  4. * Version: 0.9
  5. * Description: IrDA LAN Access Protocol (IrLAN) Client
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 31 20:14:37 1997
  9. * Modified at: Tue Dec 14 15:47:02 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. * Sources: skeleton.c by Donald Becker <becker@CESDIS.gsfc.nasa.gov>
  12. * slip.c by Laurence Culhane, <loz@holmes.demon.co.uk>
  13. * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  14. *
  15. * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>,
  16. * All Rights Reserved.
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License as
  20. * published by the Free Software Foundation; either version 2 of
  21. * the License, or (at your option) any later version.
  22. *
  23. * Neither Dag Brattli nor University of Tromsø admit liability nor
  24. * provide warranty for any of this software. This material is
  25. * provided "AS-IS" and at no charge.
  26. *
  27. ********************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/if_arp.h>
  35. #include <linux/bitops.h>
  36. #include <net/arp.h>
  37. #include <asm/system.h>
  38. #include <asm/byteorder.h>
  39. #include <net/irda/irda.h>
  40. #include <net/irda/irttp.h>
  41. #include <net/irda/irlmp.h>
  42. #include <net/irda/irias_object.h>
  43. #include <net/irda/iriap.h>
  44. #include <net/irda/timer.h>
  45. #include <net/irda/irlan_common.h>
  46. #include <net/irda/irlan_event.h>
  47. #include <net/irda/irlan_eth.h>
  48. #include <net/irda/irlan_provider.h>
  49. #include <net/irda/irlan_client.h>
  50. #undef CONFIG_IRLAN_GRATUITOUS_ARP
  51. static void irlan_client_ctrl_disconnect_indication(void *instance, void *sap,
  52. LM_REASON reason,
  53. struct sk_buff *);
  54. static int irlan_client_ctrl_data_indication(void *instance, void *sap,
  55. struct sk_buff *skb);
  56. static void irlan_client_ctrl_connect_confirm(void *instance, void *sap,
  57. struct qos_info *qos,
  58. __u32 max_sdu_size,
  59. __u8 max_header_size,
  60. struct sk_buff *);
  61. static void irlan_check_response_param(struct irlan_cb *self, char *param,
  62. char *value, int val_len);
  63. static void irlan_client_open_ctrl_tsap(struct irlan_cb *self);
  64. static void irlan_client_kick_timer_expired(void *data)
  65. {
  66. struct irlan_cb *self = (struct irlan_cb *) data;
  67. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  68. IRDA_ASSERT(self != NULL, return;);
  69. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  70. /*
  71. * If we are in peer mode, the client may not have got the discovery
  72. * indication it needs to make progress. If the client is still in
  73. * IDLE state, we must kick it to, but only if the provider is not IDLE
  74. */
  75. if ((self->provider.access_type == ACCESS_PEER) &&
  76. (self->client.state == IRLAN_IDLE) &&
  77. (self->provider.state != IRLAN_IDLE)) {
  78. irlan_client_wakeup(self, self->saddr, self->daddr);
  79. }
  80. }
  81. static void irlan_client_start_kick_timer(struct irlan_cb *self, int timeout)
  82. {
  83. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  84. irda_start_timer(&self->client.kick_timer, timeout, (void *) self,
  85. irlan_client_kick_timer_expired);
  86. }
  87. /*
  88. * Function irlan_client_wakeup (self, saddr, daddr)
  89. *
  90. * Wake up client
  91. *
  92. */
  93. void irlan_client_wakeup(struct irlan_cb *self, __u32 saddr, __u32 daddr)
  94. {
  95. IRDA_DEBUG(1, "%s()\n", __FUNCTION__ );
  96. IRDA_ASSERT(self != NULL, return;);
  97. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  98. /*
  99. * Check if we are already awake, or if we are a provider in direct
  100. * mode (in that case we must leave the client idle
  101. */
  102. if ((self->client.state != IRLAN_IDLE) ||
  103. (self->provider.access_type == ACCESS_DIRECT))
  104. {
  105. IRDA_DEBUG(0, "%s(), already awake!\n", __FUNCTION__ );
  106. return;
  107. }
  108. /* Addresses may have changed! */
  109. self->saddr = saddr;
  110. self->daddr = daddr;
  111. if (self->disconnect_reason == LM_USER_REQUEST) {
  112. IRDA_DEBUG(0, "%s(), still stopped by user\n", __FUNCTION__ );
  113. return;
  114. }
  115. /* Open TSAPs */
  116. irlan_client_open_ctrl_tsap(self);
  117. irlan_open_data_tsap(self);
  118. irlan_do_client_event(self, IRLAN_DISCOVERY_INDICATION, NULL);
  119. /* Start kick timer */
  120. irlan_client_start_kick_timer(self, 2*HZ);
  121. }
  122. /*
  123. * Function irlan_discovery_indication (daddr)
  124. *
  125. * Remote device with IrLAN server support discovered
  126. *
  127. */
  128. void irlan_client_discovery_indication(discinfo_t *discovery,
  129. DISCOVERY_MODE mode,
  130. void *priv)
  131. {
  132. struct irlan_cb *self;
  133. __u32 saddr, daddr;
  134. IRDA_DEBUG(1, "%s()\n", __FUNCTION__ );
  135. IRDA_ASSERT(discovery != NULL, return;);
  136. /*
  137. * I didn't check it, but I bet that IrLAN suffer from the same
  138. * deficiency as IrComm and doesn't handle two instances
  139. * simultaneously connecting to each other.
  140. * Same workaround, drop passive discoveries.
  141. * Jean II */
  142. if(mode == DISCOVERY_PASSIVE)
  143. return;
  144. saddr = discovery->saddr;
  145. daddr = discovery->daddr;
  146. /* Find instance */
  147. rcu_read_lock();
  148. self = irlan_get_any();
  149. if (self) {
  150. IRDA_ASSERT(self->magic == IRLAN_MAGIC, goto out;);
  151. IRDA_DEBUG(1, "%s(), Found instance (%08x)!\n", __FUNCTION__ ,
  152. daddr);
  153. irlan_client_wakeup(self, saddr, daddr);
  154. }
  155. IRDA_ASSERT_LABEL(out:)
  156. rcu_read_unlock();
  157. }
  158. /*
  159. * Function irlan_client_data_indication (handle, skb)
  160. *
  161. * This function gets the data that is received on the control channel
  162. *
  163. */
  164. static int irlan_client_ctrl_data_indication(void *instance, void *sap,
  165. struct sk_buff *skb)
  166. {
  167. struct irlan_cb *self;
  168. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  169. self = (struct irlan_cb *) instance;
  170. IRDA_ASSERT(self != NULL, return -1;);
  171. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  172. IRDA_ASSERT(skb != NULL, return -1;);
  173. irlan_do_client_event(self, IRLAN_DATA_INDICATION, skb);
  174. /* Ready for a new command */
  175. IRDA_DEBUG(2, "%s(), clearing tx_busy\n", __FUNCTION__ );
  176. self->client.tx_busy = FALSE;
  177. /* Check if we have some queued commands waiting to be sent */
  178. irlan_run_ctrl_tx_queue(self);
  179. return 0;
  180. }
  181. static void irlan_client_ctrl_disconnect_indication(void *instance, void *sap,
  182. LM_REASON reason,
  183. struct sk_buff *userdata)
  184. {
  185. struct irlan_cb *self;
  186. struct tsap_cb *tsap;
  187. struct sk_buff *skb;
  188. IRDA_DEBUG(4, "%s(), reason=%d\n", __FUNCTION__ , reason);
  189. self = (struct irlan_cb *) instance;
  190. tsap = (struct tsap_cb *) sap;
  191. IRDA_ASSERT(self != NULL, return;);
  192. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  193. IRDA_ASSERT(tsap != NULL, return;);
  194. IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
  195. IRDA_ASSERT(tsap == self->client.tsap_ctrl, return;);
  196. /* Remove frames queued on the control channel */
  197. while ((skb = skb_dequeue(&self->client.txq)) != NULL) {
  198. dev_kfree_skb(skb);
  199. }
  200. self->client.tx_busy = FALSE;
  201. irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
  202. }
  203. /*
  204. * Function irlan_client_open_tsaps (self)
  205. *
  206. * Initialize callbacks and open IrTTP TSAPs
  207. *
  208. */
  209. static void irlan_client_open_ctrl_tsap(struct irlan_cb *self)
  210. {
  211. struct tsap_cb *tsap;
  212. notify_t notify;
  213. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  214. IRDA_ASSERT(self != NULL, return;);
  215. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  216. /* Check if already open */
  217. if (self->client.tsap_ctrl)
  218. return;
  219. irda_notify_init(&notify);
  220. /* Set up callbacks */
  221. notify.data_indication = irlan_client_ctrl_data_indication;
  222. notify.connect_confirm = irlan_client_ctrl_connect_confirm;
  223. notify.disconnect_indication = irlan_client_ctrl_disconnect_indication;
  224. notify.instance = self;
  225. strlcpy(notify.name, "IrLAN ctrl (c)", sizeof(notify.name));
  226. tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
  227. if (!tsap) {
  228. IRDA_DEBUG(2, "%s(), Got no tsap!\n", __FUNCTION__ );
  229. return;
  230. }
  231. self->client.tsap_ctrl = tsap;
  232. }
  233. /*
  234. * Function irlan_client_connect_confirm (handle, skb)
  235. *
  236. * Connection to peer IrLAN laye confirmed
  237. *
  238. */
  239. static void irlan_client_ctrl_connect_confirm(void *instance, void *sap,
  240. struct qos_info *qos,
  241. __u32 max_sdu_size,
  242. __u8 max_header_size,
  243. struct sk_buff *skb)
  244. {
  245. struct irlan_cb *self;
  246. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  247. self = (struct irlan_cb *) instance;
  248. IRDA_ASSERT(self != NULL, return;);
  249. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  250. self->client.max_sdu_size = max_sdu_size;
  251. self->client.max_header_size = max_header_size;
  252. /* TODO: we could set the MTU depending on the max_sdu_size */
  253. irlan_do_client_event(self, IRLAN_CONNECT_COMPLETE, NULL);
  254. }
  255. /*
  256. * Function print_ret_code (code)
  257. *
  258. * Print return code of request to peer IrLAN layer.
  259. *
  260. */
  261. static void print_ret_code(__u8 code)
  262. {
  263. switch(code) {
  264. case 0:
  265. printk(KERN_INFO "Success\n");
  266. break;
  267. case 1:
  268. IRDA_WARNING("IrLAN: Insufficient resources\n");
  269. break;
  270. case 2:
  271. IRDA_WARNING("IrLAN: Invalid command format\n");
  272. break;
  273. case 3:
  274. IRDA_WARNING("IrLAN: Command not supported\n");
  275. break;
  276. case 4:
  277. IRDA_WARNING("IrLAN: Parameter not supported\n");
  278. break;
  279. case 5:
  280. IRDA_WARNING("IrLAN: Value not supported\n");
  281. break;
  282. case 6:
  283. IRDA_WARNING("IrLAN: Not open\n");
  284. break;
  285. case 7:
  286. IRDA_WARNING("IrLAN: Authentication required\n");
  287. break;
  288. case 8:
  289. IRDA_WARNING("IrLAN: Invalid password\n");
  290. break;
  291. case 9:
  292. IRDA_WARNING("IrLAN: Protocol error\n");
  293. break;
  294. case 255:
  295. IRDA_WARNING("IrLAN: Asynchronous status\n");
  296. break;
  297. }
  298. }
  299. /*
  300. * Function irlan_client_parse_response (self, skb)
  301. *
  302. * Extract all parameters from received buffer, then feed them to
  303. * check_params for parsing
  304. */
  305. void irlan_client_parse_response(struct irlan_cb *self, struct sk_buff *skb)
  306. {
  307. __u8 *frame;
  308. __u8 *ptr;
  309. int count;
  310. int ret;
  311. __u16 val_len;
  312. int i;
  313. char *name;
  314. char *value;
  315. IRDA_ASSERT(skb != NULL, return;);
  316. IRDA_DEBUG(4, "%s() skb->len=%d\n", __FUNCTION__ , (int) skb->len);
  317. IRDA_ASSERT(self != NULL, return;);
  318. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  319. if (!skb) {
  320. IRDA_ERROR("%s(), Got NULL skb!\n", __FUNCTION__);
  321. return;
  322. }
  323. frame = skb->data;
  324. /*
  325. * Check return code and print it if not success
  326. */
  327. if (frame[0]) {
  328. print_ret_code(frame[0]);
  329. return;
  330. }
  331. name = kmalloc(255, GFP_ATOMIC);
  332. if (!name)
  333. return;
  334. value = kmalloc(1016, GFP_ATOMIC);
  335. if (!value) {
  336. kfree(name);
  337. return;
  338. }
  339. /* How many parameters? */
  340. count = frame[1];
  341. IRDA_DEBUG(4, "%s(), got %d parameters\n", __FUNCTION__ , count);
  342. ptr = frame+2;
  343. /* For all parameters */
  344. for (i=0; i<count;i++) {
  345. ret = irlan_extract_param(ptr, name, value, &val_len);
  346. if (ret < 0) {
  347. IRDA_DEBUG(2, "%s(), IrLAN, Error!\n", __FUNCTION__ );
  348. break;
  349. }
  350. ptr += ret;
  351. irlan_check_response_param(self, name, value, val_len);
  352. }
  353. /* Cleanup */
  354. kfree(name);
  355. kfree(value);
  356. }
  357. /*
  358. * Function irlan_check_response_param (self, param, value, val_len)
  359. *
  360. * Check which parameter is received and update local variables
  361. *
  362. */
  363. static void irlan_check_response_param(struct irlan_cb *self, char *param,
  364. char *value, int val_len)
  365. {
  366. __u16 tmp_cpu; /* Temporary value in host order */
  367. __u8 *bytes;
  368. int i;
  369. IRDA_DEBUG(4, "%s(), parm=%s\n", __FUNCTION__ , param);
  370. IRDA_ASSERT(self != NULL, return;);
  371. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  372. /* Media type */
  373. if (strcmp(param, "MEDIA") == 0) {
  374. if (strcmp(value, "802.3") == 0)
  375. self->media = MEDIA_802_3;
  376. else
  377. self->media = MEDIA_802_5;
  378. return;
  379. }
  380. if (strcmp(param, "FILTER_TYPE") == 0) {
  381. if (strcmp(value, "DIRECTED") == 0)
  382. self->client.filter_type |= IRLAN_DIRECTED;
  383. else if (strcmp(value, "FUNCTIONAL") == 0)
  384. self->client.filter_type |= IRLAN_FUNCTIONAL;
  385. else if (strcmp(value, "GROUP") == 0)
  386. self->client.filter_type |= IRLAN_GROUP;
  387. else if (strcmp(value, "MAC_FRAME") == 0)
  388. self->client.filter_type |= IRLAN_MAC_FRAME;
  389. else if (strcmp(value, "MULTICAST") == 0)
  390. self->client.filter_type |= IRLAN_MULTICAST;
  391. else if (strcmp(value, "BROADCAST") == 0)
  392. self->client.filter_type |= IRLAN_BROADCAST;
  393. else if (strcmp(value, "IPX_SOCKET") == 0)
  394. self->client.filter_type |= IRLAN_IPX_SOCKET;
  395. }
  396. if (strcmp(param, "ACCESS_TYPE") == 0) {
  397. if (strcmp(value, "DIRECT") == 0)
  398. self->client.access_type = ACCESS_DIRECT;
  399. else if (strcmp(value, "PEER") == 0)
  400. self->client.access_type = ACCESS_PEER;
  401. else if (strcmp(value, "HOSTED") == 0)
  402. self->client.access_type = ACCESS_HOSTED;
  403. else {
  404. IRDA_DEBUG(2, "%s(), unknown access type!\n", __FUNCTION__ );
  405. }
  406. }
  407. /* IRLAN version */
  408. if (strcmp(param, "IRLAN_VER") == 0) {
  409. IRDA_DEBUG(4, "IrLAN version %d.%d\n", (__u8) value[0],
  410. (__u8) value[1]);
  411. self->version[0] = value[0];
  412. self->version[1] = value[1];
  413. return;
  414. }
  415. /* Which remote TSAP to use for data channel */
  416. if (strcmp(param, "DATA_CHAN") == 0) {
  417. self->dtsap_sel_data = value[0];
  418. IRDA_DEBUG(4, "Data TSAP = %02x\n", self->dtsap_sel_data);
  419. return;
  420. }
  421. if (strcmp(param, "CON_ARB") == 0) {
  422. memcpy(&tmp_cpu, value, 2); /* Align value */
  423. le16_to_cpus(&tmp_cpu); /* Convert to host order */
  424. self->client.recv_arb_val = tmp_cpu;
  425. IRDA_DEBUG(2, "%s(), receive arb val=%d\n", __FUNCTION__ ,
  426. self->client.recv_arb_val);
  427. }
  428. if (strcmp(param, "MAX_FRAME") == 0) {
  429. memcpy(&tmp_cpu, value, 2); /* Align value */
  430. le16_to_cpus(&tmp_cpu); /* Convert to host order */
  431. self->client.max_frame = tmp_cpu;
  432. IRDA_DEBUG(4, "%s(), max frame=%d\n", __FUNCTION__ ,
  433. self->client.max_frame);
  434. }
  435. /* RECONNECT_KEY, in case the link goes down! */
  436. if (strcmp(param, "RECONNECT_KEY") == 0) {
  437. IRDA_DEBUG(4, "Got reconnect key: ");
  438. /* for (i = 0; i < val_len; i++) */
  439. /* printk("%02x", value[i]); */
  440. memcpy(self->client.reconnect_key, value, val_len);
  441. self->client.key_len = val_len;
  442. IRDA_DEBUG(4, "\n");
  443. }
  444. /* FILTER_ENTRY, have we got an ethernet address? */
  445. if (strcmp(param, "FILTER_ENTRY") == 0) {
  446. bytes = value;
  447. IRDA_DEBUG(4, "Ethernet address = %02x:%02x:%02x:%02x:%02x:%02x\n",
  448. bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
  449. bytes[5]);
  450. for (i = 0; i < 6; i++)
  451. self->dev->dev_addr[i] = bytes[i];
  452. }
  453. }
  454. /*
  455. * Function irlan_client_get_value_confirm (obj_id, value)
  456. *
  457. * Got results from remote LM-IAS
  458. *
  459. */
  460. void irlan_client_get_value_confirm(int result, __u16 obj_id,
  461. struct ias_value *value, void *priv)
  462. {
  463. struct irlan_cb *self;
  464. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  465. IRDA_ASSERT(priv != NULL, return;);
  466. self = (struct irlan_cb *) priv;
  467. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  468. /* We probably don't need to make any more queries */
  469. iriap_close(self->client.iriap);
  470. self->client.iriap = NULL;
  471. /* Check if request succeeded */
  472. if (result != IAS_SUCCESS) {
  473. IRDA_DEBUG(2, "%s(), got NULL value!\n", __FUNCTION__ );
  474. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL,
  475. NULL);
  476. return;
  477. }
  478. switch (value->type) {
  479. case IAS_INTEGER:
  480. self->dtsap_sel_ctrl = value->t.integer;
  481. if (value->t.integer != -1) {
  482. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_AVAIL,
  483. NULL);
  484. return;
  485. }
  486. irias_delete_value(value);
  487. break;
  488. default:
  489. IRDA_DEBUG(2, "%s(), unknown type!\n", __FUNCTION__ );
  490. break;
  491. }
  492. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL, NULL);
  493. }