irlap_frame.c 36 KB

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