1 /*********************************************************************
3 * Filename: irlap_frame.c
5 * Description: Build and transmit IrLAP frames
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 10:27:26 1997
9 * Modified at: Wed Jan 5 08:59:04 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * Neither Dag Brattli nor University of Tromsø admit liability nor
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
25 ********************************************************************/
27 #include <linux/skbuff.h>
29 #include <linux/if_ether.h>
30 #include <linux/netdevice.h>
31 #include <linux/irda.h>
33 #include <net/pkt_sched.h>
36 #include <asm/byteorder.h>
38 #include <net/irda/irda.h>
39 #include <net/irda/irda_device.h>
40 #include <net/irda/irlap.h>
41 #include <net/irda/wrapper.h>
42 #include <net/irda/timer.h>
43 #include <net/irda/irlap_frame.h>
44 #include <net/irda/qos.h>
46 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
50 * Function irlap_insert_info (self, skb)
52 * Insert minimum turnaround time and speed information into the skb. We
53 * need to do this since it's per packet relevant information. Safe to
54 * have this function inlined since it's only called from one place
56 static inline void irlap_insert_info(struct irlap_cb *self,
59 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
62 * Insert MTT (min. turn time) and speed into skb, so that the
63 * device driver knows which settings to use
65 cb->magic = LAP_MAGIC;
66 cb->mtt = self->mtt_required;
67 cb->next_speed = self->speed;
70 self->mtt_required = 0;
73 * Delay equals negotiated BOFs count, plus the number of BOFs to
74 * force the negotiated minimum turnaround time
76 cb->xbofs = self->bofs_count;
77 cb->next_xbofs = self->next_bofs;
78 cb->xbofs_delay = self->xbofs_delay;
80 /* Reset XBOF's delay (used only for getting min turn time) */
81 self->xbofs_delay = 0;
82 /* Put the correct xbofs value for the next packet */
83 self->bofs_count = self->next_bofs;
87 * Function irlap_queue_xmit (self, skb)
89 * A little wrapper for dev_queue_xmit, so we can insert some common
92 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
94 /* Some common init stuff */
95 skb->dev = self->netdev;
96 skb_reset_mac_header(skb);
97 skb_reset_network_header(skb);
98 skb_reset_transport_header(skb);
99 skb->protocol = htons(ETH_P_IRDA);
100 skb->priority = TC_PRIO_BESTEFFORT;
102 irlap_insert_info(self, skb);
108 * Function irlap_send_snrm_cmd (void)
110 * Transmits a connect SNRM command frame
112 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
114 struct sk_buff *tx_skb;
115 struct snrm_frame *frame;
118 IRDA_ASSERT(self != NULL, return;);
119 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
122 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
123 IRLAP_NEGOCIATION_PARAMS_LEN,
128 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
130 /* Insert connection address field */
132 frame->caddr = CMD_FRAME | CBROADCAST;
134 frame->caddr = CMD_FRAME | self->caddr;
136 /* Insert control field */
137 frame->control = SNRM_CMD | PF_BIT;
140 * If we are establishing a connection then insert QoS paramerters
143 skb_put(tx_skb, 9); /* 25 left */
144 frame->saddr = cpu_to_le32(self->saddr);
145 frame->daddr = cpu_to_le32(self->daddr);
147 frame->ncaddr = self->caddr;
149 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
151 dev_kfree_skb(tx_skb);
155 irlap_queue_xmit(self, tx_skb);
159 * Function irlap_recv_snrm_cmd (skb, info)
161 * Received SNRM (Set Normal Response Mode) command frame
164 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
165 struct irlap_info *info)
167 struct snrm_frame *frame;
169 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
170 frame = (struct snrm_frame *) skb->data;
172 /* Copy the new connection address ignoring the C/R bit */
173 info->caddr = frame->ncaddr & 0xFE;
175 /* Check if the new connection address is valid */
176 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
177 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
182 /* Copy peer device address */
183 info->daddr = le32_to_cpu(frame->saddr);
184 info->saddr = le32_to_cpu(frame->daddr);
186 /* Only accept if addressed directly to us */
187 if (info->saddr != self->saddr) {
188 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
192 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
194 /* Signal that this SNRM frame does not contain and I-field */
195 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
200 * Function irlap_send_ua_response_frame (qos)
202 * Send UA (Unnumbered Acknowledgement) frame
205 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
207 struct sk_buff *tx_skb;
208 struct ua_frame *frame;
211 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
213 IRDA_ASSERT(self != NULL, return;);
214 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
217 tx_skb = alloc_skb(sizeof(struct ua_frame) +
218 IRLAP_NEGOCIATION_PARAMS_LEN,
223 frame = (struct ua_frame *) skb_put(tx_skb, 10);
225 /* Build UA response */
226 frame->caddr = self->caddr;
227 frame->control = UA_RSP | PF_BIT;
229 frame->saddr = cpu_to_le32(self->saddr);
230 frame->daddr = cpu_to_le32(self->daddr);
232 /* Should we send QoS negotiation parameters? */
234 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
236 dev_kfree_skb(tx_skb);
241 irlap_queue_xmit(self, tx_skb);
246 * Function irlap_send_dm_frame (void)
248 * Send disconnected mode (DM) frame
251 void irlap_send_dm_frame( struct irlap_cb *self)
253 struct sk_buff *tx_skb = NULL;
254 struct dm_frame *frame;
256 IRDA_ASSERT(self != NULL, return;);
257 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
259 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
263 frame = (struct dm_frame *)skb_put(tx_skb, 2);
265 if (self->state == LAP_NDM)
266 frame->caddr = CBROADCAST;
268 frame->caddr = self->caddr;
270 frame->control = DM_RSP | PF_BIT;
272 irlap_queue_xmit(self, tx_skb);
276 * Function irlap_send_disc_frame (void)
278 * Send disconnect (DISC) frame
281 void irlap_send_disc_frame(struct irlap_cb *self)
283 struct sk_buff *tx_skb = NULL;
284 struct disc_frame *frame;
286 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
288 IRDA_ASSERT(self != NULL, return;);
289 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
291 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
295 frame = (struct disc_frame *)skb_put(tx_skb, 2);
297 frame->caddr = self->caddr | CMD_FRAME;
298 frame->control = DISC_CMD | PF_BIT;
300 irlap_queue_xmit(self, tx_skb);
304 * Function irlap_send_discovery_xid_frame (S, s, command)
306 * Build and transmit a XID (eXchange station IDentifier) discovery
309 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
310 __u8 command, discovery_t *discovery)
312 struct sk_buff *tx_skb = NULL;
313 struct xid_frame *frame;
314 __u32 bcast = BROADCAST;
317 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
320 IRDA_ASSERT(self != NULL, return;);
321 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
322 IRDA_ASSERT(discovery != NULL, return;);
324 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
330 frame = (struct xid_frame *) tx_skb->data;
333 frame->caddr = CBROADCAST | CMD_FRAME;
334 frame->control = XID_CMD | PF_BIT;
336 frame->caddr = CBROADCAST;
337 frame->control = XID_RSP | PF_BIT;
339 frame->ident = XID_FORMAT;
341 frame->saddr = cpu_to_le32(self->saddr);
344 frame->daddr = cpu_to_le32(bcast);
346 frame->daddr = cpu_to_le32(discovery->data.daddr);
367 frame->version = 0x00;
370 * Provide info for final slot only in commands, and for all
371 * responses. Send the second byte of the hint only if the
372 * EXTENSION bit is set in the first byte.
374 if (!command || (frame->slotnr == 0xff)) {
377 if (discovery->data.hints[0] & HINT_EXTENSION) {
378 info = skb_put(tx_skb, 2);
379 info[0] = discovery->data.hints[0];
380 info[1] = discovery->data.hints[1];
382 info = skb_put(tx_skb, 1);
383 info[0] = discovery->data.hints[0];
385 info = skb_put(tx_skb, 1);
386 info[0] = discovery->data.charset;
388 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
389 info = skb_put(tx_skb, len);
390 memcpy(info, discovery->data.info, len);
392 irlap_queue_xmit(self, tx_skb);
396 * Function irlap_recv_discovery_xid_rsp (skb, info)
398 * Received a XID discovery response
401 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
403 struct irlap_info *info)
405 struct xid_frame *xid;
406 discovery_t *discovery = NULL;
407 __u8 *discovery_info;
410 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
412 IRDA_ASSERT(self != NULL, return;);
413 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
415 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
416 IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
420 xid = (struct xid_frame *) skb->data;
422 info->daddr = le32_to_cpu(xid->saddr);
423 info->saddr = le32_to_cpu(xid->daddr);
425 /* Make sure frame is addressed to us */
426 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
427 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
432 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
433 IRDA_WARNING("%s: kmalloc failed!\n", __FUNCTION__);
437 discovery->data.daddr = info->daddr;
438 discovery->data.saddr = self->saddr;
439 discovery->timestamp = jiffies;
441 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
442 discovery->data.daddr);
444 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
446 /* Get info returned from peer */
447 discovery->data.hints[0] = discovery_info[0];
448 if (discovery_info[0] & HINT_EXTENSION) {
449 IRDA_DEBUG(4, "EXTENSION\n");
450 discovery->data.hints[1] = discovery_info[1];
451 discovery->data.charset = discovery_info[2];
452 text = (char *) &discovery_info[3];
454 discovery->data.hints[1] = 0;
455 discovery->data.charset = discovery_info[1];
456 text = (char *) &discovery_info[2];
459 * Terminate info string, should be safe since this is where the
462 skb->data[skb->len] = '\0';
463 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
464 discovery->name_len = strlen(discovery->data.info);
466 info->discovery = discovery;
468 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
472 * Function irlap_recv_discovery_xid_cmd (skb, info)
474 * Received a XID discovery command
477 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
479 struct irlap_info *info)
481 struct xid_frame *xid;
482 discovery_t *discovery = NULL;
483 __u8 *discovery_info;
486 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
487 IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
491 xid = (struct xid_frame *) skb->data;
493 info->daddr = le32_to_cpu(xid->saddr);
494 info->saddr = le32_to_cpu(xid->daddr);
496 /* Make sure frame is addressed to us */
497 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
498 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
503 switch (xid->flags & 0x03) {
520 info->s = xid->slotnr;
522 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
525 * Check if last frame
527 if (info->s == 0xff) {
528 /* Check if things are sane at this point... */
529 if((discovery_info == NULL) ||
530 !pskb_may_pull(skb, 3)) {
531 IRDA_ERROR("%s: discovery frame too short!\n",
537 * We now have some discovery info to deliver!
539 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
541 IRDA_WARNING("%s: unable to malloc!\n", __FUNCTION__);
545 discovery->data.daddr = info->daddr;
546 discovery->data.saddr = self->saddr;
547 discovery->timestamp = jiffies;
549 discovery->data.hints[0] = discovery_info[0];
550 if (discovery_info[0] & HINT_EXTENSION) {
551 discovery->data.hints[1] = discovery_info[1];
552 discovery->data.charset = discovery_info[2];
553 text = (char *) &discovery_info[3];
555 discovery->data.hints[1] = 0;
556 discovery->data.charset = discovery_info[1];
557 text = (char *) &discovery_info[2];
560 * Terminate string, should be safe since this is where the
563 skb->data[skb->len] = '\0';
564 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
565 discovery->name_len = strlen(discovery->data.info);
567 info->discovery = discovery;
569 info->discovery = NULL;
571 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
575 * Function irlap_send_rr_frame (self, command)
577 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
578 * only possible to send RR frames with the poll bit set.
580 void irlap_send_rr_frame(struct irlap_cb *self, int command)
582 struct sk_buff *tx_skb;
583 struct rr_frame *frame;
585 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
589 frame = (struct rr_frame *)skb_put(tx_skb, 2);
591 frame->caddr = self->caddr;
592 frame->caddr |= (command) ? CMD_FRAME : 0;
594 frame->control = RR | PF_BIT | (self->vr << 5);
596 irlap_queue_xmit(self, tx_skb);
600 * Function irlap_send_rd_frame (self)
602 * Request disconnect. Used by a secondary station to request the
603 * disconnection of the link.
605 void irlap_send_rd_frame(struct irlap_cb *self)
607 struct sk_buff *tx_skb;
608 struct rd_frame *frame;
610 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
614 frame = (struct rd_frame *)skb_put(tx_skb, 2);
616 frame->caddr = self->caddr;
617 frame->caddr = RD_RSP | PF_BIT;
619 irlap_queue_xmit(self, tx_skb);
623 * Function irlap_recv_rr_frame (skb, info)
625 * Received RR (Receive Ready) frame from peer station, no harm in
626 * making it inline since its called only from one single place
627 * (irlap_driver_rcv).
629 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
631 struct irlap_info *info, int command)
633 info->nr = skb->data[1] >> 5;
635 /* Check if this is a command or a response frame */
637 irlap_do_event(self, RECV_RR_CMD, skb, info);
639 irlap_do_event(self, RECV_RR_RSP, skb, info);
643 * Function irlap_recv_rnr_frame (self, skb, info)
645 * Received RNR (Receive Not Ready) frame from peer station
648 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
649 struct irlap_info *info, int command)
651 info->nr = skb->data[1] >> 5;
653 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
656 irlap_do_event(self, RECV_RNR_CMD, skb, info);
658 irlap_do_event(self, RECV_RNR_RSP, skb, info);
661 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
662 struct irlap_info *info, int command)
664 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
666 info->nr = skb->data[1] >> 5;
668 /* Check if this is a command or a response frame */
670 irlap_do_event(self, RECV_REJ_CMD, skb, info);
672 irlap_do_event(self, RECV_REJ_RSP, skb, info);
675 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
676 struct irlap_info *info, int command)
678 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
680 info->nr = skb->data[1] >> 5;
682 /* Check if this is a command or a response frame */
684 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
686 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
689 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
690 struct irlap_info *info, int command)
692 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
694 /* Check if this is a command or a response frame */
696 irlap_do_event(self, RECV_DISC_CMD, skb, info);
698 irlap_do_event(self, RECV_RD_RSP, skb, info);
702 * Function irlap_recv_ua_frame (skb, frame)
704 * Received UA (Unnumbered Acknowledgement) frame
707 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
709 struct irlap_info *info)
711 irlap_do_event(self, RECV_UA_RSP, skb, info);
715 * Function irlap_send_data_primary(self, skb)
717 * Send I-frames as the primary station but without the poll bit set
720 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
722 struct sk_buff *tx_skb;
724 if (skb->data[1] == I_FRAME) {
727 * Insert frame sequence number (Vs) in control field before
728 * inserting into transmit window queue.
730 skb->data[1] = I_FRAME | (self->vs << 1);
733 * Insert frame in store, in case of retransmissions
734 * Increase skb reference count, see irlap_do_event()
737 skb_queue_tail(&self->wx_list, skb);
740 tx_skb = skb_clone(skb, GFP_ATOMIC);
741 if (tx_skb == NULL) {
745 self->vs = (self->vs + 1) % 8;
746 self->ack_required = FALSE;
749 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
751 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
752 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
757 * Function irlap_send_data_primary_poll (self, skb)
759 * Send I(nformation) frame as primary with poll bit set
761 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
763 struct sk_buff *tx_skb;
764 int transmission_time;
767 del_timer(&self->poll_timer);
769 /* Is this reliable or unreliable data? */
770 if (skb->data[1] == I_FRAME) {
773 * Insert frame sequence number (Vs) in control field before
774 * inserting into transmit window queue.
776 skb->data[1] = I_FRAME | (self->vs << 1);
779 * Insert frame in store, in case of retransmissions
780 * Increase skb reference count, see irlap_do_event()
783 skb_queue_tail(&self->wx_list, skb);
786 tx_skb = skb_clone(skb, GFP_ATOMIC);
787 if (tx_skb == NULL) {
792 * Set poll bit if necessary. We do this to the copied
793 * skb, since retransmitted need to set or clear the poll
794 * bit depending on when they are sent.
796 tx_skb->data[1] |= PF_BIT;
798 self->vs = (self->vs + 1) % 8;
799 self->ack_required = FALSE;
801 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
803 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
805 if (self->ack_required) {
806 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
807 irlap_send_rr_frame(self, CMD_FRAME);
808 self->ack_required = FALSE;
810 skb->data[1] |= PF_BIT;
811 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
815 /* How much time we took for transmission of all frames.
816 * We don't know, so let assume we used the full window. Jean II */
817 transmission_time = self->final_timeout;
819 /* Reset parameter so that we can fill next window */
820 self->window = self->window_size;
822 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
823 /* Remove what we have not used. Just do a prorata of the
824 * bytes left in window to window capacity.
825 * See max_line_capacities[][] in qos.c for details. Jean II */
826 transmission_time -= (self->final_timeout * self->bytes_left
827 / self->line_capacity);
828 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);
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 begining 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;
977 IRDA_ASSERT(self != NULL, return;);
978 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
980 /* Initialize variables */
981 count = skb_queue_len(&self->wx_list);
983 /* Resend unacknowledged frame(s) */
984 skb = skb_peek(&self->wx_list);
985 while (skb != NULL) {
986 irlap_wait_min_turn_around(self, &self->qos_tx);
988 /* We copy the skb to be retransmitted since we will have to
989 * modify it. Cloning will confuse packet sniffers
991 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
992 tx_skb = skb_copy(skb, GFP_ATOMIC);
994 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
998 /* Clear old Nr field + poll bit */
999 tx_skb->data[1] &= 0x0f;
1002 * Set poll bit on the last frame retransmitted
1005 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1007 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1009 irlap_send_i_frame(self, tx_skb, command);
1012 * If our skb is the last buffer in the list, then
1013 * we are finished, if not, move to the next sk-buffer
1015 if (skb == skb_peek_tail(&self->wx_list))
1022 * We can now fill the window with additional data frames
1024 while (!skb_queue_empty(&self->txq)) {
1026 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
1027 if (self->window > 0) {
1028 skb = skb_dequeue( &self->txq);
1029 IRDA_ASSERT(skb != NULL, return;);
1032 * If send window > 1 then send frame with pf
1035 if ((self->window > 1) &&
1036 !skb_queue_empty(&self->txq)) {
1037 irlap_send_data_primary(self, skb);
1039 irlap_send_data_primary_poll(self, skb);
1047 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1049 struct sk_buff *tx_skb;
1050 struct sk_buff *skb;
1052 IRDA_ASSERT(self != NULL, return;);
1053 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1055 /* Resend unacknowledged frame(s) */
1056 skb = skb_peek(&self->wx_list);
1058 irlap_wait_min_turn_around(self, &self->qos_tx);
1060 /* We copy the skb to be retransmitted since we will have to
1061 * modify it. Cloning will confuse packet sniffers
1063 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1064 tx_skb = skb_copy(skb, GFP_ATOMIC);
1066 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1070 /* Clear old Nr field + poll bit */
1071 tx_skb->data[1] &= 0x0f;
1073 /* Set poll/final bit */
1074 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1076 irlap_send_i_frame(self, tx_skb, command);
1081 * Function irlap_send_ui_frame (self, skb, command)
1083 * Contruct and transmit an Unnumbered Information (UI) frame
1086 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1087 __u8 caddr, int command)
1089 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1091 IRDA_ASSERT(self != NULL, return;);
1092 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1093 IRDA_ASSERT(skb != NULL, return;);
1095 /* Insert connection address */
1096 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1098 irlap_queue_xmit(self, skb);
1102 * Function irlap_send_i_frame (skb)
1104 * Contruct and transmit Information (I) frame
1106 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1109 /* Insert connection address */
1110 skb->data[0] = self->caddr;
1111 skb->data[0] |= (command) ? CMD_FRAME : 0;
1113 /* Insert next to receive (Vr) */
1114 skb->data[1] |= (self->vr << 5); /* insert nr */
1116 irlap_queue_xmit(self, skb);
1120 * Function irlap_recv_i_frame (skb, frame)
1122 * Receive and parse an I (Information) frame, no harm in making it inline
1123 * since it's called only from one single place (irlap_driver_rcv).
1125 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1126 struct sk_buff *skb,
1127 struct irlap_info *info, int command)
1129 info->nr = skb->data[1] >> 5; /* Next to receive */
1130 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1131 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1133 /* Check if this is a command or a response frame */
1135 irlap_do_event(self, RECV_I_CMD, skb, info);
1137 irlap_do_event(self, RECV_I_RSP, skb, info);
1141 * Function irlap_recv_ui_frame (self, skb, info)
1143 * Receive and parse an Unnumbered Information (UI) frame
1146 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1147 struct irlap_info *info)
1149 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1151 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1153 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1157 * Function irlap_recv_frmr_frame (skb, frame)
1159 * Received Frame Reject response.
1162 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1163 struct irlap_info *info)
1168 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1170 IRDA_ASSERT(self != NULL, return;);
1171 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1172 IRDA_ASSERT(skb != NULL, return;);
1173 IRDA_ASSERT(info != NULL, return;);
1175 if (!pskb_may_pull(skb, 4)) {
1176 IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
1182 info->nr = frame[2] >> 5; /* Next to receive */
1183 info->pf = frame[2] & PF_BIT; /* Final bit */
1184 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1186 w = frame[3] & 0x01;
1187 x = frame[3] & 0x02;
1188 y = frame[3] & 0x04;
1189 z = frame[3] & 0x08;
1192 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1196 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1197 "contained a non permitted I field.\n");
1200 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1201 "for the existing connection or exceeded the maximum "
1202 "this station supports if no connection exists.\n");
1205 IRDA_DEBUG(0, "Rejected control field control field contained an "
1206 "invalid Nr count.\n");
1208 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1212 * Function irlap_send_test_frame (self, daddr)
1214 * Send a test frame response
1217 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1218 struct sk_buff *cmd)
1220 struct sk_buff *tx_skb;
1221 struct test_frame *frame;
1224 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
1228 /* Broadcast frames must include saddr and daddr fields */
1229 if (caddr == CBROADCAST) {
1230 frame = (struct test_frame *)
1231 skb_put(tx_skb, sizeof(struct test_frame));
1233 /* Insert the swapped addresses */
1234 frame->saddr = cpu_to_le32(self->saddr);
1235 frame->daddr = cpu_to_le32(daddr);
1237 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1239 frame->caddr = caddr;
1240 frame->control = TEST_RSP | PF_BIT;
1243 info = skb_put(tx_skb, cmd->len);
1244 memcpy(info, cmd->data, cmd->len);
1246 /* Return to sender */
1247 irlap_wait_min_turn_around(self, &self->qos_tx);
1248 irlap_queue_xmit(self, tx_skb);
1252 * Function irlap_recv_test_frame (self, skb)
1254 * Receive a test frame
1257 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1258 struct irlap_info *info, int command)
1260 struct test_frame *frame;
1262 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1264 if (!pskb_may_pull(skb, sizeof(*frame))) {
1265 IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
1268 frame = (struct test_frame *) skb->data;
1270 /* Broadcast frames must carry saddr and daddr fields */
1271 if (info->caddr == CBROADCAST) {
1272 if (skb->len < sizeof(struct test_frame)) {
1273 IRDA_DEBUG(0, "%s() test frame too short!\n",
1278 /* Read and swap addresses */
1279 info->daddr = le32_to_cpu(frame->saddr);
1280 info->saddr = le32_to_cpu(frame->daddr);
1282 /* Make sure frame is addressed to us */
1283 if ((info->saddr != self->saddr) &&
1284 (info->saddr != BROADCAST)) {
1290 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1292 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1296 * Function irlap_driver_rcv (skb, netdev, ptype)
1298 * Called when a frame is received. Dispatches the right receive function
1299 * for processing of the frame.
1301 * Note on skb management :
1302 * After calling the higher layers of the IrDA stack, we always
1303 * kfree() the skb, which drop the reference count (and potentially
1305 * If a higher layer of the stack want to keep the skb around (to put
1306 * in a queue or pass it to the higher layer), it will need to use
1307 * skb_get() to keep a reference on it. This is usually done at the
1308 * LMP level in irlmp.c.
1311 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1312 struct packet_type *ptype, struct net_device *orig_dev)
1314 struct irlap_info info;
1315 struct irlap_cb *self;
1319 /* FIXME: should we get our own field? */
1320 self = (struct irlap_cb *) dev->atalk_ptr;
1322 /* If the net device is down, then IrLAP is gone! */
1323 if (!self || self->magic != LAP_MAGIC) {
1328 /* We are no longer an "old" protocol, so we need to handle
1329 * share and non linear skbs. This should never happen, so
1330 * we don't need to be clever about it. Jean II */
1331 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1332 IRDA_ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
1337 /* Check if frame is large enough for parsing */
1338 if (!pskb_may_pull(skb, 2)) {
1339 IRDA_ERROR("%s: frame too short!\n", __FUNCTION__);
1344 command = skb->data[0] & CMD_FRAME;
1345 info.caddr = skb->data[0] & CBROADCAST;
1347 info.pf = skb->data[1] & PF_BIT;
1348 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1350 control = info.control;
1352 /* First we check if this frame has a valid connection address */
1353 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1354 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1359 * Optimize for the common case and check if the frame is an
1360 * I(nformation) frame. Only I-frames have bit 0 set to 0
1362 if (~control & 0x01) {
1363 irlap_recv_i_frame(self, skb, &info, command);
1367 * We now check is the frame is an S(upervisory) frame. Only
1368 * S-frames have bit 0 set to 1 and bit 1 set to 0
1370 if (~control & 0x02) {
1372 * Received S(upervisory) frame, check which frame type it is
1373 * only the first nibble is of interest
1375 switch (control & 0x0f) {
1377 irlap_recv_rr_frame(self, skb, &info, command);
1380 irlap_recv_rnr_frame(self, skb, &info, command);
1383 irlap_recv_rej_frame(self, skb, &info, command);
1386 irlap_recv_srej_frame(self, skb, &info, command);
1389 IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
1390 __FUNCTION__, info.control);
1396 * This must be a C(ontrol) frame
1400 irlap_recv_discovery_xid_rsp(self, skb, &info);
1403 irlap_recv_discovery_xid_cmd(self, skb, &info);
1406 irlap_recv_snrm_cmd(self, skb, &info);
1409 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1411 case DISC_CMD: /* And RD_RSP since they have the same value */
1412 irlap_recv_disc_frame(self, skb, &info, command);
1415 irlap_recv_test_frame(self, skb, &info, command);
1418 irlap_recv_ua_frame(self, skb, &info);
1421 irlap_recv_frmr_frame(self, skb, &info);
1424 irlap_recv_ui_frame(self, skb, &info);
1427 IRDA_WARNING("%s: Unknown frame %02x received!\n",
1428 __FUNCTION__, info.control);
1432 /* Always drop our reference on the skb */