irlap_frame.c 36 KB

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