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