File: /usr/src/linux/net/irda/irlap_frame.c

1     /*********************************************************************
2      *                
3      * Filename:      irlap_frame.c
4      * Version:       1.0
5      * Description:   Build and transmit IrLAP frames
6      * Status:        Stable
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>
11      * 
12      *     Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>, 
13      *     All Rights Reserved.
14      *     
15      *     This program is free software; you can redistribute it and/or 
16      *     modify it under the terms of the GNU General Public License as 
17      *     published by the Free Software Foundation; either version 2 of 
18      *     the License, or (at your option) any later version.
19      *
20      *     Neither Dag Brattli nor University of Tromsų admit liability nor
21      *     provide warranty for any of this software. This material is 
22      *     provided "AS-IS" and at no charge.
23      *
24      ********************************************************************/
25     
26     #include <linux/skbuff.h>
27     #include <linux/if.h>
28     #include <linux/if_ether.h>
29     #include <linux/netdevice.h>
30     #include <linux/irda.h>
31      
32     #include <net/pkt_sched.h>
33     #include <net/sock.h>
34      
35     #include <asm/byteorder.h>
36     
37     #include <net/irda/irda.h>
38     #include <net/irda/irda_device.h>
39     #include <net/irda/irlap.h>
40     #include <net/irda/wrapper.h>
41     #include <net/irda/timer.h>
42     #include <net/irda/irlap_frame.h>
43     #include <net/irda/qos.h>
44     
45     /*
46      * Function irlap_insert_info (self, skb)
47      *
48      *    Insert minimum turnaround time and speed information into the skb. We 
49      *    need to do this since it's per packet relevant information. Safe to
50      *    have this function inlined since it's only called from one place
51      */
52     static inline void irlap_insert_info(struct irlap_cb *self, 
53     				     struct sk_buff *skb)
54     {
55     	struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
56     
57     	/*  
58     	 * Insert MTT (min. turn time) and speed into skb, so that the
59     	 * device driver knows which settings to use 
60     	 */
61     	cb->magic = LAP_MAGIC;
62     	cb->mtt = self->mtt_required;
63     	cb->next_speed = self->speed;
64     
65     	/* Reset */
66     	self->mtt_required = 0;
67     	
68     	/* 
69     	 * Delay equals negotiated BOFs count, plus the number of BOFs to 
70     	 * force the negotiated minimum turnaround time 
71     	 */
72     	cb->xbofs = self->bofs_count;
73     	cb->next_xbofs = self->next_bofs;
74     	cb->xbofs_delay = self->xbofs_delay;
75     	
76     	/* Reset XBOF's delay (used only for getting min turn time) */
77     	self->xbofs_delay = 0;
78     	/* Put the correct xbofs value for the next packet */
79     	self->bofs_count = self->next_bofs;
80     }
81     
82     /*
83      * Function irlap_queue_xmit (self, skb)
84      *
85      *    A little wrapper for dev_queue_xmit, so we can insert some common
86      *    code into it.
87      */
88     void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
89     {
90     	/* Some common init stuff */
91     	skb->dev = self->netdev;
92     	skb->h.raw = skb->nh.raw = skb->mac.raw = skb->data;
93      	skb->protocol = htons(ETH_P_IRDA);
94     	skb->priority = TC_PRIO_BESTEFFORT;
95     
96     	irlap_insert_info(self, skb);
97     
98     	dev_queue_xmit(skb);
99     }
100     
101     /*
102      * Function irlap_send_snrm_cmd (void)
103      *
104      *    Transmits a connect SNRM command frame
105      */
106     void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos) 
107     {
108     	struct sk_buff *skb;
109     	struct snrm_frame *frame;
110     	int ret;
111     
112     	ASSERT(self != NULL, return;);
113     	ASSERT(self->magic == LAP_MAGIC, return;);
114     
115     	/* Allocate frame */
116     	skb = dev_alloc_skb(64);
117     	if (!skb)
118     		return;
119     
120     	frame = (struct snrm_frame *) skb_put(skb, 2); 
121     
122     	/* Insert connection address field */
123     	if (qos)
124     		frame->caddr = CMD_FRAME | CBROADCAST;
125     	else
126     		frame->caddr = CMD_FRAME | self->caddr;
127     
128     	/* Insert control field */
129      	frame->control = SNRM_CMD | PF_BIT;
130     	
131     	/*
132     	 *  If we are establishing a connection then insert QoS paramerters 
133     	 */
134     	if (qos) {
135     		skb_put(skb, 9); /* 21 left */
136     		frame->saddr = cpu_to_le32(self->saddr);
137     		frame->daddr = cpu_to_le32(self->daddr);
138     
139     		frame->ncaddr = self->caddr;
140     				
141     		ret = irlap_insert_qos_negotiation_params(self, skb);
142     		if (ret < 0) {
143     			dev_kfree_skb(skb);
144     			return;
145     		}
146     	}
147     	irlap_queue_xmit(self, skb);
148     }
149     
150     /*
151      * Function irlap_recv_snrm_cmd (skb, info)
152      *
153      *    Received SNRM (Set Normal Response Mode) command frame
154      *
155      */
156     static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb, 
157     				struct irlap_info *info) 
158     {
159     	struct snrm_frame *frame;
160     
161     	frame = (struct snrm_frame *) skb->data;
162     	
163     	if (skb->len >= sizeof(struct snrm_frame)) {
164     		/* Copy the new connection address */
165     		info->caddr = frame->ncaddr;
166     
167     		/* Check if the new connection address is valid */
168     		if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
169     			IRDA_DEBUG(3, __FUNCTION__ 
170     			      "(), invalid connection address!\n");
171     			return;
172     		}
173     		
174     		/* Copy peer device address */
175     		info->daddr = le32_to_cpu(frame->saddr);
176     		info->saddr = le32_to_cpu(frame->daddr);
177     		
178     		/* Only accept if addressed directly to us */
179     		if (info->saddr != self->saddr) {
180     			IRDA_DEBUG(2, __FUNCTION__ "(), not addressed to us!\n");
181     			return;
182     		}
183     		irlap_do_event(self, RECV_SNRM_CMD, skb, info);
184     	} else {
185     		/* Signal that this SNRM frame does not contain and I-field */
186     		irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
187     	}
188     }
189     
190     /*
191      * Function irlap_send_ua_response_frame (qos)
192      *
193      *    Send UA (Unnumbered Acknowledgement) frame
194      *
195      */
196     void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
197     {
198     	struct sk_buff *skb;
199     	struct ua_frame *frame;
200     	int ret;
201     	
202     	IRDA_DEBUG(2, __FUNCTION__ "() <%ld>\n", jiffies);
203     	
204     	ASSERT(self != NULL, return;);
205     	ASSERT(self->magic == LAP_MAGIC, return;);
206     
207     	skb = NULL;
208     
209     	/* Allocate frame */
210     	skb = dev_alloc_skb(64);
211     	if (!skb)
212     		return;
213     
214     	frame = (struct ua_frame *) skb_put(skb, 10);
215     	
216     	/* Build UA response */
217     	frame->caddr = self->caddr;
218      	frame->control = UA_RSP | PF_BIT;
219     
220     	frame->saddr = cpu_to_le32(self->saddr);
221     	frame->daddr = cpu_to_le32(self->daddr);
222     
223     	/* Should we send QoS negotiation parameters? */
224     	if (qos) {
225     		ret = irlap_insert_qos_negotiation_params(self, skb);
226     		if (ret < 0) {
227     			dev_kfree_skb(skb);
228     			return;
229     		}
230     	}
231     
232     	irlap_queue_xmit(self, skb);
233     }
234     
235     
236     /*
237      * Function irlap_send_dm_frame (void)
238      *
239      *    Send disconnected mode (DM) frame
240      *
241      */
242     void irlap_send_dm_frame( struct irlap_cb *self)
243     {
244     	struct sk_buff *skb = NULL;
245     	__u8 *frame;
246     	
247     	ASSERT(self != NULL, return;);
248     	ASSERT(self->magic == LAP_MAGIC, return;);
249     
250     	skb = dev_alloc_skb(32);
251     	if (!skb)
252     		return;
253     
254     	frame = skb_put( skb, 2);
255     	
256     	if (self->state == LAP_NDM)
257     		frame[0] = CBROADCAST;
258     	else
259     		frame[0] = self->caddr;
260     
261     	frame[1] = DM_RSP | PF_BIT;
262     
263     	irlap_queue_xmit(self, skb);	
264     }
265     
266     /*
267      * Function irlap_send_disc_frame (void)
268      *
269      *    Send disconnect (DISC) frame
270      *
271      */
272     void irlap_send_disc_frame(struct irlap_cb *self) 
273     {
274     	struct sk_buff *skb = NULL;
275     	__u8 *frame;
276     	
277     	IRDA_DEBUG(3, __FUNCTION__ "()\n");
278     
279     	ASSERT(self != NULL, return;);
280     	ASSERT(self->magic == LAP_MAGIC, return;);
281     
282     	skb = dev_alloc_skb(16);
283     	if (!skb)
284     		return;
285     
286     	frame = skb_put(skb, 2);
287     	
288     	frame[0] = self->caddr | CMD_FRAME;
289     	frame[1] = DISC_CMD | PF_BIT;
290     
291     	irlap_queue_xmit(self, skb);
292     }
293     
294     /*
295      * Function irlap_send_discovery_xid_frame (S, s, command)
296      *
297      *    Build and transmit a XID (eXchange station IDentifier) discovery
298      *    frame. 
299      */
300     void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s, 
301     				    __u8 command, discovery_t *discovery) 
302     {
303     	struct sk_buff *skb = NULL;
304     	struct xid_frame *frame;
305     	__u32 bcast = BROADCAST;
306     	__u8 *info;
307     
308      	IRDA_DEBUG(4, __FUNCTION__ "(), s=%d, S=%d, command=%d\n", s, S, 
309     		   command);
310     
311     	ASSERT(self != NULL, return;);
312     	ASSERT(self->magic == LAP_MAGIC, return;);
313     	ASSERT(discovery != NULL, return;);
314     
315     	skb = dev_alloc_skb(64);
316     	if (!skb)
317     		return;
318     
319     	skb_put(skb, 14);
320     	frame = (struct xid_frame *) skb->data;
321     
322     	if (command) {
323     		frame->caddr = CBROADCAST | CMD_FRAME;
324     		frame->control =  XID_CMD | PF_BIT;
325     	} else {
326     		frame->caddr = CBROADCAST;
327     		frame->control =  XID_RSP | PF_BIT;
328     	}
329     	frame->ident = XID_FORMAT;
330     
331     	frame->saddr = cpu_to_le32(self->saddr);
332     
333     	if (command)
334     		frame->daddr = cpu_to_le32(bcast);
335     	else
336     		frame->daddr = cpu_to_le32(discovery->daddr);
337     	
338     	switch (S) {
339     	case 1:
340     		frame->flags = 0x00;
341     		break;
342     	case 6:
343     		frame->flags = 0x01;
344     		break;
345     	case 8:
346     		frame->flags = 0x02;
347     		break;
348     	case 16:
349     		frame->flags = 0x03;
350     		break;
351     	default:
352     		frame->flags = 0x02;
353     		break;
354     	}
355     
356     	frame->slotnr = s; 
357     	frame->version = 0x00;
358     
359     	/*  
360     	 *  Provide info for final slot only in commands, and for all
361     	 *  responses. Send the second byte of the hint only if the
362     	 *  EXTENSION bit is set in the first byte.
363     	 */
364     	if (!command || (frame->slotnr == 0xff)) {
365     		int len;
366     
367     		if (discovery->hints.byte[0] & HINT_EXTENSION) {
368     			info = skb_put(skb, 2);		
369     			info[0] = discovery->hints.byte[0];
370     			info[1] = discovery->hints.byte[1];
371     		} else {
372     			info = skb_put(skb, 1);
373     			info[0] = discovery->hints.byte[0];
374     		}
375     		info = skb_put(skb, 1);
376     		info[0] = discovery->charset;
377     
378     		len = IRDA_MIN(discovery->name_len, skb_tailroom(skb));
379     		info = skb_put(skb, len);
380     		memcpy(info, discovery->nickname, len);
381     	} 
382     	irlap_queue_xmit(self, skb);
383     }
384     
385     /*
386      * Function irlap_recv_discovery_xid_rsp (skb, info)
387      *
388      *    Received a XID discovery response
389      *
390      */
391     static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self, 
392     					 struct sk_buff *skb, 
393     					 struct irlap_info *info) 
394     {
395     	struct xid_frame *xid;
396     	discovery_t *discovery = NULL;
397     	__u8 *discovery_info;
398     	char *text;
399     
400     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
401     
402     	ASSERT(self != NULL, return;);
403     	ASSERT(self->magic == LAP_MAGIC, return;);
404     
405     	xid = (struct xid_frame *) skb->data;
406     
407     	info->daddr = le32_to_cpu(xid->saddr);
408     	info->saddr = le32_to_cpu(xid->daddr);
409     
410     	/* Make sure frame is addressed to us */
411     	if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
412     		IRDA_DEBUG(0, __FUNCTION__ 
413     			   "(), frame is not addressed to us!\n");
414     		return;
415     	}
416     
417     	if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
418     		WARNING(__FUNCTION__ "(), kmalloc failed!\n");
419     		return;
420     	}
421     	memset(discovery, 0, sizeof(discovery_t));
422     
423     	discovery->daddr = info->daddr;
424     	discovery->saddr = self->saddr;
425     	discovery->timestamp = jiffies;
426     
427     	IRDA_DEBUG(4, __FUNCTION__ "(), daddr=%08x\n", discovery->daddr);
428     
429     	discovery_info = skb_pull(skb, sizeof(struct xid_frame));
430     
431     	/* Get info returned from peer */
432     	discovery->hints.byte[0] = discovery_info[0];
433     	if (discovery_info[0] & HINT_EXTENSION) {
434     		IRDA_DEBUG(4, "EXTENSION\n");
435     		discovery->hints.byte[1] = discovery_info[1];
436     		discovery->charset = discovery_info[2];
437     		text = (char *) &discovery_info[3];
438     	} else {
439     		discovery->hints.byte[1] = 0;
440     		discovery->charset = discovery_info[1];
441     		text = (char *) &discovery_info[2];
442     	}
443     	/* 
444     	 *  Terminate info string, should be safe since this is where the 
445     	 *  FCS bytes resides.
446     	 */
447     	skb->data[skb->len] = '\0'; 
448     	strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
449     	discovery->name_len = strlen(discovery->nickname);
450     
451     	info->discovery = discovery;
452     
453     	irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
454     }
455     
456     /*
457      * Function irlap_recv_discovery_xid_cmd (skb, info)
458      *
459      *    Received a XID discovery command
460      *
461      */
462     static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self, 
463     					 struct sk_buff *skb, 
464     					 struct irlap_info *info) 
465     {
466     	struct xid_frame *xid;
467     	discovery_t *discovery = NULL;
468     	__u8 *discovery_info;
469     	char *text;
470     
471     	xid = (struct xid_frame *) skb->data;
472     
473     	info->daddr = le32_to_cpu(xid->saddr);
474     	info->saddr = le32_to_cpu(xid->daddr);
475     
476     	/* Make sure frame is addressed to us */
477     	if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
478     		IRDA_DEBUG(0, __FUNCTION__ 
479     			   "(), frame is not addressed to us!\n");
480     		return;
481     	}
482     
483     	switch (xid->flags & 0x03) {
484     	case 0x00:
485     		info->S = 1;
486     		break;
487     	case 0x01:
488     		info->S = 6;
489     		break;
490     	case 0x02:
491     		info->S = 8;
492     		break;
493     	case 0x03:
494     		info->S = 16;
495     		break;
496     	default:
497     		/* Error!! */
498     		dev_kfree_skb(skb);
499     		return;
500     	}
501     	info->s = xid->slotnr;
502     	
503     	discovery_info = skb_pull(skb, sizeof(struct xid_frame));
504     
505     	/* 
506     	 *  Check if last frame 
507     	 */
508     	if (info->s == 0xff) {
509     		/* Check if things are sane at this point... */
510     		if((discovery_info == NULL) || (skb->len < 3)) {
511     			ERROR(__FUNCTION__ "(), discovery frame to short!\n");
512     			return;
513     		}
514     
515     		/*
516     		 *  We now have some discovery info to deliver!
517     		 */
518     		discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
519     		if (!discovery) {
520     			WARNING(__FUNCTION__ "(), unable to malloc!\n");
521     			return;
522     		}
523     	      
524     		discovery->daddr = info->daddr;
525     		discovery->saddr = self->saddr;
526     		discovery->timestamp = jiffies;
527     
528     		discovery->hints.byte[0] = discovery_info[0];
529     		if (discovery_info[0] & HINT_EXTENSION) {
530     			discovery->hints.byte[1] = discovery_info[1];
531     			discovery->charset = discovery_info[2];
532     			text = (char *) &discovery_info[3];
533     		} else {
534     			discovery->hints.byte[1] = 0;
535     			discovery->charset = discovery_info[1];
536     			text = (char *) &discovery_info[2];
537     		}
538     		/* 
539     		 *  Terminate string, should be safe since this is where the 
540     		 *  FCS bytes resides.
541     		 */
542     		skb->data[skb->len] = '\0'; 
543     		strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
544     		discovery->name_len = strlen(discovery->nickname);
545     
546     		info->discovery = discovery;
547     	} else
548     		info->discovery = NULL;
549     
550     	irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
551     }
552     
553     /*
554      * Function irlap_send_rr_frame (self, command)
555      *
556      *    Build and transmit RR (Receive Ready) frame. Notice that it is currently
557      *    only possible to send RR frames with the poll bit set.
558      */
559     void irlap_send_rr_frame(struct irlap_cb *self, int command) 
560     {
561     	struct sk_buff *skb;
562     	__u8 *frame;
563     
564     	skb = dev_alloc_skb(16);
565     	if (!skb)
566     		return;
567     	
568     	frame = skb_put(skb, 2);
569     	
570     	frame[0] = self->caddr;
571     	frame[0] |= (command) ? CMD_FRAME : 0;
572     
573     	frame[1] = RR | PF_BIT | (self->vr << 5);
574     
575     	irlap_queue_xmit(self, skb);
576     }
577     
578     /*
579      * Function irlap_send_rd_frame (self)
580      *
581      *    Request disconnect. Used by a secondary station to request the 
582      *    disconnection of the link.
583      */
584     void irlap_send_rd_frame(struct irlap_cb *self)
585     {
586     	struct sk_buff *skb;
587     	__u8 *frame;
588     
589     	skb = dev_alloc_skb(16);
590     	if (!skb)
591     		return;
592     	
593     	frame = skb_put(skb, 2);
594     	
595     	frame[0] = self->caddr;
596     	frame[1] = RD_RSP | PF_BIT;
597     
598     	irlap_queue_xmit(self, skb);
599     }
600     
601     /*
602      * Function irlap_recv_rr_frame (skb, info)
603      *
604      *    Received RR (Receive Ready) frame from peer station, no harm in
605      *    making it inline since its called only from one single place
606      *    (irlap_driver_rcv).
607      */
608     static inline void irlap_recv_rr_frame(struct irlap_cb *self, 
609     				       struct sk_buff *skb, 
610     				       struct irlap_info *info, int command)
611     {
612     	info->nr = skb->data[1] >> 5;
613     
614     	/* Check if this is a command or a response frame */
615     	if (command)
616     		irlap_do_event(self, RECV_RR_CMD, skb, info);
617     	else
618     		irlap_do_event(self, RECV_RR_RSP, skb, info);
619     }
620     
621     void irlap_send_frmr_frame( struct irlap_cb *self, int command)
622     {
623     	struct sk_buff *skb = NULL;
624     	__u8 *frame;
625     	
626     	ASSERT( self != NULL, return;);
627     	ASSERT( self->magic == LAP_MAGIC, return;);
628     
629     	skb = dev_alloc_skb( 32);
630     	if (!skb)
631     		return;
632     
633     	frame = skb_put( skb, 2);
634     	
635     	frame[0] = self->caddr;
636     	frame[0] |= (command) ? CMD_FRAME : 0;
637     
638     	frame[1]  = (self->vs << 1);
639     	frame[1] |= PF_BIT;
640     	frame[1] |= (self->vr << 5);
641     
642     	frame[2] = 0;
643     
644        	IRDA_DEBUG(4, __FUNCTION__ "(), vr=%d, %ld\n",self->vr, jiffies); 
645     
646     	irlap_queue_xmit(self, skb);
647     }
648     
649     /*
650      * Function irlap_recv_rnr_frame (self, skb, info)
651      *
652      *    Received RNR (Receive Not Ready) frame from peer station
653      *
654      */
655     static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb, 
656     				 struct irlap_info *info, int command) 
657     {
658     	info->nr = skb->data[1] >> 5;
659     
660     	IRDA_DEBUG(4, __FUNCTION__ "(), nr=%d, %ld\n", info->nr, jiffies);
661     
662     	if (command)
663     		irlap_do_event(self, RECV_RNR_CMD, skb, info);
664     	else
665     		irlap_do_event(self, RECV_RNR_RSP, skb, info);
666     }
667     
668     static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb, 
669     				 struct irlap_info *info, int command)
670     {
671     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
672     
673     	info->nr = skb->data[1] >> 5;
674     	
675     	/* Check if this is a command or a response frame */
676     	if (command)
677     		irlap_do_event(self, RECV_REJ_CMD, skb, info);
678     	else
679     		irlap_do_event(self, RECV_REJ_RSP, skb, info);
680     }
681     
682     static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb, 
683     				  struct irlap_info *info, int command)
684     {
685     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
686     
687     	info->nr = skb->data[1] >> 5;
688     	
689     	/* Check if this is a command or a response frame */
690     	if (command)
691     		irlap_do_event(self, RECV_SREJ_CMD, skb, info);
692     	else
693     		irlap_do_event(self, RECV_SREJ_RSP, skb, info);
694     }
695     
696     static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb, 
697     				  struct irlap_info *info, int command)
698     {
699     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
700     
701     	/* Check if this is a command or a response frame */
702     	if (command)
703     		irlap_do_event(self, RECV_DISC_CMD, skb, info);
704     	else
705     		irlap_do_event(self, RECV_RD_RSP, skb, info);
706     }
707     
708     /*
709      * Function irlap_recv_ua_frame (skb, frame)
710      *
711      *    Received UA (Unnumbered Acknowledgement) frame
712      *
713      */
714     static inline void irlap_recv_ua_frame(struct irlap_cb *self, 
715     				       struct sk_buff *skb, 
716     				       struct irlap_info *info) 
717     {
718     	irlap_do_event(self, RECV_UA_RSP, skb, info);
719     }
720     
721     /*
722      * Function irlap_send_data_primary(self, skb)
723      *
724      *    Send I-frames as the primary station but without the poll bit set
725      *
726      */
727     void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
728     {
729     	struct sk_buff *tx_skb;
730     
731     	if (skb->data[1] == I_FRAME) {
732     
733     		/*  
734     		 *  Insert frame sequence number (Vs) in control field before
735     		 *  inserting into transmit window queue.
736     		 */
737     		skb->data[1] = I_FRAME | (self->vs << 1);
738     		
739     		/* Copy buffer */
740     		tx_skb = skb_clone(skb, GFP_ATOMIC);
741     		if (tx_skb == NULL) {
742     			return;
743     		}
744     		
745     		/* 
746     		 *  Insert frame in store, in case of retransmissions 
747     		 */
748     		skb_queue_tail(&self->wx_list, skb_get(skb));
749     		
750     		self->vs = (self->vs + 1) % 8;
751     		self->ack_required = FALSE;		
752     		self->window -= 1;
753     
754     		irlap_send_i_frame( self, tx_skb, CMD_FRAME);
755     	} else {
756     		IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
757     		irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
758     		self->window -= 1;
759     	}
760     }
761     /*
762      * Function irlap_send_data_primary_poll (self, skb)
763      *
764      *    Send I(nformation) frame as primary with poll bit set
765      */
766     void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb) 
767     {
768     	struct sk_buff *tx_skb;
769     
770     	/* Is this reliable or unreliable data? */
771     	if (skb->data[1] == I_FRAME) {
772     		
773     		/*  
774     		 *  Insert frame sequence number (Vs) in control field before
775     		 *  inserting into transmit window queue.
776     		 */
777     		skb->data[1] = I_FRAME | (self->vs << 1);
778     		
779     		/* Copy buffer */
780     		tx_skb = skb_clone(skb, GFP_ATOMIC);
781     		if (tx_skb == NULL) {
782     			return;
783     		}
784     		
785     		/* 
786     		 *  Insert frame in store, in case of retransmissions 
787     		 */
788     		skb_queue_tail(&self->wx_list, skb_get(skb));
789     		
790     		/*  
791     		 *  Set poll bit if necessary. We do this to the copied
792     		 *  skb, since retransmitted need to set or clear the poll
793     		 *  bit depending on when they are sent.  
794     		 */
795     		/* Stop P timer */
796     		del_timer(&self->poll_timer);
797     		
798     		tx_skb->data[1] |= PF_BIT;
799     		
800     		self->vs = (self->vs + 1) % 8;
801     		self->ack_required = FALSE;
802     		self->window = self->window_size;
803     
804     		irlap_start_final_timer(self, self->final_timeout);
805     
806     		irlap_send_i_frame(self, tx_skb, CMD_FRAME);
807     	} else {
808     		IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
809     
810     		del_timer(&self->poll_timer);
811     
812     		if (self->ack_required) {
813     			irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
814     			irlap_send_rr_frame(self, CMD_FRAME);
815     			self->ack_required = FALSE;
816     		} else {
817     			skb->data[1] |= PF_BIT;
818     			irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
819     		}
820     		self->window = self->window_size;
821     		irlap_start_final_timer(self, self->final_timeout);
822     	}
823     }
824     
825     /*
826      * Function irlap_send_data_secondary_final (self, skb)
827      *
828      *    Send I(nformation) frame as secondary with final bit set
829      *
830      */
831     void irlap_send_data_secondary_final(struct irlap_cb *self, 
832     				     struct sk_buff *skb) 
833     {
834     	struct sk_buff *tx_skb = NULL;
835     
836     	ASSERT(self != NULL, return;);
837     	ASSERT(self->magic == LAP_MAGIC, return;);
838     	ASSERT(skb != NULL, return;);
839     
840     	/* Is this reliable or unreliable data? */
841     	if (skb->data[1] == I_FRAME) {
842     
843     		/*  
844     		 *  Insert frame sequence number (Vs) in control field before
845     		 *  inserting into transmit window queue.
846     		 */
847     		skb->data[1] = I_FRAME | (self->vs << 1);
848     		
849     		tx_skb = skb_clone(skb, GFP_ATOMIC);
850     		if (tx_skb == NULL) {
851     			return;
852     		}		
853     
854     		/* Insert frame in store */
855     		skb_queue_tail(&self->wx_list, skb_get(skb));
856     		
857     		tx_skb->data[1] |= PF_BIT;
858     		
859     		self->vs = (self->vs + 1) % 8; 
860     		self->window = self->window_size;
861     		self->ack_required = FALSE;
862     		
863     		irlap_start_wd_timer(self, self->wd_timeout);
864     
865     		irlap_send_i_frame(self, tx_skb, RSP_FRAME); 
866     	} else {
867     		if (self->ack_required) {
868     			irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
869     			irlap_send_rr_frame(self, RSP_FRAME);
870     			self->ack_required = FALSE;
871     		} else {
872     			skb->data[1] |= PF_BIT;
873     			irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
874     		}
875     		self->window = self->window_size;
876     
877     		irlap_start_wd_timer(self, self->wd_timeout);
878     	}
879     }
880     
881     /*
882      * Function irlap_send_data_secondary (self, skb)
883      *
884      *    Send I(nformation) frame as secondary without final bit set
885      *
886      */
887     void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb) 
888     {
889     	struct sk_buff *tx_skb = NULL;
890     
891     	/* Is this reliable or unreliable data? */
892     	if (skb->data[1] == I_FRAME) {
893     		
894     		/*  
895     		 *  Insert frame sequence number (Vs) in control field before
896     		 *  inserting into transmit window queue.
897     		 */
898     		skb->data[1] = I_FRAME | (self->vs << 1);
899     		
900     		tx_skb = skb_clone(skb, GFP_ATOMIC);
901     		if (tx_skb == NULL) {
902     			return;
903     		}		
904     		
905     		/* Insert frame in store */
906     		skb_queue_tail(&self->wx_list, skb_get(skb));
907     		
908     		self->vs = (self->vs + 1) % 8;
909     		self->ack_required = FALSE;		
910     		self->window -= 1;
911     
912     		irlap_send_i_frame(self, tx_skb, RSP_FRAME); 
913     	} else {
914     		irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
915     		self->window -= 1;
916     	}
917     }
918     
919     /*
920      * Function irlap_resend_rejected_frames (nr)
921      *
922      *    Resend frames which has not been acknowledged. Should be safe to 
923      *    traverse the list without locking it since this function will only be 
924      *    called from interrupt context (BH)
925      */
926     void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
927     {
928     	struct sk_buff *tx_skb;
929     	struct sk_buff *skb;
930     	int count;
931     
932     	ASSERT(self != NULL, return;);
933     	ASSERT(self->magic == LAP_MAGIC, return;);
934     
935     	/* Initialize variables */
936     	skb = tx_skb = NULL;
937     
938     	count = skb_queue_len(&self->wx_list);
939     
940     	/*  Resend unacknowledged frame(s) */
941     	skb = skb_peek(&self->wx_list);
942     	while (skb != NULL) {
943     		irlap_wait_min_turn_around(self, &self->qos_tx);
944     
945     		/* We copy the skb to be retransmitted since we will have to 
946     		 * modify it. Cloning will confuse packet sniffers 
947     		 */
948     		/* tx_skb = skb_clone( skb, GFP_ATOMIC); */
949     		tx_skb = skb_copy(skb, GFP_ATOMIC);
950     		if (!tx_skb) {
951     			IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
952     			return;
953     		}
954     		/* Unlink tx_skb from list */
955     		tx_skb->next = tx_skb->prev = NULL;
956     		tx_skb->list = NULL;
957     
958     		/* Clear old Nr field + poll bit */
959     		tx_skb->data[1] &= 0x0f;
960     
961     		/* 
962     		 *  Set poll bit on the last frame retransmitted
963     		 */
964     	 	if (count-- == 1)
965     	 		tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
966     		else
967     			tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
968     	      	
969     		irlap_send_i_frame(self, tx_skb, command);
970     
971     		/* 
972     		 *  If our skb is the last buffer in the list, then
973     		 *  we are finished, if not, move to the next sk-buffer
974     		 */
975     		if (skb == skb_peek_tail(&self->wx_list))
976     			skb = NULL;
977     		else
978     			skb = skb->next;
979     	}
980     #if 0 /* Not yet */
981     	/* 
982     	 *  We can now fill the window with additinal data frames
983     	 */
984     	while (skb_queue_len( &self->txq) > 0) {
985     		
986     		IRDA_DEBUG(0, __FUNCTION__ "(), sending additional frames!\n");
987     		if ((skb_queue_len( &self->txq) > 0) && 
988     		    (self->window > 0)) {
989     			skb = skb_dequeue( &self->txq); 
990     			ASSERT(skb != NULL, return;);
991     
992     			/*
993     			 *  If send window > 1 then send frame with pf 
994     			 *  bit cleared
995     			 */ 
996     			if ((self->window > 1) && 
997     			    skb_queue_len(&self->txq) > 0) 
998     			{
999     				irlap_send_data_primary(self, skb);
1000     			} else {
1001     				irlap_send_data_primary_poll(self, skb);
1002     			}
1003     			kfree_skb(skb);
1004     		}
1005     	}
1006     #endif
1007     }
1008     
1009     void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1010     {
1011     	struct sk_buff *tx_skb;
1012     	struct sk_buff *skb;
1013     
1014     	ASSERT(self != NULL, return;);
1015     	ASSERT(self->magic == LAP_MAGIC, return;);
1016     
1017     	/* Initialize variables */
1018     	skb = tx_skb = NULL;
1019     
1020     	/*  Resend unacknowledged frame(s) */
1021     	skb = skb_peek(&self->wx_list);
1022     	if (skb != NULL) {
1023     		irlap_wait_min_turn_around(self, &self->qos_tx);
1024     
1025     		/* We copy the skb to be retransmitted since we will have to 
1026     		 * modify it. Cloning will confuse packet sniffers 
1027     		 */
1028     		/* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1029     		tx_skb = skb_copy(skb, GFP_ATOMIC);
1030     		if (!tx_skb) {
1031     			IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
1032     			return;	
1033     		}
1034     		/* Unlink tx_skb from list */
1035     		tx_skb->next = tx_skb->prev = NULL;
1036     		tx_skb->list = NULL;
1037     
1038     		/* Clear old Nr field + poll bit */
1039     		tx_skb->data[1] &= 0x0f;
1040     
1041     		/*  Set poll/final bit */
1042     		tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1043     	      	
1044     		irlap_send_i_frame(self, tx_skb, command);
1045     	}
1046     }
1047     
1048     /*
1049      * Function irlap_send_ui_frame (self, skb, command)
1050      *
1051      *    Contruct and transmit an Unnumbered Information (UI) frame
1052      *
1053      */
1054     void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb, 
1055     			 __u8 caddr, int command)
1056     {
1057     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
1058     	
1059     	ASSERT(self != NULL, return;);
1060     	ASSERT(self->magic == LAP_MAGIC, return;);
1061     	ASSERT(skb != NULL, return;);
1062     	
1063     	/* Insert connection address */
1064     	skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1065     
1066     	irlap_queue_xmit(self, skb);
1067     }
1068     
1069     /*
1070      * Function irlap_send_i_frame (skb)
1071      *
1072      *    Contruct and transmit Information (I) frame
1073      */
1074     void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb, 
1075     			int command) 
1076     {
1077     	/* Insert connection address */
1078     	skb->data[0] = self->caddr;
1079     	skb->data[0] |= (command) ? CMD_FRAME : 0;
1080     	
1081     	/* Insert next to receive (Vr) */
1082     	skb->data[1] |= (self->vr << 5);  /* insert nr */
1083     
1084     	irlap_queue_xmit(self, skb);
1085     }
1086     
1087     /*
1088      * Function irlap_recv_i_frame (skb, frame)
1089      *
1090      *    Receive and parse an I (Information) frame, no harm in making it inline
1091      *    since it's called only from one single place (irlap_driver_rcv).
1092      */
1093     static inline void irlap_recv_i_frame(struct irlap_cb *self, 
1094     				      struct sk_buff *skb, 
1095     				      struct irlap_info *info, int command) 
1096     {
1097     	info->nr = skb->data[1] >> 5;          /* Next to receive */
1098     	info->pf = skb->data[1] & PF_BIT;      /* Final bit */
1099     	info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1100     
1101     	/* Check if this is a command or a response frame */
1102     	if (command)
1103     		irlap_do_event(self, RECV_I_CMD, skb, info);
1104     	else
1105     		irlap_do_event(self, RECV_I_RSP, skb, info);
1106     }
1107     
1108     /*
1109      * Function irlap_recv_ui_frame (self, skb, info)
1110      *
1111      *    Receive and parse an Unnumbered Information (UI) frame
1112      *
1113      */
1114     static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb, 
1115     				struct irlap_info *info)
1116     {
1117     	IRDA_DEBUG( 4, __FUNCTION__ "()\n");
1118     
1119     	info->pf = skb->data[1] & PF_BIT;      /* Final bit */
1120     
1121     	irlap_do_event(self, RECV_UI_FRAME, skb, info);
1122     }
1123     
1124     /*
1125      * Function irlap_recv_frmr_frame (skb, frame)
1126      *
1127      *    Received Frame Reject response.
1128      *
1129      */
1130     static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb, 
1131     				  struct irlap_info *info) 
1132     {
1133     	__u8 *frame;
1134     	int w, x, y, z;
1135     
1136     	IRDA_DEBUG(0, __FUNCTION__ "()\n");
1137     	
1138     	ASSERT(self != NULL, return;);
1139     	ASSERT(self->magic == LAP_MAGIC, return;);
1140     	ASSERT(skb != NULL, return;);
1141     	ASSERT(info != NULL, return;);
1142     	
1143     	frame = skb->data;
1144     
1145     	info->nr = frame[2] >> 5;          /* Next to receive */
1146     	info->pf = frame[2] & PF_BIT;      /* Final bit */
1147     	info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1148     
1149     	w = frame[3] & 0x01;
1150     	x = frame[3] & 0x02;
1151     	y = frame[3] & 0x04;
1152     	z = frame[3] & 0x08;
1153     	
1154     	if (w) {
1155     		IRDA_DEBUG(0, "Rejected control field is undefined or not "
1156     		      "implemented.\n");
1157     	} 
1158     	if (x) {
1159     		IRDA_DEBUG(0, "Rejected control field was invalid because it "
1160     		      "contained a non permitted I field.\n");
1161     	}
1162     	if (y) {
1163     		IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1164     		      "for the existing connection or exceeded the maximum "
1165     		      "this station supports if no connection exists.\n");
1166     	}
1167     	if (z) {
1168     		IRDA_DEBUG(0, "Rejected control field control field contained an "
1169     		      "invalid Nr count.\n");
1170     	}
1171     	irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1172     }
1173     
1174     /*
1175      * Function irlap_send_test_frame (self, daddr)
1176      *
1177      *    Send a test frame response
1178      *
1179      */
1180     void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr, 
1181     			   struct sk_buff *cmd)
1182     {
1183     	struct sk_buff *skb;
1184     	struct test_frame *frame;
1185     	__u8 *info;
1186     
1187     	skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1188     	if (!skb)
1189     		return;
1190     
1191     	/* Broadcast frames must include saddr and daddr fields */
1192     	if (caddr == CBROADCAST) {
1193     		frame = (struct test_frame *) 
1194     			skb_put(skb, sizeof(struct test_frame));
1195     
1196     		/* Insert the swapped addresses */
1197     		frame->saddr = cpu_to_le32(self->saddr);
1198     		frame->daddr = cpu_to_le32(daddr);
1199     	} else
1200     		frame = (struct test_frame *) skb_put(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1201     
1202     	frame->caddr = caddr;
1203     	frame->control = TEST_RSP | PF_BIT;
1204     
1205     	/* Copy info */
1206     	info = skb_put(skb, cmd->len);
1207     	memcpy(info, cmd->data, cmd->len);
1208     
1209     	/* Return to sender */
1210     	irlap_wait_min_turn_around(self, &self->qos_tx);
1211     	irlap_queue_xmit(self, skb);
1212     }
1213     
1214     /*
1215      * Function irlap_recv_test_frame (self, skb)
1216      *
1217      *    Receive a test frame
1218      *
1219      */
1220     static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb, 
1221     				  struct irlap_info *info, int command)
1222     {
1223     	struct test_frame *frame;
1224     
1225     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
1226     	
1227     	frame = (struct test_frame *) skb->data;
1228     		
1229     	/* Broadcast frames must carry saddr and daddr fields */
1230     	if (info->caddr == CBROADCAST) {
1231     		if (skb->len < sizeof(struct test_frame)) {
1232     			IRDA_DEBUG(0, __FUNCTION__ 
1233     				   "() test frame to short!\n");
1234     			return;
1235     		}
1236     		
1237     		/* Read and swap addresses */
1238     		info->daddr = le32_to_cpu(frame->saddr);
1239     		info->saddr = le32_to_cpu(frame->daddr);
1240     
1241     		/* Make sure frame is addressed to us */
1242     		if ((info->saddr != self->saddr) && 
1243     		    (info->saddr != BROADCAST)) {
1244     			return;
1245     		}
1246     	}
1247     
1248     	if (command)
1249     		irlap_do_event(self, RECV_TEST_CMD, skb, info);
1250     	else
1251     		irlap_do_event(self, RECV_TEST_RSP, skb, info);
1252     }
1253     
1254     /*
1255      * Function irlap_driver_rcv (skb, netdev, ptype)
1256      *
1257      *    Called when a frame is received. Dispatches the right receive function 
1258      *    for processing of the frame.
1259      *
1260      */
1261     int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev, 
1262     		     struct packet_type *ptype)
1263     {
1264     	struct irlap_info info;
1265     	struct irlap_cb *self;
1266     	int command;
1267     	__u8 control;
1268     	
1269     	/* FIXME: should we get our own field? */
1270     	self = (struct irlap_cb *) dev->atalk_ptr;
1271     
1272     	/* If the net device is down, then IrLAP is gone! */
1273     	if (!self || self->magic != LAP_MAGIC) {
1274     		dev_kfree_skb(skb);
1275     		return -1;
1276     	}
1277     
1278     	/* Check if frame is large enough for parsing */
1279     	if (skb->len < 2) {
1280     		ERROR(__FUNCTION__ "(), frame to short!\n");
1281     		dev_kfree_skb(skb);
1282     		return -1;
1283     	}
1284     	
1285     	command    = skb->data[0] & CMD_FRAME;
1286     	info.caddr = skb->data[0] & CBROADCAST;
1287     	
1288     	info.pf      = skb->data[1] &  PF_BIT;
1289     	info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1290     
1291     	control = info.control;
1292     
1293     	/*  First we check if this frame has a valid connection address */
1294     	if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1295     		IRDA_DEBUG(0, __FUNCTION__ "(), wrong connection address!\n");
1296     		goto out;
1297     	}
1298     	/*  
1299     	 *  Optimize for the common case and check if the frame is an
1300     	 *  I(nformation) frame. Only I-frames have bit 0 set to 0
1301     	 */
1302     	if (~control & 0x01) {
1303     		irlap_recv_i_frame(self, skb, &info, command);
1304     		goto out;
1305     	}
1306     	/*
1307     	 *  We now check is the frame is an S(upervisory) frame. Only 
1308     	 *  S-frames have bit 0 set to 1 and bit 1 set to 0
1309     	 */
1310     	if (~control & 0x02) {
1311     		/* 
1312     		 *  Received S(upervisory) frame, check which frame type it is
1313     		 *  only the first nibble is of interest
1314     		 */
1315     		switch (control & 0x0f) {
1316     		case RR:
1317     			irlap_recv_rr_frame(self, skb, &info, command);
1318     			break;
1319     		case RNR:
1320     			irlap_recv_rnr_frame(self, skb, &info, command);
1321     			break;
1322     		case REJ:
1323     			irlap_recv_rej_frame(self, skb, &info, command);
1324     			break;
1325     		case SREJ:
1326     			irlap_recv_srej_frame(self, skb, &info, command);
1327     			break;
1328     		default:
1329     			WARNING(__FUNCTION__ 
1330     				"() Unknown S-frame %02x received!\n",
1331     				info.control);
1332     			break;
1333     		}
1334     		goto out;
1335     	}
1336     	/* 
1337     	 *  This must be a C(ontrol) frame 
1338     	 */
1339     	switch (control) {
1340     	case XID_RSP:
1341     		irlap_recv_discovery_xid_rsp(self, skb, &info);
1342     		break;
1343     	case XID_CMD:
1344     		irlap_recv_discovery_xid_cmd(self, skb, &info);
1345     		break;
1346     	case SNRM_CMD:
1347     		irlap_recv_snrm_cmd(self, skb, &info);
1348     		break;
1349     	case DM_RSP:
1350     		irlap_do_event(self, RECV_DM_RSP, skb, &info);
1351     		break;
1352     	case DISC_CMD: /* And RD_RSP since they have the same value */
1353     		irlap_recv_disc_frame(self, skb, &info, command);
1354     		break;
1355     	case TEST_CMD:
1356     		irlap_recv_test_frame(self, skb, &info, command);
1357     		break;
1358     	case UA_RSP:
1359     		irlap_recv_ua_frame(self, skb, &info);
1360     		break;
1361     	case FRMR_RSP:
1362     		irlap_recv_frmr_frame(self, skb, &info);
1363     		break;
1364     	case UI_FRAME:
1365     		irlap_recv_ui_frame(self, skb, &info);
1366     		break;
1367     	default:
1368     		WARNING(__FUNCTION__ "(), Unknown frame %02x received!\n", 
1369     			info.control);
1370     		break;
1371     	}
1372     out:
1373     	dev_kfree_skb(skb); 
1374     	return 0;
1375     }
1376