irlap_frame.c 36 KB

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