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