irlan_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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, return;);
  151. IRDA_DEBUG(1, "%s(), Found instance (%08x)!\n", __FUNCTION__ ,
  152. daddr);
  153. irlan_client_wakeup(self, saddr, daddr);
  154. }
  155. rcu_read_unlock();
  156. }
  157. /*
  158. * Function irlan_client_data_indication (handle, skb)
  159. *
  160. * This function gets the data that is received on the control channel
  161. *
  162. */
  163. static int irlan_client_ctrl_data_indication(void *instance, void *sap,
  164. struct sk_buff *skb)
  165. {
  166. struct irlan_cb *self;
  167. IRDA_DEBUG(2, "%s()\n", __FUNCTION__ );
  168. self = (struct irlan_cb *) instance;
  169. IRDA_ASSERT(self != NULL, return -1;);
  170. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  171. IRDA_ASSERT(skb != NULL, return -1;);
  172. irlan_do_client_event(self, IRLAN_DATA_INDICATION, skb);
  173. /* Ready for a new command */
  174. IRDA_DEBUG(2, "%s(), clearing tx_busy\n", __FUNCTION__ );
  175. self->client.tx_busy = FALSE;
  176. /* Check if we have some queued commands waiting to be sent */
  177. irlan_run_ctrl_tx_queue(self);
  178. return 0;
  179. }
  180. static void irlan_client_ctrl_disconnect_indication(void *instance, void *sap,
  181. LM_REASON reason,
  182. struct sk_buff *userdata)
  183. {
  184. struct irlan_cb *self;
  185. struct tsap_cb *tsap;
  186. struct sk_buff *skb;
  187. IRDA_DEBUG(4, "%s(), reason=%d\n", __FUNCTION__ , reason);
  188. self = (struct irlan_cb *) instance;
  189. tsap = (struct tsap_cb *) sap;
  190. IRDA_ASSERT(self != NULL, return;);
  191. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  192. IRDA_ASSERT(tsap != NULL, return;);
  193. IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
  194. IRDA_ASSERT(tsap == self->client.tsap_ctrl, return;);
  195. /* Remove frames queued on the control channel */
  196. while ((skb = skb_dequeue(&self->client.txq)) != NULL) {
  197. dev_kfree_skb(skb);
  198. }
  199. self->client.tx_busy = FALSE;
  200. irlan_do_client_event(self, IRLAN_LMP_DISCONNECT, NULL);
  201. }
  202. /*
  203. * Function irlan_client_open_tsaps (self)
  204. *
  205. * Initialize callbacks and open IrTTP TSAPs
  206. *
  207. */
  208. static void irlan_client_open_ctrl_tsap(struct irlan_cb *self)
  209. {
  210. struct tsap_cb *tsap;
  211. notify_t notify;
  212. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  213. IRDA_ASSERT(self != NULL, return;);
  214. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  215. /* Check if already open */
  216. if (self->client.tsap_ctrl)
  217. return;
  218. irda_notify_init(&notify);
  219. /* Set up callbacks */
  220. notify.data_indication = irlan_client_ctrl_data_indication;
  221. notify.connect_confirm = irlan_client_ctrl_connect_confirm;
  222. notify.disconnect_indication = irlan_client_ctrl_disconnect_indication;
  223. notify.instance = self;
  224. strlcpy(notify.name, "IrLAN ctrl (c)", sizeof(notify.name));
  225. tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT, &notify);
  226. if (!tsap) {
  227. IRDA_DEBUG(2, "%s(), Got no tsap!\n", __FUNCTION__ );
  228. return;
  229. }
  230. self->client.tsap_ctrl = tsap;
  231. }
  232. /*
  233. * Function irlan_client_connect_confirm (handle, skb)
  234. *
  235. * Connection to peer IrLAN laye confirmed
  236. *
  237. */
  238. static void irlan_client_ctrl_connect_confirm(void *instance, void *sap,
  239. struct qos_info *qos,
  240. __u32 max_sdu_size,
  241. __u8 max_header_size,
  242. struct sk_buff *skb)
  243. {
  244. struct irlan_cb *self;
  245. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  246. self = (struct irlan_cb *) instance;
  247. IRDA_ASSERT(self != NULL, return;);
  248. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  249. self->client.max_sdu_size = max_sdu_size;
  250. self->client.max_header_size = max_header_size;
  251. /* TODO: we could set the MTU depending on the max_sdu_size */
  252. irlan_do_client_event(self, IRLAN_CONNECT_COMPLETE, NULL);
  253. }
  254. /*
  255. * Function print_ret_code (code)
  256. *
  257. * Print return code of request to peer IrLAN layer.
  258. *
  259. */
  260. static void print_ret_code(__u8 code)
  261. {
  262. switch(code) {
  263. case 0:
  264. printk(KERN_INFO "Success\n");
  265. break;
  266. case 1:
  267. IRDA_WARNING("IrLAN: Insufficient resources\n");
  268. break;
  269. case 2:
  270. IRDA_WARNING("IrLAN: Invalid command format\n");
  271. break;
  272. case 3:
  273. IRDA_WARNING("IrLAN: Command not supported\n");
  274. break;
  275. case 4:
  276. IRDA_WARNING("IrLAN: Parameter not supported\n");
  277. break;
  278. case 5:
  279. IRDA_WARNING("IrLAN: Value not supported\n");
  280. break;
  281. case 6:
  282. IRDA_WARNING("IrLAN: Not open\n");
  283. break;
  284. case 7:
  285. IRDA_WARNING("IrLAN: Authentication required\n");
  286. break;
  287. case 8:
  288. IRDA_WARNING("IrLAN: Invalid password\n");
  289. break;
  290. case 9:
  291. IRDA_WARNING("IrLAN: Protocol error\n");
  292. break;
  293. case 255:
  294. IRDA_WARNING("IrLAN: Asynchronous status\n");
  295. break;
  296. }
  297. }
  298. /*
  299. * Function irlan_client_parse_response (self, skb)
  300. *
  301. * Extract all parameters from received buffer, then feed them to
  302. * check_params for parsing
  303. */
  304. void irlan_client_parse_response(struct irlan_cb *self, struct sk_buff *skb)
  305. {
  306. __u8 *frame;
  307. __u8 *ptr;
  308. int count;
  309. int ret;
  310. __u16 val_len;
  311. int i;
  312. char *name;
  313. char *value;
  314. IRDA_ASSERT(skb != NULL, return;);
  315. IRDA_DEBUG(4, "%s() skb->len=%d\n", __FUNCTION__ , (int) skb->len);
  316. IRDA_ASSERT(self != NULL, return;);
  317. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  318. if (!skb) {
  319. IRDA_ERROR("%s(), Got NULL skb!\n", __FUNCTION__);
  320. return;
  321. }
  322. frame = skb->data;
  323. /*
  324. * Check return code and print it if not success
  325. */
  326. if (frame[0]) {
  327. print_ret_code(frame[0]);
  328. return;
  329. }
  330. name = kmalloc(255, GFP_ATOMIC);
  331. if (!name)
  332. return;
  333. value = kmalloc(1016, GFP_ATOMIC);
  334. if (!value) {
  335. kfree(name);
  336. return;
  337. }
  338. /* How many parameters? */
  339. count = frame[1];
  340. IRDA_DEBUG(4, "%s(), got %d parameters\n", __FUNCTION__ , count);
  341. ptr = frame+2;
  342. /* For all parameters */
  343. for (i=0; i<count;i++) {
  344. ret = irlan_extract_param(ptr, name, value, &val_len);
  345. if (ret < 0) {
  346. IRDA_DEBUG(2, "%s(), IrLAN, Error!\n", __FUNCTION__ );
  347. break;
  348. }
  349. ptr += ret;
  350. irlan_check_response_param(self, name, value, val_len);
  351. }
  352. /* Cleanup */
  353. kfree(name);
  354. kfree(value);
  355. }
  356. /*
  357. * Function irlan_check_response_param (self, param, value, val_len)
  358. *
  359. * Check which parameter is received and update local variables
  360. *
  361. */
  362. static void irlan_check_response_param(struct irlan_cb *self, char *param,
  363. char *value, int val_len)
  364. {
  365. __u16 tmp_cpu; /* Temporary value in host order */
  366. __u8 *bytes;
  367. int i;
  368. IRDA_DEBUG(4, "%s(), parm=%s\n", __FUNCTION__ , param);
  369. IRDA_ASSERT(self != NULL, return;);
  370. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  371. /* Media type */
  372. if (strcmp(param, "MEDIA") == 0) {
  373. if (strcmp(value, "802.3") == 0)
  374. self->media = MEDIA_802_3;
  375. else
  376. self->media = MEDIA_802_5;
  377. return;
  378. }
  379. if (strcmp(param, "FILTER_TYPE") == 0) {
  380. if (strcmp(value, "DIRECTED") == 0)
  381. self->client.filter_type |= IRLAN_DIRECTED;
  382. else if (strcmp(value, "FUNCTIONAL") == 0)
  383. self->client.filter_type |= IRLAN_FUNCTIONAL;
  384. else if (strcmp(value, "GROUP") == 0)
  385. self->client.filter_type |= IRLAN_GROUP;
  386. else if (strcmp(value, "MAC_FRAME") == 0)
  387. self->client.filter_type |= IRLAN_MAC_FRAME;
  388. else if (strcmp(value, "MULTICAST") == 0)
  389. self->client.filter_type |= IRLAN_MULTICAST;
  390. else if (strcmp(value, "BROADCAST") == 0)
  391. self->client.filter_type |= IRLAN_BROADCAST;
  392. else if (strcmp(value, "IPX_SOCKET") == 0)
  393. self->client.filter_type |= IRLAN_IPX_SOCKET;
  394. }
  395. if (strcmp(param, "ACCESS_TYPE") == 0) {
  396. if (strcmp(value, "DIRECT") == 0)
  397. self->client.access_type = ACCESS_DIRECT;
  398. else if (strcmp(value, "PEER") == 0)
  399. self->client.access_type = ACCESS_PEER;
  400. else if (strcmp(value, "HOSTED") == 0)
  401. self->client.access_type = ACCESS_HOSTED;
  402. else {
  403. IRDA_DEBUG(2, "%s(), unknown access type!\n", __FUNCTION__ );
  404. }
  405. }
  406. /* IRLAN version */
  407. if (strcmp(param, "IRLAN_VER") == 0) {
  408. IRDA_DEBUG(4, "IrLAN version %d.%d\n", (__u8) value[0],
  409. (__u8) value[1]);
  410. self->version[0] = value[0];
  411. self->version[1] = value[1];
  412. return;
  413. }
  414. /* Which remote TSAP to use for data channel */
  415. if (strcmp(param, "DATA_CHAN") == 0) {
  416. self->dtsap_sel_data = value[0];
  417. IRDA_DEBUG(4, "Data TSAP = %02x\n", self->dtsap_sel_data);
  418. return;
  419. }
  420. if (strcmp(param, "CON_ARB") == 0) {
  421. memcpy(&tmp_cpu, value, 2); /* Align value */
  422. le16_to_cpus(&tmp_cpu); /* Convert to host order */
  423. self->client.recv_arb_val = tmp_cpu;
  424. IRDA_DEBUG(2, "%s(), receive arb val=%d\n", __FUNCTION__ ,
  425. self->client.recv_arb_val);
  426. }
  427. if (strcmp(param, "MAX_FRAME") == 0) {
  428. memcpy(&tmp_cpu, value, 2); /* Align value */
  429. le16_to_cpus(&tmp_cpu); /* Convert to host order */
  430. self->client.max_frame = tmp_cpu;
  431. IRDA_DEBUG(4, "%s(), max frame=%d\n", __FUNCTION__ ,
  432. self->client.max_frame);
  433. }
  434. /* RECONNECT_KEY, in case the link goes down! */
  435. if (strcmp(param, "RECONNECT_KEY") == 0) {
  436. IRDA_DEBUG(4, "Got reconnect key: ");
  437. /* for (i = 0; i < val_len; i++) */
  438. /* printk("%02x", value[i]); */
  439. memcpy(self->client.reconnect_key, value, val_len);
  440. self->client.key_len = val_len;
  441. IRDA_DEBUG(4, "\n");
  442. }
  443. /* FILTER_ENTRY, have we got an ethernet address? */
  444. if (strcmp(param, "FILTER_ENTRY") == 0) {
  445. bytes = value;
  446. IRDA_DEBUG(4, "Ethernet address = %02x:%02x:%02x:%02x:%02x:%02x\n",
  447. bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
  448. bytes[5]);
  449. for (i = 0; i < 6; i++)
  450. self->dev->dev_addr[i] = bytes[i];
  451. }
  452. }
  453. /*
  454. * Function irlan_client_get_value_confirm (obj_id, value)
  455. *
  456. * Got results from remote LM-IAS
  457. *
  458. */
  459. void irlan_client_get_value_confirm(int result, __u16 obj_id,
  460. struct ias_value *value, void *priv)
  461. {
  462. struct irlan_cb *self;
  463. IRDA_DEBUG(4, "%s()\n", __FUNCTION__ );
  464. IRDA_ASSERT(priv != NULL, return;);
  465. self = (struct irlan_cb *) priv;
  466. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  467. /* We probably don't need to make any more queries */
  468. iriap_close(self->client.iriap);
  469. self->client.iriap = NULL;
  470. /* Check if request succeeded */
  471. if (result != IAS_SUCCESS) {
  472. IRDA_DEBUG(2, "%s(), got NULL value!\n", __FUNCTION__ );
  473. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL,
  474. NULL);
  475. return;
  476. }
  477. switch (value->type) {
  478. case IAS_INTEGER:
  479. self->dtsap_sel_ctrl = value->t.integer;
  480. if (value->t.integer != -1) {
  481. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_AVAIL,
  482. NULL);
  483. return;
  484. }
  485. irias_delete_value(value);
  486. break;
  487. default:
  488. IRDA_DEBUG(2, "%s(), unknown type!\n", __FUNCTION__ );
  489. break;
  490. }
  491. irlan_do_client_event(self, IRLAN_IAS_PROVIDER_NOT_AVAIL, NULL);
  492. }