irlap_frame.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*********************************************************************
  2. *
  3. * Filename: irlap_frame.c
  4. * Version: 1.0
  5. * Description: Build and transmit IrLAP frames
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Tue Aug 19 10:27:26 1997
  9. * Modified at: Wed Jan 5 08:59:04 2000
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-2000 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/if.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/irda.h>
  31. #include <net/pkt_sched.h>
  32. #include <net/sock.h>
  33. #include <asm/byteorder.h>
  34. #include <net/irda/irda.h>
  35. #include <net/irda/irda_device.h>
  36. #include <net/irda/irlap.h>
  37. #include <net/irda/wrapper.h>
  38. #include <net/irda/timer.h>
  39. #include <net/irda/irlap_frame.h>
  40. #include <net/irda/qos.h>
  41. static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
  42. int command);
  43. /*
  44. * Function irlap_insert_info (self, skb)
  45. *
  46. * Insert minimum turnaround time and speed information into the skb. We
  47. * need to do this since it's per packet relevant information. Safe to
  48. * have this function inlined since it's only called from one place
  49. */
  50. static inline void irlap_insert_info(struct irlap_cb *self,
  51. struct sk_buff *skb)
  52. {
  53. struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
  54. /*
  55. * Insert MTT (min. turn time) and speed into skb, so that the
  56. * device driver knows which settings to use
  57. */
  58. cb->magic = LAP_MAGIC;
  59. cb->mtt = self->mtt_required;
  60. cb->next_speed = self->speed;
  61. /* Reset */
  62. self->mtt_required = 0;
  63. /*
  64. * Delay equals negotiated BOFs count, plus the number of BOFs to
  65. * force the negotiated minimum turnaround time
  66. */
  67. cb->xbofs = self->bofs_count;
  68. cb->next_xbofs = self->next_bofs;
  69. cb->xbofs_delay = self->xbofs_delay;
  70. /* Reset XBOF's delay (used only for getting min turn time) */
  71. self->xbofs_delay = 0;
  72. /* Put the correct xbofs value for the next packet */
  73. self->bofs_count = self->next_bofs;
  74. }
  75. /*
  76. * Function irlap_queue_xmit (self, skb)
  77. *
  78. * A little wrapper for dev_queue_xmit, so we can insert some common
  79. * code into it.
  80. */
  81. void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
  82. {
  83. /* Some common init stuff */
  84. skb->dev = self->netdev;
  85. skb_reset_mac_header(skb);
  86. skb_reset_network_header(skb);
  87. skb_reset_transport_header(skb);
  88. skb->protocol = htons(ETH_P_IRDA);
  89. skb->priority = TC_PRIO_BESTEFFORT;
  90. irlap_insert_info(self, skb);
  91. dev_queue_xmit(skb);
  92. }
  93. /*
  94. * Function irlap_send_snrm_cmd (void)
  95. *
  96. * Transmits a connect SNRM command frame
  97. */
  98. void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
  99. {
  100. struct sk_buff *tx_skb;
  101. struct snrm_frame *frame;
  102. int ret;
  103. IRDA_ASSERT(self != NULL, return;);
  104. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  105. /* Allocate frame */
  106. tx_skb = alloc_skb(sizeof(struct snrm_frame) +
  107. IRLAP_NEGOCIATION_PARAMS_LEN,
  108. GFP_ATOMIC);
  109. if (!tx_skb)
  110. return;
  111. frame = (struct snrm_frame *) skb_put(tx_skb, 2);
  112. /* Insert connection address field */
  113. if (qos)
  114. frame->caddr = CMD_FRAME | CBROADCAST;
  115. else
  116. frame->caddr = CMD_FRAME | self->caddr;
  117. /* Insert control field */
  118. frame->control = SNRM_CMD | PF_BIT;
  119. /*
  120. * If we are establishing a connection then insert QoS paramerters
  121. */
  122. if (qos) {
  123. skb_put(tx_skb, 9); /* 25 left */
  124. frame->saddr = cpu_to_le32(self->saddr);
  125. frame->daddr = cpu_to_le32(self->daddr);
  126. frame->ncaddr = self->caddr;
  127. ret = irlap_insert_qos_negotiation_params(self, tx_skb);
  128. if (ret < 0) {
  129. dev_kfree_skb(tx_skb);
  130. return;
  131. }
  132. }
  133. irlap_queue_xmit(self, tx_skb);
  134. }
  135. /*
  136. * Function irlap_recv_snrm_cmd (skb, info)
  137. *
  138. * Received SNRM (Set Normal Response Mode) command frame
  139. *
  140. */
  141. static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
  142. struct irlap_info *info)
  143. {
  144. struct snrm_frame *frame;
  145. if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
  146. frame = (struct snrm_frame *) skb->data;
  147. /* Copy the new connection address ignoring the C/R bit */
  148. info->caddr = frame->ncaddr & 0xFE;
  149. /* Check if the new connection address is valid */
  150. if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
  151. IRDA_DEBUG(3, "%s(), invalid connection address!\n",
  152. __FUNCTION__);
  153. return;
  154. }
  155. /* Copy peer device address */
  156. info->daddr = le32_to_cpu(frame->saddr);
  157. info->saddr = le32_to_cpu(frame->daddr);
  158. /* Only accept if addressed directly to us */
  159. if (info->saddr != self->saddr) {
  160. IRDA_DEBUG(2, "%s(), not addressed to us!\n",
  161. __FUNCTION__);
  162. return;
  163. }
  164. irlap_do_event(self, RECV_SNRM_CMD, skb, info);
  165. } else {
  166. /* Signal that this SNRM frame does not contain and I-field */
  167. irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
  168. }
  169. }
  170. /*
  171. * Function irlap_send_ua_response_frame (qos)
  172. *
  173. * Send UA (Unnumbered Acknowledgement) frame
  174. *
  175. */
  176. void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
  177. {
  178. struct sk_buff *tx_skb;
  179. struct ua_frame *frame;
  180. int ret;
  181. IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
  182. IRDA_ASSERT(self != NULL, return;);
  183. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  184. /* Allocate frame */
  185. tx_skb = alloc_skb(sizeof(struct ua_frame) +
  186. IRLAP_NEGOCIATION_PARAMS_LEN,
  187. GFP_ATOMIC);
  188. if (!tx_skb)
  189. return;
  190. frame = (struct ua_frame *) skb_put(tx_skb, 10);
  191. /* Build UA response */
  192. frame->caddr = self->caddr;
  193. frame->control = UA_RSP | PF_BIT;
  194. frame->saddr = cpu_to_le32(self->saddr);
  195. frame->daddr = cpu_to_le32(self->daddr);
  196. /* Should we send QoS negotiation parameters? */
  197. if (qos) {
  198. ret = irlap_insert_qos_negotiation_params(self, tx_skb);
  199. if (ret < 0) {
  200. dev_kfree_skb(tx_skb);
  201. return;
  202. }
  203. }
  204. irlap_queue_xmit(self, tx_skb);
  205. }
  206. /*
  207. * Function irlap_send_dm_frame (void)
  208. *
  209. * Send disconnected mode (DM) frame
  210. *
  211. */
  212. void irlap_send_dm_frame( struct irlap_cb *self)
  213. {
  214. struct sk_buff *tx_skb = NULL;
  215. struct dm_frame *frame;
  216. IRDA_ASSERT(self != NULL, return;);
  217. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  218. tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
  219. if (!tx_skb)
  220. return;
  221. frame = (struct dm_frame *)skb_put(tx_skb, 2);
  222. if (self->state == LAP_NDM)
  223. frame->caddr = CBROADCAST;
  224. else
  225. frame->caddr = self->caddr;
  226. frame->control = DM_RSP | PF_BIT;
  227. irlap_queue_xmit(self, tx_skb);
  228. }
  229. /*
  230. * Function irlap_send_disc_frame (void)
  231. *
  232. * Send disconnect (DISC) frame
  233. *
  234. */
  235. void irlap_send_disc_frame(struct irlap_cb *self)
  236. {
  237. struct sk_buff *tx_skb = NULL;
  238. struct disc_frame *frame;
  239. IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
  240. IRDA_ASSERT(self != NULL, return;);
  241. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  242. tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
  243. if (!tx_skb)
  244. return;
  245. frame = (struct disc_frame *)skb_put(tx_skb, 2);
  246. frame->caddr = self->caddr | CMD_FRAME;
  247. frame->control = DISC_CMD | PF_BIT;
  248. irlap_queue_xmit(self, tx_skb);
  249. }
  250. /*
  251. * Function irlap_send_discovery_xid_frame (S, s, command)
  252. *
  253. * Build and transmit a XID (eXchange station IDentifier) discovery
  254. * frame.
  255. */
  256. void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
  257. __u8 command, discovery_t *discovery)
  258. {
  259. struct sk_buff *tx_skb = NULL;
  260. struct xid_frame *frame;
  261. __u32 bcast = BROADCAST;
  262. __u8 *info;
  263. IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
  264. s, S, command);
  265. IRDA_ASSERT(self != NULL, return;);
  266. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  267. IRDA_ASSERT(discovery != NULL, return;);
  268. tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
  269. GFP_ATOMIC);
  270. if (!tx_skb)
  271. return;
  272. skb_put(tx_skb, 14);
  273. frame = (struct xid_frame *) tx_skb->data;
  274. if (command) {
  275. frame->caddr = CBROADCAST | CMD_FRAME;
  276. frame->control = XID_CMD | PF_BIT;
  277. } else {
  278. frame->caddr = CBROADCAST;
  279. frame->control = XID_RSP | PF_BIT;
  280. }
  281. frame->ident = XID_FORMAT;
  282. frame->saddr = cpu_to_le32(self->saddr);
  283. if (command)
  284. frame->daddr = cpu_to_le32(bcast);
  285. else
  286. frame->daddr = cpu_to_le32(discovery->data.daddr);
  287. switch (S) {
  288. case 1:
  289. frame->flags = 0x00;
  290. break;
  291. case 6:
  292. frame->flags = 0x01;
  293. break;
  294. case 8:
  295. frame->flags = 0x02;
  296. break;
  297. case 16:
  298. frame->flags = 0x03;
  299. break;
  300. default:
  301. frame->flags = 0x02;
  302. break;
  303. }
  304. frame->slotnr = s;
  305. frame->version = 0x00;
  306. /*
  307. * Provide info for final slot only in commands, and for all
  308. * responses. Send the second byte of the hint only if the
  309. * EXTENSION bit is set in the first byte.
  310. */
  311. if (!command || (frame->slotnr == 0xff)) {
  312. int len;
  313. if (discovery->data.hints[0] & HINT_EXTENSION) {
  314. info = skb_put(tx_skb, 2);
  315. info[0] = discovery->data.hints[0];
  316. info[1] = discovery->data.hints[1];
  317. } else {
  318. info = skb_put(tx_skb, 1);
  319. info[0] = discovery->data.hints[0];
  320. }
  321. info = skb_put(tx_skb, 1);
  322. info[0] = discovery->data.charset;
  323. len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
  324. info = skb_put(tx_skb, len);
  325. memcpy(info, discovery->data.info, len);
  326. }
  327. irlap_queue_xmit(self, tx_skb);
  328. }
  329. /*
  330. * Function irlap_recv_discovery_xid_rsp (skb, info)
  331. *
  332. * Received a XID discovery response
  333. *
  334. */
  335. static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
  336. struct sk_buff *skb,
  337. struct irlap_info *info)
  338. {
  339. struct xid_frame *xid;
  340. discovery_t *discovery = NULL;
  341. __u8 *discovery_info;
  342. char *text;
  343. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  344. IRDA_ASSERT(self != NULL, return;);
  345. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  346. if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
  347. IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
  348. return;
  349. }
  350. xid = (struct xid_frame *) skb->data;
  351. info->daddr = le32_to_cpu(xid->saddr);
  352. info->saddr = le32_to_cpu(xid->daddr);
  353. /* Make sure frame is addressed to us */
  354. if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
  355. IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
  356. __FUNCTION__);
  357. return;
  358. }
  359. if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
  360. IRDA_WARNING("%s: kmalloc failed!\n", __FUNCTION__);
  361. return;
  362. }
  363. discovery->data.daddr = info->daddr;
  364. discovery->data.saddr = self->saddr;
  365. discovery->timestamp = jiffies;
  366. IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
  367. discovery->data.daddr);
  368. discovery_info = skb_pull(skb, sizeof(struct xid_frame));
  369. /* Get info returned from peer */
  370. discovery->data.hints[0] = discovery_info[0];
  371. if (discovery_info[0] & HINT_EXTENSION) {
  372. IRDA_DEBUG(4, "EXTENSION\n");
  373. discovery->data.hints[1] = discovery_info[1];
  374. discovery->data.charset = discovery_info[2];
  375. text = (char *) &discovery_info[3];
  376. } else {
  377. discovery->data.hints[1] = 0;
  378. discovery->data.charset = discovery_info[1];
  379. text = (char *) &discovery_info[2];
  380. }
  381. /*
  382. * Terminate info string, should be safe since this is where the
  383. * FCS bytes resides.
  384. */
  385. skb->data[skb->len] = '\0';
  386. strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
  387. discovery->name_len = strlen(discovery->data.info);
  388. info->discovery = discovery;
  389. irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
  390. }
  391. /*
  392. * Function irlap_recv_discovery_xid_cmd (skb, info)
  393. *
  394. * Received a XID discovery command
  395. *
  396. */
  397. static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
  398. struct sk_buff *skb,
  399. struct irlap_info *info)
  400. {
  401. struct xid_frame *xid;
  402. discovery_t *discovery = NULL;
  403. __u8 *discovery_info;
  404. char *text;
  405. if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
  406. IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
  407. return;
  408. }
  409. xid = (struct xid_frame *) skb->data;
  410. info->daddr = le32_to_cpu(xid->saddr);
  411. info->saddr = le32_to_cpu(xid->daddr);
  412. /* Make sure frame is addressed to us */
  413. if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
  414. IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
  415. __FUNCTION__);
  416. return;
  417. }
  418. switch (xid->flags & 0x03) {
  419. case 0x00:
  420. info->S = 1;
  421. break;
  422. case 0x01:
  423. info->S = 6;
  424. break;
  425. case 0x02:
  426. info->S = 8;
  427. break;
  428. case 0x03:
  429. info->S = 16;
  430. break;
  431. default:
  432. /* Error!! */
  433. return;
  434. }
  435. info->s = xid->slotnr;
  436. discovery_info = skb_pull(skb, sizeof(struct xid_frame));
  437. /*
  438. * Check if last frame
  439. */
  440. if (info->s == 0xff) {
  441. /* Check if things are sane at this point... */
  442. if((discovery_info == NULL) ||
  443. !pskb_may_pull(skb, 3)) {
  444. IRDA_ERROR("%s: discovery frame too short!\n",
  445. __FUNCTION__);
  446. return;
  447. }
  448. /*
  449. * We now have some discovery info to deliver!
  450. */
  451. discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
  452. if (!discovery) {
  453. IRDA_WARNING("%s: unable to malloc!\n", __FUNCTION__);
  454. return;
  455. }
  456. discovery->data.daddr = info->daddr;
  457. discovery->data.saddr = self->saddr;
  458. discovery->timestamp = jiffies;
  459. discovery->data.hints[0] = discovery_info[0];
  460. if (discovery_info[0] & HINT_EXTENSION) {
  461. discovery->data.hints[1] = discovery_info[1];
  462. discovery->data.charset = discovery_info[2];
  463. text = (char *) &discovery_info[3];
  464. } else {
  465. discovery->data.hints[1] = 0;
  466. discovery->data.charset = discovery_info[1];
  467. text = (char *) &discovery_info[2];
  468. }
  469. /*
  470. * Terminate string, should be safe since this is where the
  471. * FCS bytes resides.
  472. */
  473. skb->data[skb->len] = '\0';
  474. strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
  475. discovery->name_len = strlen(discovery->data.info);
  476. info->discovery = discovery;
  477. } else
  478. info->discovery = NULL;
  479. irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
  480. }
  481. /*
  482. * Function irlap_send_rr_frame (self, command)
  483. *
  484. * Build and transmit RR (Receive Ready) frame. Notice that it is currently
  485. * only possible to send RR frames with the poll bit set.
  486. */
  487. void irlap_send_rr_frame(struct irlap_cb *self, int command)
  488. {
  489. struct sk_buff *tx_skb;
  490. struct rr_frame *frame;
  491. tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
  492. if (!tx_skb)
  493. return;
  494. frame = (struct rr_frame *)skb_put(tx_skb, 2);
  495. frame->caddr = self->caddr;
  496. frame->caddr |= (command) ? CMD_FRAME : 0;
  497. frame->control = RR | PF_BIT | (self->vr << 5);
  498. irlap_queue_xmit(self, tx_skb);
  499. }
  500. /*
  501. * Function irlap_send_rd_frame (self)
  502. *
  503. * Request disconnect. Used by a secondary station to request the
  504. * disconnection of the link.
  505. */
  506. void irlap_send_rd_frame(struct irlap_cb *self)
  507. {
  508. struct sk_buff *tx_skb;
  509. struct rd_frame *frame;
  510. tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
  511. if (!tx_skb)
  512. return;
  513. frame = (struct rd_frame *)skb_put(tx_skb, 2);
  514. frame->caddr = self->caddr;
  515. frame->caddr = RD_RSP | PF_BIT;
  516. irlap_queue_xmit(self, tx_skb);
  517. }
  518. /*
  519. * Function irlap_recv_rr_frame (skb, info)
  520. *
  521. * Received RR (Receive Ready) frame from peer station, no harm in
  522. * making it inline since its called only from one single place
  523. * (irlap_driver_rcv).
  524. */
  525. static inline void irlap_recv_rr_frame(struct irlap_cb *self,
  526. struct sk_buff *skb,
  527. struct irlap_info *info, int command)
  528. {
  529. info->nr = skb->data[1] >> 5;
  530. /* Check if this is a command or a response frame */
  531. if (command)
  532. irlap_do_event(self, RECV_RR_CMD, skb, info);
  533. else
  534. irlap_do_event(self, RECV_RR_RSP, skb, info);
  535. }
  536. /*
  537. * Function irlap_recv_rnr_frame (self, skb, info)
  538. *
  539. * Received RNR (Receive Not Ready) frame from peer station
  540. *
  541. */
  542. static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
  543. struct irlap_info *info, int command)
  544. {
  545. info->nr = skb->data[1] >> 5;
  546. IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
  547. if (command)
  548. irlap_do_event(self, RECV_RNR_CMD, skb, info);
  549. else
  550. irlap_do_event(self, RECV_RNR_RSP, skb, info);
  551. }
  552. static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
  553. struct irlap_info *info, int command)
  554. {
  555. IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
  556. info->nr = skb->data[1] >> 5;
  557. /* Check if this is a command or a response frame */
  558. if (command)
  559. irlap_do_event(self, RECV_REJ_CMD, skb, info);
  560. else
  561. irlap_do_event(self, RECV_REJ_RSP, skb, info);
  562. }
  563. static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
  564. struct irlap_info *info, int command)
  565. {
  566. IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
  567. info->nr = skb->data[1] >> 5;
  568. /* Check if this is a command or a response frame */
  569. if (command)
  570. irlap_do_event(self, RECV_SREJ_CMD, skb, info);
  571. else
  572. irlap_do_event(self, RECV_SREJ_RSP, skb, info);
  573. }
  574. static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
  575. struct irlap_info *info, int command)
  576. {
  577. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  578. /* Check if this is a command or a response frame */
  579. if (command)
  580. irlap_do_event(self, RECV_DISC_CMD, skb, info);
  581. else
  582. irlap_do_event(self, RECV_RD_RSP, skb, info);
  583. }
  584. /*
  585. * Function irlap_recv_ua_frame (skb, frame)
  586. *
  587. * Received UA (Unnumbered Acknowledgement) frame
  588. *
  589. */
  590. static inline void irlap_recv_ua_frame(struct irlap_cb *self,
  591. struct sk_buff *skb,
  592. struct irlap_info *info)
  593. {
  594. irlap_do_event(self, RECV_UA_RSP, skb, info);
  595. }
  596. /*
  597. * Function irlap_send_data_primary(self, skb)
  598. *
  599. * Send I-frames as the primary station but without the poll bit set
  600. *
  601. */
  602. void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
  603. {
  604. struct sk_buff *tx_skb;
  605. if (skb->data[1] == I_FRAME) {
  606. /*
  607. * Insert frame sequence number (Vs) in control field before
  608. * inserting into transmit window queue.
  609. */
  610. skb->data[1] = I_FRAME | (self->vs << 1);
  611. /*
  612. * Insert frame in store, in case of retransmissions
  613. * Increase skb reference count, see irlap_do_event()
  614. */
  615. skb_get(skb);
  616. skb_queue_tail(&self->wx_list, skb);
  617. /* Copy buffer */
  618. tx_skb = skb_clone(skb, GFP_ATOMIC);
  619. if (tx_skb == NULL) {
  620. return;
  621. }
  622. self->vs = (self->vs + 1) % 8;
  623. self->ack_required = FALSE;
  624. self->window -= 1;
  625. irlap_send_i_frame( self, tx_skb, CMD_FRAME);
  626. } else {
  627. IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
  628. irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
  629. self->window -= 1;
  630. }
  631. }
  632. /*
  633. * Function irlap_send_data_primary_poll (self, skb)
  634. *
  635. * Send I(nformation) frame as primary with poll bit set
  636. */
  637. void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
  638. {
  639. struct sk_buff *tx_skb;
  640. int transmission_time;
  641. /* Stop P timer */
  642. del_timer(&self->poll_timer);
  643. /* Is this reliable or unreliable data? */
  644. if (skb->data[1] == I_FRAME) {
  645. /*
  646. * Insert frame sequence number (Vs) in control field before
  647. * inserting into transmit window queue.
  648. */
  649. skb->data[1] = I_FRAME | (self->vs << 1);
  650. /*
  651. * Insert frame in store, in case of retransmissions
  652. * Increase skb reference count, see irlap_do_event()
  653. */
  654. skb_get(skb);
  655. skb_queue_tail(&self->wx_list, skb);
  656. /* Copy buffer */
  657. tx_skb = skb_clone(skb, GFP_ATOMIC);
  658. if (tx_skb == NULL) {
  659. return;
  660. }
  661. /*
  662. * Set poll bit if necessary. We do this to the copied
  663. * skb, since retransmitted need to set or clear the poll
  664. * bit depending on when they are sent.
  665. */
  666. tx_skb->data[1] |= PF_BIT;
  667. self->vs = (self->vs + 1) % 8;
  668. self->ack_required = FALSE;
  669. irlap_next_state(self, LAP_NRM_P);
  670. irlap_send_i_frame(self, tx_skb, CMD_FRAME);
  671. } else {
  672. IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
  673. if (self->ack_required) {
  674. irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
  675. irlap_next_state(self, LAP_NRM_P);
  676. irlap_send_rr_frame(self, CMD_FRAME);
  677. self->ack_required = FALSE;
  678. } else {
  679. skb->data[1] |= PF_BIT;
  680. irlap_next_state(self, LAP_NRM_P);
  681. irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
  682. }
  683. }
  684. /* How much time we took for transmission of all frames.
  685. * We don't know, so let assume we used the full window. Jean II */
  686. transmission_time = self->final_timeout;
  687. /* Reset parameter so that we can fill next window */
  688. self->window = self->window_size;
  689. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  690. /* Remove what we have not used. Just do a prorata of the
  691. * bytes left in window to window capacity.
  692. * See max_line_capacities[][] in qos.c for details. Jean II */
  693. transmission_time -= (self->final_timeout * self->bytes_left
  694. / self->line_capacity);
  695. IRDA_DEBUG(4, "%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n", __FUNCTION__, self->final_timeout, self->bytes_left, self->line_capacity, transmission_time);
  696. /* We are allowed to transmit a maximum number of bytes again. */
  697. self->bytes_left = self->line_capacity;
  698. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  699. /*
  700. * The network layer has a intermediate buffer between IrLAP
  701. * and the IrDA driver which can contain 8 frames. So, even
  702. * though IrLAP is currently sending the *last* frame of the
  703. * tx-window, the driver most likely has only just started
  704. * sending the *first* frame of the same tx-window.
  705. * I.e. we are always at the very begining of or Tx window.
  706. * Now, we are supposed to set the final timer from the end
  707. * of our tx-window to let the other peer reply. So, we need
  708. * to add extra time to compensate for the fact that we
  709. * are really at the start of tx-window, otherwise the final timer
  710. * might expire before he can answer...
  711. * Jean II
  712. */
  713. irlap_start_final_timer(self, self->final_timeout + transmission_time);
  714. /*
  715. * The clever amongst you might ask why we do this adjustement
  716. * only here, and not in all the other cases in irlap_event.c.
  717. * In all those other case, we only send a very short management
  718. * frame (few bytes), so the adjustement would be lost in the
  719. * noise...
  720. * The exception of course is irlap_resend_rejected_frame().
  721. * Jean II */
  722. }
  723. /*
  724. * Function irlap_send_data_secondary_final (self, skb)
  725. *
  726. * Send I(nformation) frame as secondary with final bit set
  727. *
  728. */
  729. void irlap_send_data_secondary_final(struct irlap_cb *self,
  730. struct sk_buff *skb)
  731. {
  732. struct sk_buff *tx_skb = NULL;
  733. IRDA_ASSERT(self != NULL, return;);
  734. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  735. IRDA_ASSERT(skb != NULL, return;);
  736. /* Is this reliable or unreliable data? */
  737. if (skb->data[1] == I_FRAME) {
  738. /*
  739. * Insert frame sequence number (Vs) in control field before
  740. * inserting into transmit window queue.
  741. */
  742. skb->data[1] = I_FRAME | (self->vs << 1);
  743. /*
  744. * Insert frame in store, in case of retransmissions
  745. * Increase skb reference count, see irlap_do_event()
  746. */
  747. skb_get(skb);
  748. skb_queue_tail(&self->wx_list, skb);
  749. tx_skb = skb_clone(skb, GFP_ATOMIC);
  750. if (tx_skb == NULL) {
  751. return;
  752. }
  753. tx_skb->data[1] |= PF_BIT;
  754. self->vs = (self->vs + 1) % 8;
  755. self->ack_required = FALSE;
  756. irlap_send_i_frame(self, tx_skb, RSP_FRAME);
  757. } else {
  758. if (self->ack_required) {
  759. irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
  760. irlap_send_rr_frame(self, RSP_FRAME);
  761. self->ack_required = FALSE;
  762. } else {
  763. skb->data[1] |= PF_BIT;
  764. irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
  765. }
  766. }
  767. self->window = self->window_size;
  768. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  769. /* We are allowed to transmit a maximum number of bytes again. */
  770. self->bytes_left = self->line_capacity;
  771. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  772. irlap_start_wd_timer(self, self->wd_timeout);
  773. }
  774. /*
  775. * Function irlap_send_data_secondary (self, skb)
  776. *
  777. * Send I(nformation) frame as secondary without final bit set
  778. *
  779. */
  780. void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
  781. {
  782. struct sk_buff *tx_skb = NULL;
  783. /* Is this reliable or unreliable data? */
  784. if (skb->data[1] == I_FRAME) {
  785. /*
  786. * Insert frame sequence number (Vs) in control field before
  787. * inserting into transmit window queue.
  788. */
  789. skb->data[1] = I_FRAME | (self->vs << 1);
  790. /*
  791. * Insert frame in store, in case of retransmissions
  792. * Increase skb reference count, see irlap_do_event()
  793. */
  794. skb_get(skb);
  795. skb_queue_tail(&self->wx_list, skb);
  796. tx_skb = skb_clone(skb, GFP_ATOMIC);
  797. if (tx_skb == NULL) {
  798. return;
  799. }
  800. self->vs = (self->vs + 1) % 8;
  801. self->ack_required = FALSE;
  802. self->window -= 1;
  803. irlap_send_i_frame(self, tx_skb, RSP_FRAME);
  804. } else {
  805. irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
  806. self->window -= 1;
  807. }
  808. }
  809. /*
  810. * Function irlap_resend_rejected_frames (nr)
  811. *
  812. * Resend frames which has not been acknowledged. Should be safe to
  813. * traverse the list without locking it since this function will only be
  814. * called from interrupt context (BH)
  815. */
  816. void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
  817. {
  818. struct sk_buff *tx_skb;
  819. struct sk_buff *skb;
  820. int count;
  821. IRDA_ASSERT(self != NULL, return;);
  822. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  823. /* Initialize variables */
  824. count = skb_queue_len(&self->wx_list);
  825. /* Resend unacknowledged frame(s) */
  826. skb = skb_peek(&self->wx_list);
  827. while (skb != NULL) {
  828. irlap_wait_min_turn_around(self, &self->qos_tx);
  829. /* We copy the skb to be retransmitted since we will have to
  830. * modify it. Cloning will confuse packet sniffers
  831. */
  832. /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
  833. tx_skb = skb_copy(skb, GFP_ATOMIC);
  834. if (!tx_skb) {
  835. IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
  836. return;
  837. }
  838. /* Clear old Nr field + poll bit */
  839. tx_skb->data[1] &= 0x0f;
  840. /*
  841. * Set poll bit on the last frame retransmitted
  842. */
  843. if (count-- == 1)
  844. tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
  845. else
  846. tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
  847. irlap_send_i_frame(self, tx_skb, command);
  848. /*
  849. * If our skb is the last buffer in the list, then
  850. * we are finished, if not, move to the next sk-buffer
  851. */
  852. if (skb == skb_peek_tail(&self->wx_list))
  853. skb = NULL;
  854. else
  855. skb = skb->next;
  856. }
  857. #if 0 /* Not yet */
  858. /*
  859. * We can now fill the window with additional data frames
  860. */
  861. while (!skb_queue_empty(&self->txq)) {
  862. IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
  863. if (self->window > 0) {
  864. skb = skb_dequeue( &self->txq);
  865. IRDA_ASSERT(skb != NULL, return;);
  866. /*
  867. * If send window > 1 then send frame with pf
  868. * bit cleared
  869. */
  870. if ((self->window > 1) &&
  871. !skb_queue_empty(&self->txq)) {
  872. irlap_send_data_primary(self, skb);
  873. } else {
  874. irlap_send_data_primary_poll(self, skb);
  875. }
  876. kfree_skb(skb);
  877. }
  878. }
  879. #endif
  880. }
  881. void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
  882. {
  883. struct sk_buff *tx_skb;
  884. struct sk_buff *skb;
  885. IRDA_ASSERT(self != NULL, return;);
  886. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  887. /* Resend unacknowledged frame(s) */
  888. skb = skb_peek(&self->wx_list);
  889. if (skb != NULL) {
  890. irlap_wait_min_turn_around(self, &self->qos_tx);
  891. /* We copy the skb to be retransmitted since we will have to
  892. * modify it. Cloning will confuse packet sniffers
  893. */
  894. /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
  895. tx_skb = skb_copy(skb, GFP_ATOMIC);
  896. if (!tx_skb) {
  897. IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
  898. return;
  899. }
  900. /* Clear old Nr field + poll bit */
  901. tx_skb->data[1] &= 0x0f;
  902. /* Set poll/final bit */
  903. tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
  904. irlap_send_i_frame(self, tx_skb, command);
  905. }
  906. }
  907. /*
  908. * Function irlap_send_ui_frame (self, skb, command)
  909. *
  910. * Contruct and transmit an Unnumbered Information (UI) frame
  911. *
  912. */
  913. void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
  914. __u8 caddr, int command)
  915. {
  916. IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
  917. IRDA_ASSERT(self != NULL, return;);
  918. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  919. IRDA_ASSERT(skb != NULL, return;);
  920. /* Insert connection address */
  921. skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
  922. irlap_queue_xmit(self, skb);
  923. }
  924. /*
  925. * Function irlap_send_i_frame (skb)
  926. *
  927. * Contruct and transmit Information (I) frame
  928. */
  929. static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
  930. int command)
  931. {
  932. /* Insert connection address */
  933. skb->data[0] = self->caddr;
  934. skb->data[0] |= (command) ? CMD_FRAME : 0;
  935. /* Insert next to receive (Vr) */
  936. skb->data[1] |= (self->vr << 5); /* insert nr */
  937. irlap_queue_xmit(self, skb);
  938. }
  939. /*
  940. * Function irlap_recv_i_frame (skb, frame)
  941. *
  942. * Receive and parse an I (Information) frame, no harm in making it inline
  943. * since it's called only from one single place (irlap_driver_rcv).
  944. */
  945. static inline void irlap_recv_i_frame(struct irlap_cb *self,
  946. struct sk_buff *skb,
  947. struct irlap_info *info, int command)
  948. {
  949. info->nr = skb->data[1] >> 5; /* Next to receive */
  950. info->pf = skb->data[1] & PF_BIT; /* Final bit */
  951. info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
  952. /* Check if this is a command or a response frame */
  953. if (command)
  954. irlap_do_event(self, RECV_I_CMD, skb, info);
  955. else
  956. irlap_do_event(self, RECV_I_RSP, skb, info);
  957. }
  958. /*
  959. * Function irlap_recv_ui_frame (self, skb, info)
  960. *
  961. * Receive and parse an Unnumbered Information (UI) frame
  962. *
  963. */
  964. static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
  965. struct irlap_info *info)
  966. {
  967. IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
  968. info->pf = skb->data[1] & PF_BIT; /* Final bit */
  969. irlap_do_event(self, RECV_UI_FRAME, skb, info);
  970. }
  971. /*
  972. * Function irlap_recv_frmr_frame (skb, frame)
  973. *
  974. * Received Frame Reject response.
  975. *
  976. */
  977. static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
  978. struct irlap_info *info)
  979. {
  980. __u8 *frame;
  981. int w, x, y, z;
  982. IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
  983. IRDA_ASSERT(self != NULL, return;);
  984. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  985. IRDA_ASSERT(skb != NULL, return;);
  986. IRDA_ASSERT(info != NULL, return;);
  987. if (!pskb_may_pull(skb, 4)) {
  988. IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
  989. return;
  990. }
  991. frame = skb->data;
  992. info->nr = frame[2] >> 5; /* Next to receive */
  993. info->pf = frame[2] & PF_BIT; /* Final bit */
  994. info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
  995. w = frame[3] & 0x01;
  996. x = frame[3] & 0x02;
  997. y = frame[3] & 0x04;
  998. z = frame[3] & 0x08;
  999. if (w) {
  1000. IRDA_DEBUG(0, "Rejected control field is undefined or not "
  1001. "implemented.\n");
  1002. }
  1003. if (x) {
  1004. IRDA_DEBUG(0, "Rejected control field was invalid because it "
  1005. "contained a non permitted I field.\n");
  1006. }
  1007. if (y) {
  1008. IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
  1009. "for the existing connection or exceeded the maximum "
  1010. "this station supports if no connection exists.\n");
  1011. }
  1012. if (z) {
  1013. IRDA_DEBUG(0, "Rejected control field control field contained an "
  1014. "invalid Nr count.\n");
  1015. }
  1016. irlap_do_event(self, RECV_FRMR_RSP, skb, info);
  1017. }
  1018. /*
  1019. * Function irlap_send_test_frame (self, daddr)
  1020. *
  1021. * Send a test frame response
  1022. *
  1023. */
  1024. void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
  1025. struct sk_buff *cmd)
  1026. {
  1027. struct sk_buff *tx_skb;
  1028. struct test_frame *frame;
  1029. __u8 *info;
  1030. tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
  1031. if (!tx_skb)
  1032. return;
  1033. /* Broadcast frames must include saddr and daddr fields */
  1034. if (caddr == CBROADCAST) {
  1035. frame = (struct test_frame *)
  1036. skb_put(tx_skb, sizeof(struct test_frame));
  1037. /* Insert the swapped addresses */
  1038. frame->saddr = cpu_to_le32(self->saddr);
  1039. frame->daddr = cpu_to_le32(daddr);
  1040. } else
  1041. frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
  1042. frame->caddr = caddr;
  1043. frame->control = TEST_RSP | PF_BIT;
  1044. /* Copy info */
  1045. info = skb_put(tx_skb, cmd->len);
  1046. memcpy(info, cmd->data, cmd->len);
  1047. /* Return to sender */
  1048. irlap_wait_min_turn_around(self, &self->qos_tx);
  1049. irlap_queue_xmit(self, tx_skb);
  1050. }
  1051. /*
  1052. * Function irlap_recv_test_frame (self, skb)
  1053. *
  1054. * Receive a test frame
  1055. *
  1056. */
  1057. static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
  1058. struct irlap_info *info, int command)
  1059. {
  1060. struct test_frame *frame;
  1061. IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
  1062. if (!pskb_may_pull(skb, sizeof(*frame))) {
  1063. IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
  1064. return;
  1065. }
  1066. frame = (struct test_frame *) skb->data;
  1067. /* Broadcast frames must carry saddr and daddr fields */
  1068. if (info->caddr == CBROADCAST) {
  1069. if (skb->len < sizeof(struct test_frame)) {
  1070. IRDA_DEBUG(0, "%s() test frame too short!\n",
  1071. __FUNCTION__);
  1072. return;
  1073. }
  1074. /* Read and swap addresses */
  1075. info->daddr = le32_to_cpu(frame->saddr);
  1076. info->saddr = le32_to_cpu(frame->daddr);
  1077. /* Make sure frame is addressed to us */
  1078. if ((info->saddr != self->saddr) &&
  1079. (info->saddr != BROADCAST)) {
  1080. return;
  1081. }
  1082. }
  1083. if (command)
  1084. irlap_do_event(self, RECV_TEST_CMD, skb, info);
  1085. else
  1086. irlap_do_event(self, RECV_TEST_RSP, skb, info);
  1087. }
  1088. /*
  1089. * Function irlap_driver_rcv (skb, netdev, ptype)
  1090. *
  1091. * Called when a frame is received. Dispatches the right receive function
  1092. * for processing of the frame.
  1093. *
  1094. * Note on skb management :
  1095. * After calling the higher layers of the IrDA stack, we always
  1096. * kfree() the skb, which drop the reference count (and potentially
  1097. * destroy it).
  1098. * If a higher layer of the stack want to keep the skb around (to put
  1099. * in a queue or pass it to the higher layer), it will need to use
  1100. * skb_get() to keep a reference on it. This is usually done at the
  1101. * LMP level in irlmp.c.
  1102. * Jean II
  1103. */
  1104. int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
  1105. struct packet_type *ptype, struct net_device *orig_dev)
  1106. {
  1107. struct irlap_info info;
  1108. struct irlap_cb *self;
  1109. int command;
  1110. __u8 control;
  1111. /* FIXME: should we get our own field? */
  1112. self = (struct irlap_cb *) dev->atalk_ptr;
  1113. /* If the net device is down, then IrLAP is gone! */
  1114. if (!self || self->magic != LAP_MAGIC) {
  1115. dev_kfree_skb(skb);
  1116. return -1;
  1117. }
  1118. /* We are no longer an "old" protocol, so we need to handle
  1119. * share and non linear skbs. This should never happen, so
  1120. * we don't need to be clever about it. Jean II */
  1121. if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
  1122. IRDA_ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
  1123. dev_kfree_skb(skb);
  1124. return -1;
  1125. }
  1126. /* Check if frame is large enough for parsing */
  1127. if (!pskb_may_pull(skb, 2)) {
  1128. IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
  1129. dev_kfree_skb(skb);
  1130. return -1;
  1131. }
  1132. command = skb->data[0] & CMD_FRAME;
  1133. info.caddr = skb->data[0] & CBROADCAST;
  1134. info.pf = skb->data[1] & PF_BIT;
  1135. info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
  1136. control = info.control;
  1137. /* First we check if this frame has a valid connection address */
  1138. if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
  1139. IRDA_DEBUG(0, "%s(), wrong connection address!\n",
  1140. __FUNCTION__);
  1141. goto out;
  1142. }
  1143. /*
  1144. * Optimize for the common case and check if the frame is an
  1145. * I(nformation) frame. Only I-frames have bit 0 set to 0
  1146. */
  1147. if (~control & 0x01) {
  1148. irlap_recv_i_frame(self, skb, &info, command);
  1149. goto out;
  1150. }
  1151. /*
  1152. * We now check is the frame is an S(upervisory) frame. Only
  1153. * S-frames have bit 0 set to 1 and bit 1 set to 0
  1154. */
  1155. if (~control & 0x02) {
  1156. /*
  1157. * Received S(upervisory) frame, check which frame type it is
  1158. * only the first nibble is of interest
  1159. */
  1160. switch (control & 0x0f) {
  1161. case RR:
  1162. irlap_recv_rr_frame(self, skb, &info, command);
  1163. break;
  1164. case RNR:
  1165. irlap_recv_rnr_frame(self, skb, &info, command);
  1166. break;
  1167. case REJ:
  1168. irlap_recv_rej_frame(self, skb, &info, command);
  1169. break;
  1170. case SREJ:
  1171. irlap_recv_srej_frame(self, skb, &info, command);
  1172. break;
  1173. default:
  1174. IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
  1175. __FUNCTION__, info.control);
  1176. break;
  1177. }
  1178. goto out;
  1179. }
  1180. /*
  1181. * This must be a C(ontrol) frame
  1182. */
  1183. switch (control) {
  1184. case XID_RSP:
  1185. irlap_recv_discovery_xid_rsp(self, skb, &info);
  1186. break;
  1187. case XID_CMD:
  1188. irlap_recv_discovery_xid_cmd(self, skb, &info);
  1189. break;
  1190. case SNRM_CMD:
  1191. irlap_recv_snrm_cmd(self, skb, &info);
  1192. break;
  1193. case DM_RSP:
  1194. irlap_do_event(self, RECV_DM_RSP, skb, &info);
  1195. break;
  1196. case DISC_CMD: /* And RD_RSP since they have the same value */
  1197. irlap_recv_disc_frame(self, skb, &info, command);
  1198. break;
  1199. case TEST_CMD:
  1200. irlap_recv_test_frame(self, skb, &info, command);
  1201. break;
  1202. case UA_RSP:
  1203. irlap_recv_ua_frame(self, skb, &info);
  1204. break;
  1205. case FRMR_RSP:
  1206. irlap_recv_frmr_frame(self, skb, &info);
  1207. break;
  1208. case UI_FRAME:
  1209. irlap_recv_ui_frame(self, skb, &info);
  1210. break;
  1211. default:
  1212. IRDA_WARNING("%s: Unknown frame %02x received!\n",
  1213. __FUNCTION__, info.control);
  1214. break;
  1215. }
  1216. out:
  1217. /* Always drop our reference on the skb */
  1218. dev_kfree_skb(skb);
  1219. return 0;
  1220. }