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);
104 if (unlikely(self->mode & IRDA_MODE_MONITOR)) {
105 IRDA_DEBUG(3, "%s(): %s is in monitor mode\n", __func__,
115 * Function irlap_send_snrm_cmd (void)
117 * Transmits a connect SNRM command frame
119 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
121 struct sk_buff *tx_skb;
122 struct snrm_frame *frame;
125 IRDA_ASSERT(self != NULL, return;);
126 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
129 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
130 IRLAP_NEGOCIATION_PARAMS_LEN,
135 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
137 /* Insert connection address field */
139 frame->caddr = CMD_FRAME | CBROADCAST;
141 frame->caddr = CMD_FRAME | self->caddr;
143 /* Insert control field */
144 frame->control = SNRM_CMD | PF_BIT;
147 * If we are establishing a connection then insert QoS parameters
150 skb_put(tx_skb, 9); /* 25 left */
151 frame->saddr = cpu_to_le32(self->saddr);
152 frame->daddr = cpu_to_le32(self->daddr);
154 frame->ncaddr = self->caddr;
156 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
158 dev_kfree_skb(tx_skb);
162 irlap_queue_xmit(self, tx_skb);
166 * Function irlap_recv_snrm_cmd (skb, info)
168 * Received SNRM (Set Normal Response Mode) command frame
171 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
172 struct irlap_info *info)
174 struct snrm_frame *frame;
176 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
177 frame = (struct snrm_frame *) skb->data;
179 /* Copy the new connection address ignoring the C/R bit */
180 info->caddr = frame->ncaddr & 0xFE;
182 /* Check if the new connection address is valid */
183 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
184 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
189 /* Copy peer device address */
190 info->daddr = le32_to_cpu(frame->saddr);
191 info->saddr = le32_to_cpu(frame->daddr);
193 /* Only accept if addressed directly to us */
194 if (info->saddr != self->saddr) {
195 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
199 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
201 /* Signal that this SNRM frame does not contain and I-field */
202 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
207 * Function irlap_send_ua_response_frame (qos)
209 * Send UA (Unnumbered Acknowledgement) frame
212 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
214 struct sk_buff *tx_skb;
215 struct ua_frame *frame;
218 IRDA_DEBUG(2, "%s() <%ld>\n", __func__, jiffies);
220 IRDA_ASSERT(self != NULL, return;);
221 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
224 tx_skb = alloc_skb(sizeof(struct ua_frame) +
225 IRLAP_NEGOCIATION_PARAMS_LEN,
230 frame = (struct ua_frame *) skb_put(tx_skb, 10);
232 /* Build UA response */
233 frame->caddr = self->caddr;
234 frame->control = UA_RSP | PF_BIT;
236 frame->saddr = cpu_to_le32(self->saddr);
237 frame->daddr = cpu_to_le32(self->daddr);
239 /* Should we send QoS negotiation parameters? */
241 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
243 dev_kfree_skb(tx_skb);
248 irlap_queue_xmit(self, tx_skb);
253 * Function irlap_send_dm_frame (void)
255 * Send disconnected mode (DM) frame
258 void irlap_send_dm_frame( struct irlap_cb *self)
260 struct sk_buff *tx_skb = NULL;
261 struct dm_frame *frame;
263 IRDA_ASSERT(self != NULL, return;);
264 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
266 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
270 frame = (struct dm_frame *)skb_put(tx_skb, 2);
272 if (self->state == LAP_NDM)
273 frame->caddr = CBROADCAST;
275 frame->caddr = self->caddr;
277 frame->control = DM_RSP | PF_BIT;
279 irlap_queue_xmit(self, tx_skb);
283 * Function irlap_send_disc_frame (void)
285 * Send disconnect (DISC) frame
288 void irlap_send_disc_frame(struct irlap_cb *self)
290 struct sk_buff *tx_skb = NULL;
291 struct disc_frame *frame;
293 IRDA_DEBUG(3, "%s()\n", __func__);
295 IRDA_ASSERT(self != NULL, return;);
296 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
298 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
302 frame = (struct disc_frame *)skb_put(tx_skb, 2);
304 frame->caddr = self->caddr | CMD_FRAME;
305 frame->control = DISC_CMD | PF_BIT;
307 irlap_queue_xmit(self, tx_skb);
311 * Function irlap_send_discovery_xid_frame (S, s, command)
313 * Build and transmit a XID (eXchange station IDentifier) discovery
316 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
317 __u8 command, discovery_t *discovery)
319 struct sk_buff *tx_skb = NULL;
320 struct xid_frame *frame;
321 __u32 bcast = BROADCAST;
324 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __func__,
327 IRDA_ASSERT(self != NULL, return;);
328 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
329 IRDA_ASSERT(discovery != NULL, return;);
331 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
337 frame = (struct xid_frame *) tx_skb->data;
340 frame->caddr = CBROADCAST | CMD_FRAME;
341 frame->control = XID_CMD | PF_BIT;
343 frame->caddr = CBROADCAST;
344 frame->control = XID_RSP | PF_BIT;
346 frame->ident = XID_FORMAT;
348 frame->saddr = cpu_to_le32(self->saddr);
351 frame->daddr = cpu_to_le32(bcast);
353 frame->daddr = cpu_to_le32(discovery->data.daddr);
374 frame->version = 0x00;
377 * Provide info for final slot only in commands, and for all
378 * responses. Send the second byte of the hint only if the
379 * EXTENSION bit is set in the first byte.
381 if (!command || (frame->slotnr == 0xff)) {
384 if (discovery->data.hints[0] & HINT_EXTENSION) {
385 info = skb_put(tx_skb, 2);
386 info[0] = discovery->data.hints[0];
387 info[1] = discovery->data.hints[1];
389 info = skb_put(tx_skb, 1);
390 info[0] = discovery->data.hints[0];
392 info = skb_put(tx_skb, 1);
393 info[0] = discovery->data.charset;
395 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
396 info = skb_put(tx_skb, len);
397 memcpy(info, discovery->data.info, len);
399 irlap_queue_xmit(self, tx_skb);
403 * Function irlap_recv_discovery_xid_rsp (skb, info)
405 * Received a XID discovery response
408 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
410 struct irlap_info *info)
412 struct xid_frame *xid;
413 discovery_t *discovery = NULL;
414 __u8 *discovery_info;
417 IRDA_DEBUG(4, "%s()\n", __func__);
419 IRDA_ASSERT(self != NULL, return;);
420 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
422 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
423 IRDA_ERROR("%s: frame too short!\n", __func__);
427 xid = (struct xid_frame *) skb->data;
429 info->daddr = le32_to_cpu(xid->saddr);
430 info->saddr = le32_to_cpu(xid->daddr);
432 /* Make sure frame is addressed to us */
433 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
434 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
439 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
440 IRDA_WARNING("%s: kmalloc failed!\n", __func__);
444 discovery->data.daddr = info->daddr;
445 discovery->data.saddr = self->saddr;
446 discovery->timestamp = jiffies;
448 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __func__,
449 discovery->data.daddr);
451 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
453 /* Get info returned from peer */
454 discovery->data.hints[0] = discovery_info[0];
455 if (discovery_info[0] & HINT_EXTENSION) {
456 IRDA_DEBUG(4, "EXTENSION\n");
457 discovery->data.hints[1] = discovery_info[1];
458 discovery->data.charset = discovery_info[2];
459 text = (char *) &discovery_info[3];
461 discovery->data.hints[1] = 0;
462 discovery->data.charset = discovery_info[1];
463 text = (char *) &discovery_info[2];
466 * Terminate info string, should be safe since this is where the
469 skb->data[skb->len] = '\0';
470 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
471 discovery->name_len = strlen(discovery->data.info);
473 info->discovery = discovery;
475 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
479 * Function irlap_recv_discovery_xid_cmd (skb, info)
481 * Received a XID discovery command
484 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
486 struct irlap_info *info)
488 struct xid_frame *xid;
489 discovery_t *discovery = NULL;
490 __u8 *discovery_info;
493 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
494 IRDA_ERROR("%s: frame too short!\n", __func__);
498 xid = (struct xid_frame *) skb->data;
500 info->daddr = le32_to_cpu(xid->saddr);
501 info->saddr = le32_to_cpu(xid->daddr);
503 /* Make sure frame is addressed to us */
504 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
505 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
510 switch (xid->flags & 0x03) {
527 info->s = xid->slotnr;
529 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
532 * Check if last frame
534 if (info->s == 0xff) {
535 /* Check if things are sane at this point... */
536 if((discovery_info == NULL) ||
537 !pskb_may_pull(skb, 3)) {
538 IRDA_ERROR("%s: discovery frame too short!\n",
544 * We now have some discovery info to deliver!
546 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
548 IRDA_WARNING("%s: unable to malloc!\n", __func__);
552 discovery->data.daddr = info->daddr;
553 discovery->data.saddr = self->saddr;
554 discovery->timestamp = jiffies;
556 discovery->data.hints[0] = discovery_info[0];
557 if (discovery_info[0] & HINT_EXTENSION) {
558 discovery->data.hints[1] = discovery_info[1];
559 discovery->data.charset = discovery_info[2];
560 text = (char *) &discovery_info[3];
562 discovery->data.hints[1] = 0;
563 discovery->data.charset = discovery_info[1];
564 text = (char *) &discovery_info[2];
567 * Terminate string, should be safe since this is where the
570 skb->data[skb->len] = '\0';
571 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
572 discovery->name_len = strlen(discovery->data.info);
574 info->discovery = discovery;
576 info->discovery = NULL;
578 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
582 * Function irlap_send_rr_frame (self, command)
584 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
585 * only possible to send RR frames with the poll bit set.
587 void irlap_send_rr_frame(struct irlap_cb *self, int command)
589 struct sk_buff *tx_skb;
590 struct rr_frame *frame;
592 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
596 frame = (struct rr_frame *)skb_put(tx_skb, 2);
598 frame->caddr = self->caddr;
599 frame->caddr |= (command) ? CMD_FRAME : 0;
601 frame->control = RR | PF_BIT | (self->vr << 5);
603 irlap_queue_xmit(self, tx_skb);
607 * Function irlap_send_rd_frame (self)
609 * Request disconnect. Used by a secondary station to request the
610 * disconnection of the link.
612 void irlap_send_rd_frame(struct irlap_cb *self)
614 struct sk_buff *tx_skb;
615 struct rd_frame *frame;
617 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
621 frame = (struct rd_frame *)skb_put(tx_skb, 2);
623 frame->caddr = self->caddr;
624 frame->caddr = RD_RSP | PF_BIT;
626 irlap_queue_xmit(self, tx_skb);
630 * Function irlap_recv_rr_frame (skb, info)
632 * Received RR (Receive Ready) frame from peer station, no harm in
633 * making it inline since its called only from one single place
634 * (irlap_driver_rcv).
636 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
638 struct irlap_info *info, int command)
640 info->nr = skb->data[1] >> 5;
642 /* Check if this is a command or a response frame */
644 irlap_do_event(self, RECV_RR_CMD, skb, info);
646 irlap_do_event(self, RECV_RR_RSP, skb, info);
650 * Function irlap_recv_rnr_frame (self, skb, info)
652 * Received RNR (Receive Not Ready) frame from peer station
655 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
656 struct irlap_info *info, int command)
658 info->nr = skb->data[1] >> 5;
660 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __func__, info->nr, jiffies);
663 irlap_do_event(self, RECV_RNR_CMD, skb, info);
665 irlap_do_event(self, RECV_RNR_RSP, skb, info);
668 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
669 struct irlap_info *info, int command)
671 IRDA_DEBUG(0, "%s()\n", __func__);
673 info->nr = skb->data[1] >> 5;
675 /* Check if this is a command or a response frame */
677 irlap_do_event(self, RECV_REJ_CMD, skb, info);
679 irlap_do_event(self, RECV_REJ_RSP, skb, info);
682 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
683 struct irlap_info *info, int command)
685 IRDA_DEBUG(0, "%s()\n", __func__);
687 info->nr = skb->data[1] >> 5;
689 /* Check if this is a command or a response frame */
691 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
693 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
696 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
697 struct irlap_info *info, int command)
699 IRDA_DEBUG(2, "%s()\n", __func__);
701 /* Check if this is a command or a response frame */
703 irlap_do_event(self, RECV_DISC_CMD, skb, info);
705 irlap_do_event(self, RECV_RD_RSP, skb, info);
709 * Function irlap_recv_ua_frame (skb, frame)
711 * Received UA (Unnumbered Acknowledgement) frame
714 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
716 struct irlap_info *info)
718 irlap_do_event(self, RECV_UA_RSP, skb, info);
722 * Function irlap_send_data_primary(self, skb)
724 * Send I-frames as the primary station but without the poll bit set
727 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
729 struct sk_buff *tx_skb;
731 if (skb->data[1] == I_FRAME) {
734 * Insert frame sequence number (Vs) in control field before
735 * inserting into transmit window queue.
737 skb->data[1] = I_FRAME | (self->vs << 1);
740 * Insert frame in store, in case of retransmissions
741 * Increase skb reference count, see irlap_do_event()
744 skb_queue_tail(&self->wx_list, skb);
747 tx_skb = skb_clone(skb, GFP_ATOMIC);
748 if (tx_skb == NULL) {
752 self->vs = (self->vs + 1) % 8;
753 self->ack_required = FALSE;
756 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
758 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __func__);
759 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
764 * Function irlap_send_data_primary_poll (self, skb)
766 * Send I(nformation) frame as primary with poll bit set
768 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
770 struct sk_buff *tx_skb;
771 int transmission_time;
774 del_timer(&self->poll_timer);
776 /* Is this reliable or unreliable data? */
777 if (skb->data[1] == I_FRAME) {
780 * Insert frame sequence number (Vs) in control field before
781 * inserting into transmit window queue.
783 skb->data[1] = I_FRAME | (self->vs << 1);
786 * Insert frame in store, in case of retransmissions
787 * Increase skb reference count, see irlap_do_event()
790 skb_queue_tail(&self->wx_list, skb);
793 tx_skb = skb_clone(skb, GFP_ATOMIC);
794 if (tx_skb == NULL) {
799 * Set poll bit if necessary. We do this to the copied
800 * skb, since retransmitted need to set or clear the poll
801 * bit depending on when they are sent.
803 tx_skb->data[1] |= PF_BIT;
805 self->vs = (self->vs + 1) % 8;
806 self->ack_required = FALSE;
808 irlap_next_state(self, LAP_NRM_P);
809 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
811 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __func__);
813 if (self->ack_required) {
814 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
815 irlap_next_state(self, LAP_NRM_P);
816 irlap_send_rr_frame(self, CMD_FRAME);
817 self->ack_required = FALSE;
819 skb->data[1] |= PF_BIT;
820 irlap_next_state(self, LAP_NRM_P);
821 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
825 /* How much time we took for transmission of all frames.
826 * We don't know, so let assume we used the full window. Jean II */
827 transmission_time = self->final_timeout;
829 /* Reset parameter so that we can fill next window */
830 self->window = self->window_size;
832 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
833 /* Remove what we have not used. Just do a prorata of the
834 * bytes left in window to window capacity.
835 * See max_line_capacities[][] in qos.c for details. Jean II */
836 transmission_time -= (self->final_timeout * self->bytes_left
837 / self->line_capacity);
838 IRDA_DEBUG(4, "%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n", __func__, self->final_timeout, self->bytes_left, self->line_capacity, transmission_time);
840 /* We are allowed to transmit a maximum number of bytes again. */
841 self->bytes_left = self->line_capacity;
842 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
845 * The network layer has a intermediate buffer between IrLAP
846 * and the IrDA driver which can contain 8 frames. So, even
847 * though IrLAP is currently sending the *last* frame of the
848 * tx-window, the driver most likely has only just started
849 * sending the *first* frame of the same tx-window.
850 * I.e. we are always at the very begining of or Tx window.
851 * Now, we are supposed to set the final timer from the end
852 * of our tx-window to let the other peer reply. So, we need
853 * to add extra time to compensate for the fact that we
854 * are really at the start of tx-window, otherwise the final timer
855 * might expire before he can answer...
858 irlap_start_final_timer(self, self->final_timeout + transmission_time);
861 * The clever amongst you might ask why we do this adjustement
862 * only here, and not in all the other cases in irlap_event.c.
863 * In all those other case, we only send a very short management
864 * frame (few bytes), so the adjustement would be lost in the
866 * The exception of course is irlap_resend_rejected_frame().
871 * Function irlap_send_data_secondary_final (self, skb)
873 * Send I(nformation) frame as secondary with final bit set
876 void irlap_send_data_secondary_final(struct irlap_cb *self,
879 struct sk_buff *tx_skb = NULL;
881 IRDA_ASSERT(self != NULL, return;);
882 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
883 IRDA_ASSERT(skb != NULL, return;);
885 /* Is this reliable or unreliable data? */
886 if (skb->data[1] == I_FRAME) {
889 * Insert frame sequence number (Vs) in control field before
890 * inserting into transmit window queue.
892 skb->data[1] = I_FRAME | (self->vs << 1);
895 * Insert frame in store, in case of retransmissions
896 * Increase skb reference count, see irlap_do_event()
899 skb_queue_tail(&self->wx_list, skb);
901 tx_skb = skb_clone(skb, GFP_ATOMIC);
902 if (tx_skb == NULL) {
906 tx_skb->data[1] |= PF_BIT;
908 self->vs = (self->vs + 1) % 8;
909 self->ack_required = FALSE;
911 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
913 if (self->ack_required) {
914 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
915 irlap_send_rr_frame(self, RSP_FRAME);
916 self->ack_required = FALSE;
918 skb->data[1] |= PF_BIT;
919 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
923 self->window = self->window_size;
924 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
925 /* We are allowed to transmit a maximum number of bytes again. */
926 self->bytes_left = self->line_capacity;
927 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
929 irlap_start_wd_timer(self, self->wd_timeout);
933 * Function irlap_send_data_secondary (self, skb)
935 * Send I(nformation) frame as secondary without final bit set
938 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
940 struct sk_buff *tx_skb = NULL;
942 /* Is this reliable or unreliable data? */
943 if (skb->data[1] == I_FRAME) {
946 * Insert frame sequence number (Vs) in control field before
947 * inserting into transmit window queue.
949 skb->data[1] = I_FRAME | (self->vs << 1);
952 * Insert frame in store, in case of retransmissions
953 * Increase skb reference count, see irlap_do_event()
956 skb_queue_tail(&self->wx_list, skb);
958 tx_skb = skb_clone(skb, GFP_ATOMIC);
959 if (tx_skb == NULL) {
963 self->vs = (self->vs + 1) % 8;
964 self->ack_required = FALSE;
967 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
969 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
975 * Function irlap_resend_rejected_frames (nr)
977 * Resend frames which has not been acknowledged. Should be safe to
978 * traverse the list without locking it since this function will only be
979 * called from interrupt context (BH)
981 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
983 struct sk_buff *tx_skb;
987 IRDA_ASSERT(self != NULL, return;);
988 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
990 /* Initialize variables */
991 count = skb_queue_len(&self->wx_list);
993 /* Resend unacknowledged frame(s) */
994 skb = skb_peek(&self->wx_list);
995 while (skb != NULL) {
996 irlap_wait_min_turn_around(self, &self->qos_tx);
998 /* We copy the skb to be retransmitted since we will have to
999 * modify it. Cloning will confuse packet sniffers
1001 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1002 tx_skb = skb_copy(skb, GFP_ATOMIC);
1004 IRDA_DEBUG(0, "%s(), unable to copy\n", __func__);
1008 /* Clear old Nr field + poll bit */
1009 tx_skb->data[1] &= 0x0f;
1012 * Set poll bit on the last frame retransmitted
1015 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1017 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1019 irlap_send_i_frame(self, tx_skb, command);
1022 * If our skb is the last buffer in the list, then
1023 * we are finished, if not, move to the next sk-buffer
1025 if (skb == skb_peek_tail(&self->wx_list))
1032 * We can now fill the window with additional data frames
1034 while (!skb_queue_empty(&self->txq)) {
1036 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __func__);
1037 if (self->window > 0) {
1038 skb = skb_dequeue( &self->txq);
1039 IRDA_ASSERT(skb != NULL, return;);
1042 * If send window > 1 then send frame with pf
1045 if ((self->window > 1) &&
1046 !skb_queue_empty(&self->txq)) {
1047 irlap_send_data_primary(self, skb);
1049 irlap_send_data_primary_poll(self, skb);
1057 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1059 struct sk_buff *tx_skb;
1060 struct sk_buff *skb;
1062 IRDA_ASSERT(self != NULL, return;);
1063 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1065 /* Resend unacknowledged frame(s) */
1066 skb = skb_peek(&self->wx_list);
1068 irlap_wait_min_turn_around(self, &self->qos_tx);
1070 /* We copy the skb to be retransmitted since we will have to
1071 * modify it. Cloning will confuse packet sniffers
1073 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1074 tx_skb = skb_copy(skb, GFP_ATOMIC);
1076 IRDA_DEBUG(0, "%s(), unable to copy\n", __func__);
1080 /* Clear old Nr field + poll bit */
1081 tx_skb->data[1] &= 0x0f;
1083 /* Set poll/final bit */
1084 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1086 irlap_send_i_frame(self, tx_skb, command);
1091 * Function irlap_send_ui_frame (self, skb, command)
1093 * Contruct and transmit an Unnumbered Information (UI) frame
1096 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1097 __u8 caddr, int command)
1099 IRDA_DEBUG(4, "%s()\n", __func__);
1101 IRDA_ASSERT(self != NULL, return;);
1102 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1103 IRDA_ASSERT(skb != NULL, return;);
1105 /* Insert connection address */
1106 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1108 irlap_queue_xmit(self, skb);
1112 * Function irlap_send_i_frame (skb)
1114 * Contruct and transmit Information (I) frame
1116 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1119 /* Insert connection address */
1120 skb->data[0] = self->caddr;
1121 skb->data[0] |= (command) ? CMD_FRAME : 0;
1123 /* Insert next to receive (Vr) */
1124 skb->data[1] |= (self->vr << 5); /* insert nr */
1126 irlap_queue_xmit(self, skb);
1130 * Function irlap_recv_i_frame (skb, frame)
1132 * Receive and parse an I (Information) frame, no harm in making it inline
1133 * since it's called only from one single place (irlap_driver_rcv).
1135 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1136 struct sk_buff *skb,
1137 struct irlap_info *info, int command)
1139 info->nr = skb->data[1] >> 5; /* Next to receive */
1140 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1141 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1143 /* Check if this is a command or a response frame */
1145 irlap_do_event(self, RECV_I_CMD, skb, info);
1147 irlap_do_event(self, RECV_I_RSP, skb, info);
1151 * Function irlap_recv_ui_frame (self, skb, info)
1153 * Receive and parse an Unnumbered Information (UI) frame
1156 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1157 struct irlap_info *info)
1159 IRDA_DEBUG( 4, "%s()\n", __func__);
1161 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1163 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1167 * Function irlap_recv_frmr_frame (skb, frame)
1169 * Received Frame Reject response.
1172 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1173 struct irlap_info *info)
1178 IRDA_DEBUG(0, "%s()\n", __func__);
1180 IRDA_ASSERT(self != NULL, return;);
1181 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1182 IRDA_ASSERT(skb != NULL, return;);
1183 IRDA_ASSERT(info != NULL, return;);
1185 if (!pskb_may_pull(skb, 4)) {
1186 IRDA_ERROR("%s: frame too short!\n", __func__);
1192 info->nr = frame[2] >> 5; /* Next to receive */
1193 info->pf = frame[2] & PF_BIT; /* Final bit */
1194 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1196 w = frame[3] & 0x01;
1197 x = frame[3] & 0x02;
1198 y = frame[3] & 0x04;
1199 z = frame[3] & 0x08;
1202 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1206 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1207 "contained a non permitted I field.\n");
1210 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1211 "for the existing connection or exceeded the maximum "
1212 "this station supports if no connection exists.\n");
1215 IRDA_DEBUG(0, "Rejected control field control field contained an "
1216 "invalid Nr count.\n");
1218 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1222 * Function irlap_send_test_frame (self, daddr)
1224 * Send a test frame response
1227 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1228 struct sk_buff *cmd)
1230 struct sk_buff *tx_skb;
1231 struct test_frame *frame;
1234 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
1238 /* Broadcast frames must include saddr and daddr fields */
1239 if (caddr == CBROADCAST) {
1240 frame = (struct test_frame *)
1241 skb_put(tx_skb, sizeof(struct test_frame));
1243 /* Insert the swapped addresses */
1244 frame->saddr = cpu_to_le32(self->saddr);
1245 frame->daddr = cpu_to_le32(daddr);
1247 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1249 frame->caddr = caddr;
1250 frame->control = TEST_RSP | PF_BIT;
1253 info = skb_put(tx_skb, cmd->len);
1254 memcpy(info, cmd->data, cmd->len);
1256 /* Return to sender */
1257 irlap_wait_min_turn_around(self, &self->qos_tx);
1258 irlap_queue_xmit(self, tx_skb);
1262 * Function irlap_recv_test_frame (self, skb)
1264 * Receive a test frame
1267 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1268 struct irlap_info *info, int command)
1270 struct test_frame *frame;
1272 IRDA_DEBUG(2, "%s()\n", __func__);
1274 if (!pskb_may_pull(skb, sizeof(*frame))) {
1275 IRDA_ERROR("%s: frame too short!\n", __func__);
1278 frame = (struct test_frame *) skb->data;
1280 /* Broadcast frames must carry saddr and daddr fields */
1281 if (info->caddr == CBROADCAST) {
1282 if (skb->len < sizeof(struct test_frame)) {
1283 IRDA_DEBUG(0, "%s() test frame too short!\n",
1288 /* Read and swap addresses */
1289 info->daddr = le32_to_cpu(frame->saddr);
1290 info->saddr = le32_to_cpu(frame->daddr);
1292 /* Make sure frame is addressed to us */
1293 if ((info->saddr != self->saddr) &&
1294 (info->saddr != BROADCAST)) {
1300 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1302 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1306 * Function irlap_driver_rcv (skb, netdev, ptype)
1308 * Called when a frame is received. Dispatches the right receive function
1309 * for processing of the frame.
1311 * Note on skb management :
1312 * After calling the higher layers of the IrDA stack, we always
1313 * kfree() the skb, which drop the reference count (and potentially
1315 * If a higher layer of the stack want to keep the skb around (to put
1316 * in a queue or pass it to the higher layer), it will need to use
1317 * skb_get() to keep a reference on it. This is usually done at the
1318 * LMP level in irlmp.c.
1321 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1322 struct packet_type *ptype, struct net_device *orig_dev)
1324 struct irlap_info info;
1325 struct irlap_cb *self;
1329 if (!net_eq(dev_net(dev), &init_net))
1332 /* FIXME: should we get our own field? */
1333 self = (struct irlap_cb *) dev->atalk_ptr;
1335 /* If the net device is down, then IrLAP is gone! */
1336 if (!self || self->magic != LAP_MAGIC) {
1341 /* We are no longer an "old" protocol, so we need to handle
1342 * share and non linear skbs. This should never happen, so
1343 * we don't need to be clever about it. Jean II */
1344 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1345 IRDA_ERROR("%s: can't clone shared skb!\n", __func__);
1350 /* Check if frame is large enough for parsing */
1351 if (!pskb_may_pull(skb, 2)) {
1352 IRDA_ERROR("%s: frame too short!\n", __func__);
1357 command = skb->data[0] & CMD_FRAME;
1358 info.caddr = skb->data[0] & CBROADCAST;
1360 info.pf = skb->data[1] & PF_BIT;
1361 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1363 control = info.control;
1365 /* First we check if this frame has a valid connection address */
1366 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1367 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1372 * Optimize for the common case and check if the frame is an
1373 * I(nformation) frame. Only I-frames have bit 0 set to 0
1375 if (~control & 0x01) {
1376 irlap_recv_i_frame(self, skb, &info, command);
1380 * We now check is the frame is an S(upervisory) frame. Only
1381 * S-frames have bit 0 set to 1 and bit 1 set to 0
1383 if (~control & 0x02) {
1385 * Received S(upervisory) frame, check which frame type it is
1386 * only the first nibble is of interest
1388 switch (control & 0x0f) {
1390 irlap_recv_rr_frame(self, skb, &info, command);
1393 irlap_recv_rnr_frame(self, skb, &info, command);
1396 irlap_recv_rej_frame(self, skb, &info, command);
1399 irlap_recv_srej_frame(self, skb, &info, command);
1402 IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
1403 __func__, info.control);
1409 * This must be a C(ontrol) frame
1413 irlap_recv_discovery_xid_rsp(self, skb, &info);
1416 irlap_recv_discovery_xid_cmd(self, skb, &info);
1419 irlap_recv_snrm_cmd(self, skb, &info);
1422 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1424 case DISC_CMD: /* And RD_RSP since they have the same value */
1425 irlap_recv_disc_frame(self, skb, &info, command);
1428 irlap_recv_test_frame(self, skb, &info, command);
1431 irlap_recv_ua_frame(self, skb, &info);
1434 irlap_recv_frmr_frame(self, skb, &info);
1437 irlap_recv_ui_frame(self, skb, &info);
1440 IRDA_WARNING("%s: Unknown frame %02x received!\n",
1441 __func__, info.control);
1445 /* Always drop our reference on the skb */