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