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