irlmp_frame.c 14 KB

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