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>
32 #include <linux/slab.h>
34 #include <net/pkt_sched.h>
37 #include <asm/byteorder.h>
39 #include <net/irda/irda.h>
40 #include <net/irda/irda_device.h>
41 #include <net/irda/irlap.h>
42 #include <net/irda/wrapper.h>
43 #include <net/irda/timer.h>
44 #include <net/irda/irlap_frame.h>
45 #include <net/irda/qos.h>
47 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
51 * Function irlap_insert_info (self, skb)
53 * Insert minimum turnaround time and speed information into the skb. We
54 * need to do this since it's per packet relevant information. Safe to
55 * have this function inlined since it's only called from one place
57 static inline void irlap_insert_info(struct irlap_cb *self,
60 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
63 * Insert MTT (min. turn time) and speed into skb, so that the
64 * device driver knows which settings to use
66 cb->magic = LAP_MAGIC;
67 cb->mtt = self->mtt_required;
68 cb->next_speed = self->speed;
71 self->mtt_required = 0;
74 * Delay equals negotiated BOFs count, plus the number of BOFs to
75 * force the negotiated minimum turnaround time
77 cb->xbofs = self->bofs_count;
78 cb->next_xbofs = self->next_bofs;
79 cb->xbofs_delay = self->xbofs_delay;
81 /* Reset XBOF's delay (used only for getting min turn time) */
82 self->xbofs_delay = 0;
83 /* Put the correct xbofs value for the next packet */
84 self->bofs_count = self->next_bofs;
88 * Function irlap_queue_xmit (self, skb)
90 * A little wrapper for dev_queue_xmit, so we can insert some common
93 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
95 /* Some common init stuff */
96 skb->dev = self->netdev;
97 skb_reset_mac_header(skb);
98 skb_reset_network_header(skb);
99 skb_reset_transport_header(skb);
100 skb->protocol = htons(ETH_P_IRDA);
101 skb->priority = TC_PRIO_BESTEFFORT;
103 irlap_insert_info(self, skb);
105 if (unlikely(self->mode & IRDA_MODE_MONITOR)) {
106 pr_debug("%s(): %s is in monitor mode\n", __func__,
116 * Function irlap_send_snrm_cmd (void)
118 * Transmits a connect SNRM command frame
120 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
122 struct sk_buff *tx_skb;
123 struct snrm_frame *frame;
126 IRDA_ASSERT(self != NULL, return;);
127 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
130 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
131 IRLAP_NEGOCIATION_PARAMS_LEN,
136 frame = skb_put(tx_skb, 2);
138 /* Insert connection address field */
140 frame->caddr = CMD_FRAME | CBROADCAST;
142 frame->caddr = CMD_FRAME | self->caddr;
144 /* Insert control field */
145 frame->control = SNRM_CMD | PF_BIT;
148 * If we are establishing a connection then insert QoS parameters
151 skb_put(tx_skb, 9); /* 25 left */
152 frame->saddr = cpu_to_le32(self->saddr);
153 frame->daddr = cpu_to_le32(self->daddr);
155 frame->ncaddr = self->caddr;
157 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
159 dev_kfree_skb(tx_skb);
163 irlap_queue_xmit(self, tx_skb);
167 * Function irlap_recv_snrm_cmd (skb, info)
169 * Received SNRM (Set Normal Response Mode) command frame
172 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
173 struct irlap_info *info)
175 struct snrm_frame *frame;
177 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
178 frame = (struct snrm_frame *) skb->data;
180 /* Copy the new connection address ignoring the C/R bit */
181 info->caddr = frame->ncaddr & 0xFE;
183 /* Check if the new connection address is valid */
184 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
185 pr_debug("%s(), invalid connection address!\n",
190 /* Copy peer device address */
191 info->daddr = le32_to_cpu(frame->saddr);
192 info->saddr = le32_to_cpu(frame->daddr);
194 /* Only accept if addressed directly to us */
195 if (info->saddr != self->saddr) {
196 pr_debug("%s(), not addressed to us!\n",
200 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
202 /* Signal that this SNRM frame does not contain and I-field */
203 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
208 * Function irlap_send_ua_response_frame (qos)
210 * Send UA (Unnumbered Acknowledgement) frame
213 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
215 struct sk_buff *tx_skb;
216 struct ua_frame *frame;
219 pr_debug("%s() <%ld>\n", __func__, jiffies);
221 IRDA_ASSERT(self != NULL, return;);
222 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
225 tx_skb = alloc_skb(sizeof(struct ua_frame) +
226 IRLAP_NEGOCIATION_PARAMS_LEN,
231 frame = skb_put(tx_skb, 10);
233 /* Build UA response */
234 frame->caddr = self->caddr;
235 frame->control = UA_RSP | PF_BIT;
237 frame->saddr = cpu_to_le32(self->saddr);
238 frame->daddr = cpu_to_le32(self->daddr);
240 /* Should we send QoS negotiation parameters? */
242 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
244 dev_kfree_skb(tx_skb);
249 irlap_queue_xmit(self, tx_skb);
254 * Function irlap_send_dm_frame (void)
256 * Send disconnected mode (DM) frame
259 void irlap_send_dm_frame( struct irlap_cb *self)
261 struct sk_buff *tx_skb = NULL;
262 struct dm_frame *frame;
264 IRDA_ASSERT(self != NULL, return;);
265 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
267 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
271 frame = skb_put(tx_skb, 2);
273 if (self->state == LAP_NDM)
274 frame->caddr = CBROADCAST;
276 frame->caddr = self->caddr;
278 frame->control = DM_RSP | PF_BIT;
280 irlap_queue_xmit(self, tx_skb);
284 * Function irlap_send_disc_frame (void)
286 * Send disconnect (DISC) frame
289 void irlap_send_disc_frame(struct irlap_cb *self)
291 struct sk_buff *tx_skb = NULL;
292 struct disc_frame *frame;
294 IRDA_ASSERT(self != NULL, return;);
295 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
297 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
301 frame = skb_put(tx_skb, 2);
303 frame->caddr = self->caddr | CMD_FRAME;
304 frame->control = DISC_CMD | PF_BIT;
306 irlap_queue_xmit(self, tx_skb);
310 * Function irlap_send_discovery_xid_frame (S, s, command)
312 * Build and transmit a XID (eXchange station IDentifier) discovery
315 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
316 __u8 command, discovery_t *discovery)
318 struct sk_buff *tx_skb = NULL;
319 struct xid_frame *frame;
320 __u32 bcast = BROADCAST;
323 pr_debug("%s(), s=%d, S=%d, command=%d\n", __func__,
326 IRDA_ASSERT(self != NULL, return;);
327 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
328 IRDA_ASSERT(discovery != NULL, return;);
330 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
336 frame = (struct xid_frame *) tx_skb->data;
339 frame->caddr = CBROADCAST | CMD_FRAME;
340 frame->control = XID_CMD | PF_BIT;
342 frame->caddr = CBROADCAST;
343 frame->control = XID_RSP | PF_BIT;
345 frame->ident = XID_FORMAT;
347 frame->saddr = cpu_to_le32(self->saddr);
350 frame->daddr = cpu_to_le32(bcast);
352 frame->daddr = cpu_to_le32(discovery->data.daddr);
373 frame->version = 0x00;
376 * Provide info for final slot only in commands, and for all
377 * responses. Send the second byte of the hint only if the
378 * EXTENSION bit is set in the first byte.
380 if (!command || (frame->slotnr == 0xff)) {
383 if (discovery->data.hints[0] & HINT_EXTENSION) {
384 info = skb_put(tx_skb, 2);
385 info[0] = discovery->data.hints[0];
386 info[1] = discovery->data.hints[1];
388 info = skb_put(tx_skb, 1);
389 info[0] = discovery->data.hints[0];
391 info = skb_put(tx_skb, 1);
392 info[0] = discovery->data.charset;
394 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
395 skb_put_data(tx_skb, discovery->data.info, len);
397 irlap_queue_xmit(self, tx_skb);
401 * Function irlap_recv_discovery_xid_rsp (skb, info)
403 * Received a XID discovery response
406 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
408 struct irlap_info *info)
410 struct xid_frame *xid;
411 discovery_t *discovery = NULL;
412 __u8 *discovery_info;
415 IRDA_ASSERT(self != NULL, return;);
416 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
418 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
419 net_err_ratelimited("%s: frame too short!\n", __func__);
423 xid = (struct xid_frame *) skb->data;
425 info->daddr = le32_to_cpu(xid->saddr);
426 info->saddr = le32_to_cpu(xid->daddr);
428 /* Make sure frame is addressed to us */
429 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
430 pr_debug("%s(), frame is not addressed to us!\n",
435 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
436 net_warn_ratelimited("%s: kmalloc failed!\n", __func__);
440 discovery->data.daddr = info->daddr;
441 discovery->data.saddr = self->saddr;
442 discovery->timestamp = jiffies;
444 pr_debug("%s(), daddr=%08x\n", __func__,
445 discovery->data.daddr);
447 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
449 /* Get info returned from peer */
450 discovery->data.hints[0] = discovery_info[0];
451 if (discovery_info[0] & HINT_EXTENSION) {
452 pr_debug("EXTENSION\n");
453 discovery->data.hints[1] = discovery_info[1];
454 discovery->data.charset = discovery_info[2];
455 text = (char *) &discovery_info[3];
457 discovery->data.hints[1] = 0;
458 discovery->data.charset = discovery_info[1];
459 text = (char *) &discovery_info[2];
462 * Terminate info string, should be safe since this is where the
465 skb->data[skb->len] = '\0';
466 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
467 discovery->name_len = strlen(discovery->data.info);
469 info->discovery = discovery;
471 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
475 * Function irlap_recv_discovery_xid_cmd (skb, info)
477 * Received a XID discovery command
480 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
482 struct irlap_info *info)
484 struct xid_frame *xid;
485 discovery_t *discovery = NULL;
486 __u8 *discovery_info;
489 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
490 net_err_ratelimited("%s: frame too short!\n", __func__);
494 xid = (struct xid_frame *) skb->data;
496 info->daddr = le32_to_cpu(xid->saddr);
497 info->saddr = le32_to_cpu(xid->daddr);
499 /* Make sure frame is addressed to us */
500 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
501 pr_debug("%s(), frame is not addressed to us!\n",
506 switch (xid->flags & 0x03) {
523 info->s = xid->slotnr;
525 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
528 * Check if last frame
530 if (info->s == 0xff) {
531 /* Check if things are sane at this point... */
532 if((discovery_info == NULL) ||
533 !pskb_may_pull(skb, 3)) {
534 net_err_ratelimited("%s: discovery frame too short!\n",
540 * We now have some discovery info to deliver!
542 discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC);
546 discovery->data.daddr = info->daddr;
547 discovery->data.saddr = self->saddr;
548 discovery->timestamp = jiffies;
550 discovery->data.hints[0] = discovery_info[0];
551 if (discovery_info[0] & HINT_EXTENSION) {
552 discovery->data.hints[1] = discovery_info[1];
553 discovery->data.charset = discovery_info[2];
554 text = (char *) &discovery_info[3];
556 discovery->data.hints[1] = 0;
557 discovery->data.charset = discovery_info[1];
558 text = (char *) &discovery_info[2];
561 * Terminate string, should be safe since this is where the
564 skb->data[skb->len] = '\0';
565 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
566 discovery->name_len = strlen(discovery->data.info);
568 info->discovery = discovery;
570 info->discovery = NULL;
572 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
576 * Function irlap_send_rr_frame (self, command)
578 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
579 * only possible to send RR frames with the poll bit set.
581 void irlap_send_rr_frame(struct irlap_cb *self, int command)
583 struct sk_buff *tx_skb;
584 struct rr_frame *frame;
586 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
590 frame = skb_put(tx_skb, 2);
592 frame->caddr = self->caddr;
593 frame->caddr |= (command) ? CMD_FRAME : 0;
595 frame->control = RR | PF_BIT | (self->vr << 5);
597 irlap_queue_xmit(self, tx_skb);
601 * Function irlap_send_rd_frame (self)
603 * Request disconnect. Used by a secondary station to request the
604 * disconnection of the link.
606 void irlap_send_rd_frame(struct irlap_cb *self)
608 struct sk_buff *tx_skb;
609 struct rd_frame *frame;
611 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
615 frame = skb_put(tx_skb, 2);
617 frame->caddr = self->caddr;
618 frame->control = RD_RSP | PF_BIT;
620 irlap_queue_xmit(self, tx_skb);
624 * Function irlap_recv_rr_frame (skb, info)
626 * Received RR (Receive Ready) frame from peer station, no harm in
627 * making it inline since its called only from one single place
628 * (irlap_driver_rcv).
630 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
632 struct irlap_info *info, int command)
634 info->nr = skb->data[1] >> 5;
636 /* Check if this is a command or a response frame */
638 irlap_do_event(self, RECV_RR_CMD, skb, info);
640 irlap_do_event(self, RECV_RR_RSP, skb, info);
644 * Function irlap_recv_rnr_frame (self, skb, info)
646 * Received RNR (Receive Not Ready) frame from peer station
649 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
650 struct irlap_info *info, int command)
652 info->nr = skb->data[1] >> 5;
654 pr_debug("%s(), nr=%d, %ld\n", __func__, info->nr, jiffies);
657 irlap_do_event(self, RECV_RNR_CMD, skb, info);
659 irlap_do_event(self, RECV_RNR_RSP, skb, info);
662 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
663 struct irlap_info *info, int command)
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 info->nr = skb->data[1] >> 5;
679 /* Check if this is a command or a response frame */
681 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
683 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
686 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
687 struct irlap_info *info, int command)
689 /* Check if this is a command or a response frame */
691 irlap_do_event(self, RECV_DISC_CMD, skb, info);
693 irlap_do_event(self, RECV_RD_RSP, skb, info);
697 * Function irlap_recv_ua_frame (skb, frame)
699 * Received UA (Unnumbered Acknowledgement) frame
702 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
704 struct irlap_info *info)
706 irlap_do_event(self, RECV_UA_RSP, skb, info);
710 * Function irlap_send_data_primary(self, skb)
712 * Send I-frames as the primary station but without the poll bit set
715 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
717 struct sk_buff *tx_skb;
719 if (skb->data[1] == I_FRAME) {
722 * Insert frame sequence number (Vs) in control field before
723 * inserting into transmit window queue.
725 skb->data[1] = I_FRAME | (self->vs << 1);
728 * Insert frame in store, in case of retransmissions
729 * Increase skb reference count, see irlap_do_event()
732 skb_queue_tail(&self->wx_list, skb);
735 tx_skb = skb_clone(skb, GFP_ATOMIC);
736 if (tx_skb == NULL) {
740 self->vs = (self->vs + 1) % 8;
741 self->ack_required = FALSE;
744 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
746 pr_debug("%s(), sending unreliable frame\n", __func__);
747 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
752 * Function irlap_send_data_primary_poll (self, skb)
754 * Send I(nformation) frame as primary with poll bit set
756 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
758 struct sk_buff *tx_skb;
759 int transmission_time;
762 del_timer(&self->poll_timer);
764 /* Is this reliable or unreliable data? */
765 if (skb->data[1] == I_FRAME) {
768 * Insert frame sequence number (Vs) in control field before
769 * inserting into transmit window queue.
771 skb->data[1] = I_FRAME | (self->vs << 1);
774 * Insert frame in store, in case of retransmissions
775 * Increase skb reference count, see irlap_do_event()
778 skb_queue_tail(&self->wx_list, skb);
781 tx_skb = skb_clone(skb, GFP_ATOMIC);
782 if (tx_skb == NULL) {
787 * Set poll bit if necessary. We do this to the copied
788 * skb, since retransmitted need to set or clear the poll
789 * bit depending on when they are sent.
791 tx_skb->data[1] |= PF_BIT;
793 self->vs = (self->vs + 1) % 8;
794 self->ack_required = FALSE;
796 irlap_next_state(self, LAP_NRM_P);
797 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
799 pr_debug("%s(), sending unreliable frame\n", __func__);
801 if (self->ack_required) {
802 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
803 irlap_next_state(self, LAP_NRM_P);
804 irlap_send_rr_frame(self, CMD_FRAME);
805 self->ack_required = FALSE;
807 skb->data[1] |= PF_BIT;
808 irlap_next_state(self, LAP_NRM_P);
809 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
813 /* How much time we took for transmission of all frames.
814 * We don't know, so let assume we used the full window. Jean II */
815 transmission_time = self->final_timeout;
817 /* Reset parameter so that we can fill next window */
818 self->window = self->window_size;
820 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
821 /* Remove what we have not used. Just do a prorata of the
822 * bytes left in window to window capacity.
823 * See max_line_capacities[][] in qos.c for details. Jean II */
824 transmission_time -= (self->final_timeout * self->bytes_left
825 / self->line_capacity);
826 pr_debug("%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n",
827 __func__, self->final_timeout, self->bytes_left,
828 self->line_capacity, transmission_time);
830 /* We are allowed to transmit a maximum number of bytes again. */
831 self->bytes_left = self->line_capacity;
832 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
835 * The network layer has a intermediate buffer between IrLAP
836 * and the IrDA driver which can contain 8 frames. So, even
837 * though IrLAP is currently sending the *last* frame of the
838 * tx-window, the driver most likely has only just started
839 * sending the *first* frame of the same tx-window.
840 * I.e. we are always at the very beginning of or Tx window.
841 * Now, we are supposed to set the final timer from the end
842 * of our tx-window to let the other peer reply. So, we need
843 * to add extra time to compensate for the fact that we
844 * are really at the start of tx-window, otherwise the final timer
845 * might expire before he can answer...
848 irlap_start_final_timer(self, self->final_timeout + transmission_time);
851 * The clever amongst you might ask why we do this adjustement
852 * only here, and not in all the other cases in irlap_event.c.
853 * In all those other case, we only send a very short management
854 * frame (few bytes), so the adjustement would be lost in the
856 * The exception of course is irlap_resend_rejected_frame().
861 * Function irlap_send_data_secondary_final (self, skb)
863 * Send I(nformation) frame as secondary with final bit set
866 void irlap_send_data_secondary_final(struct irlap_cb *self,
869 struct sk_buff *tx_skb = NULL;
871 IRDA_ASSERT(self != NULL, return;);
872 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
873 IRDA_ASSERT(skb != NULL, return;);
875 /* Is this reliable or unreliable data? */
876 if (skb->data[1] == I_FRAME) {
879 * Insert frame sequence number (Vs) in control field before
880 * inserting into transmit window queue.
882 skb->data[1] = I_FRAME | (self->vs << 1);
885 * Insert frame in store, in case of retransmissions
886 * Increase skb reference count, see irlap_do_event()
889 skb_queue_tail(&self->wx_list, skb);
891 tx_skb = skb_clone(skb, GFP_ATOMIC);
892 if (tx_skb == NULL) {
896 tx_skb->data[1] |= PF_BIT;
898 self->vs = (self->vs + 1) % 8;
899 self->ack_required = FALSE;
901 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
903 if (self->ack_required) {
904 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
905 irlap_send_rr_frame(self, RSP_FRAME);
906 self->ack_required = FALSE;
908 skb->data[1] |= PF_BIT;
909 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
913 self->window = self->window_size;
914 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
915 /* We are allowed to transmit a maximum number of bytes again. */
916 self->bytes_left = self->line_capacity;
917 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
919 irlap_start_wd_timer(self, self->wd_timeout);
923 * Function irlap_send_data_secondary (self, skb)
925 * Send I(nformation) frame as secondary without final bit set
928 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
930 struct sk_buff *tx_skb = NULL;
932 /* Is this reliable or unreliable data? */
933 if (skb->data[1] == I_FRAME) {
936 * Insert frame sequence number (Vs) in control field before
937 * inserting into transmit window queue.
939 skb->data[1] = I_FRAME | (self->vs << 1);
942 * Insert frame in store, in case of retransmissions
943 * Increase skb reference count, see irlap_do_event()
946 skb_queue_tail(&self->wx_list, skb);
948 tx_skb = skb_clone(skb, GFP_ATOMIC);
949 if (tx_skb == NULL) {
953 self->vs = (self->vs + 1) % 8;
954 self->ack_required = FALSE;
957 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
959 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
965 * Function irlap_resend_rejected_frames (nr)
967 * Resend frames which has not been acknowledged. Should be safe to
968 * traverse the list without locking it since this function will only be
969 * called from interrupt context (BH)
971 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
973 struct sk_buff *tx_skb;
976 IRDA_ASSERT(self != NULL, return;);
977 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
979 /* Resend unacknowledged frame(s) */
980 skb_queue_walk(&self->wx_list, skb) {
981 irlap_wait_min_turn_around(self, &self->qos_tx);
983 /* We copy the skb to be retransmitted since we will have to
984 * modify it. Cloning will confuse packet sniffers
986 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
987 tx_skb = skb_copy(skb, GFP_ATOMIC);
989 pr_debug("%s(), unable to copy\n", __func__);
993 /* Clear old Nr field + poll bit */
994 tx_skb->data[1] &= 0x0f;
997 * Set poll bit on the last frame retransmitted
999 if (skb_queue_is_last(&self->wx_list, skb))
1000 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1002 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1004 irlap_send_i_frame(self, tx_skb, command);
1008 * We can now fill the window with additional data frames
1010 while (!skb_queue_empty(&self->txq)) {
1012 pr_debug("%s(), sending additional frames!\n", __func__);
1013 if (self->window > 0) {
1014 skb = skb_dequeue( &self->txq);
1015 IRDA_ASSERT(skb != NULL, return;);
1018 * If send window > 1 then send frame with pf
1021 if ((self->window > 1) &&
1022 !skb_queue_empty(&self->txq)) {
1023 irlap_send_data_primary(self, skb);
1025 irlap_send_data_primary_poll(self, skb);
1033 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1035 struct sk_buff *tx_skb;
1036 struct sk_buff *skb;
1038 IRDA_ASSERT(self != NULL, return;);
1039 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1041 /* Resend unacknowledged frame(s) */
1042 skb = skb_peek(&self->wx_list);
1044 irlap_wait_min_turn_around(self, &self->qos_tx);
1046 /* We copy the skb to be retransmitted since we will have to
1047 * modify it. Cloning will confuse packet sniffers
1049 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1050 tx_skb = skb_copy(skb, GFP_ATOMIC);
1052 pr_debug("%s(), unable to copy\n", __func__);
1056 /* Clear old Nr field + poll bit */
1057 tx_skb->data[1] &= 0x0f;
1059 /* Set poll/final bit */
1060 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1062 irlap_send_i_frame(self, tx_skb, command);
1067 * Function irlap_send_ui_frame (self, skb, command)
1069 * Contruct and transmit an Unnumbered Information (UI) frame
1072 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1073 __u8 caddr, int command)
1075 IRDA_ASSERT(self != NULL, return;);
1076 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1077 IRDA_ASSERT(skb != NULL, return;);
1079 /* Insert connection address */
1080 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1082 irlap_queue_xmit(self, skb);
1086 * Function irlap_send_i_frame (skb)
1088 * Contruct and transmit Information (I) frame
1090 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1093 /* Insert connection address */
1094 skb->data[0] = self->caddr;
1095 skb->data[0] |= (command) ? CMD_FRAME : 0;
1097 /* Insert next to receive (Vr) */
1098 skb->data[1] |= (self->vr << 5); /* insert nr */
1100 irlap_queue_xmit(self, skb);
1104 * Function irlap_recv_i_frame (skb, frame)
1106 * Receive and parse an I (Information) frame, no harm in making it inline
1107 * since it's called only from one single place (irlap_driver_rcv).
1109 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1110 struct sk_buff *skb,
1111 struct irlap_info *info, int command)
1113 info->nr = skb->data[1] >> 5; /* Next to receive */
1114 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1115 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1117 /* Check if this is a command or a response frame */
1119 irlap_do_event(self, RECV_I_CMD, skb, info);
1121 irlap_do_event(self, RECV_I_RSP, skb, info);
1125 * Function irlap_recv_ui_frame (self, skb, info)
1127 * Receive and parse an Unnumbered Information (UI) frame
1130 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1131 struct irlap_info *info)
1133 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1135 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1139 * Function irlap_recv_frmr_frame (skb, frame)
1141 * Received Frame Reject response.
1144 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1145 struct irlap_info *info)
1150 IRDA_ASSERT(self != NULL, return;);
1151 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1152 IRDA_ASSERT(skb != NULL, return;);
1153 IRDA_ASSERT(info != NULL, return;);
1155 if (!pskb_may_pull(skb, 4)) {
1156 net_err_ratelimited("%s: frame too short!\n", __func__);
1162 info->nr = frame[2] >> 5; /* Next to receive */
1163 info->pf = frame[2] & PF_BIT; /* Final bit */
1164 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1166 w = frame[3] & 0x01;
1167 x = frame[3] & 0x02;
1168 y = frame[3] & 0x04;
1169 z = frame[3] & 0x08;
1172 pr_debug("Rejected control field is undefined or not implemented\n");
1175 pr_debug("Rejected control field was invalid because it contained a non permitted I field\n");
1178 pr_debug("Received I field exceeded the maximum negotiated for the existing connection or exceeded the maximum this station supports if no connection exists\n");
1181 pr_debug("Rejected control field control field contained an invalid Nr count\n");
1183 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1187 * Function irlap_send_test_frame (self, daddr)
1189 * Send a test frame response
1192 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1193 struct sk_buff *cmd)
1195 struct sk_buff *tx_skb;
1196 struct test_frame *frame;
1198 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
1202 /* Broadcast frames must include saddr and daddr fields */
1203 if (caddr == CBROADCAST) {
1204 frame = skb_put(tx_skb, sizeof(struct test_frame));
1206 /* Insert the swapped addresses */
1207 frame->saddr = cpu_to_le32(self->saddr);
1208 frame->daddr = cpu_to_le32(daddr);
1210 frame = skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1212 frame->caddr = caddr;
1213 frame->control = TEST_RSP | PF_BIT;
1216 skb_put_data(tx_skb, cmd->data, cmd->len);
1218 /* Return to sender */
1219 irlap_wait_min_turn_around(self, &self->qos_tx);
1220 irlap_queue_xmit(self, tx_skb);
1224 * Function irlap_recv_test_frame (self, skb)
1226 * Receive a test frame
1229 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1230 struct irlap_info *info, int command)
1232 struct test_frame *frame;
1234 if (!pskb_may_pull(skb, sizeof(*frame))) {
1235 net_err_ratelimited("%s: frame too short!\n", __func__);
1238 frame = (struct test_frame *) skb->data;
1240 /* Broadcast frames must carry saddr and daddr fields */
1241 if (info->caddr == CBROADCAST) {
1242 if (skb->len < sizeof(struct test_frame)) {
1243 pr_debug("%s() test frame too short!\n",
1248 /* Read and swap addresses */
1249 info->daddr = le32_to_cpu(frame->saddr);
1250 info->saddr = le32_to_cpu(frame->daddr);
1252 /* Make sure frame is addressed to us */
1253 if ((info->saddr != self->saddr) &&
1254 (info->saddr != BROADCAST)) {
1260 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1262 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1266 * Function irlap_driver_rcv (skb, netdev, ptype)
1268 * Called when a frame is received. Dispatches the right receive function
1269 * for processing of the frame.
1271 * Note on skb management :
1272 * After calling the higher layers of the IrDA stack, we always
1273 * kfree() the skb, which drop the reference count (and potentially
1275 * If a higher layer of the stack want to keep the skb around (to put
1276 * in a queue or pass it to the higher layer), it will need to use
1277 * skb_get() to keep a reference on it. This is usually done at the
1278 * LMP level in irlmp.c.
1281 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1282 struct packet_type *ptype, struct net_device *orig_dev)
1284 struct irlap_info info;
1285 struct irlap_cb *self;
1290 if (!net_eq(dev_net(dev), &init_net))
1293 /* FIXME: should we get our own field? */
1294 self = (struct irlap_cb *) dev->atalk_ptr;
1296 /* If the net device is down, then IrLAP is gone! */
1297 if (!self || self->magic != LAP_MAGIC)
1300 /* We are no longer an "old" protocol, so we need to handle
1301 * share and non linear skbs. This should never happen, so
1302 * we don't need to be clever about it. Jean II */
1303 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1304 net_err_ratelimited("%s: can't clone shared skb!\n", __func__);
1308 /* Check if frame is large enough for parsing */
1309 if (!pskb_may_pull(skb, 2)) {
1310 net_err_ratelimited("%s: frame too short!\n", __func__);
1314 command = skb->data[0] & CMD_FRAME;
1315 info.caddr = skb->data[0] & CBROADCAST;
1317 info.pf = skb->data[1] & PF_BIT;
1318 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1320 control = info.control;
1322 /* First we check if this frame has a valid connection address */
1323 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1324 pr_debug("%s(), wrong connection address!\n",
1329 * Optimize for the common case and check if the frame is an
1330 * I(nformation) frame. Only I-frames have bit 0 set to 0
1332 if (~control & 0x01) {
1333 irlap_recv_i_frame(self, skb, &info, command);
1337 * We now check is the frame is an S(upervisory) frame. Only
1338 * S-frames have bit 0 set to 1 and bit 1 set to 0
1340 if (~control & 0x02) {
1342 * Received S(upervisory) frame, check which frame type it is
1343 * only the first nibble is of interest
1345 switch (control & 0x0f) {
1347 irlap_recv_rr_frame(self, skb, &info, command);
1350 irlap_recv_rnr_frame(self, skb, &info, command);
1353 irlap_recv_rej_frame(self, skb, &info, command);
1356 irlap_recv_srej_frame(self, skb, &info, command);
1359 net_warn_ratelimited("%s: Unknown S-frame %02x received!\n",
1360 __func__, info.control);
1366 * This must be a C(ontrol) frame
1370 irlap_recv_discovery_xid_rsp(self, skb, &info);
1373 irlap_recv_discovery_xid_cmd(self, skb, &info);
1376 irlap_recv_snrm_cmd(self, skb, &info);
1379 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1381 case DISC_CMD: /* And RD_RSP since they have the same value */
1382 irlap_recv_disc_frame(self, skb, &info, command);
1385 irlap_recv_test_frame(self, skb, &info, command);
1388 irlap_recv_ua_frame(self, skb, &info);
1391 irlap_recv_frmr_frame(self, skb, &info);
1394 irlap_recv_ui_frame(self, skb, &info);
1397 net_warn_ratelimited("%s: Unknown frame %02x received!\n",
1398 __func__, info.control);
1404 /* Always drop our reference on the skb */