irlap_frame.c 36 KB

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