irlan_client.c 14 KB

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