1 /*********************************************************************
3 * Filename: irlap_frame.c
5 * Description: Build and transmit IrLAP frames
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>
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>
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.
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.
25 ********************************************************************/
27 #include <linux/skbuff.h>
29 #include <linux/if_ether.h>
30 #include <linux/netdevice.h>
31 #include <linux/irda.h>
33 #include <net/pkt_sched.h>
36 #include <asm/byteorder.h>
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>
46 static void irlap_send_i_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
50 * Function irlap_insert_info (self, skb)
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
56 static inline void irlap_insert_info(struct irlap_cb
*self
,
59 struct irda_skb_cb
*cb
= (struct irda_skb_cb
*) skb
->cb
;
62 * Insert MTT (min. turn time) and speed into skb, so that the
63 * device driver knows which settings to use
65 cb
->magic
= LAP_MAGIC
;
66 cb
->mtt
= self
->mtt_required
;
67 cb
->next_speed
= self
->speed
;
70 self
->mtt_required
= 0;
73 * Delay equals negotiated BOFs count, plus the number of BOFs to
74 * force the negotiated minimum turnaround time
76 cb
->xbofs
= self
->bofs_count
;
77 cb
->next_xbofs
= self
->next_bofs
;
78 cb
->xbofs_delay
= self
->xbofs_delay
;
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
;
87 * Function irlap_queue_xmit (self, skb)
89 * A little wrapper for dev_queue_xmit, so we can insert some common
92 void irlap_queue_xmit(struct irlap_cb
*self
, struct sk_buff
*skb
)
94 /* Some common init stuff */
95 skb
->dev
= self
->netdev
;
96 skb_reset_mac_header(skb
);
97 skb
->h
.raw
= skb
->nh
.raw
= skb
->data
;
98 skb
->protocol
= htons(ETH_P_IRDA
);
99 skb
->priority
= TC_PRIO_BESTEFFORT
;
101 irlap_insert_info(self
, skb
);
107 * Function irlap_send_snrm_cmd (void)
109 * Transmits a connect SNRM command frame
111 void irlap_send_snrm_frame(struct irlap_cb
*self
, struct qos_info
*qos
)
113 struct sk_buff
*tx_skb
;
114 struct snrm_frame
*frame
;
117 IRDA_ASSERT(self
!= NULL
, return;);
118 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
121 tx_skb
= alloc_skb(sizeof(struct snrm_frame
) +
122 IRLAP_NEGOCIATION_PARAMS_LEN
,
127 frame
= (struct snrm_frame
*) skb_put(tx_skb
, 2);
129 /* Insert connection address field */
131 frame
->caddr
= CMD_FRAME
| CBROADCAST
;
133 frame
->caddr
= CMD_FRAME
| self
->caddr
;
135 /* Insert control field */
136 frame
->control
= SNRM_CMD
| PF_BIT
;
139 * If we are establishing a connection then insert QoS paramerters
142 skb_put(tx_skb
, 9); /* 25 left */
143 frame
->saddr
= cpu_to_le32(self
->saddr
);
144 frame
->daddr
= cpu_to_le32(self
->daddr
);
146 frame
->ncaddr
= self
->caddr
;
148 ret
= irlap_insert_qos_negotiation_params(self
, tx_skb
);
150 dev_kfree_skb(tx_skb
);
154 irlap_queue_xmit(self
, tx_skb
);
158 * Function irlap_recv_snrm_cmd (skb, info)
160 * Received SNRM (Set Normal Response Mode) command frame
163 static void irlap_recv_snrm_cmd(struct irlap_cb
*self
, struct sk_buff
*skb
,
164 struct irlap_info
*info
)
166 struct snrm_frame
*frame
;
168 if (pskb_may_pull(skb
,sizeof(struct snrm_frame
))) {
169 frame
= (struct snrm_frame
*) skb
->data
;
171 /* Copy the new connection address ignoring the C/R bit */
172 info
->caddr
= frame
->ncaddr
& 0xFE;
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",
181 /* Copy peer device address */
182 info
->daddr
= le32_to_cpu(frame
->saddr
);
183 info
->saddr
= le32_to_cpu(frame
->daddr
);
185 /* Only accept if addressed directly to us */
186 if (info
->saddr
!= self
->saddr
) {
187 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
191 irlap_do_event(self
, RECV_SNRM_CMD
, skb
, info
);
193 /* Signal that this SNRM frame does not contain and I-field */
194 irlap_do_event(self
, RECV_SNRM_CMD
, skb
, NULL
);
199 * Function irlap_send_ua_response_frame (qos)
201 * Send UA (Unnumbered Acknowledgement) frame
204 void irlap_send_ua_response_frame(struct irlap_cb
*self
, struct qos_info
*qos
)
206 struct sk_buff
*tx_skb
;
207 struct ua_frame
*frame
;
210 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__
, jiffies
);
212 IRDA_ASSERT(self
!= NULL
, return;);
213 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
216 tx_skb
= alloc_skb(sizeof(struct ua_frame
) +
217 IRLAP_NEGOCIATION_PARAMS_LEN
,
222 frame
= (struct ua_frame
*) skb_put(tx_skb
, 10);
224 /* Build UA response */
225 frame
->caddr
= self
->caddr
;
226 frame
->control
= UA_RSP
| PF_BIT
;
228 frame
->saddr
= cpu_to_le32(self
->saddr
);
229 frame
->daddr
= cpu_to_le32(self
->daddr
);
231 /* Should we send QoS negotiation parameters? */
233 ret
= irlap_insert_qos_negotiation_params(self
, tx_skb
);
235 dev_kfree_skb(tx_skb
);
240 irlap_queue_xmit(self
, tx_skb
);
245 * Function irlap_send_dm_frame (void)
247 * Send disconnected mode (DM) frame
250 void irlap_send_dm_frame( struct irlap_cb
*self
)
252 struct sk_buff
*tx_skb
= NULL
;
253 struct dm_frame
*frame
;
255 IRDA_ASSERT(self
!= NULL
, return;);
256 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
258 tx_skb
= alloc_skb(sizeof(struct dm_frame
), GFP_ATOMIC
);
262 frame
= (struct dm_frame
*)skb_put(tx_skb
, 2);
264 if (self
->state
== LAP_NDM
)
265 frame
->caddr
= CBROADCAST
;
267 frame
->caddr
= self
->caddr
;
269 frame
->control
= DM_RSP
| PF_BIT
;
271 irlap_queue_xmit(self
, tx_skb
);
275 * Function irlap_send_disc_frame (void)
277 * Send disconnect (DISC) frame
280 void irlap_send_disc_frame(struct irlap_cb
*self
)
282 struct sk_buff
*tx_skb
= NULL
;
283 struct disc_frame
*frame
;
285 IRDA_DEBUG(3, "%s()\n", __FUNCTION__
);
287 IRDA_ASSERT(self
!= NULL
, return;);
288 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
290 tx_skb
= alloc_skb(sizeof(struct disc_frame
), GFP_ATOMIC
);
294 frame
= (struct disc_frame
*)skb_put(tx_skb
, 2);
296 frame
->caddr
= self
->caddr
| CMD_FRAME
;
297 frame
->control
= DISC_CMD
| PF_BIT
;
299 irlap_queue_xmit(self
, tx_skb
);
303 * Function irlap_send_discovery_xid_frame (S, s, command)
305 * Build and transmit a XID (eXchange station IDentifier) discovery
308 void irlap_send_discovery_xid_frame(struct irlap_cb
*self
, int S
, __u8 s
,
309 __u8 command
, discovery_t
*discovery
)
311 struct sk_buff
*tx_skb
= NULL
;
312 struct xid_frame
*frame
;
313 __u32 bcast
= BROADCAST
;
316 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__
,
319 IRDA_ASSERT(self
!= NULL
, return;);
320 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
321 IRDA_ASSERT(discovery
!= NULL
, return;);
323 tx_skb
= alloc_skb(sizeof(struct xid_frame
) + IRLAP_DISCOVERY_INFO_LEN
,
329 frame
= (struct xid_frame
*) tx_skb
->data
;
332 frame
->caddr
= CBROADCAST
| CMD_FRAME
;
333 frame
->control
= XID_CMD
| PF_BIT
;
335 frame
->caddr
= CBROADCAST
;
336 frame
->control
= XID_RSP
| PF_BIT
;
338 frame
->ident
= XID_FORMAT
;
340 frame
->saddr
= cpu_to_le32(self
->saddr
);
343 frame
->daddr
= cpu_to_le32(bcast
);
345 frame
->daddr
= cpu_to_le32(discovery
->data
.daddr
);
366 frame
->version
= 0x00;
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.
373 if (!command
|| (frame
->slotnr
== 0xff)) {
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];
381 info
= skb_put(tx_skb
, 1);
382 info
[0] = discovery
->data
.hints
[0];
384 info
= skb_put(tx_skb
, 1);
385 info
[0] = discovery
->data
.charset
;
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
);
391 irlap_queue_xmit(self
, tx_skb
);
395 * Function irlap_recv_discovery_xid_rsp (skb, info)
397 * Received a XID discovery response
400 static void irlap_recv_discovery_xid_rsp(struct irlap_cb
*self
,
402 struct irlap_info
*info
)
404 struct xid_frame
*xid
;
405 discovery_t
*discovery
= NULL
;
406 __u8
*discovery_info
;
409 IRDA_DEBUG(4, "%s()\n", __FUNCTION__
);
411 IRDA_ASSERT(self
!= NULL
, return;);
412 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
414 if (!pskb_may_pull(skb
, sizeof(struct xid_frame
))) {
415 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__
);
419 xid
= (struct xid_frame
*) skb
->data
;
421 info
->daddr
= le32_to_cpu(xid
->saddr
);
422 info
->saddr
= le32_to_cpu(xid
->daddr
);
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",
431 if ((discovery
= kzalloc(sizeof(discovery_t
), GFP_ATOMIC
)) == NULL
) {
432 IRDA_WARNING("%s: kmalloc failed!\n", __FUNCTION__
);
436 discovery
->data
.daddr
= info
->daddr
;
437 discovery
->data
.saddr
= self
->saddr
;
438 discovery
->timestamp
= jiffies
;
440 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__
,
441 discovery
->data
.daddr
);
443 discovery_info
= skb_pull(skb
, sizeof(struct xid_frame
));
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];
453 discovery
->data
.hints
[1] = 0;
454 discovery
->data
.charset
= discovery_info
[1];
455 text
= (char *) &discovery_info
[2];
458 * Terminate info string, should be safe since this is where the
461 skb
->data
[skb
->len
] = '\0';
462 strncpy(discovery
->data
.info
, text
, NICKNAME_MAX_LEN
);
463 discovery
->name_len
= strlen(discovery
->data
.info
);
465 info
->discovery
= discovery
;
467 irlap_do_event(self
, RECV_DISCOVERY_XID_RSP
, skb
, info
);
471 * Function irlap_recv_discovery_xid_cmd (skb, info)
473 * Received a XID discovery command
476 static void irlap_recv_discovery_xid_cmd(struct irlap_cb
*self
,
478 struct irlap_info
*info
)
480 struct xid_frame
*xid
;
481 discovery_t
*discovery
= NULL
;
482 __u8
*discovery_info
;
485 if (!pskb_may_pull(skb
, sizeof(struct xid_frame
))) {
486 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__
);
490 xid
= (struct xid_frame
*) skb
->data
;
492 info
->daddr
= le32_to_cpu(xid
->saddr
);
493 info
->saddr
= le32_to_cpu(xid
->daddr
);
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",
502 switch (xid
->flags
& 0x03) {
519 info
->s
= xid
->slotnr
;
521 discovery_info
= skb_pull(skb
, sizeof(struct xid_frame
));
524 * Check if last frame
526 if (info
->s
== 0xff) {
527 /* Check if things are sane at this point... */
528 if((discovery_info
== NULL
) ||
529 !pskb_may_pull(skb
, 3)) {
530 IRDA_ERROR("%s: discovery frame to short!\n",
536 * We now have some discovery info to deliver!
538 discovery
= kmalloc(sizeof(discovery_t
), GFP_ATOMIC
);
540 IRDA_WARNING("%s: unable to malloc!\n", __FUNCTION__
);
544 discovery
->data
.daddr
= info
->daddr
;
545 discovery
->data
.saddr
= self
->saddr
;
546 discovery
->timestamp
= jiffies
;
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];
554 discovery
->data
.hints
[1] = 0;
555 discovery
->data
.charset
= discovery_info
[1];
556 text
= (char *) &discovery_info
[2];
559 * Terminate string, should be safe since this is where the
562 skb
->data
[skb
->len
] = '\0';
563 strncpy(discovery
->data
.info
, text
, NICKNAME_MAX_LEN
);
564 discovery
->name_len
= strlen(discovery
->data
.info
);
566 info
->discovery
= discovery
;
568 info
->discovery
= NULL
;
570 irlap_do_event(self
, RECV_DISCOVERY_XID_CMD
, skb
, info
);
574 * Function irlap_send_rr_frame (self, command)
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.
579 void irlap_send_rr_frame(struct irlap_cb
*self
, int command
)
581 struct sk_buff
*tx_skb
;
582 struct rr_frame
*frame
;
584 tx_skb
= alloc_skb(sizeof(struct rr_frame
), GFP_ATOMIC
);
588 frame
= (struct rr_frame
*)skb_put(tx_skb
, 2);
590 frame
->caddr
= self
->caddr
;
591 frame
->caddr
|= (command
) ? CMD_FRAME
: 0;
593 frame
->control
= RR
| PF_BIT
| (self
->vr
<< 5);
595 irlap_queue_xmit(self
, tx_skb
);
599 * Function irlap_send_rd_frame (self)
601 * Request disconnect. Used by a secondary station to request the
602 * disconnection of the link.
604 void irlap_send_rd_frame(struct irlap_cb
*self
)
606 struct sk_buff
*tx_skb
;
607 struct rd_frame
*frame
;
609 tx_skb
= alloc_skb(sizeof(struct rd_frame
), GFP_ATOMIC
);
613 frame
= (struct rd_frame
*)skb_put(tx_skb
, 2);
615 frame
->caddr
= self
->caddr
;
616 frame
->caddr
= RD_RSP
| PF_BIT
;
618 irlap_queue_xmit(self
, tx_skb
);
622 * Function irlap_recv_rr_frame (skb, info)
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).
628 static inline void irlap_recv_rr_frame(struct irlap_cb
*self
,
630 struct irlap_info
*info
, int command
)
632 info
->nr
= skb
->data
[1] >> 5;
634 /* Check if this is a command or a response frame */
636 irlap_do_event(self
, RECV_RR_CMD
, skb
, info
);
638 irlap_do_event(self
, RECV_RR_RSP
, skb
, info
);
642 * Function irlap_recv_rnr_frame (self, skb, info)
644 * Received RNR (Receive Not Ready) frame from peer station
647 static void irlap_recv_rnr_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
648 struct irlap_info
*info
, int command
)
650 info
->nr
= skb
->data
[1] >> 5;
652 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__
, info
->nr
, jiffies
);
655 irlap_do_event(self
, RECV_RNR_CMD
, skb
, info
);
657 irlap_do_event(self
, RECV_RNR_RSP
, skb
, info
);
660 static void irlap_recv_rej_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
661 struct irlap_info
*info
, int command
)
663 IRDA_DEBUG(0, "%s()\n", __FUNCTION__
);
665 info
->nr
= skb
->data
[1] >> 5;
667 /* Check if this is a command or a response frame */
669 irlap_do_event(self
, RECV_REJ_CMD
, skb
, info
);
671 irlap_do_event(self
, RECV_REJ_RSP
, skb
, info
);
674 static void irlap_recv_srej_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
675 struct irlap_info
*info
, int command
)
677 IRDA_DEBUG(0, "%s()\n", __FUNCTION__
);
679 info
->nr
= skb
->data
[1] >> 5;
681 /* Check if this is a command or a response frame */
683 irlap_do_event(self
, RECV_SREJ_CMD
, skb
, info
);
685 irlap_do_event(self
, RECV_SREJ_RSP
, skb
, info
);
688 static void irlap_recv_disc_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
689 struct irlap_info
*info
, int command
)
691 IRDA_DEBUG(2, "%s()\n", __FUNCTION__
);
693 /* Check if this is a command or a response frame */
695 irlap_do_event(self
, RECV_DISC_CMD
, skb
, info
);
697 irlap_do_event(self
, RECV_RD_RSP
, skb
, info
);
701 * Function irlap_recv_ua_frame (skb, frame)
703 * Received UA (Unnumbered Acknowledgement) frame
706 static inline void irlap_recv_ua_frame(struct irlap_cb
*self
,
708 struct irlap_info
*info
)
710 irlap_do_event(self
, RECV_UA_RSP
, skb
, info
);
714 * Function irlap_send_data_primary(self, skb)
716 * Send I-frames as the primary station but without the poll bit set
719 void irlap_send_data_primary(struct irlap_cb
*self
, struct sk_buff
*skb
)
721 struct sk_buff
*tx_skb
;
723 if (skb
->data
[1] == I_FRAME
) {
726 * Insert frame sequence number (Vs) in control field before
727 * inserting into transmit window queue.
729 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
732 * Insert frame in store, in case of retransmissions
733 * Increase skb reference count, see irlap_do_event()
736 skb_queue_tail(&self
->wx_list
, skb
);
739 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
740 if (tx_skb
== NULL
) {
744 self
->vs
= (self
->vs
+ 1) % 8;
745 self
->ack_required
= FALSE
;
748 irlap_send_i_frame( self
, tx_skb
, CMD_FRAME
);
750 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__
);
751 irlap_send_ui_frame(self
, skb_get(skb
), self
->caddr
, CMD_FRAME
);
756 * Function irlap_send_data_primary_poll (self, skb)
758 * Send I(nformation) frame as primary with poll bit set
760 void irlap_send_data_primary_poll(struct irlap_cb
*self
, struct sk_buff
*skb
)
762 struct sk_buff
*tx_skb
;
763 int transmission_time
;
766 del_timer(&self
->poll_timer
);
768 /* Is this reliable or unreliable data? */
769 if (skb
->data
[1] == I_FRAME
) {
772 * Insert frame sequence number (Vs) in control field before
773 * inserting into transmit window queue.
775 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
778 * Insert frame in store, in case of retransmissions
779 * Increase skb reference count, see irlap_do_event()
782 skb_queue_tail(&self
->wx_list
, skb
);
785 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
786 if (tx_skb
== NULL
) {
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.
795 tx_skb
->data
[1] |= PF_BIT
;
797 self
->vs
= (self
->vs
+ 1) % 8;
798 self
->ack_required
= FALSE
;
800 irlap_send_i_frame(self
, tx_skb
, CMD_FRAME
);
802 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__
);
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
;
809 skb
->data
[1] |= PF_BIT
;
810 irlap_send_ui_frame(self
, skb_get(skb
), self
->caddr
, CMD_FRAME
);
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
;
818 /* Reset parameter so that we can fill next window */
819 self
->window
= self
->window_size
;
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
);
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 */
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...
847 irlap_start_final_timer(self
, self
->final_timeout
+ transmission_time
);
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
855 * The exception of course is irlap_resend_rejected_frame().
860 * Function irlap_send_data_secondary_final (self, skb)
862 * Send I(nformation) frame as secondary with final bit set
865 void irlap_send_data_secondary_final(struct irlap_cb
*self
,
868 struct sk_buff
*tx_skb
= NULL
;
870 IRDA_ASSERT(self
!= NULL
, return;);
871 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
872 IRDA_ASSERT(skb
!= NULL
, return;);
874 /* Is this reliable or unreliable data? */
875 if (skb
->data
[1] == I_FRAME
) {
878 * Insert frame sequence number (Vs) in control field before
879 * inserting into transmit window queue.
881 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
884 * Insert frame in store, in case of retransmissions
885 * Increase skb reference count, see irlap_do_event()
888 skb_queue_tail(&self
->wx_list
, skb
);
890 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
891 if (tx_skb
== NULL
) {
895 tx_skb
->data
[1] |= PF_BIT
;
897 self
->vs
= (self
->vs
+ 1) % 8;
898 self
->ack_required
= FALSE
;
900 irlap_send_i_frame(self
, tx_skb
, RSP_FRAME
);
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
;
907 skb
->data
[1] |= PF_BIT
;
908 irlap_send_ui_frame(self
, skb_get(skb
), self
->caddr
, RSP_FRAME
);
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 */
918 irlap_start_wd_timer(self
, self
->wd_timeout
);
922 * Function irlap_send_data_secondary (self, skb)
924 * Send I(nformation) frame as secondary without final bit set
927 void irlap_send_data_secondary(struct irlap_cb
*self
, struct sk_buff
*skb
)
929 struct sk_buff
*tx_skb
= NULL
;
931 /* Is this reliable or unreliable data? */
932 if (skb
->data
[1] == I_FRAME
) {
935 * Insert frame sequence number (Vs) in control field before
936 * inserting into transmit window queue.
938 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
941 * Insert frame in store, in case of retransmissions
942 * Increase skb reference count, see irlap_do_event()
945 skb_queue_tail(&self
->wx_list
, skb
);
947 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
948 if (tx_skb
== NULL
) {
952 self
->vs
= (self
->vs
+ 1) % 8;
953 self
->ack_required
= FALSE
;
956 irlap_send_i_frame(self
, tx_skb
, RSP_FRAME
);
958 irlap_send_ui_frame(self
, skb_get(skb
), self
->caddr
, RSP_FRAME
);
964 * Function irlap_resend_rejected_frames (nr)
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)
970 void irlap_resend_rejected_frames(struct irlap_cb
*self
, int command
)
972 struct sk_buff
*tx_skb
;
976 IRDA_ASSERT(self
!= NULL
, return;);
977 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
979 /* Initialize variables */
980 count
= skb_queue_len(&self
->wx_list
);
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
);
987 /* We copy the skb to be retransmitted since we will have to
988 * modify it. Cloning will confuse packet sniffers
990 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
991 tx_skb
= skb_copy(skb
, GFP_ATOMIC
);
993 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__
);
997 /* Clear old Nr field + poll bit */
998 tx_skb
->data
[1] &= 0x0f;
1001 * Set poll bit on the last frame retransmitted
1004 tx_skb
->data
[1] |= PF_BIT
; /* Set p/f bit */
1006 tx_skb
->data
[1] &= ~PF_BIT
; /* Clear p/f bit */
1008 irlap_send_i_frame(self
, tx_skb
, command
);
1011 * If our skb is the last buffer in the list, then
1012 * we are finished, if not, move to the next sk-buffer
1014 if (skb
== skb_peek_tail(&self
->wx_list
))
1021 * We can now fill the window with additional data frames
1023 while (!skb_queue_empty(&self
->txq
)) {
1025 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__
);
1026 if (self
->window
> 0) {
1027 skb
= skb_dequeue( &self
->txq
);
1028 IRDA_ASSERT(skb
!= NULL
, return;);
1031 * If send window > 1 then send frame with pf
1034 if ((self
->window
> 1) &&
1035 !skb_queue_empty(&self
->txq
)) {
1036 irlap_send_data_primary(self
, skb
);
1038 irlap_send_data_primary_poll(self
, skb
);
1046 void irlap_resend_rejected_frame(struct irlap_cb
*self
, int command
)
1048 struct sk_buff
*tx_skb
;
1049 struct sk_buff
*skb
;
1051 IRDA_ASSERT(self
!= NULL
, return;);
1052 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
1054 /* Resend unacknowledged frame(s) */
1055 skb
= skb_peek(&self
->wx_list
);
1057 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1059 /* We copy the skb to be retransmitted since we will have to
1060 * modify it. Cloning will confuse packet sniffers
1062 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1063 tx_skb
= skb_copy(skb
, GFP_ATOMIC
);
1065 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__
);
1069 /* Clear old Nr field + poll bit */
1070 tx_skb
->data
[1] &= 0x0f;
1072 /* Set poll/final bit */
1073 tx_skb
->data
[1] |= PF_BIT
; /* Set p/f bit */
1075 irlap_send_i_frame(self
, tx_skb
, command
);
1080 * Function irlap_send_ui_frame (self, skb, command)
1082 * Contruct and transmit an Unnumbered Information (UI) frame
1085 void irlap_send_ui_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1086 __u8 caddr
, int command
)
1088 IRDA_DEBUG(4, "%s()\n", __FUNCTION__
);
1090 IRDA_ASSERT(self
!= NULL
, return;);
1091 IRDA_ASSERT(self
->magic
== LAP_MAGIC
, return;);
1092 IRDA_ASSERT(skb
!= NULL
, return;);
1094 /* Insert connection address */
1095 skb
->data
[0] = caddr
| ((command
) ? CMD_FRAME
: 0);
1097 irlap_queue_xmit(self
, skb
);
1101 * Function irlap_send_i_frame (skb)
1103 * Contruct and transmit Information (I) frame
1105 static void irlap_send_i_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1108 /* Insert connection address */
1109 skb
->data
[0] = self
->caddr
;
1110 skb
->data
[0] |= (command
) ? CMD_FRAME
: 0;
1112 /* Insert next to receive (Vr) */
1113 skb
->data
[1] |= (self
->vr
<< 5); /* insert nr */
1115 irlap_queue_xmit(self
, skb
);
1119 * Function irlap_recv_i_frame (skb, frame)
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).
1124 static inline void irlap_recv_i_frame(struct irlap_cb
*self
,
1125 struct sk_buff
*skb
,
1126 struct irlap_info
*info
, int command
)
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 */
1132 /* Check if this is a command or a response frame */
1134 irlap_do_event(self
, RECV_I_CMD
, skb
, info
);
1136 irlap_do_event(self
, RECV_I_RSP
, skb
, info
);
1140 * Function irlap_recv_ui_frame (self, skb, info)
1142 * Receive and parse an Unnumbered Information (UI) frame
1145 static void irlap_recv_ui_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1146 struct irlap_info
*info
)
1148 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__
);
1150 info
->pf
= skb
->data
[1] & PF_BIT
; /* Final bit */
1152 irlap_do_event(self
, RECV_UI_FRAME
, skb
, info
);
1156 * Function irlap_recv_frmr_frame (skb, frame)
1158 * Received Frame Reject response.
1161 static void irlap_recv_frmr_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1162 struct irlap_info
*info
)
1167 IRDA_DEBUG(0, "%s()\n", __FUNCTION__
);
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;);
1174 if (!pskb_may_pull(skb
, 4)) {
1175 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__
);
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 */
1185 w
= frame
[3] & 0x01;
1186 x
= frame
[3] & 0x02;
1187 y
= frame
[3] & 0x04;
1188 z
= frame
[3] & 0x08;
1191 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1195 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1196 "contained a non permitted I field.\n");
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");
1204 IRDA_DEBUG(0, "Rejected control field control field contained an "
1205 "invalid Nr count.\n");
1207 irlap_do_event(self
, RECV_FRMR_RSP
, skb
, info
);
1211 * Function irlap_send_test_frame (self, daddr)
1213 * Send a test frame response
1216 void irlap_send_test_frame(struct irlap_cb
*self
, __u8 caddr
, __u32 daddr
,
1217 struct sk_buff
*cmd
)
1219 struct sk_buff
*tx_skb
;
1220 struct test_frame
*frame
;
1223 tx_skb
= alloc_skb(cmd
->len
+ sizeof(struct test_frame
), GFP_ATOMIC
);
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
));
1232 /* Insert the swapped addresses */
1233 frame
->saddr
= cpu_to_le32(self
->saddr
);
1234 frame
->daddr
= cpu_to_le32(daddr
);
1236 frame
= (struct test_frame
*) skb_put(tx_skb
, LAP_ADDR_HEADER
+ LAP_CTRL_HEADER
);
1238 frame
->caddr
= caddr
;
1239 frame
->control
= TEST_RSP
| PF_BIT
;
1242 info
= skb_put(tx_skb
, cmd
->len
);
1243 memcpy(info
, cmd
->data
, cmd
->len
);
1245 /* Return to sender */
1246 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1247 irlap_queue_xmit(self
, tx_skb
);
1251 * Function irlap_recv_test_frame (self, skb)
1253 * Receive a test frame
1256 static void irlap_recv_test_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1257 struct irlap_info
*info
, int command
)
1259 struct test_frame
*frame
;
1261 IRDA_DEBUG(2, "%s()\n", __FUNCTION__
);
1263 if (!pskb_may_pull(skb
, sizeof(*frame
))) {
1264 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__
);
1267 frame
= (struct test_frame
*) skb
->data
;
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",
1277 /* Read and swap addresses */
1278 info
->daddr
= le32_to_cpu(frame
->saddr
);
1279 info
->saddr
= le32_to_cpu(frame
->daddr
);
1281 /* Make sure frame is addressed to us */
1282 if ((info
->saddr
!= self
->saddr
) &&
1283 (info
->saddr
!= BROADCAST
)) {
1289 irlap_do_event(self
, RECV_TEST_CMD
, skb
, info
);
1291 irlap_do_event(self
, RECV_TEST_RSP
, skb
, info
);
1295 * Function irlap_driver_rcv (skb, netdev, ptype)
1297 * Called when a frame is received. Dispatches the right receive function
1298 * for processing of the frame.
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
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.
1310 int irlap_driver_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
1311 struct packet_type
*ptype
, struct net_device
*orig_dev
)
1313 struct irlap_info info
;
1314 struct irlap_cb
*self
;
1318 /* FIXME: should we get our own field? */
1319 self
= (struct irlap_cb
*) dev
->atalk_ptr
;
1321 /* If the net device is down, then IrLAP is gone! */
1322 if (!self
|| self
->magic
!= LAP_MAGIC
) {
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__
);
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__
);
1343 command
= skb
->data
[0] & CMD_FRAME
;
1344 info
.caddr
= skb
->data
[0] & CBROADCAST
;
1346 info
.pf
= skb
->data
[1] & PF_BIT
;
1347 info
.control
= skb
->data
[1] & ~PF_BIT
; /* Mask away poll/final bit */
1349 control
= info
.control
;
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",
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
1361 if (~control
& 0x01) {
1362 irlap_recv_i_frame(self
, skb
, &info
, command
);
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
1369 if (~control
& 0x02) {
1371 * Received S(upervisory) frame, check which frame type it is
1372 * only the first nibble is of interest
1374 switch (control
& 0x0f) {
1376 irlap_recv_rr_frame(self
, skb
, &info
, command
);
1379 irlap_recv_rnr_frame(self
, skb
, &info
, command
);
1382 irlap_recv_rej_frame(self
, skb
, &info
, command
);
1385 irlap_recv_srej_frame(self
, skb
, &info
, command
);
1388 IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
1389 __FUNCTION__
, info
.control
);
1395 * This must be a C(ontrol) frame
1399 irlap_recv_discovery_xid_rsp(self
, skb
, &info
);
1402 irlap_recv_discovery_xid_cmd(self
, skb
, &info
);
1405 irlap_recv_snrm_cmd(self
, skb
, &info
);
1408 irlap_do_event(self
, RECV_DM_RSP
, skb
, &info
);
1410 case DISC_CMD
: /* And RD_RSP since they have the same value */
1411 irlap_recv_disc_frame(self
, skb
, &info
, command
);
1414 irlap_recv_test_frame(self
, skb
, &info
, command
);
1417 irlap_recv_ua_frame(self
, skb
, &info
);
1420 irlap_recv_frmr_frame(self
, skb
, &info
);
1423 irlap_recv_ui_frame(self
, skb
, &info
);
1426 IRDA_WARNING("%s: Unknown frame %02x received!\n",
1427 __FUNCTION__
, info
.control
);
1431 /* Always drop our reference on the skb */