irlmp_frame.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*********************************************************************
  2. *
  3. * Filename: irlmp_frame.c
  4. * Version: 0.9
  5. * Description: IrLMP frame implementation
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Tue Aug 19 02:09:59 1997
  9. * Modified at: Mon Dec 13 13:41:12 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>
  13. * All Rights Reserved.
  14. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * Neither Dag Brattli nor University of Tromsø admit liability nor
  22. * provide warranty for any of this software. This material is
  23. * provided "AS-IS" and at no charge.
  24. *
  25. ********************************************************************/
  26. #include <linux/skbuff.h>
  27. #include <linux/kernel.h>
  28. #include <net/irda/irda.h>
  29. #include <net/irda/irlap.h>
  30. #include <net/irda/timer.h>
  31. #include <net/irda/irlmp.h>
  32. #include <net/irda/irlmp_frame.h>
  33. #include <net/irda/discovery.h>
  34. static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap,
  35. __u8 slsap, int status, hashbin_t *);
  36. inline void irlmp_send_data_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
  37. int expedited, struct sk_buff *skb)
  38. {
  39. skb->data[0] = dlsap;
  40. skb->data[1] = slsap;
  41. if (expedited) {
  42. pr_debug("%s(), sending expedited data\n", __func__);
  43. irlap_data_request(self->irlap, skb, TRUE);
  44. } else
  45. irlap_data_request(self->irlap, skb, FALSE);
  46. }
  47. /*
  48. * Function irlmp_send_lcf_pdu (dlsap, slsap, opcode,skb)
  49. *
  50. * Send Link Control Frame to IrLAP
  51. */
  52. void irlmp_send_lcf_pdu(struct lap_cb *self, __u8 dlsap, __u8 slsap,
  53. __u8 opcode, struct sk_buff *skb)
  54. {
  55. __u8 *frame;
  56. IRDA_ASSERT(self != NULL, return;);
  57. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  58. IRDA_ASSERT(skb != NULL, return;);
  59. frame = skb->data;
  60. frame[0] = dlsap | CONTROL_BIT;
  61. frame[1] = slsap;
  62. frame[2] = opcode;
  63. if (opcode == DISCONNECT)
  64. frame[3] = 0x01; /* Service user request */
  65. else
  66. frame[3] = 0x00; /* rsvd */
  67. irlap_data_request(self->irlap, skb, FALSE);
  68. }
  69. /*
  70. * Function irlmp_input (skb)
  71. *
  72. * Used by IrLAP to pass received data frames to IrLMP layer
  73. *
  74. */
  75. void irlmp_link_data_indication(struct lap_cb *self, struct sk_buff *skb,
  76. int unreliable)
  77. {
  78. struct lsap_cb *lsap;
  79. __u8 slsap_sel; /* Source (this) LSAP address */
  80. __u8 dlsap_sel; /* Destination LSAP address */
  81. __u8 *fp;
  82. IRDA_ASSERT(self != NULL, return;);
  83. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  84. IRDA_ASSERT(skb->len > 2, return;);
  85. fp = skb->data;
  86. /*
  87. * The next statements may be confusing, but we do this so that
  88. * destination LSAP of received frame is source LSAP in our view
  89. */
  90. slsap_sel = fp[0] & LSAP_MASK;
  91. dlsap_sel = fp[1];
  92. /*
  93. * Check if this is an incoming connection, since we must deal with
  94. * it in a different way than other established connections.
  95. */
  96. if ((fp[0] & CONTROL_BIT) && (fp[2] == CONNECT_CMD)) {
  97. pr_debug("%s(), incoming connection, source LSAP=%d, dest LSAP=%d\n",
  98. __func__, slsap_sel, dlsap_sel);
  99. /* Try to find LSAP among the unconnected LSAPs */
  100. lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, CONNECT_CMD,
  101. irlmp->unconnected_lsaps);
  102. /* Maybe LSAP was already connected, so try one more time */
  103. if (!lsap) {
  104. pr_debug("%s(), incoming connection for LSAP already connected\n",
  105. __func__);
  106. lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
  107. self->lsaps);
  108. }
  109. } else
  110. lsap = irlmp_find_lsap(self, dlsap_sel, slsap_sel, 0,
  111. self->lsaps);
  112. if (lsap == NULL) {
  113. pr_debug("IrLMP, Sorry, no LSAP for received frame!\n");
  114. pr_debug("%s(), slsap_sel = %02x, dlsap_sel = %02x\n",
  115. __func__, slsap_sel, dlsap_sel);
  116. if (fp[0] & CONTROL_BIT) {
  117. pr_debug("%s(), received control frame %02x\n",
  118. __func__, fp[2]);
  119. } else {
  120. pr_debug("%s(), received data frame\n", __func__);
  121. }
  122. return;
  123. }
  124. /*
  125. * Check if we received a control frame?
  126. */
  127. if (fp[0] & CONTROL_BIT) {
  128. switch (fp[2]) {
  129. case CONNECT_CMD:
  130. lsap->lap = self;
  131. irlmp_do_lsap_event(lsap, LM_CONNECT_INDICATION, skb);
  132. break;
  133. case CONNECT_CNF:
  134. irlmp_do_lsap_event(lsap, LM_CONNECT_CONFIRM, skb);
  135. break;
  136. case DISCONNECT:
  137. pr_debug("%s(), Disconnect indication!\n",
  138. __func__);
  139. irlmp_do_lsap_event(lsap, LM_DISCONNECT_INDICATION,
  140. skb);
  141. break;
  142. case ACCESSMODE_CMD:
  143. pr_debug("Access mode cmd not implemented!\n");
  144. break;
  145. case ACCESSMODE_CNF:
  146. pr_debug("Access mode cnf not implemented!\n");
  147. break;
  148. default:
  149. pr_debug("%s(), Unknown control frame %02x\n",
  150. __func__, fp[2]);
  151. break;
  152. }
  153. } else if (unreliable) {
  154. /* Optimize and bypass the state machine if possible */
  155. if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
  156. irlmp_udata_indication(lsap, skb);
  157. else
  158. irlmp_do_lsap_event(lsap, LM_UDATA_INDICATION, skb);
  159. } else {
  160. /* Optimize and bypass the state machine if possible */
  161. if (lsap->lsap_state == LSAP_DATA_TRANSFER_READY)
  162. irlmp_data_indication(lsap, skb);
  163. else
  164. irlmp_do_lsap_event(lsap, LM_DATA_INDICATION, skb);
  165. }
  166. }
  167. /*
  168. * Function irlmp_link_unitdata_indication (self, skb)
  169. *
  170. *
  171. *
  172. */
  173. #ifdef CONFIG_IRDA_ULTRA
  174. void irlmp_link_unitdata_indication(struct lap_cb *self, struct sk_buff *skb)
  175. {
  176. struct lsap_cb *lsap;
  177. __u8 slsap_sel; /* Source (this) LSAP address */
  178. __u8 dlsap_sel; /* Destination LSAP address */
  179. __u8 pid; /* Protocol identifier */
  180. __u8 *fp;
  181. unsigned long flags;
  182. IRDA_ASSERT(self != NULL, return;);
  183. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  184. IRDA_ASSERT(skb->len > 2, return;);
  185. fp = skb->data;
  186. /*
  187. * The next statements may be confusing, but we do this so that
  188. * destination LSAP of received frame is source LSAP in our view
  189. */
  190. slsap_sel = fp[0] & LSAP_MASK;
  191. dlsap_sel = fp[1];
  192. pid = fp[2];
  193. if (pid & 0x80) {
  194. pr_debug("%s(), extension in PID not supp!\n",
  195. __func__);
  196. return;
  197. }
  198. /* Check if frame is addressed to the connectionless LSAP */
  199. if ((slsap_sel != LSAP_CONNLESS) || (dlsap_sel != LSAP_CONNLESS)) {
  200. pr_debug("%s(), dropping frame!\n", __func__);
  201. return;
  202. }
  203. /* Search the connectionless LSAP */
  204. spin_lock_irqsave(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  205. lsap = (struct lsap_cb *) hashbin_get_first(irlmp->unconnected_lsaps);
  206. while (lsap != NULL) {
  207. /*
  208. * Check if source LSAP and dest LSAP selectors and PID match.
  209. */
  210. if ((lsap->slsap_sel == slsap_sel) &&
  211. (lsap->dlsap_sel == dlsap_sel) &&
  212. (lsap->pid == pid))
  213. {
  214. break;
  215. }
  216. lsap = (struct lsap_cb *) hashbin_get_next(irlmp->unconnected_lsaps);
  217. }
  218. spin_unlock_irqrestore(&irlmp->unconnected_lsaps->hb_spinlock, flags);
  219. if (lsap)
  220. irlmp_connless_data_indication(lsap, skb);
  221. else {
  222. pr_debug("%s(), found no matching LSAP!\n", __func__);
  223. }
  224. }
  225. #endif /* CONFIG_IRDA_ULTRA */
  226. /*
  227. * Function irlmp_link_disconnect_indication (reason, userdata)
  228. *
  229. * IrLAP has disconnected
  230. *
  231. */
  232. void irlmp_link_disconnect_indication(struct lap_cb *lap,
  233. struct irlap_cb *irlap,
  234. LAP_REASON reason,
  235. struct sk_buff *skb)
  236. {
  237. IRDA_ASSERT(lap != NULL, return;);
  238. IRDA_ASSERT(lap->magic == LMP_LAP_MAGIC, return;);
  239. lap->reason = reason;
  240. lap->daddr = DEV_ADDR_ANY;
  241. /* FIXME: must do something with the skb if any */
  242. /*
  243. * Inform station state machine
  244. */
  245. irlmp_do_lap_event(lap, LM_LAP_DISCONNECT_INDICATION, NULL);
  246. }
  247. /*
  248. * Function irlmp_link_connect_indication (qos)
  249. *
  250. * Incoming LAP connection!
  251. *
  252. */
  253. void irlmp_link_connect_indication(struct lap_cb *self, __u32 saddr,
  254. __u32 daddr, struct qos_info *qos,
  255. struct sk_buff *skb)
  256. {
  257. /* Copy QoS settings for this session */
  258. self->qos = qos;
  259. /* Update destination device address */
  260. self->daddr = daddr;
  261. IRDA_ASSERT(self->saddr == saddr, return;);
  262. irlmp_do_lap_event(self, LM_LAP_CONNECT_INDICATION, skb);
  263. }
  264. /*
  265. * Function irlmp_link_connect_confirm (qos)
  266. *
  267. * LAP connection confirmed!
  268. *
  269. */
  270. void irlmp_link_connect_confirm(struct lap_cb *self, struct qos_info *qos,
  271. struct sk_buff *skb)
  272. {
  273. IRDA_ASSERT(self != NULL, return;);
  274. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  275. IRDA_ASSERT(qos != NULL, return;);
  276. /* Don't need use the skb for now */
  277. /* Copy QoS settings for this session */
  278. self->qos = qos;
  279. irlmp_do_lap_event(self, LM_LAP_CONNECT_CONFIRM, NULL);
  280. }
  281. /*
  282. * Function irlmp_link_discovery_indication (self, log)
  283. *
  284. * Device is discovering us
  285. *
  286. * It's not an answer to our own discoveries, just another device trying
  287. * to perform discovery, but we don't want to miss the opportunity
  288. * to exploit this information, because :
  289. * o We may not actively perform discovery (just passive discovery)
  290. * o This type of discovery is much more reliable. In some cases, it
  291. * seem that less than 50% of our discoveries get an answer, while
  292. * we always get ~100% of these.
  293. * o Make faster discovery, statistically divide time of discovery
  294. * events by 2 (important for the latency aspect and user feel)
  295. * o Even is we do active discovery, the other node might not
  296. * answer our discoveries (ex: Palm). The Palm will just perform
  297. * one active discovery and connect directly to us.
  298. *
  299. * However, when both devices discover each other, they might attempt to
  300. * connect to each other following the discovery event, and it would create
  301. * collisions on the medium (SNRM battle).
  302. * The "fix" for that is to disable all connection requests in IrLAP
  303. * for 100ms after a discovery indication by setting the media_busy flag.
  304. * Previously, we used to postpone the event which was quite ugly. Now
  305. * that IrLAP takes care of this problem, just pass the event up...
  306. *
  307. * Jean II
  308. */
  309. void irlmp_link_discovery_indication(struct lap_cb *self,
  310. discovery_t *discovery)
  311. {
  312. IRDA_ASSERT(self != NULL, return;);
  313. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  314. /* Add to main log, cleanup */
  315. irlmp_add_discovery(irlmp->cachelog, discovery);
  316. /* Just handle it the same way as a discovery confirm,
  317. * bypass the LM_LAP state machine (see below) */
  318. irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_PASSIVE);
  319. }
  320. /*
  321. * Function irlmp_link_discovery_confirm (self, log)
  322. *
  323. * Called by IrLAP with a list of discoveries after the discovery
  324. * request has been carried out. A NULL log is received if IrLAP
  325. * was unable to carry out the discovery request
  326. *
  327. */
  328. void irlmp_link_discovery_confirm(struct lap_cb *self, hashbin_t *log)
  329. {
  330. IRDA_ASSERT(self != NULL, return;);
  331. IRDA_ASSERT(self->magic == LMP_LAP_MAGIC, return;);
  332. /* Add to main log, cleanup */
  333. irlmp_add_discovery_log(irlmp->cachelog, log);
  334. /* Propagate event to various LSAPs registered for it.
  335. * We bypass the LM_LAP state machine because
  336. * 1) We do it regardless of the LM_LAP state
  337. * 2) It doesn't affect the LM_LAP state
  338. * 3) Faster, slimer, simpler, ...
  339. * Jean II */
  340. irlmp_discovery_confirm(irlmp->cachelog, DISCOVERY_ACTIVE);
  341. }
  342. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  343. static inline void irlmp_update_cache(struct lap_cb *lap,
  344. struct lsap_cb *lsap)
  345. {
  346. /* Prevent concurrent read to get garbage */
  347. lap->cache.valid = FALSE;
  348. /* Update cache entry */
  349. lap->cache.dlsap_sel = lsap->dlsap_sel;
  350. lap->cache.slsap_sel = lsap->slsap_sel;
  351. lap->cache.lsap = lsap;
  352. lap->cache.valid = TRUE;
  353. }
  354. #endif
  355. /*
  356. * Function irlmp_find_handle (self, dlsap_sel, slsap_sel, status, queue)
  357. *
  358. * Find handle associated with destination and source LSAP
  359. *
  360. * Any IrDA connection (LSAP/TSAP) is uniquely identified by
  361. * 3 parameters, the local lsap, the remote lsap and the remote address.
  362. * We may initiate multiple connections to the same remote service
  363. * (they will have different local lsap), a remote device may initiate
  364. * multiple connections to the same local service (they will have
  365. * different remote lsap), or multiple devices may connect to the same
  366. * service and may use the same remote lsap (and they will have
  367. * different remote address).
  368. * So, where is the remote address ? Each LAP connection is made with
  369. * a single remote device, so imply a specific remote address.
  370. * Jean II
  371. */
  372. static struct lsap_cb *irlmp_find_lsap(struct lap_cb *self, __u8 dlsap_sel,
  373. __u8 slsap_sel, int status,
  374. hashbin_t *queue)
  375. {
  376. struct lsap_cb *lsap;
  377. unsigned long flags;
  378. /*
  379. * Optimize for the common case. We assume that the last frame
  380. * received is in the same connection as the last one, so check in
  381. * cache first to avoid the linear search
  382. */
  383. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  384. if ((self->cache.valid) &&
  385. (self->cache.slsap_sel == slsap_sel) &&
  386. (self->cache.dlsap_sel == dlsap_sel))
  387. {
  388. return self->cache.lsap;
  389. }
  390. #endif
  391. spin_lock_irqsave(&queue->hb_spinlock, flags);
  392. lsap = (struct lsap_cb *) hashbin_get_first(queue);
  393. while (lsap != NULL) {
  394. /*
  395. * If this is an incoming connection, then the destination
  396. * LSAP selector may have been specified as LM_ANY so that
  397. * any client can connect. In that case we only need to check
  398. * if the source LSAP (in our view!) match!
  399. */
  400. if ((status == CONNECT_CMD) &&
  401. (lsap->slsap_sel == slsap_sel) &&
  402. (lsap->dlsap_sel == LSAP_ANY)) {
  403. /* This is where the dest lsap sel is set on incoming
  404. * lsaps */
  405. lsap->dlsap_sel = dlsap_sel;
  406. break;
  407. }
  408. /*
  409. * Check if source LSAP and dest LSAP selectors match.
  410. */
  411. if ((lsap->slsap_sel == slsap_sel) &&
  412. (lsap->dlsap_sel == dlsap_sel))
  413. break;
  414. lsap = (struct lsap_cb *) hashbin_get_next(queue);
  415. }
  416. #ifdef CONFIG_IRDA_CACHE_LAST_LSAP
  417. if(lsap)
  418. irlmp_update_cache(self, lsap);
  419. #endif
  420. spin_unlock_irqrestore(&queue->hb_spinlock, flags);
  421. /* Return what we've found or NULL */
  422. return lsap;
  423. }