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->h.raw = skb->nh.raw = skb->mac.raw = skb->data;
97 skb->protocol = htons(ETH_P_IRDA);
98 skb->priority = TC_PRIO_BESTEFFORT;
100 irlap_insert_info(self, skb);
106 * Function irlap_send_snrm_cmd (void)
108 * Transmits a connect SNRM command frame
110 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
112 struct sk_buff *tx_skb;
113 struct snrm_frame *frame;
116 IRDA_ASSERT(self != NULL, return;);
117 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
120 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
121 IRLAP_NEGOCIATION_PARAMS_LEN,
126 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
128 /* Insert connection address field */
130 frame->caddr = CMD_FRAME | CBROADCAST;
132 frame->caddr = CMD_FRAME | self->caddr;
134 /* Insert control field */
135 frame->control = SNRM_CMD | PF_BIT;
138 * If we are establishing a connection then insert QoS paramerters
141 skb_put(tx_skb, 9); /* 25 left */
142 frame->saddr = cpu_to_le32(self->saddr);
143 frame->daddr = cpu_to_le32(self->daddr);
145 frame->ncaddr = self->caddr;
147 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
149 dev_kfree_skb(tx_skb);
153 irlap_queue_xmit(self, tx_skb);
157 * Function irlap_recv_snrm_cmd (skb, info)
159 * Received SNRM (Set Normal Response Mode) command frame
162 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
163 struct irlap_info *info)
165 struct snrm_frame *frame;
167 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
168 frame = (struct snrm_frame *) skb->data;
170 /* Copy the new connection address ignoring the C/R bit */
171 info->caddr = frame->ncaddr & 0xFE;
173 /* Check if the new connection address is valid */
174 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
175 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
180 /* Copy peer device address */
181 info->daddr = le32_to_cpu(frame->saddr);
182 info->saddr = le32_to_cpu(frame->daddr);
184 /* Only accept if addressed directly to us */
185 if (info->saddr != self->saddr) {
186 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
190 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
192 /* Signal that this SNRM frame does not contain and I-field */
193 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
198 * Function irlap_send_ua_response_frame (qos)
200 * Send UA (Unnumbered Acknowledgement) frame
203 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
205 struct sk_buff *tx_skb;
206 struct ua_frame *frame;
209 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
211 IRDA_ASSERT(self != NULL, return;);
212 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
215 tx_skb = alloc_skb(sizeof(struct ua_frame) +
216 IRLAP_NEGOCIATION_PARAMS_LEN,
221 frame = (struct ua_frame *) skb_put(tx_skb, 10);
223 /* Build UA response */
224 frame->caddr = self->caddr;
225 frame->control = UA_RSP | PF_BIT;
227 frame->saddr = cpu_to_le32(self->saddr);
228 frame->daddr = cpu_to_le32(self->daddr);
230 /* Should we send QoS negotiation parameters? */
232 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
234 dev_kfree_skb(tx_skb);
239 irlap_queue_xmit(self, tx_skb);
244 * Function irlap_send_dm_frame (void)
246 * Send disconnected mode (DM) frame
249 void irlap_send_dm_frame( struct irlap_cb *self)
251 struct sk_buff *tx_skb = NULL;
252 struct dm_frame *frame;
254 IRDA_ASSERT(self != NULL, return;);
255 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
257 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
261 frame = (struct dm_frame *)skb_put(tx_skb, 2);
263 if (self->state == LAP_NDM)
264 frame->caddr = CBROADCAST;
266 frame->caddr = self->caddr;
268 frame->control = DM_RSP | PF_BIT;
270 irlap_queue_xmit(self, tx_skb);
274 * Function irlap_send_disc_frame (void)
276 * Send disconnect (DISC) frame
279 void irlap_send_disc_frame(struct irlap_cb *self)
281 struct sk_buff *tx_skb = NULL;
282 struct disc_frame *frame;
284 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
286 IRDA_ASSERT(self != NULL, return;);
287 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
289 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
293 frame = (struct disc_frame *)skb_put(tx_skb, 2);
295 frame->caddr = self->caddr | CMD_FRAME;
296 frame->control = DISC_CMD | PF_BIT;
298 irlap_queue_xmit(self, tx_skb);
302 * Function irlap_send_discovery_xid_frame (S, s, command)
304 * Build and transmit a XID (eXchange station IDentifier) discovery
307 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
308 __u8 command, discovery_t *discovery)
310 struct sk_buff *tx_skb = NULL;
311 struct xid_frame *frame;
312 __u32 bcast = BROADCAST;
315 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
318 IRDA_ASSERT(self != NULL, return;);
319 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
320 IRDA_ASSERT(discovery != NULL, return;);
322 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
328 frame = (struct xid_frame *) tx_skb->data;
331 frame->caddr = CBROADCAST | CMD_FRAME;
332 frame->control = XID_CMD | PF_BIT;
334 frame->caddr = CBROADCAST;
335 frame->control = XID_RSP | PF_BIT;
337 frame->ident = XID_FORMAT;
339 frame->saddr = cpu_to_le32(self->saddr);
342 frame->daddr = cpu_to_le32(bcast);
344 frame->daddr = cpu_to_le32(discovery->data.daddr);
365 frame->version = 0x00;
368 * Provide info for final slot only in commands, and for all
369 * responses. Send the second byte of the hint only if the
370 * EXTENSION bit is set in the first byte.
372 if (!command || (frame->slotnr == 0xff)) {
375 if (discovery->data.hints[0] & HINT_EXTENSION) {
376 info = skb_put(tx_skb, 2);
377 info[0] = discovery->data.hints[0];
378 info[1] = discovery->data.hints[1];
380 info = skb_put(tx_skb, 1);
381 info[0] = discovery->data.hints[0];
383 info = skb_put(tx_skb, 1);
384 info[0] = discovery->data.charset;
386 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
387 info = skb_put(tx_skb, len);
388 memcpy(info, discovery->data.info, len);
390 irlap_queue_xmit(self, tx_skb);
394 * Function irlap_recv_discovery_xid_rsp (skb, info)
396 * Received a XID discovery response
399 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
401 struct irlap_info *info)
403 struct xid_frame *xid;
404 discovery_t *discovery = NULL;
405 __u8 *discovery_info;
408 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
410 IRDA_ASSERT(self != NULL, return;);
411 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
413 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
414 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
418 xid = (struct xid_frame *) skb->data;
420 info->daddr = le32_to_cpu(xid->saddr);
421 info->saddr = le32_to_cpu(xid->daddr);
423 /* Make sure frame is addressed to us */
424 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
425 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
430 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
431 IRDA_WARNING("%s: kmalloc failed!\n", __FUNCTION__);
435 discovery->data.daddr = info->daddr;
436 discovery->data.saddr = self->saddr;
437 discovery->timestamp = jiffies;
439 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
440 discovery->data.daddr);
442 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
444 /* Get info returned from peer */
445 discovery->data.hints[0] = discovery_info[0];
446 if (discovery_info[0] & HINT_EXTENSION) {
447 IRDA_DEBUG(4, "EXTENSION\n");
448 discovery->data.hints[1] = discovery_info[1];
449 discovery->data.charset = discovery_info[2];
450 text = (char *) &discovery_info[3];
452 discovery->data.hints[1] = 0;
453 discovery->data.charset = discovery_info[1];
454 text = (char *) &discovery_info[2];
457 * Terminate info string, should be safe since this is where the
460 skb->data[skb->len] = '\0';
461 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
462 discovery->name_len = strlen(discovery->data.info);
464 info->discovery = discovery;
466 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
470 * Function irlap_recv_discovery_xid_cmd (skb, info)
472 * Received a XID discovery command
475 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
477 struct irlap_info *info)
479 struct xid_frame *xid;
480 discovery_t *discovery = NULL;
481 __u8 *discovery_info;
484 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
485 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
489 xid = (struct xid_frame *) skb->data;
491 info->daddr = le32_to_cpu(xid->saddr);
492 info->saddr = le32_to_cpu(xid->daddr);
494 /* Make sure frame is addressed to us */
495 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
496 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
501 switch (xid->flags & 0x03) {
518 info->s = xid->slotnr;
520 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
523 * Check if last frame
525 if (info->s == 0xff) {
526 /* Check if things are sane at this point... */
527 if((discovery_info == NULL) ||
528 !pskb_may_pull(skb, 3)) {
529 IRDA_ERROR("%s: discovery frame to short!\n",
535 * We now have some discovery info to deliver!
537 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
539 IRDA_WARNING("%s: unable to malloc!\n", __FUNCTION__);
543 discovery->data.daddr = info->daddr;
544 discovery->data.saddr = self->saddr;
545 discovery->timestamp = jiffies;
547 discovery->data.hints[0] = discovery_info[0];
548 if (discovery_info[0] & HINT_EXTENSION) {
549 discovery->data.hints[1] = discovery_info[1];
550 discovery->data.charset = discovery_info[2];
551 text = (char *) &discovery_info[3];
553 discovery->data.hints[1] = 0;
554 discovery->data.charset = discovery_info[1];
555 text = (char *) &discovery_info[2];
558 * Terminate string, should be safe since this is where the
561 skb->data[skb->len] = '\0';
562 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
563 discovery->name_len = strlen(discovery->data.info);
565 info->discovery = discovery;
567 info->discovery = NULL;
569 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
573 * Function irlap_send_rr_frame (self, command)
575 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
576 * only possible to send RR frames with the poll bit set.
578 void irlap_send_rr_frame(struct irlap_cb *self, int command)
580 struct sk_buff *tx_skb;
581 struct rr_frame *frame;
583 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
587 frame = (struct rr_frame *)skb_put(tx_skb, 2);
589 frame->caddr = self->caddr;
590 frame->caddr |= (command) ? CMD_FRAME : 0;
592 frame->control = RR | PF_BIT | (self->vr << 5);
594 irlap_queue_xmit(self, tx_skb);
598 * Function irlap_send_rd_frame (self)
600 * Request disconnect. Used by a secondary station to request the
601 * disconnection of the link.
603 void irlap_send_rd_frame(struct irlap_cb *self)
605 struct sk_buff *tx_skb;
606 struct rd_frame *frame;
608 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
612 frame = (struct rd_frame *)skb_put(tx_skb, 2);
614 frame->caddr = self->caddr;
615 frame->caddr = RD_RSP | PF_BIT;
617 irlap_queue_xmit(self, tx_skb);
621 * Function irlap_recv_rr_frame (skb, info)
623 * Received RR (Receive Ready) frame from peer station, no harm in
624 * making it inline since its called only from one single place
625 * (irlap_driver_rcv).
627 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
629 struct irlap_info *info, int command)
631 info->nr = skb->data[1] >> 5;
633 /* Check if this is a command or a response frame */
635 irlap_do_event(self, RECV_RR_CMD, skb, info);
637 irlap_do_event(self, RECV_RR_RSP, skb, info);
641 * Function irlap_recv_rnr_frame (self, skb, info)
643 * Received RNR (Receive Not Ready) frame from peer station
646 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
647 struct irlap_info *info, int command)
649 info->nr = skb->data[1] >> 5;
651 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
654 irlap_do_event(self, RECV_RNR_CMD, skb, info);
656 irlap_do_event(self, RECV_RNR_RSP, skb, info);
659 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
660 struct irlap_info *info, int command)
662 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
664 info->nr = skb->data[1] >> 5;
666 /* Check if this is a command or a response frame */
668 irlap_do_event(self, RECV_REJ_CMD, skb, info);
670 irlap_do_event(self, RECV_REJ_RSP, skb, info);
673 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
674 struct irlap_info *info, int command)
676 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
678 info->nr = skb->data[1] >> 5;
680 /* Check if this is a command or a response frame */
682 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
684 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
687 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
688 struct irlap_info *info, int command)
690 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
692 /* Check if this is a command or a response frame */
694 irlap_do_event(self, RECV_DISC_CMD, skb, info);
696 irlap_do_event(self, RECV_RD_RSP, skb, info);
700 * Function irlap_recv_ua_frame (skb, frame)
702 * Received UA (Unnumbered Acknowledgement) frame
705 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
707 struct irlap_info *info)
709 irlap_do_event(self, RECV_UA_RSP, skb, info);
713 * Function irlap_send_data_primary(self, skb)
715 * Send I-frames as the primary station but without the poll bit set
718 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
720 struct sk_buff *tx_skb;
722 if (skb->data[1] == I_FRAME) {
725 * Insert frame sequence number (Vs) in control field before
726 * inserting into transmit window queue.
728 skb->data[1] = I_FRAME | (self->vs << 1);
731 * Insert frame in store, in case of retransmissions
732 * Increase skb reference count, see irlap_do_event()
735 skb_queue_tail(&self->wx_list, skb);
738 tx_skb = skb_clone(skb, GFP_ATOMIC);
739 if (tx_skb == NULL) {
743 self->vs = (self->vs + 1) % 8;
744 self->ack_required = FALSE;
747 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
749 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
750 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
755 * Function irlap_send_data_primary_poll (self, skb)
757 * Send I(nformation) frame as primary with poll bit set
759 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
761 struct sk_buff *tx_skb;
762 int transmission_time;
765 del_timer(&self->poll_timer);
767 /* Is this reliable or unreliable data? */
768 if (skb->data[1] == I_FRAME) {
771 * Insert frame sequence number (Vs) in control field before
772 * inserting into transmit window queue.
774 skb->data[1] = I_FRAME | (self->vs << 1);
777 * Insert frame in store, in case of retransmissions
778 * Increase skb reference count, see irlap_do_event()
781 skb_queue_tail(&self->wx_list, skb);
784 tx_skb = skb_clone(skb, GFP_ATOMIC);
785 if (tx_skb == NULL) {
790 * Set poll bit if necessary. We do this to the copied
791 * skb, since retransmitted need to set or clear the poll
792 * bit depending on when they are sent.
794 tx_skb->data[1] |= PF_BIT;
796 self->vs = (self->vs + 1) % 8;
797 self->ack_required = FALSE;
799 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
801 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
803 if (self->ack_required) {
804 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
805 irlap_send_rr_frame(self, CMD_FRAME);
806 self->ack_required = FALSE;
808 skb->data[1] |= PF_BIT;
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 IRDA_DEBUG(4, "%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n", __FUNCTION__, self->final_timeout, self->bytes_left, self->line_capacity, transmission_time);
828 /* We are allowed to transmit a maximum number of bytes again. */
829 self->bytes_left = self->line_capacity;
830 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
833 * The network layer has a intermediate buffer between IrLAP
834 * and the IrDA driver which can contain 8 frames. So, even
835 * though IrLAP is currently sending the *last* frame of the
836 * tx-window, the driver most likely has only just started
837 * sending the *first* frame of the same tx-window.
838 * I.e. we are always at the very begining of or Tx window.
839 * Now, we are supposed to set the final timer from the end
840 * of our tx-window to let the other peer reply. So, we need
841 * to add extra time to compensate for the fact that we
842 * are really at the start of tx-window, otherwise the final timer
843 * might expire before he can answer...
846 irlap_start_final_timer(self, self->final_timeout + transmission_time);
849 * The clever amongst you might ask why we do this adjustement
850 * only here, and not in all the other cases in irlap_event.c.
851 * In all those other case, we only send a very short management
852 * frame (few bytes), so the adjustement would be lost in the
854 * The exception of course is irlap_resend_rejected_frame().
859 * Function irlap_send_data_secondary_final (self, skb)
861 * Send I(nformation) frame as secondary with final bit set
864 void irlap_send_data_secondary_final(struct irlap_cb *self,
867 struct sk_buff *tx_skb = NULL;
869 IRDA_ASSERT(self != NULL, return;);
870 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
871 IRDA_ASSERT(skb != NULL, return;);
873 /* Is this reliable or unreliable data? */
874 if (skb->data[1] == I_FRAME) {
877 * Insert frame sequence number (Vs) in control field before
878 * inserting into transmit window queue.
880 skb->data[1] = I_FRAME | (self->vs << 1);
883 * Insert frame in store, in case of retransmissions
884 * Increase skb reference count, see irlap_do_event()
887 skb_queue_tail(&self->wx_list, skb);
889 tx_skb = skb_clone(skb, GFP_ATOMIC);
890 if (tx_skb == NULL) {
894 tx_skb->data[1] |= PF_BIT;
896 self->vs = (self->vs + 1) % 8;
897 self->ack_required = FALSE;
899 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
901 if (self->ack_required) {
902 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
903 irlap_send_rr_frame(self, RSP_FRAME);
904 self->ack_required = FALSE;
906 skb->data[1] |= PF_BIT;
907 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
911 self->window = self->window_size;
912 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
913 /* We are allowed to transmit a maximum number of bytes again. */
914 self->bytes_left = self->line_capacity;
915 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
917 irlap_start_wd_timer(self, self->wd_timeout);
921 * Function irlap_send_data_secondary (self, skb)
923 * Send I(nformation) frame as secondary without final bit set
926 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
928 struct sk_buff *tx_skb = NULL;
930 /* Is this reliable or unreliable data? */
931 if (skb->data[1] == I_FRAME) {
934 * Insert frame sequence number (Vs) in control field before
935 * inserting into transmit window queue.
937 skb->data[1] = I_FRAME | (self->vs << 1);
940 * Insert frame in store, in case of retransmissions
941 * Increase skb reference count, see irlap_do_event()
944 skb_queue_tail(&self->wx_list, skb);
946 tx_skb = skb_clone(skb, GFP_ATOMIC);
947 if (tx_skb == NULL) {
951 self->vs = (self->vs + 1) % 8;
952 self->ack_required = FALSE;
955 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
957 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
963 * Function irlap_resend_rejected_frames (nr)
965 * Resend frames which has not been acknowledged. Should be safe to
966 * traverse the list without locking it since this function will only be
967 * called from interrupt context (BH)
969 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
971 struct sk_buff *tx_skb;
975 IRDA_ASSERT(self != NULL, return;);
976 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
978 /* Initialize variables */
979 count = skb_queue_len(&self->wx_list);
981 /* Resend unacknowledged frame(s) */
982 skb = skb_peek(&self->wx_list);
983 while (skb != NULL) {
984 irlap_wait_min_turn_around(self, &self->qos_tx);
986 /* We copy the skb to be retransmitted since we will have to
987 * modify it. Cloning will confuse packet sniffers
989 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
990 tx_skb = skb_copy(skb, GFP_ATOMIC);
992 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
996 /* Clear old Nr field + poll bit */
997 tx_skb->data[1] &= 0x0f;
1000 * Set poll bit on the last frame retransmitted
1003 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1005 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1007 irlap_send_i_frame(self, tx_skb, command);
1010 * If our skb is the last buffer in the list, then
1011 * we are finished, if not, move to the next sk-buffer
1013 if (skb == skb_peek_tail(&self->wx_list))
1020 * We can now fill the window with additional data frames
1022 while (!skb_queue_empty(&self->txq)) {
1024 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
1025 if (self->window > 0) {
1026 skb = skb_dequeue( &self->txq);
1027 IRDA_ASSERT(skb != NULL, return;);
1030 * If send window > 1 then send frame with pf
1033 if ((self->window > 1) &&
1034 !skb_queue_empty(&self->txq)) {
1035 irlap_send_data_primary(self, skb);
1037 irlap_send_data_primary_poll(self, skb);
1045 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1047 struct sk_buff *tx_skb;
1048 struct sk_buff *skb;
1050 IRDA_ASSERT(self != NULL, return;);
1051 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1053 /* Resend unacknowledged frame(s) */
1054 skb = skb_peek(&self->wx_list);
1056 irlap_wait_min_turn_around(self, &self->qos_tx);
1058 /* We copy the skb to be retransmitted since we will have to
1059 * modify it. Cloning will confuse packet sniffers
1061 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1062 tx_skb = skb_copy(skb, GFP_ATOMIC);
1064 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1068 /* Clear old Nr field + poll bit */
1069 tx_skb->data[1] &= 0x0f;
1071 /* Set poll/final bit */
1072 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1074 irlap_send_i_frame(self, tx_skb, command);
1079 * Function irlap_send_ui_frame (self, skb, command)
1081 * Contruct and transmit an Unnumbered Information (UI) frame
1084 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1085 __u8 caddr, int command)
1087 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1089 IRDA_ASSERT(self != NULL, return;);
1090 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1091 IRDA_ASSERT(skb != NULL, return;);
1093 /* Insert connection address */
1094 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1096 irlap_queue_xmit(self, skb);
1100 * Function irlap_send_i_frame (skb)
1102 * Contruct and transmit Information (I) frame
1104 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1107 /* Insert connection address */
1108 skb->data[0] = self->caddr;
1109 skb->data[0] |= (command) ? CMD_FRAME : 0;
1111 /* Insert next to receive (Vr) */
1112 skb->data[1] |= (self->vr << 5); /* insert nr */
1114 irlap_queue_xmit(self, skb);
1118 * Function irlap_recv_i_frame (skb, frame)
1120 * Receive and parse an I (Information) frame, no harm in making it inline
1121 * since it's called only from one single place (irlap_driver_rcv).
1123 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1124 struct sk_buff *skb,
1125 struct irlap_info *info, int command)
1127 info->nr = skb->data[1] >> 5; /* Next to receive */
1128 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1129 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1131 /* Check if this is a command or a response frame */
1133 irlap_do_event(self, RECV_I_CMD, skb, info);
1135 irlap_do_event(self, RECV_I_RSP, skb, info);
1139 * Function irlap_recv_ui_frame (self, skb, info)
1141 * Receive and parse an Unnumbered Information (UI) frame
1144 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1145 struct irlap_info *info)
1147 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1149 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1151 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1155 * Function irlap_recv_frmr_frame (skb, frame)
1157 * Received Frame Reject response.
1160 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1161 struct irlap_info *info)
1166 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1168 IRDA_ASSERT(self != NULL, return;);
1169 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1170 IRDA_ASSERT(skb != NULL, return;);
1171 IRDA_ASSERT(info != NULL, return;);
1173 if (!pskb_may_pull(skb, 4)) {
1174 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1180 info->nr = frame[2] >> 5; /* Next to receive */
1181 info->pf = frame[2] & PF_BIT; /* Final bit */
1182 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1184 w = frame[3] & 0x01;
1185 x = frame[3] & 0x02;
1186 y = frame[3] & 0x04;
1187 z = frame[3] & 0x08;
1190 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1194 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1195 "contained a non permitted I field.\n");
1198 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1199 "for the existing connection or exceeded the maximum "
1200 "this station supports if no connection exists.\n");
1203 IRDA_DEBUG(0, "Rejected control field control field contained an "
1204 "invalid Nr count.\n");
1206 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1210 * Function irlap_send_test_frame (self, daddr)
1212 * Send a test frame response
1215 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1216 struct sk_buff *cmd)
1218 struct sk_buff *tx_skb;
1219 struct test_frame *frame;
1222 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
1226 /* Broadcast frames must include saddr and daddr fields */
1227 if (caddr == CBROADCAST) {
1228 frame = (struct test_frame *)
1229 skb_put(tx_skb, sizeof(struct test_frame));
1231 /* Insert the swapped addresses */
1232 frame->saddr = cpu_to_le32(self->saddr);
1233 frame->daddr = cpu_to_le32(daddr);
1235 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1237 frame->caddr = caddr;
1238 frame->control = TEST_RSP | PF_BIT;
1241 info = skb_put(tx_skb, cmd->len);
1242 memcpy(info, cmd->data, cmd->len);
1244 /* Return to sender */
1245 irlap_wait_min_turn_around(self, &self->qos_tx);
1246 irlap_queue_xmit(self, tx_skb);
1250 * Function irlap_recv_test_frame (self, skb)
1252 * Receive a test frame
1255 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1256 struct irlap_info *info, int command)
1258 struct test_frame *frame;
1260 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1262 if (!pskb_may_pull(skb, sizeof(*frame))) {
1263 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1266 frame = (struct test_frame *) skb->data;
1268 /* Broadcast frames must carry saddr and daddr fields */
1269 if (info->caddr == CBROADCAST) {
1270 if (skb->len < sizeof(struct test_frame)) {
1271 IRDA_DEBUG(0, "%s() test frame to short!\n",
1276 /* Read and swap addresses */
1277 info->daddr = le32_to_cpu(frame->saddr);
1278 info->saddr = le32_to_cpu(frame->daddr);
1280 /* Make sure frame is addressed to us */
1281 if ((info->saddr != self->saddr) &&
1282 (info->saddr != BROADCAST)) {
1288 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1290 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1294 * Function irlap_driver_rcv (skb, netdev, ptype)
1296 * Called when a frame is received. Dispatches the right receive function
1297 * for processing of the frame.
1299 * Note on skb management :
1300 * After calling the higher layers of the IrDA stack, we always
1301 * kfree() the skb, which drop the reference count (and potentially
1303 * If a higher layer of the stack want to keep the skb around (to put
1304 * in a queue or pass it to the higher layer), it will need to use
1305 * skb_get() to keep a reference on it. This is usually done at the
1306 * LMP level in irlmp.c.
1309 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1310 struct packet_type *ptype, struct net_device *orig_dev)
1312 struct irlap_info info;
1313 struct irlap_cb *self;
1317 /* FIXME: should we get our own field? */
1318 self = (struct irlap_cb *) dev->atalk_ptr;
1320 /* If the net device is down, then IrLAP is gone! */
1321 if (!self || self->magic != LAP_MAGIC) {
1326 /* We are no longer an "old" protocol, so we need to handle
1327 * share and non linear skbs. This should never happen, so
1328 * we don't need to be clever about it. Jean II */
1329 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1330 IRDA_ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
1335 /* Check if frame is large enough for parsing */
1336 if (!pskb_may_pull(skb, 2)) {
1337 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1342 command = skb->data[0] & CMD_FRAME;
1343 info.caddr = skb->data[0] & CBROADCAST;
1345 info.pf = skb->data[1] & PF_BIT;
1346 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1348 control = info.control;
1350 /* First we check if this frame has a valid connection address */
1351 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1352 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1357 * Optimize for the common case and check if the frame is an
1358 * I(nformation) frame. Only I-frames have bit 0 set to 0
1360 if (~control & 0x01) {
1361 irlap_recv_i_frame(self, skb, &info, command);
1365 * We now check is the frame is an S(upervisory) frame. Only
1366 * S-frames have bit 0 set to 1 and bit 1 set to 0
1368 if (~control & 0x02) {
1370 * Received S(upervisory) frame, check which frame type it is
1371 * only the first nibble is of interest
1373 switch (control & 0x0f) {
1375 irlap_recv_rr_frame(self, skb, &info, command);
1378 irlap_recv_rnr_frame(self, skb, &info, command);
1381 irlap_recv_rej_frame(self, skb, &info, command);
1384 irlap_recv_srej_frame(self, skb, &info, command);
1387 IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
1388 __FUNCTION__, info.control);
1394 * This must be a C(ontrol) frame
1398 irlap_recv_discovery_xid_rsp(self, skb, &info);
1401 irlap_recv_discovery_xid_cmd(self, skb, &info);
1404 irlap_recv_snrm_cmd(self, skb, &info);
1407 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1409 case DISC_CMD: /* And RD_RSP since they have the same value */
1410 irlap_recv_disc_frame(self, skb, &info, command);
1413 irlap_recv_test_frame(self, skb, &info, command);
1416 irlap_recv_ua_frame(self, skb, &info);
1419 irlap_recv_frmr_frame(self, skb, &info);
1422 irlap_recv_ui_frame(self, skb, &info);
1425 IRDA_WARNING("%s: Unknown frame %02x received!\n",
1426 __FUNCTION__, info.control);
1430 /* Always drop our reference on the skb */