irlap_frame.c 36 KB

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