irlap_frame.c 35 KB

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