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>
47 * Function irlap_insert_info (self, skb)
49 * Insert minimum turnaround time and speed information into the skb. We
50 * need to do this since it's per packet relevant information. Safe to
51 * have this function inlined since it's only called from one place
53 static inline void irlap_insert_info(struct irlap_cb *self,
56 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
59 * Insert MTT (min. turn time) and speed into skb, so that the
60 * device driver knows which settings to use
62 cb->magic = LAP_MAGIC;
63 cb->mtt = self->mtt_required;
64 cb->next_speed = self->speed;
67 self->mtt_required = 0;
70 * Delay equals negotiated BOFs count, plus the number of BOFs to
71 * force the negotiated minimum turnaround time
73 cb->xbofs = self->bofs_count;
74 cb->next_xbofs = self->next_bofs;
75 cb->xbofs_delay = self->xbofs_delay;
77 /* Reset XBOF's delay (used only for getting min turn time) */
78 self->xbofs_delay = 0;
79 /* Put the correct xbofs value for the next packet */
80 self->bofs_count = self->next_bofs;
84 * Function irlap_queue_xmit (self, skb)
86 * A little wrapper for dev_queue_xmit, so we can insert some common
89 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
91 /* Some common init stuff */
92 skb->dev = self->netdev;
93 skb->h.raw = skb->nh.raw = skb->mac.raw = skb->data;
94 skb->protocol = htons(ETH_P_IRDA);
95 skb->priority = TC_PRIO_BESTEFFORT;
97 irlap_insert_info(self, skb);
103 * Function irlap_send_snrm_cmd (void)
105 * Transmits a connect SNRM command frame
107 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
109 struct sk_buff *tx_skb;
110 struct snrm_frame *frame;
113 ASSERT(self != NULL, return;);
114 ASSERT(self->magic == LAP_MAGIC, return;);
117 tx_skb = dev_alloc_skb(64);
121 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
123 /* Insert connection address field */
125 frame->caddr = CMD_FRAME | CBROADCAST;
127 frame->caddr = CMD_FRAME | self->caddr;
129 /* Insert control field */
130 frame->control = SNRM_CMD | PF_BIT;
133 * If we are establishing a connection then insert QoS paramerters
136 skb_put(tx_skb, 9); /* 21 left */
137 frame->saddr = cpu_to_le32(self->saddr);
138 frame->daddr = cpu_to_le32(self->daddr);
140 frame->ncaddr = self->caddr;
142 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
144 dev_kfree_skb(tx_skb);
148 irlap_queue_xmit(self, tx_skb);
152 * Function irlap_recv_snrm_cmd (skb, info)
154 * Received SNRM (Set Normal Response Mode) command frame
157 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
158 struct irlap_info *info)
160 struct snrm_frame *frame;
162 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
163 frame = (struct snrm_frame *) skb->data;
165 /* Copy the new connection address ignoring the C/R bit */
166 info->caddr = frame->ncaddr & 0xFE;
168 /* Check if the new connection address is valid */
169 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
170 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
175 /* Copy peer device address */
176 info->daddr = le32_to_cpu(frame->saddr);
177 info->saddr = le32_to_cpu(frame->daddr);
179 /* Only accept if addressed directly to us */
180 if (info->saddr != self->saddr) {
181 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
185 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
187 /* Signal that this SNRM frame does not contain and I-field */
188 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
193 * Function irlap_send_ua_response_frame (qos)
195 * Send UA (Unnumbered Acknowledgement) frame
198 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
200 struct sk_buff *tx_skb;
201 struct ua_frame *frame;
204 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
206 ASSERT(self != NULL, return;);
207 ASSERT(self->magic == LAP_MAGIC, return;);
210 tx_skb = dev_alloc_skb(64);
214 frame = (struct ua_frame *) skb_put(tx_skb, 10);
216 /* Build UA response */
217 frame->caddr = self->caddr;
218 frame->control = UA_RSP | PF_BIT;
220 frame->saddr = cpu_to_le32(self->saddr);
221 frame->daddr = cpu_to_le32(self->daddr);
223 /* Should we send QoS negotiation parameters? */
225 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
227 dev_kfree_skb(tx_skb);
232 irlap_queue_xmit(self, tx_skb);
237 * Function irlap_send_dm_frame (void)
239 * Send disconnected mode (DM) frame
242 void irlap_send_dm_frame( struct irlap_cb *self)
244 struct sk_buff *tx_skb = NULL;
247 ASSERT(self != NULL, return;);
248 ASSERT(self->magic == LAP_MAGIC, return;);
250 tx_skb = dev_alloc_skb(32);
254 frame = skb_put(tx_skb, 2);
256 if (self->state == LAP_NDM)
257 frame[0] = CBROADCAST;
259 frame[0] = self->caddr;
261 frame[1] = DM_RSP | PF_BIT;
263 irlap_queue_xmit(self, tx_skb);
267 * Function irlap_send_disc_frame (void)
269 * Send disconnect (DISC) frame
272 void irlap_send_disc_frame(struct irlap_cb *self)
274 struct sk_buff *tx_skb = NULL;
277 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
279 ASSERT(self != NULL, return;);
280 ASSERT(self->magic == LAP_MAGIC, return;);
282 tx_skb = dev_alloc_skb(16);
286 frame = skb_put(tx_skb, 2);
288 frame[0] = self->caddr | CMD_FRAME;
289 frame[1] = DISC_CMD | PF_BIT;
291 irlap_queue_xmit(self, tx_skb);
295 * Function irlap_send_discovery_xid_frame (S, s, command)
297 * Build and transmit a XID (eXchange station IDentifier) discovery
300 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
301 __u8 command, discovery_t *discovery)
303 struct sk_buff *tx_skb = NULL;
304 struct xid_frame *frame;
305 __u32 bcast = BROADCAST;
308 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
311 ASSERT(self != NULL, return;);
312 ASSERT(self->magic == LAP_MAGIC, return;);
313 ASSERT(discovery != NULL, return;);
315 tx_skb = dev_alloc_skb(64);
320 frame = (struct xid_frame *) tx_skb->data;
323 frame->caddr = CBROADCAST | CMD_FRAME;
324 frame->control = XID_CMD | PF_BIT;
326 frame->caddr = CBROADCAST;
327 frame->control = XID_RSP | PF_BIT;
329 frame->ident = XID_FORMAT;
331 frame->saddr = cpu_to_le32(self->saddr);
334 frame->daddr = cpu_to_le32(bcast);
336 frame->daddr = cpu_to_le32(discovery->data.daddr);
357 frame->version = 0x00;
360 * Provide info for final slot only in commands, and for all
361 * responses. Send the second byte of the hint only if the
362 * EXTENSION bit is set in the first byte.
364 if (!command || (frame->slotnr == 0xff)) {
367 if (discovery->data.hints[0] & HINT_EXTENSION) {
368 info = skb_put(tx_skb, 2);
369 info[0] = discovery->data.hints[0];
370 info[1] = discovery->data.hints[1];
372 info = skb_put(tx_skb, 1);
373 info[0] = discovery->data.hints[0];
375 info = skb_put(tx_skb, 1);
376 info[0] = discovery->data.charset;
378 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
379 info = skb_put(tx_skb, len);
380 memcpy(info, discovery->data.info, len);
382 irlap_queue_xmit(self, tx_skb);
386 * Function irlap_recv_discovery_xid_rsp (skb, info)
388 * Received a XID discovery response
391 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
393 struct irlap_info *info)
395 struct xid_frame *xid;
396 discovery_t *discovery = NULL;
397 __u8 *discovery_info;
400 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
402 ASSERT(self != NULL, return;);
403 ASSERT(self->magic == LAP_MAGIC, return;);
405 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
406 ERROR("%s: frame to short!\n", __FUNCTION__);
410 xid = (struct xid_frame *) skb->data;
412 info->daddr = le32_to_cpu(xid->saddr);
413 info->saddr = le32_to_cpu(xid->daddr);
415 /* Make sure frame is addressed to us */
416 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
417 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
422 if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
423 WARNING("%s: kmalloc failed!\n", __FUNCTION__);
426 memset(discovery, 0, sizeof(discovery_t));
428 discovery->data.daddr = info->daddr;
429 discovery->data.saddr = self->saddr;
430 discovery->timestamp = jiffies;
432 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
433 discovery->data.daddr);
435 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
437 /* Get info returned from peer */
438 discovery->data.hints[0] = discovery_info[0];
439 if (discovery_info[0] & HINT_EXTENSION) {
440 IRDA_DEBUG(4, "EXTENSION\n");
441 discovery->data.hints[1] = discovery_info[1];
442 discovery->data.charset = discovery_info[2];
443 text = (char *) &discovery_info[3];
445 discovery->data.hints[1] = 0;
446 discovery->data.charset = discovery_info[1];
447 text = (char *) &discovery_info[2];
450 * Terminate info string, should be safe since this is where the
453 skb->data[skb->len] = '\0';
454 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
455 discovery->name_len = strlen(discovery->data.info);
457 info->discovery = discovery;
459 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
463 * Function irlap_recv_discovery_xid_cmd (skb, info)
465 * Received a XID discovery command
468 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
470 struct irlap_info *info)
472 struct xid_frame *xid;
473 discovery_t *discovery = NULL;
474 __u8 *discovery_info;
477 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
478 ERROR("%s: frame to short!\n", __FUNCTION__);
482 xid = (struct xid_frame *) skb->data;
484 info->daddr = le32_to_cpu(xid->saddr);
485 info->saddr = le32_to_cpu(xid->daddr);
487 /* Make sure frame is addressed to us */
488 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
489 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
494 switch (xid->flags & 0x03) {
511 info->s = xid->slotnr;
513 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
516 * Check if last frame
518 if (info->s == 0xff) {
519 /* Check if things are sane at this point... */
520 if((discovery_info == NULL) ||
521 !pskb_may_pull(skb, 3)) {
522 ERROR("%s: discovery frame to short!\n", __FUNCTION__);
527 * We now have some discovery info to deliver!
529 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
531 WARNING("%s: unable to malloc!\n", __FUNCTION__);
535 discovery->data.daddr = info->daddr;
536 discovery->data.saddr = self->saddr;
537 discovery->timestamp = jiffies;
539 discovery->data.hints[0] = discovery_info[0];
540 if (discovery_info[0] & HINT_EXTENSION) {
541 discovery->data.hints[1] = discovery_info[1];
542 discovery->data.charset = discovery_info[2];
543 text = (char *) &discovery_info[3];
545 discovery->data.hints[1] = 0;
546 discovery->data.charset = discovery_info[1];
547 text = (char *) &discovery_info[2];
550 * Terminate string, should be safe since this is where the
553 skb->data[skb->len] = '\0';
554 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
555 discovery->name_len = strlen(discovery->data.info);
557 info->discovery = discovery;
559 info->discovery = NULL;
561 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
565 * Function irlap_send_rr_frame (self, command)
567 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
568 * only possible to send RR frames with the poll bit set.
570 void irlap_send_rr_frame(struct irlap_cb *self, int command)
572 struct sk_buff *tx_skb;
575 tx_skb = dev_alloc_skb(16);
579 frame = skb_put(tx_skb, 2);
581 frame[0] = self->caddr;
582 frame[0] |= (command) ? CMD_FRAME : 0;
584 frame[1] = RR | PF_BIT | (self->vr << 5);
586 irlap_queue_xmit(self, tx_skb);
590 * Function irlap_send_rd_frame (self)
592 * Request disconnect. Used by a secondary station to request the
593 * disconnection of the link.
595 void irlap_send_rd_frame(struct irlap_cb *self)
597 struct sk_buff *tx_skb;
600 tx_skb = dev_alloc_skb(16);
604 frame = skb_put(tx_skb, 2);
606 frame[0] = self->caddr;
607 frame[1] = RD_RSP | PF_BIT;
609 irlap_queue_xmit(self, tx_skb);
613 * Function irlap_recv_rr_frame (skb, info)
615 * Received RR (Receive Ready) frame from peer station, no harm in
616 * making it inline since its called only from one single place
617 * (irlap_driver_rcv).
619 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
621 struct irlap_info *info, int command)
623 info->nr = skb->data[1] >> 5;
625 /* Check if this is a command or a response frame */
627 irlap_do_event(self, RECV_RR_CMD, skb, info);
629 irlap_do_event(self, RECV_RR_RSP, skb, info);
632 void irlap_send_frmr_frame( struct irlap_cb *self, int command)
634 struct sk_buff *tx_skb = NULL;
637 ASSERT( self != NULL, return;);
638 ASSERT( self->magic == LAP_MAGIC, return;);
640 tx_skb = dev_alloc_skb( 32);
644 frame = skb_put(tx_skb, 2);
646 frame[0] = self->caddr;
647 frame[0] |= (command) ? CMD_FRAME : 0;
649 frame[1] = (self->vs << 1);
651 frame[1] |= (self->vr << 5);
655 IRDA_DEBUG(4, "%s(), vr=%d, %ld\n", __FUNCTION__, self->vr, jiffies);
657 irlap_queue_xmit(self, tx_skb);
661 * Function irlap_recv_rnr_frame (self, skb, info)
663 * Received RNR (Receive Not Ready) frame from peer station
666 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
667 struct irlap_info *info, int command)
669 info->nr = skb->data[1] >> 5;
671 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
674 irlap_do_event(self, RECV_RNR_CMD, skb, info);
676 irlap_do_event(self, RECV_RNR_RSP, skb, info);
679 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
680 struct irlap_info *info, int command)
682 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
684 info->nr = skb->data[1] >> 5;
686 /* Check if this is a command or a response frame */
688 irlap_do_event(self, RECV_REJ_CMD, skb, info);
690 irlap_do_event(self, RECV_REJ_RSP, skb, info);
693 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
694 struct irlap_info *info, int command)
696 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
698 info->nr = skb->data[1] >> 5;
700 /* Check if this is a command or a response frame */
702 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
704 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
707 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
708 struct irlap_info *info, int command)
710 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
712 /* Check if this is a command or a response frame */
714 irlap_do_event(self, RECV_DISC_CMD, skb, info);
716 irlap_do_event(self, RECV_RD_RSP, skb, info);
720 * Function irlap_recv_ua_frame (skb, frame)
722 * Received UA (Unnumbered Acknowledgement) frame
725 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
727 struct irlap_info *info)
729 irlap_do_event(self, RECV_UA_RSP, skb, info);
733 * Function irlap_send_data_primary(self, skb)
735 * Send I-frames as the primary station but without the poll bit set
738 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
740 struct sk_buff *tx_skb;
742 if (skb->data[1] == I_FRAME) {
745 * Insert frame sequence number (Vs) in control field before
746 * inserting into transmit window queue.
748 skb->data[1] = I_FRAME | (self->vs << 1);
751 * Insert frame in store, in case of retransmissions
752 * Increase skb reference count, see irlap_do_event()
755 skb_queue_tail(&self->wx_list, skb);
758 tx_skb = skb_clone(skb, GFP_ATOMIC);
759 if (tx_skb == NULL) {
763 self->vs = (self->vs + 1) % 8;
764 self->ack_required = FALSE;
767 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
769 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
770 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
775 * Function irlap_send_data_primary_poll (self, skb)
777 * Send I(nformation) frame as primary with poll bit set
779 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
781 struct sk_buff *tx_skb;
782 int transmission_time;
785 del_timer(&self->poll_timer);
787 /* Is this reliable or unreliable data? */
788 if (skb->data[1] == I_FRAME) {
791 * Insert frame sequence number (Vs) in control field before
792 * inserting into transmit window queue.
794 skb->data[1] = I_FRAME | (self->vs << 1);
797 * Insert frame in store, in case of retransmissions
798 * Increase skb reference count, see irlap_do_event()
801 skb_queue_tail(&self->wx_list, skb);
804 tx_skb = skb_clone(skb, GFP_ATOMIC);
805 if (tx_skb == NULL) {
810 * Set poll bit if necessary. We do this to the copied
811 * skb, since retransmitted need to set or clear the poll
812 * bit depending on when they are sent.
814 tx_skb->data[1] |= PF_BIT;
816 self->vs = (self->vs + 1) % 8;
817 self->ack_required = FALSE;
819 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
821 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
823 if (self->ack_required) {
824 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
825 irlap_send_rr_frame(self, CMD_FRAME);
826 self->ack_required = FALSE;
828 skb->data[1] |= PF_BIT;
829 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
833 /* How much time we took for transmission of all frames.
834 * We don't know, so let assume we used the full window. Jean II */
835 transmission_time = self->final_timeout;
837 /* Reset parameter so that we can fill next window */
838 self->window = self->window_size;
840 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
841 /* Remove what we have not used. Just do a prorata of the
842 * bytes left in window to window capacity.
843 * See max_line_capacities[][] in qos.c for details. Jean II */
844 transmission_time -= (self->final_timeout * self->bytes_left
845 / self->line_capacity);
846 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);
848 /* We are allowed to transmit a maximum number of bytes again. */
849 self->bytes_left = self->line_capacity;
850 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
853 * The network layer has a intermediate buffer between IrLAP
854 * and the IrDA driver which can contain 8 frames. So, even
855 * though IrLAP is currently sending the *last* frame of the
856 * tx-window, the driver most likely has only just started
857 * sending the *first* frame of the same tx-window.
858 * I.e. we are always at the very begining of or Tx window.
859 * Now, we are supposed to set the final timer from the end
860 * of our tx-window to let the other peer reply. So, we need
861 * to add extra time to compensate for the fact that we
862 * are really at the start of tx-window, otherwise the final timer
863 * might expire before he can answer...
866 irlap_start_final_timer(self, self->final_timeout + transmission_time);
869 * The clever amongst you might ask why we do this adjustement
870 * only here, and not in all the other cases in irlap_event.c.
871 * In all those other case, we only send a very short management
872 * frame (few bytes), so the adjustement would be lost in the
874 * The exception of course is irlap_resend_rejected_frame().
879 * Function irlap_send_data_secondary_final (self, skb)
881 * Send I(nformation) frame as secondary with final bit set
884 void irlap_send_data_secondary_final(struct irlap_cb *self,
887 struct sk_buff *tx_skb = NULL;
889 ASSERT(self != NULL, return;);
890 ASSERT(self->magic == LAP_MAGIC, return;);
891 ASSERT(skb != NULL, return;);
893 /* Is this reliable or unreliable data? */
894 if (skb->data[1] == I_FRAME) {
897 * Insert frame sequence number (Vs) in control field before
898 * inserting into transmit window queue.
900 skb->data[1] = I_FRAME | (self->vs << 1);
903 * Insert frame in store, in case of retransmissions
904 * Increase skb reference count, see irlap_do_event()
907 skb_queue_tail(&self->wx_list, skb);
909 tx_skb = skb_clone(skb, GFP_ATOMIC);
910 if (tx_skb == NULL) {
914 tx_skb->data[1] |= PF_BIT;
916 self->vs = (self->vs + 1) % 8;
917 self->ack_required = FALSE;
919 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
921 if (self->ack_required) {
922 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
923 irlap_send_rr_frame(self, RSP_FRAME);
924 self->ack_required = FALSE;
926 skb->data[1] |= PF_BIT;
927 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
931 self->window = self->window_size;
932 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
933 /* We are allowed to transmit a maximum number of bytes again. */
934 self->bytes_left = self->line_capacity;
935 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
937 irlap_start_wd_timer(self, self->wd_timeout);
941 * Function irlap_send_data_secondary (self, skb)
943 * Send I(nformation) frame as secondary without final bit set
946 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
948 struct sk_buff *tx_skb = NULL;
950 /* Is this reliable or unreliable data? */
951 if (skb->data[1] == I_FRAME) {
954 * Insert frame sequence number (Vs) in control field before
955 * inserting into transmit window queue.
957 skb->data[1] = I_FRAME | (self->vs << 1);
960 * Insert frame in store, in case of retransmissions
961 * Increase skb reference count, see irlap_do_event()
964 skb_queue_tail(&self->wx_list, skb);
966 tx_skb = skb_clone(skb, GFP_ATOMIC);
967 if (tx_skb == NULL) {
971 self->vs = (self->vs + 1) % 8;
972 self->ack_required = FALSE;
975 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
977 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
983 * Function irlap_resend_rejected_frames (nr)
985 * Resend frames which has not been acknowledged. Should be safe to
986 * traverse the list without locking it since this function will only be
987 * called from interrupt context (BH)
989 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
991 struct sk_buff *tx_skb;
995 ASSERT(self != NULL, return;);
996 ASSERT(self->magic == LAP_MAGIC, return;);
998 /* Initialize variables */
999 count = skb_queue_len(&self->wx_list);
1001 /* Resend unacknowledged frame(s) */
1002 skb = skb_peek(&self->wx_list);
1003 while (skb != NULL) {
1004 irlap_wait_min_turn_around(self, &self->qos_tx);
1006 /* We copy the skb to be retransmitted since we will have to
1007 * modify it. Cloning will confuse packet sniffers
1009 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1010 tx_skb = skb_copy(skb, GFP_ATOMIC);
1012 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1015 /* Unlink tx_skb from list */
1016 tx_skb->next = tx_skb->prev = NULL;
1017 tx_skb->list = NULL;
1019 /* Clear old Nr field + poll bit */
1020 tx_skb->data[1] &= 0x0f;
1023 * Set poll bit on the last frame retransmitted
1026 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1028 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1030 irlap_send_i_frame(self, tx_skb, command);
1033 * If our skb is the last buffer in the list, then
1034 * we are finished, if not, move to the next sk-buffer
1036 if (skb == skb_peek_tail(&self->wx_list))
1043 * We can now fill the window with additional data frames
1045 while (skb_queue_len( &self->txq) > 0) {
1047 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
1048 if ((skb_queue_len( &self->txq) > 0) &&
1049 (self->window > 0)) {
1050 skb = skb_dequeue( &self->txq);
1051 ASSERT(skb != NULL, return;);
1054 * If send window > 1 then send frame with pf
1057 if ((self->window > 1) &&
1058 skb_queue_len(&self->txq) > 0)
1060 irlap_send_data_primary(self, skb);
1062 irlap_send_data_primary_poll(self, skb);
1070 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1072 struct sk_buff *tx_skb;
1073 struct sk_buff *skb;
1075 ASSERT(self != NULL, return;);
1076 ASSERT(self->magic == LAP_MAGIC, return;);
1078 /* Resend unacknowledged frame(s) */
1079 skb = skb_peek(&self->wx_list);
1081 irlap_wait_min_turn_around(self, &self->qos_tx);
1083 /* We copy the skb to be retransmitted since we will have to
1084 * modify it. Cloning will confuse packet sniffers
1086 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1087 tx_skb = skb_copy(skb, GFP_ATOMIC);
1089 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1092 /* Unlink tx_skb from list */
1093 tx_skb->next = tx_skb->prev = NULL;
1094 tx_skb->list = NULL;
1096 /* Clear old Nr field + poll bit */
1097 tx_skb->data[1] &= 0x0f;
1099 /* Set poll/final bit */
1100 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1102 irlap_send_i_frame(self, tx_skb, command);
1107 * Function irlap_send_ui_frame (self, skb, command)
1109 * Contruct and transmit an Unnumbered Information (UI) frame
1112 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1113 __u8 caddr, int command)
1115 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1117 ASSERT(self != NULL, return;);
1118 ASSERT(self->magic == LAP_MAGIC, return;);
1119 ASSERT(skb != NULL, return;);
1121 /* Insert connection address */
1122 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1124 irlap_queue_xmit(self, skb);
1128 * Function irlap_send_i_frame (skb)
1130 * Contruct and transmit Information (I) frame
1132 void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1135 /* Insert connection address */
1136 skb->data[0] = self->caddr;
1137 skb->data[0] |= (command) ? CMD_FRAME : 0;
1139 /* Insert next to receive (Vr) */
1140 skb->data[1] |= (self->vr << 5); /* insert nr */
1142 irlap_queue_xmit(self, skb);
1146 * Function irlap_recv_i_frame (skb, frame)
1148 * Receive and parse an I (Information) frame, no harm in making it inline
1149 * since it's called only from one single place (irlap_driver_rcv).
1151 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1152 struct sk_buff *skb,
1153 struct irlap_info *info, int command)
1155 info->nr = skb->data[1] >> 5; /* Next to receive */
1156 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1157 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1159 /* Check if this is a command or a response frame */
1161 irlap_do_event(self, RECV_I_CMD, skb, info);
1163 irlap_do_event(self, RECV_I_RSP, skb, info);
1167 * Function irlap_recv_ui_frame (self, skb, info)
1169 * Receive and parse an Unnumbered Information (UI) frame
1172 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1173 struct irlap_info *info)
1175 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1177 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1179 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1183 * Function irlap_recv_frmr_frame (skb, frame)
1185 * Received Frame Reject response.
1188 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1189 struct irlap_info *info)
1194 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1196 ASSERT(self != NULL, return;);
1197 ASSERT(self->magic == LAP_MAGIC, return;);
1198 ASSERT(skb != NULL, return;);
1199 ASSERT(info != NULL, return;);
1201 if (!pskb_may_pull(skb, 4)) {
1202 ERROR("%s: frame to short!\n", __FUNCTION__);
1208 info->nr = frame[2] >> 5; /* Next to receive */
1209 info->pf = frame[2] & PF_BIT; /* Final bit */
1210 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1212 w = frame[3] & 0x01;
1213 x = frame[3] & 0x02;
1214 y = frame[3] & 0x04;
1215 z = frame[3] & 0x08;
1218 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1222 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1223 "contained a non permitted I field.\n");
1226 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1227 "for the existing connection or exceeded the maximum "
1228 "this station supports if no connection exists.\n");
1231 IRDA_DEBUG(0, "Rejected control field control field contained an "
1232 "invalid Nr count.\n");
1234 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1238 * Function irlap_send_test_frame (self, daddr)
1240 * Send a test frame response
1243 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1244 struct sk_buff *cmd)
1246 struct sk_buff *tx_skb;
1247 struct test_frame *frame;
1250 tx_skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1254 /* Broadcast frames must include saddr and daddr fields */
1255 if (caddr == CBROADCAST) {
1256 frame = (struct test_frame *)
1257 skb_put(tx_skb, sizeof(struct test_frame));
1259 /* Insert the swapped addresses */
1260 frame->saddr = cpu_to_le32(self->saddr);
1261 frame->daddr = cpu_to_le32(daddr);
1263 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1265 frame->caddr = caddr;
1266 frame->control = TEST_RSP | PF_BIT;
1269 info = skb_put(tx_skb, cmd->len);
1270 memcpy(info, cmd->data, cmd->len);
1272 /* Return to sender */
1273 irlap_wait_min_turn_around(self, &self->qos_tx);
1274 irlap_queue_xmit(self, tx_skb);
1278 * Function irlap_recv_test_frame (self, skb)
1280 * Receive a test frame
1283 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1284 struct irlap_info *info, int command)
1286 struct test_frame *frame;
1288 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1290 if (!pskb_may_pull(skb, sizeof(*frame))) {
1291 ERROR("%s: frame to short!\n", __FUNCTION__);
1294 frame = (struct test_frame *) skb->data;
1296 /* Broadcast frames must carry saddr and daddr fields */
1297 if (info->caddr == CBROADCAST) {
1298 if (skb->len < sizeof(struct test_frame)) {
1299 IRDA_DEBUG(0, "%s() test frame to short!\n",
1304 /* Read and swap addresses */
1305 info->daddr = le32_to_cpu(frame->saddr);
1306 info->saddr = le32_to_cpu(frame->daddr);
1308 /* Make sure frame is addressed to us */
1309 if ((info->saddr != self->saddr) &&
1310 (info->saddr != BROADCAST)) {
1316 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1318 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1322 * Function irlap_driver_rcv (skb, netdev, ptype)
1324 * Called when a frame is received. Dispatches the right receive function
1325 * for processing of the frame.
1327 * Note on skb management :
1328 * After calling the higher layers of the IrDA stack, we always
1329 * kfree() the skb, which drop the reference count (and potentially
1331 * If a higher layer of the stack want to keep the skb around (to put
1332 * in a queue or pass it to the higher layer), it will need to use
1333 * skb_get() to keep a reference on it. This is usually done at the
1334 * LMP level in irlmp.c.
1337 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1338 struct packet_type *ptype)
1340 struct irlap_info info;
1341 struct irlap_cb *self;
1345 /* FIXME: should we get our own field? */
1346 self = (struct irlap_cb *) dev->atalk_ptr;
1348 /* If the net device is down, then IrLAP is gone! */
1349 if (!self || self->magic != LAP_MAGIC) {
1354 /* We are no longer an "old" protocol, so we need to handle
1355 * share and non linear skbs. This should never happen, so
1356 * we don't need to be clever about it. Jean II */
1357 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1358 ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
1363 /* Check if frame is large enough for parsing */
1364 if (!pskb_may_pull(skb, 2)) {
1365 ERROR("%s: frame to short!\n", __FUNCTION__);
1370 command = skb->data[0] & CMD_FRAME;
1371 info.caddr = skb->data[0] & CBROADCAST;
1373 info.pf = skb->data[1] & PF_BIT;
1374 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1376 control = info.control;
1378 /* First we check if this frame has a valid connection address */
1379 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1380 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1385 * Optimize for the common case and check if the frame is an
1386 * I(nformation) frame. Only I-frames have bit 0 set to 0
1388 if (~control & 0x01) {
1389 irlap_recv_i_frame(self, skb, &info, command);
1393 * We now check is the frame is an S(upervisory) frame. Only
1394 * S-frames have bit 0 set to 1 and bit 1 set to 0
1396 if (~control & 0x02) {
1398 * Received S(upervisory) frame, check which frame type it is
1399 * only the first nibble is of interest
1401 switch (control & 0x0f) {
1403 irlap_recv_rr_frame(self, skb, &info, command);
1406 irlap_recv_rnr_frame(self, skb, &info, command);
1409 irlap_recv_rej_frame(self, skb, &info, command);
1412 irlap_recv_srej_frame(self, skb, &info, command);
1415 WARNING("%s: Unknown S-frame %02x received!\n",
1416 __FUNCTION__, info.control);
1422 * This must be a C(ontrol) frame
1426 irlap_recv_discovery_xid_rsp(self, skb, &info);
1429 irlap_recv_discovery_xid_cmd(self, skb, &info);
1432 irlap_recv_snrm_cmd(self, skb, &info);
1435 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1437 case DISC_CMD: /* And RD_RSP since they have the same value */
1438 irlap_recv_disc_frame(self, skb, &info, command);
1441 irlap_recv_test_frame(self, skb, &info, command);
1444 irlap_recv_ua_frame(self, skb, &info);
1447 irlap_recv_frmr_frame(self, skb, &info);
1450 irlap_recv_ui_frame(self, skb, &info);
1453 WARNING("%s: Unknown frame %02x received!\n",
1454 __FUNCTION__, info.control);
1458 /* Always drop our reference on the skb */