irlan_provider.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*********************************************************************
  2. *
  3. * Filename: irlan_provider.c
  4. * Version: 0.9
  5. * Description: IrDA LAN Access Protocol Implementation
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Sun Aug 31 20:14:37 1997
  9. * Modified at: Sat Oct 30 12:52:10 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/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/init.h>
  34. #include <linux/random.h>
  35. #include <linux/bitops.h>
  36. #include <linux/slab.h>
  37. #include <asm/byteorder.h>
  38. #include <net/irda/irda.h>
  39. #include <net/irda/irttp.h>
  40. #include <net/irda/irlmp.h>
  41. #include <net/irda/irias_object.h>
  42. #include <net/irda/iriap.h>
  43. #include <net/irda/timer.h>
  44. #include <net/irda/irlan_common.h>
  45. #include <net/irda/irlan_eth.h>
  46. #include <net/irda/irlan_event.h>
  47. #include <net/irda/irlan_provider.h>
  48. #include <net/irda/irlan_filter.h>
  49. #include <net/irda/irlan_client.h>
  50. static void irlan_provider_connect_indication(void *instance, void *sap,
  51. struct qos_info *qos,
  52. __u32 max_sdu_size,
  53. __u8 max_header_size,
  54. struct sk_buff *skb);
  55. /*
  56. * Function irlan_provider_control_data_indication (handle, skb)
  57. *
  58. * This function gets the data that is received on the control channel
  59. *
  60. */
  61. static int irlan_provider_data_indication(void *instance, void *sap,
  62. struct sk_buff *skb)
  63. {
  64. struct irlan_cb *self;
  65. __u8 code;
  66. self = instance;
  67. IRDA_ASSERT(self != NULL, return -1;);
  68. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  69. IRDA_ASSERT(skb != NULL, return -1;);
  70. code = skb->data[0];
  71. switch(code) {
  72. case CMD_GET_PROVIDER_INFO:
  73. pr_debug("Got GET_PROVIDER_INFO command!\n");
  74. irlan_do_provider_event(self, IRLAN_GET_INFO_CMD, skb);
  75. break;
  76. case CMD_GET_MEDIA_CHAR:
  77. pr_debug("Got GET_MEDIA_CHAR command!\n");
  78. irlan_do_provider_event(self, IRLAN_GET_MEDIA_CMD, skb);
  79. break;
  80. case CMD_OPEN_DATA_CHANNEL:
  81. pr_debug("Got OPEN_DATA_CHANNEL command!\n");
  82. irlan_do_provider_event(self, IRLAN_OPEN_DATA_CMD, skb);
  83. break;
  84. case CMD_FILTER_OPERATION:
  85. pr_debug("Got FILTER_OPERATION command!\n");
  86. irlan_do_provider_event(self, IRLAN_FILTER_CONFIG_CMD, skb);
  87. break;
  88. case CMD_RECONNECT_DATA_CHAN:
  89. pr_debug("%s(), Got RECONNECT_DATA_CHAN command\n", __func__);
  90. pr_debug("%s(), NOT IMPLEMENTED\n", __func__);
  91. break;
  92. case CMD_CLOSE_DATA_CHAN:
  93. pr_debug("Got CLOSE_DATA_CHAN command!\n");
  94. pr_debug("%s(), NOT IMPLEMENTED\n", __func__);
  95. break;
  96. default:
  97. pr_debug("%s(), Unknown command!\n", __func__);
  98. break;
  99. }
  100. return 0;
  101. }
  102. /*
  103. * Function irlan_provider_connect_indication (handle, skb, priv)
  104. *
  105. * Got connection from peer IrLAN client
  106. *
  107. */
  108. static void irlan_provider_connect_indication(void *instance, void *sap,
  109. struct qos_info *qos,
  110. __u32 max_sdu_size,
  111. __u8 max_header_size,
  112. struct sk_buff *skb)
  113. {
  114. struct irlan_cb *self;
  115. struct tsap_cb *tsap;
  116. self = instance;
  117. tsap = sap;
  118. IRDA_ASSERT(self != NULL, return;);
  119. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  120. IRDA_ASSERT(tsap == self->provider.tsap_ctrl,return;);
  121. IRDA_ASSERT(self->provider.state == IRLAN_IDLE, return;);
  122. self->provider.max_sdu_size = max_sdu_size;
  123. self->provider.max_header_size = max_header_size;
  124. irlan_do_provider_event(self, IRLAN_CONNECT_INDICATION, NULL);
  125. /*
  126. * If we are in peer mode, the client may not have got the discovery
  127. * indication it needs to make progress. If the client is still in
  128. * IDLE state, we must kick it.
  129. */
  130. if ((self->provider.access_type == ACCESS_PEER) &&
  131. (self->client.state == IRLAN_IDLE))
  132. {
  133. irlan_client_wakeup(self, self->saddr, self->daddr);
  134. }
  135. }
  136. /*
  137. * Function irlan_provider_connect_response (handle)
  138. *
  139. * Accept incoming connection
  140. *
  141. */
  142. void irlan_provider_connect_response(struct irlan_cb *self,
  143. struct tsap_cb *tsap)
  144. {
  145. IRDA_ASSERT(self != NULL, return;);
  146. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  147. /* Just accept */
  148. irttp_connect_response(tsap, IRLAN_MTU, NULL);
  149. }
  150. static void irlan_provider_disconnect_indication(void *instance, void *sap,
  151. LM_REASON reason,
  152. struct sk_buff *userdata)
  153. {
  154. struct irlan_cb *self;
  155. struct tsap_cb *tsap;
  156. pr_debug("%s(), reason=%d\n", __func__ , reason);
  157. self = instance;
  158. tsap = sap;
  159. IRDA_ASSERT(self != NULL, return;);
  160. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  161. IRDA_ASSERT(tsap != NULL, return;);
  162. IRDA_ASSERT(tsap->magic == TTP_TSAP_MAGIC, return;);
  163. IRDA_ASSERT(tsap == self->provider.tsap_ctrl, return;);
  164. irlan_do_provider_event(self, IRLAN_LMP_DISCONNECT, NULL);
  165. }
  166. /*
  167. * Function irlan_parse_open_data_cmd (self, skb)
  168. *
  169. *
  170. *
  171. */
  172. int irlan_parse_open_data_cmd(struct irlan_cb *self, struct sk_buff *skb)
  173. {
  174. int ret;
  175. ret = irlan_provider_parse_command(self, CMD_OPEN_DATA_CHANNEL, skb);
  176. /* Open data channel */
  177. irlan_open_data_tsap(self);
  178. return ret;
  179. }
  180. /*
  181. * Function parse_command (skb)
  182. *
  183. * Extract all parameters from received buffer, then feed them to
  184. * check_params for parsing
  185. *
  186. */
  187. int irlan_provider_parse_command(struct irlan_cb *self, int cmd,
  188. struct sk_buff *skb)
  189. {
  190. __u8 *frame;
  191. __u8 *ptr;
  192. int count;
  193. __u16 val_len;
  194. int i;
  195. char *name;
  196. char *value;
  197. int ret = RSP_SUCCESS;
  198. IRDA_ASSERT(skb != NULL, return -RSP_PROTOCOL_ERROR;);
  199. pr_debug("%s(), skb->len=%d\n", __func__ , (int)skb->len);
  200. IRDA_ASSERT(self != NULL, return -RSP_PROTOCOL_ERROR;);
  201. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -RSP_PROTOCOL_ERROR;);
  202. if (!skb)
  203. return -RSP_PROTOCOL_ERROR;
  204. frame = skb->data;
  205. name = kmalloc(255, GFP_ATOMIC);
  206. if (!name)
  207. return -RSP_INSUFFICIENT_RESOURCES;
  208. value = kmalloc(1016, GFP_ATOMIC);
  209. if (!value) {
  210. kfree(name);
  211. return -RSP_INSUFFICIENT_RESOURCES;
  212. }
  213. /* How many parameters? */
  214. count = frame[1];
  215. pr_debug("Got %d parameters\n", count);
  216. ptr = frame+2;
  217. /* For all parameters */
  218. for (i=0; i<count;i++) {
  219. ret = irlan_extract_param(ptr, name, value, &val_len);
  220. if (ret < 0) {
  221. pr_debug("%s(), IrLAN, Error!\n", __func__);
  222. break;
  223. }
  224. ptr+=ret;
  225. ret = RSP_SUCCESS;
  226. irlan_check_command_param(self, name, value);
  227. }
  228. /* Cleanup */
  229. kfree(name);
  230. kfree(value);
  231. return ret;
  232. }
  233. /*
  234. * Function irlan_provider_send_reply (self, info)
  235. *
  236. * Send reply to query to peer IrLAN layer
  237. *
  238. */
  239. void irlan_provider_send_reply(struct irlan_cb *self, int command,
  240. int ret_code)
  241. {
  242. struct sk_buff *skb;
  243. IRDA_ASSERT(self != NULL, return;);
  244. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return;);
  245. skb = alloc_skb(IRLAN_MAX_HEADER + IRLAN_CMD_HEADER +
  246. /* Bigger param length comes from CMD_GET_MEDIA_CHAR */
  247. IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "DIRECTED") +
  248. IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "BROADCAST") +
  249. IRLAN_STRING_PARAMETER_LEN("FILTER_TYPE", "MULTICAST") +
  250. IRLAN_STRING_PARAMETER_LEN("ACCESS_TYPE", "HOSTED"),
  251. GFP_ATOMIC);
  252. if (!skb)
  253. return;
  254. /* Reserve space for TTP, LMP, and LAP header */
  255. skb_reserve(skb, self->provider.max_header_size);
  256. skb_put(skb, 2);
  257. switch (command) {
  258. case CMD_GET_PROVIDER_INFO:
  259. skb->data[0] = 0x00; /* Success */
  260. skb->data[1] = 0x02; /* 2 parameters */
  261. switch (self->media) {
  262. case MEDIA_802_3:
  263. irlan_insert_string_param(skb, "MEDIA", "802.3");
  264. break;
  265. case MEDIA_802_5:
  266. irlan_insert_string_param(skb, "MEDIA", "802.5");
  267. break;
  268. default:
  269. pr_debug("%s(), unknown media type!\n", __func__);
  270. break;
  271. }
  272. irlan_insert_short_param(skb, "IRLAN_VER", 0x0101);
  273. break;
  274. case CMD_GET_MEDIA_CHAR:
  275. skb->data[0] = 0x00; /* Success */
  276. skb->data[1] = 0x05; /* 5 parameters */
  277. irlan_insert_string_param(skb, "FILTER_TYPE", "DIRECTED");
  278. irlan_insert_string_param(skb, "FILTER_TYPE", "BROADCAST");
  279. irlan_insert_string_param(skb, "FILTER_TYPE", "MULTICAST");
  280. switch (self->provider.access_type) {
  281. case ACCESS_DIRECT:
  282. irlan_insert_string_param(skb, "ACCESS_TYPE", "DIRECT");
  283. break;
  284. case ACCESS_PEER:
  285. irlan_insert_string_param(skb, "ACCESS_TYPE", "PEER");
  286. break;
  287. case ACCESS_HOSTED:
  288. irlan_insert_string_param(skb, "ACCESS_TYPE", "HOSTED");
  289. break;
  290. default:
  291. pr_debug("%s(), Unknown access type\n", __func__);
  292. break;
  293. }
  294. irlan_insert_short_param(skb, "MAX_FRAME", 0x05ee);
  295. break;
  296. case CMD_OPEN_DATA_CHANNEL:
  297. skb->data[0] = 0x00; /* Success */
  298. if (self->provider.send_arb_val) {
  299. skb->data[1] = 0x03; /* 3 parameters */
  300. irlan_insert_short_param(skb, "CON_ARB",
  301. self->provider.send_arb_val);
  302. } else
  303. skb->data[1] = 0x02; /* 2 parameters */
  304. irlan_insert_byte_param(skb, "DATA_CHAN", self->stsap_sel_data);
  305. irlan_insert_string_param(skb, "RECONNECT_KEY", "LINUX RULES!");
  306. break;
  307. case CMD_FILTER_OPERATION:
  308. irlan_filter_request(self, skb);
  309. break;
  310. default:
  311. pr_debug("%s(), Unknown command!\n", __func__);
  312. break;
  313. }
  314. irttp_data_request(self->provider.tsap_ctrl, skb);
  315. }
  316. /*
  317. * Function irlan_provider_register(void)
  318. *
  319. * Register provider support so we can accept incoming connections.
  320. *
  321. */
  322. int irlan_provider_open_ctrl_tsap(struct irlan_cb *self)
  323. {
  324. struct tsap_cb *tsap;
  325. notify_t notify;
  326. IRDA_ASSERT(self != NULL, return -1;);
  327. IRDA_ASSERT(self->magic == IRLAN_MAGIC, return -1;);
  328. /* Check if already open */
  329. if (self->provider.tsap_ctrl)
  330. return -1;
  331. /*
  332. * First register well known control TSAP
  333. */
  334. irda_notify_init(&notify);
  335. notify.data_indication = irlan_provider_data_indication;
  336. notify.connect_indication = irlan_provider_connect_indication;
  337. notify.disconnect_indication = irlan_provider_disconnect_indication;
  338. notify.instance = self;
  339. strlcpy(notify.name, "IrLAN ctrl (p)", sizeof(notify.name));
  340. tsap = irttp_open_tsap(LSAP_ANY, 1, &notify);
  341. if (!tsap) {
  342. pr_debug("%s(), Got no tsap!\n", __func__);
  343. return -1;
  344. }
  345. self->provider.tsap_ctrl = tsap;
  346. /* Register with LM-IAS */
  347. irlan_ias_register(self, tsap->stsap_sel);
  348. return 0;
  349. }