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