irlmp_frame.c 14 KB

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