irlap_frame.c 35 KB

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