Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh...
[pandora-kernel.git] / drivers / staging / rtl8192e / ieee80211 / ieee80211_rx.c
1 /*
2  * Original code based Host AP (software wireless LAN access point) driver
3  * for Intersil Prism2/2.5/3 - hostap.o module, common routines
4  *
5  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6  * <jkmaline@cc.hut.fi>
7  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
8  * Copyright (c) 2004, Intel Corporation
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation. See README and COPYING for
13  * more details.
14  ******************************************************************************
15
16   Few modifications for Realtek's Wi-Fi drivers by
17   Andrea Merello <andreamrl@tiscali.it>
18
19   A special thanks goes to Realtek for their support !
20
21 ******************************************************************************/
22
23
24 #include <linux/compiler.h>
25 //#include <linux/config.h>
26 #include <linux/errno.h>
27 #include <linux/if_arp.h>
28 #include <linux/in6.h>
29 #include <linux/in.h>
30 #include <linux/ip.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/netdevice.h>
34 #include <linux/pci.h>
35 #include <linux/proc_fs.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <linux/tcp.h>
39 #include <linux/types.h>
40 #include <linux/version.h>
41 #include <linux/wireless.h>
42 #include <linux/etherdevice.h>
43 #include <asm/uaccess.h>
44 #include <linux/ctype.h>
45
46 #include "ieee80211.h"
47 #ifdef ENABLE_DOT11D
48 #include "dot11d.h"
49 #endif
50 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
51                                         struct sk_buff *skb,
52                                         struct ieee80211_rx_stats *rx_stats)
53 {
54         struct ieee80211_hdr_4addr *hdr = (struct ieee80211_hdr_4addr *)skb->data;
55         u16 fc = le16_to_cpu(hdr->frame_ctl);
56
57         skb->dev = ieee->dev;
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
59         skb_reset_mac_header(skb);
60 #else
61         skb->mac.raw = skb->data;
62 #endif
63
64         skb_pull(skb, ieee80211_get_hdrlen(fc));
65         skb->pkt_type = PACKET_OTHERHOST;
66         skb->protocol = __constant_htons(ETH_P_80211_RAW);
67         memset(skb->cb, 0, sizeof(skb->cb));
68         netif_rx(skb);
69 }
70
71
72 /* Called only as a tasklet (software IRQ) */
73 static struct ieee80211_frag_entry *
74 ieee80211_frag_cache_find(struct ieee80211_device *ieee, unsigned int seq,
75                           unsigned int frag, u8 tid,u8 *src, u8 *dst)
76 {
77         struct ieee80211_frag_entry *entry;
78         int i;
79
80         for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
81                 entry = &ieee->frag_cache[tid][i];
82                 if (entry->skb != NULL &&
83                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
84                         IEEE80211_DEBUG_FRAG(
85                                 "expiring fragment cache entry "
86                                 "seq=%u last_frag=%u\n",
87                                 entry->seq, entry->last_frag);
88                         dev_kfree_skb_any(entry->skb);
89                         entry->skb = NULL;
90                 }
91
92                 if (entry->skb != NULL && entry->seq == seq &&
93                     (entry->last_frag + 1 == frag || frag == -1) &&
94                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
95                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
96                         return entry;
97         }
98
99         return NULL;
100 }
101
102 /* Called only as a tasklet (software IRQ) */
103 static struct sk_buff *
104 ieee80211_frag_cache_get(struct ieee80211_device *ieee,
105                          struct ieee80211_hdr_4addr *hdr)
106 {
107         struct sk_buff *skb = NULL;
108         u16 fc = le16_to_cpu(hdr->frame_ctl);
109         u16 sc = le16_to_cpu(hdr->seq_ctl);
110         unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
111         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
112         struct ieee80211_frag_entry *entry;
113         struct ieee80211_hdr_3addrqos *hdr_3addrqos;
114         struct ieee80211_hdr_4addrqos *hdr_4addrqos;
115         u8 tid;
116
117         if (((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
118           hdr_4addrqos = (struct ieee80211_hdr_4addrqos *)hdr;
119           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & IEEE80211_QCTL_TID;
120           tid = UP2AC(tid);
121           tid ++;
122         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
123           hdr_3addrqos = (struct ieee80211_hdr_3addrqos *)hdr;
124           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & IEEE80211_QCTL_TID;
125           tid = UP2AC(tid);
126           tid ++;
127         } else {
128           tid = 0;
129         }
130
131         if (frag == 0) {
132                 /* Reserve enough space to fit maximum frame length */
133                 skb = dev_alloc_skb(ieee->dev->mtu +
134                                     sizeof(struct ieee80211_hdr_4addr) +
135                                     8 /* LLC */ +
136                                     2 /* alignment */ +
137                                     8 /* WEP */ +
138                                     ETH_ALEN /* WDS */ +
139                                     (IEEE80211_QOS_HAS_SEQ(fc)?2:0) /* QOS Control */);
140                 if (skb == NULL)
141                         return NULL;
142
143                 entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
144                 ieee->frag_next_idx[tid]++;
145                 if (ieee->frag_next_idx[tid] >= IEEE80211_FRAG_CACHE_LEN)
146                         ieee->frag_next_idx[tid] = 0;
147
148                 if (entry->skb != NULL)
149                         dev_kfree_skb_any(entry->skb);
150
151                 entry->first_frag_time = jiffies;
152                 entry->seq = seq;
153                 entry->last_frag = frag;
154                 entry->skb = skb;
155                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
156                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
157         } else {
158                 /* received a fragment of a frame for which the head fragment
159                  * should have already been received */
160                 entry = ieee80211_frag_cache_find(ieee, seq, frag, tid,hdr->addr2,
161                                                   hdr->addr1);
162                 if (entry != NULL) {
163                         entry->last_frag = frag;
164                         skb = entry->skb;
165                 }
166         }
167
168         return skb;
169 }
170
171
172 /* Called only as a tasklet (software IRQ) */
173 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
174                                            struct ieee80211_hdr_4addr *hdr)
175 {
176         u16 fc = le16_to_cpu(hdr->frame_ctl);
177         u16 sc = le16_to_cpu(hdr->seq_ctl);
178         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
179         struct ieee80211_frag_entry *entry;
180         struct ieee80211_hdr_3addrqos *hdr_3addrqos;
181         struct ieee80211_hdr_4addrqos *hdr_4addrqos;
182         u8 tid;
183
184         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
185           hdr_4addrqos = (struct ieee80211_hdr_4addrqos *)hdr;
186           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & IEEE80211_QCTL_TID;
187           tid = UP2AC(tid);
188           tid ++;
189         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
190           hdr_3addrqos = (struct ieee80211_hdr_3addrqos *)hdr;
191           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & IEEE80211_QCTL_TID;
192           tid = UP2AC(tid);
193           tid ++;
194         } else {
195           tid = 0;
196         }
197
198         entry = ieee80211_frag_cache_find(ieee, seq, -1, tid,hdr->addr2,
199                                           hdr->addr1);
200
201         if (entry == NULL) {
202                 IEEE80211_DEBUG_FRAG(
203                         "could not invalidate fragment cache "
204                         "entry (seq=%u)\n", seq);
205                 return -1;
206         }
207
208         entry->skb = NULL;
209         return 0;
210 }
211
212
213
214 /* ieee80211_rx_frame_mgtmt
215  *
216  * Responsible for handling management control frames
217  *
218  * Called by ieee80211_rx */
219 static inline int
220 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
221                         struct ieee80211_rx_stats *rx_stats, u16 type,
222                         u16 stype)
223 {
224         /* On the struct stats definition there is written that
225          * this is not mandatory.... but seems that the probe
226          * response parser uses it
227          */
228         struct ieee80211_hdr_3addr * hdr = (struct ieee80211_hdr_3addr *)skb->data;
229
230         rx_stats->len = skb->len;
231         ieee80211_rx_mgt(ieee,(struct ieee80211_hdr_4addr *)skb->data,rx_stats);
232         //if ((ieee->state == IEEE80211_LINKED) && (memcmp(hdr->addr3, ieee->current_network.bssid, ETH_ALEN)))
233         if ((memcmp(hdr->addr1, ieee->dev->dev_addr, ETH_ALEN)))//use ADDR1 to perform address matching for Management frames
234         {
235                 dev_kfree_skb_any(skb);
236                 return 0;
237         }
238
239         ieee80211_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
240
241         dev_kfree_skb_any(skb);
242
243         return 0;
244
245         #ifdef NOT_YET
246         if (ieee->iw_mode == IW_MODE_MASTER) {
247                 printk(KERN_DEBUG "%s: Master mode not yet suppported.\n",
248                        ieee->dev->name);
249                 return 0;
250 /*
251   hostap_update_sta_ps(ieee, (struct hostap_ieee80211_hdr_4addr *)
252   skb->data);*/
253         }
254
255         if (ieee->hostapd && type == IEEE80211_TYPE_MGMT) {
256                 if (stype == WLAN_FC_STYPE_BEACON &&
257                     ieee->iw_mode == IW_MODE_MASTER) {
258                         struct sk_buff *skb2;
259                         /* Process beacon frames also in kernel driver to
260                          * update STA(AP) table statistics */
261                         skb2 = skb_clone(skb, GFP_ATOMIC);
262                         if (skb2)
263                                 hostap_rx(skb2->dev, skb2, rx_stats);
264                 }
265
266                 /* send management frames to the user space daemon for
267                  * processing */
268                 ieee->apdevstats.rx_packets++;
269                 ieee->apdevstats.rx_bytes += skb->len;
270                 prism2_rx_80211(ieee->apdev, skb, rx_stats, PRISM2_RX_MGMT);
271                 return 0;
272         }
273
274             if (ieee->iw_mode == IW_MODE_MASTER) {
275                 if (type != WLAN_FC_TYPE_MGMT && type != WLAN_FC_TYPE_CTRL) {
276                         printk(KERN_DEBUG "%s: unknown management frame "
277                                "(type=0x%02x, stype=0x%02x) dropped\n",
278                                skb->dev->name, type, stype);
279                         return -1;
280                 }
281
282                 hostap_rx(skb->dev, skb, rx_stats);
283                 return 0;
284         }
285
286         printk(KERN_DEBUG "%s: hostap_rx_frame_mgmt: management frame "
287                "received in non-Host AP mode\n", skb->dev->name);
288         return -1;
289         #endif
290 }
291
292
293
294 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
295 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
296 static unsigned char rfc1042_header[] =
297 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
298 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
299 static unsigned char bridge_tunnel_header[] =
300 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
301 /* No encapsulation header if EtherType < 0x600 (=length) */
302
303 /* Called by ieee80211_rx_frame_decrypt */
304 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
305                                     struct sk_buff *skb, size_t hdrlen)
306 {
307         struct net_device *dev = ieee->dev;
308         u16 fc, ethertype;
309         struct ieee80211_hdr_4addr *hdr;
310         u8 *pos;
311
312         if (skb->len < 24)
313                 return 0;
314
315 #if 1
316         if (ieee->hwsec_active)
317         {
318                 cb_desc *tcb_desc = (cb_desc *)(skb->cb+ MAX_DEV_ADDR_SIZE);
319                 tcb_desc->bHwSec = 1;
320
321                 if(ieee->need_sw_enc)
322                         tcb_desc->bHwSec = 0;
323         }
324 #endif
325
326         hdr = (struct ieee80211_hdr_4addr *) skb->data;
327         fc = le16_to_cpu(hdr->frame_ctl);
328
329         /* check that the frame is unicast frame to us */
330         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
331             IEEE80211_FCTL_TODS &&
332             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
333             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
334                 /* ToDS frame with own addr BSSID and DA */
335         } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
336                    IEEE80211_FCTL_FROMDS &&
337                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
338                 /* FromDS frame with own addr as DA */
339         } else
340                 return 0;
341
342         if (skb->len < 24 + 8)
343                 return 0;
344
345         /* check for port access entity Ethernet type */
346 //      pos = skb->data + 24;
347         pos = skb->data + hdrlen;
348         ethertype = (pos[6] << 8) | pos[7];
349         if (ethertype == ETH_P_PAE)
350                 return 1;
351
352         return 0;
353 }
354
355 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
356 static inline int
357 ieee80211_rx_frame_decrypt(struct ieee80211_device* ieee, struct sk_buff *skb,
358                            struct ieee80211_crypt_data *crypt)
359 {
360         struct ieee80211_hdr_4addr *hdr;
361         int res, hdrlen;
362
363         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
364                 return 0;
365 #if 1
366         if (ieee->hwsec_active)
367         {
368                 cb_desc *tcb_desc = (cb_desc *)(skb->cb+ MAX_DEV_ADDR_SIZE);
369                 tcb_desc->bHwSec = 1;
370         }
371 #endif
372         hdr = (struct ieee80211_hdr_4addr *) skb->data;
373         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
374
375 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
376         if (ieee->tkip_countermeasures &&
377             strcmp(crypt->ops->name, "TKIP") == 0) {
378                 if (net_ratelimit()) {
379                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
380                                "received packet from %pM\n",
381                                ieee->dev->name, hdr->addr2);
382                 }
383                 return -1;
384         }
385 #endif
386
387         atomic_inc(&crypt->refcnt);
388         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
389         atomic_dec(&crypt->refcnt);
390         if (res < 0) {
391                 IEEE80211_DEBUG_DROP(
392                         "decryption failed (SA=%pM"
393                         ") res=%d\n", hdr->addr2, res);
394                 if (res == -2)
395                         IEEE80211_DEBUG_DROP("Decryption failed ICV "
396                                              "mismatch (key %d)\n",
397                                              skb->data[hdrlen + 3] >> 6);
398                 ieee->ieee_stats.rx_discards_undecryptable++;
399                 return -1;
400         }
401
402         return res;
403 }
404
405
406 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
407 static inline int
408 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device* ieee, struct sk_buff *skb,
409                              int keyidx, struct ieee80211_crypt_data *crypt)
410 {
411         struct ieee80211_hdr_4addr *hdr;
412         int res, hdrlen;
413
414         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
415                 return 0;
416         if (ieee->hwsec_active)
417         {
418                 cb_desc *tcb_desc = (cb_desc *)(skb->cb+ MAX_DEV_ADDR_SIZE);
419                 tcb_desc->bHwSec = 1;
420
421                 if(ieee->need_sw_enc)
422                         tcb_desc->bHwSec = 0;
423
424         }
425
426         hdr = (struct ieee80211_hdr_4addr *) skb->data;
427         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
428
429         atomic_inc(&crypt->refcnt);
430         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
431         atomic_dec(&crypt->refcnt);
432         if (res < 0) {
433                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
434                        " (SA=%pM keyidx=%d)\n",
435                        ieee->dev->name, hdr->addr2, keyidx);
436                 return -1;
437         }
438
439         return 0;
440 }
441
442
443 /* this function is stolen from ipw2200 driver*/
444 #define IEEE_PACKET_RETRY_TIME (5*HZ)
445 static int is_duplicate_packet(struct ieee80211_device *ieee,
446                                       struct ieee80211_hdr_4addr *header)
447 {
448         u16 fc = le16_to_cpu(header->frame_ctl);
449         u16 sc = le16_to_cpu(header->seq_ctl);
450         u16 seq = WLAN_GET_SEQ_SEQ(sc);
451         u16 frag = WLAN_GET_SEQ_FRAG(sc);
452         u16 *last_seq, *last_frag;
453         unsigned long *last_time;
454         struct ieee80211_hdr_3addrqos *hdr_3addrqos;
455         struct ieee80211_hdr_4addrqos *hdr_4addrqos;
456         u8 tid;
457
458
459         //TO2DS and QoS
460         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
461           hdr_4addrqos = (struct ieee80211_hdr_4addrqos *)header;
462           tid = le16_to_cpu(hdr_4addrqos->qos_ctl) & IEEE80211_QCTL_TID;
463           tid = UP2AC(tid);
464           tid ++;
465         } else if(IEEE80211_QOS_HAS_SEQ(fc)) { //QoS
466           hdr_3addrqos = (struct ieee80211_hdr_3addrqos*)header;
467           tid = le16_to_cpu(hdr_3addrqos->qos_ctl) & IEEE80211_QCTL_TID;
468           tid = UP2AC(tid);
469           tid ++;
470         } else { // no QoS
471           tid = 0;
472         }
473
474         switch (ieee->iw_mode) {
475         case IW_MODE_ADHOC:
476         {
477                 struct list_head *p;
478                 struct ieee_ibss_seq *entry = NULL;
479                 u8 *mac = header->addr2;
480                 int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
481                 //for (pos = (head)->next; pos != (head); pos = pos->next)
482                 //__list_for_each(p, &ieee->ibss_mac_hash[index]) {
483                 list_for_each(p, &ieee->ibss_mac_hash[index]) {
484                         entry = list_entry(p, struct ieee_ibss_seq, list);
485                         if (!memcmp(entry->mac, mac, ETH_ALEN))
486                                 break;
487                 }
488         //      if (memcmp(entry->mac, mac, ETH_ALEN)){
489                 if (p == &ieee->ibss_mac_hash[index]) {
490                         entry = kmalloc(sizeof(struct ieee_ibss_seq), GFP_ATOMIC);
491                         if (!entry) {
492                                 printk(KERN_WARNING "Cannot malloc new mac entry\n");
493                                 return 0;
494                         }
495                         memcpy(entry->mac, mac, ETH_ALEN);
496                         entry->seq_num[tid] = seq;
497                         entry->frag_num[tid] = frag;
498                         entry->packet_time[tid] = jiffies;
499                         list_add(&entry->list, &ieee->ibss_mac_hash[index]);
500                         return 0;
501                 }
502                 last_seq = &entry->seq_num[tid];
503                 last_frag = &entry->frag_num[tid];
504                 last_time = &entry->packet_time[tid];
505                 break;
506         }
507
508         case IW_MODE_INFRA:
509                 last_seq = &ieee->last_rxseq_num[tid];
510                 last_frag = &ieee->last_rxfrag_num[tid];
511                 last_time = &ieee->last_packet_time[tid];
512
513                 break;
514         default:
515                 return 0;
516         }
517
518 //      if(tid != 0) {
519 //              printk(KERN_WARNING ":)))))))))))%x %x %x, fc(%x)\n", tid, *last_seq, seq, header->frame_ctl);
520 //      }
521         if ((*last_seq == seq) &&
522             time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
523                 if (*last_frag == frag){
524                         //printk(KERN_WARNING "[1] go drop!\n");
525                         goto drop;
526
527                 }
528                 if (*last_frag + 1 != frag)
529                         /* out-of-order fragment */
530                         //printk(KERN_WARNING "[2] go drop!\n");
531                         goto drop;
532         } else
533                 *last_seq = seq;
534
535         *last_frag = frag;
536         *last_time = jiffies;
537         return 0;
538
539 drop:
540 //      BUG_ON(!(fc & IEEE80211_FCTL_RETRY));
541 //      printk("DUP\n");
542
543         return 1;
544 }
545 bool
546 AddReorderEntry(
547         PRX_TS_RECORD                   pTS,
548         PRX_REORDER_ENTRY               pReorderEntry
549         )
550 {
551         struct list_head *pList = &pTS->RxPendingPktList;
552 #if  1
553         while(pList->next != &pTS->RxPendingPktList)
554         {
555                 if( SN_LESS(pReorderEntry->SeqNum, ((PRX_REORDER_ENTRY)list_entry(pList->next,RX_REORDER_ENTRY,List))->SeqNum) )
556                 {
557                         pList = pList->next;
558                 }
559                 else if( SN_EQUAL(pReorderEntry->SeqNum, ((PRX_REORDER_ENTRY)list_entry(pList->next,RX_REORDER_ENTRY,List))->SeqNum) )
560                 {
561                         return false;
562                 }
563                 else
564                 {
565                         break;
566                 }
567         }
568 #endif
569         pReorderEntry->List.next = pList->next;
570         pReorderEntry->List.next->prev = &pReorderEntry->List;
571         pReorderEntry->List.prev = pList;
572         pList->next = &pReorderEntry->List;
573
574         return true;
575 }
576
577 void ieee80211_indicate_packets(struct ieee80211_device *ieee, struct ieee80211_rxb** prxbIndicateArray,u8  index)
578 {
579         u8 i = 0 , j=0;
580         u16 ethertype;
581 //      if(index > 1)
582 //              IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): hahahahhhh, We indicate packet from reorder list, index is %u\n",__FUNCTION__,index);
583         for(j = 0; j<index; j++)
584         {
585 //added by amy for reorder
586                 struct ieee80211_rxb* prxb = prxbIndicateArray[j];
587                 for(i = 0; i<prxb->nr_subframes; i++) {
588                         struct sk_buff *sub_skb = prxb->subframes[i];
589
590                 /* convert hdr + possible LLC headers into Ethernet header */
591                         ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
592                         if (sub_skb->len >= 8 &&
593                                 ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
594                                   ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
595                                  memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
596                         /* remove RFC1042 or Bridge-Tunnel encapsulation and
597                          * replace EtherType */
598                                 skb_pull(sub_skb, SNAP_SIZE);
599                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
600                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
601                         } else {
602                                 u16 len;
603                         /* Leave Ethernet header part of hdr and full payload */
604                                 len = htons(sub_skb->len);
605                                 memcpy(skb_push(sub_skb, 2), &len, 2);
606                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->src, ETH_ALEN);
607                                 memcpy(skb_push(sub_skb, ETH_ALEN), prxb->dst, ETH_ALEN);
608                         }
609                         //stats->rx_packets++;
610                         //stats->rx_bytes += sub_skb->len;
611
612                 /* Indicat the packets to upper layer */
613                         if (sub_skb) {
614                                 //printk("0skb_len(%d)\n", skb->len);
615                                 sub_skb->protocol = eth_type_trans(sub_skb, ieee->dev);
616                                 memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
617                                 sub_skb->dev = ieee->dev;
618                                 sub_skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
619                                 //skb->ip_summed = CHECKSUM_UNNECESSARY; /* 802.11 crc not sufficient */
620                                 ieee->last_rx_ps_time = jiffies;
621                                 //printk("1skb_len(%d)\n", skb->len);
622                                 netif_rx(sub_skb);
623                         }
624                 }
625                 kfree(prxb);
626                 prxb = NULL;
627         }
628 }
629
630
631 void RxReorderIndicatePacket( struct ieee80211_device *ieee,
632                 struct ieee80211_rxb* prxb,
633                 PRX_TS_RECORD           pTS,
634                 u16                     SeqNum)
635 {
636         PRT_HIGH_THROUGHPUT     pHTInfo = ieee->pHTInfo;
637         PRX_REORDER_ENTRY       pReorderEntry = NULL;
638         struct ieee80211_rxb* prxbIndicateArray[REORDER_WIN_SIZE];
639         u8                      WinSize = pHTInfo->RxReorderWinSize;
640         u16                     WinEnd = (pTS->RxIndicateSeq + WinSize -1)%4096;
641         u8                      index = 0;
642         bool                    bMatchWinStart = false, bPktInBuf = false;
643         IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): Seq is %d,pTS->RxIndicateSeq is %d, WinSize is %d\n",__FUNCTION__,SeqNum,pTS->RxIndicateSeq,WinSize);
644 #if 0
645         if(!list_empty(&ieee->RxReorder_Unused_List))
646                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): ieee->RxReorder_Unused_List is nut NULL\n");
647 #endif
648         /* Rx Reorder initialize condition.*/
649         if(pTS->RxIndicateSeq == 0xffff) {
650                 pTS->RxIndicateSeq = SeqNum;
651         }
652
653         /* Drop out the packet which SeqNum is smaller than WinStart */
654         if(SN_LESS(SeqNum, pTS->RxIndicateSeq)) {
655                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,"Packet Drop! IndicateSeq: %d, NewSeq: %d\n",
656                                  pTS->RxIndicateSeq, SeqNum);
657                 pHTInfo->RxReorderDropCounter++;
658                 {
659                         int i;
660                         for(i =0; i < prxb->nr_subframes; i++) {
661                                 dev_kfree_skb(prxb->subframes[i]);
662                         }
663                         kfree(prxb);
664                         prxb = NULL;
665                 }
666                 return;
667         }
668
669         /*
670          * Sliding window manipulation. Conditions includes:
671          * 1. Incoming SeqNum is equal to WinStart =>Window shift 1
672          * 2. Incoming SeqNum is larger than the WinEnd => Window shift N
673          */
674         if(SN_EQUAL(SeqNum, pTS->RxIndicateSeq)) {
675                 pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
676                 bMatchWinStart = true;
677         } else if(SN_LESS(WinEnd, SeqNum)) {
678                 if(SeqNum >= (WinSize - 1)) {
679                         pTS->RxIndicateSeq = SeqNum + 1 -WinSize;
680                 } else {
681                         pTS->RxIndicateSeq = 4095 - (WinSize - (SeqNum +1)) + 1;
682                 }
683                 IEEE80211_DEBUG(IEEE80211_DL_REORDER, "Window Shift! IndicateSeq: %d, NewSeq: %d\n",pTS->RxIndicateSeq, SeqNum);
684         }
685
686         /*
687          * Indication process.
688          * After Packet dropping and Sliding Window shifting as above, we can now just indicate the packets
689          * with the SeqNum smaller than latest WinStart and buffer other packets.
690          */
691         /* For Rx Reorder condition:
692          * 1. All packets with SeqNum smaller than WinStart => Indicate
693          * 2. All packets with SeqNum larger than or equal to WinStart => Buffer it.
694          */
695         if(bMatchWinStart) {
696                 /* Current packet is going to be indicated.*/
697                 IEEE80211_DEBUG(IEEE80211_DL_REORDER, "Packets indication!! IndicateSeq: %d, NewSeq: %d\n",\
698                                 pTS->RxIndicateSeq, SeqNum);
699                 prxbIndicateArray[0] = prxb;
700 //              printk("========================>%s(): SeqNum is %d\n",__FUNCTION__,SeqNum);
701                 index = 1;
702         } else {
703                 /* Current packet is going to be inserted into pending list.*/
704                 //IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): We RX no ordered packed, insert to orderd list\n",__FUNCTION__);
705                 if(!list_empty(&ieee->RxReorder_Unused_List)) {
706                         pReorderEntry = (PRX_REORDER_ENTRY)list_entry(ieee->RxReorder_Unused_List.next,RX_REORDER_ENTRY,List);
707                         list_del_init(&pReorderEntry->List);
708
709                         /* Make a reorder entry and insert into a the packet list.*/
710                         pReorderEntry->SeqNum = SeqNum;
711                         pReorderEntry->prxb = prxb;
712         //              IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): pREorderEntry->SeqNum is %d\n",__FUNCTION__,pReorderEntry->SeqNum);
713
714 #if 1
715                         if(!AddReorderEntry(pTS, pReorderEntry)) {
716                                 IEEE80211_DEBUG(IEEE80211_DL_REORDER, "%s(): Duplicate packet is dropped!! IndicateSeq: %d, NewSeq: %d\n",
717                                         __FUNCTION__, pTS->RxIndicateSeq, SeqNum);
718                                 list_add_tail(&pReorderEntry->List,&ieee->RxReorder_Unused_List);
719                                 {
720                                         int i;
721                                         for(i =0; i < prxb->nr_subframes; i++) {
722                                                 dev_kfree_skb(prxb->subframes[i]);
723                                         }
724                                         kfree(prxb);
725                                         prxb = NULL;
726                                 }
727                         } else {
728                                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,
729                                          "Pkt insert into buffer!! IndicateSeq: %d, NewSeq: %d\n",pTS->RxIndicateSeq, SeqNum);
730                         }
731 #endif
732                 }
733                 else {
734                         /*
735                          * Packets are dropped if there is not enough reorder entries.
736                          * This part shall be modified!! We can just indicate all the
737                          * packets in buffer and get reorder entries.
738                          */
739                         IEEE80211_DEBUG(IEEE80211_DL_ERR, "RxReorderIndicatePacket(): There is no reorder entry!! Packet is dropped!!\n");
740                         {
741                                 int i;
742                                 for(i =0; i < prxb->nr_subframes; i++) {
743                                         dev_kfree_skb(prxb->subframes[i]);
744                                 }
745                                 kfree(prxb);
746                                 prxb = NULL;
747                         }
748                 }
749         }
750
751         /* Check if there is any packet need indicate.*/
752         while(!list_empty(&pTS->RxPendingPktList)) {
753                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): start RREORDER indicate\n",__FUNCTION__);
754 #if 1
755                 pReorderEntry = (PRX_REORDER_ENTRY)list_entry(pTS->RxPendingPktList.prev,RX_REORDER_ENTRY,List);
756                 if( SN_LESS(pReorderEntry->SeqNum, pTS->RxIndicateSeq) ||
757                                 SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
758                 {
759                         /* This protect buffer from overflow. */
760                         if(index >= REORDER_WIN_SIZE) {
761                                 IEEE80211_DEBUG(IEEE80211_DL_ERR, "RxReorderIndicatePacket(): Buffer overflow!! \n");
762                                 bPktInBuf = true;
763                                 break;
764                         }
765
766                         list_del_init(&pReorderEntry->List);
767
768                         if(SN_EQUAL(pReorderEntry->SeqNum, pTS->RxIndicateSeq))
769                                 pTS->RxIndicateSeq = (pTS->RxIndicateSeq + 1) % 4096;
770
771                         IEEE80211_DEBUG(IEEE80211_DL_REORDER,"Packets indication!! IndicateSeq: %d, NewSeq: %d\n",pTS->RxIndicateSeq, SeqNum);
772                         prxbIndicateArray[index] = pReorderEntry->prxb;
773                 //      printk("========================>%s(): pReorderEntry->SeqNum is %d\n",__FUNCTION__,pReorderEntry->SeqNum);
774                         index++;
775
776                         list_add_tail(&pReorderEntry->List,&ieee->RxReorder_Unused_List);
777                 } else {
778                         bPktInBuf = true;
779                         break;
780                 }
781 #endif
782         }
783
784         /* Handling pending timer. Set this timer to prevent from long time Rx buffering.*/
785         if(index>0) {
786                 // Cancel previous pending timer.
787                 if (timer_pending(&pTS->RxPktPendingTimer))
788                         del_timer_sync(&pTS->RxPktPendingTimer);
789                 pTS->RxTimeoutIndicateSeq = 0xffff;
790
791                 // Indicate packets
792                 if(index>REORDER_WIN_SIZE){
793                         IEEE80211_DEBUG(IEEE80211_DL_ERR, "RxReorderIndicatePacket(): Rx Reorer buffer full!! \n");
794                         return;
795                 }
796                 ieee80211_indicate_packets(ieee, prxbIndicateArray, index);
797                 bPktInBuf = false;
798         }
799
800 #if 1
801         if(bPktInBuf && pTS->RxTimeoutIndicateSeq==0xffff) {
802                 // Set new pending timer.
803                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): SET rx timeout timer\n", __FUNCTION__);
804                 pTS->RxTimeoutIndicateSeq = pTS->RxIndicateSeq;
805 #if 0
806                 if(timer_pending(&pTS->RxPktPendingTimer))
807                         del_timer_sync(&pTS->RxPktPendingTimer);
808                 pTS->RxPktPendingTimer.expires = jiffies + MSECS(pHTInfo->RxReorderPendingTime);
809                 add_timer(&pTS->RxPktPendingTimer);
810 #else
811                 mod_timer(&pTS->RxPktPendingTimer,  jiffies + MSECS(pHTInfo->RxReorderPendingTime));
812 #endif
813         }
814 #endif
815 }
816
817 u8 parse_subframe(struct ieee80211_device* ieee,struct sk_buff *skb,
818                   struct ieee80211_rx_stats *rx_stats,
819                   struct ieee80211_rxb *rxb,u8* src,u8* dst)
820 {
821         struct ieee80211_hdr_3addr  *hdr = (struct ieee80211_hdr_3addr* )skb->data;
822         u16             fc = le16_to_cpu(hdr->frame_ctl);
823
824         u16             LLCOffset= sizeof(struct ieee80211_hdr_3addr);
825         u16             ChkLength;
826         bool            bIsAggregateFrame = false;
827         u16             nSubframe_Length;
828         u8              nPadding_Length = 0;
829         u16             SeqNum=0;
830
831         struct sk_buff *sub_skb;
832         u8             *data_ptr;
833         /* just for debug purpose */
834         SeqNum = WLAN_GET_SEQ_SEQ(le16_to_cpu(hdr->seq_ctl));
835
836         if((IEEE80211_QOS_HAS_SEQ(fc))&&\
837                         (((frameqos *)(skb->data + IEEE80211_3ADDR_LEN))->field.reserved)) {
838                 bIsAggregateFrame = true;
839         }
840
841         if(IEEE80211_QOS_HAS_SEQ(fc)) {
842                 LLCOffset += 2;
843         }
844
845         if(rx_stats->bContainHTC) {
846                 LLCOffset += sHTCLng;
847         }
848         //printk("ChkLength = %d\n", LLCOffset);
849         // Null packet, don't indicate it to upper layer
850         ChkLength = LLCOffset;/* + (Frame_WEP(frame)!=0 ?Adapter->MgntInfo.SecurityInfo.EncryptionHeadOverhead:0);*/
851
852         if( skb->len <= ChkLength ) {
853                 return 0;
854         }
855
856         skb_pull(skb, LLCOffset);
857         ieee->bIsAggregateFrame = bIsAggregateFrame;//added by amy for Leisure PS
858
859         if(!bIsAggregateFrame) {
860                 rxb->nr_subframes = 1;
861 #ifdef JOHN_NOCPY
862                 rxb->subframes[0] = skb;
863 #else
864                 rxb->subframes[0] = skb_copy(skb, GFP_ATOMIC);
865 #endif
866
867                 memcpy(rxb->src,src,ETH_ALEN);
868                 memcpy(rxb->dst,dst,ETH_ALEN);
869                 //IEEE80211_DEBUG_DATA(IEEE80211_DL_RX,skb->data,skb->len);
870                 return 1;
871         } else {
872                 rxb->nr_subframes = 0;
873                 memcpy(rxb->src,src,ETH_ALEN);
874                 memcpy(rxb->dst,dst,ETH_ALEN);
875                 while(skb->len > ETHERNET_HEADER_SIZE) {
876                         /* Offset 12 denote 2 mac address */
877                         nSubframe_Length = *((u16*)(skb->data + 12));
878                         //==m==>change the length order
879                         nSubframe_Length = (nSubframe_Length>>8) + (nSubframe_Length<<8);
880
881                         if(skb->len<(ETHERNET_HEADER_SIZE + nSubframe_Length)) {
882 #if 0//cosa
883                                 RT_ASSERT(
884                                                 (nRemain_Length>=(ETHERNET_HEADER_SIZE + nSubframe_Length)),
885                                                 ("ParseSubframe(): A-MSDU subframe parse error!! Subframe Length: %d\n", nSubframe_Length) );
886 #endif
887                                 printk("%s: A-MSDU parse error!! pRfd->nTotalSubframe : %d\n",\
888                                                 __FUNCTION__,rxb->nr_subframes);
889                                 printk("%s: A-MSDU parse error!! Subframe Length: %d\n",__FUNCTION__, nSubframe_Length);
890                                 printk("nRemain_Length is %d and nSubframe_Length is : %d\n",skb->len,nSubframe_Length);
891                                 printk("The Packet SeqNum is %d\n",SeqNum);
892                                 return 0;
893                         }
894
895                         /* move the data point to data content */
896                         skb_pull(skb, ETHERNET_HEADER_SIZE);
897
898 #ifdef JOHN_NOCPY
899                         sub_skb = skb_clone(skb, GFP_ATOMIC);
900                         sub_skb->len = nSubframe_Length;
901                         sub_skb->tail = sub_skb->data + nSubframe_Length;
902 #else
903                         /* Allocate new skb for releasing to upper layer */
904                         sub_skb = dev_alloc_skb(nSubframe_Length + 12);
905                         skb_reserve(sub_skb, 12);
906                         data_ptr = (u8 *)skb_put(sub_skb, nSubframe_Length);
907                         memcpy(data_ptr,skb->data,nSubframe_Length);
908 #endif
909                         rxb->subframes[rxb->nr_subframes++] = sub_skb;
910                         if(rxb->nr_subframes >= MAX_SUBFRAME_COUNT) {
911                                 IEEE80211_DEBUG_RX("ParseSubframe(): Too many Subframes! Packets dropped!\n");
912                                 break;
913                         }
914                         skb_pull(skb,nSubframe_Length);
915
916                         if(skb->len != 0) {
917                                 nPadding_Length = 4 - ((nSubframe_Length + ETHERNET_HEADER_SIZE) % 4);
918                                 if(nPadding_Length == 4) {
919                                         nPadding_Length = 0;
920                                 }
921
922                                 if(skb->len < nPadding_Length) {
923                                         return 0;
924                                 }
925
926                                 skb_pull(skb,nPadding_Length);
927                         }
928                 }
929 #ifdef JOHN_NOCPY
930                 dev_kfree_skb(skb);
931 #endif
932                 //{just for debug added by david
933                 //printk("AMSDU::rxb->nr_subframes = %d\n",rxb->nr_subframes);
934                 //}
935                 return rxb->nr_subframes;
936         }
937 }
938
939 /* All received frames are sent to this function. @skb contains the frame in
940  * IEEE 802.11 format, i.e., in the format it was sent over air.
941  * This function is called only as a tasklet (software IRQ). */
942 int ieee80211_rtl_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
943                  struct ieee80211_rx_stats *rx_stats)
944 {
945         struct net_device *dev = ieee->dev;
946         struct ieee80211_hdr_4addr *hdr;
947         //struct ieee80211_hdr_3addrqos *hdr;
948
949         size_t hdrlen;
950         u16 fc, type, stype, sc;
951         struct net_device_stats *stats;
952         unsigned int frag;
953         u8 *payload;
954         u16 ethertype;
955         //added by amy for reorder
956         u8      TID = 0;
957         u16     SeqNum = 0;
958         PRX_TS_RECORD pTS = NULL;
959         bool unicast_packet = false;
960         //bool bIsAggregateFrame = false;
961         //added by amy for reorder
962 #ifdef NOT_YET
963         struct net_device *wds = NULL;
964         struct sk_buff *skb2 = NULL;
965         struct net_device *wds = NULL;
966         int frame_authorized = 0;
967         int from_assoc_ap = 0;
968         void *sta = NULL;
969 #endif
970 //      u16 qos_ctl = 0;
971         u8 dst[ETH_ALEN];
972         u8 src[ETH_ALEN];
973         u8 bssid[ETH_ALEN];
974         struct ieee80211_crypt_data *crypt = NULL;
975         int keyidx = 0;
976
977         int i;
978         struct ieee80211_rxb* rxb = NULL;
979         // cheat the the hdr type
980         hdr = (struct ieee80211_hdr_4addr *)skb->data;
981         stats = &ieee->stats;
982
983         if (skb->len < 10) {
984                 printk(KERN_INFO "%s: SKB length < 10\n",
985                        dev->name);
986                 goto rx_dropped;
987         }
988
989         fc = le16_to_cpu(hdr->frame_ctl);
990         type = WLAN_FC_GET_TYPE(fc);
991         stype = WLAN_FC_GET_STYPE(fc);
992         sc = le16_to_cpu(hdr->seq_ctl);
993
994         frag = WLAN_GET_SEQ_FRAG(sc);
995         hdrlen = ieee80211_get_hdrlen(fc);
996
997         if(HTCCheck(ieee, skb->data))
998         {
999                 if(net_ratelimit())
1000                 printk("find HTCControl\n");
1001                 hdrlen += 4;
1002                 rx_stats->bContainHTC = 1;
1003         }
1004
1005         //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
1006 #ifdef NOT_YET
1007 #if WIRELESS_EXT > 15
1008         /* Put this code here so that we avoid duplicating it in all
1009          * Rx paths. - Jean II */
1010 #ifdef IW_WIRELESS_SPY          /* defined in iw_handler.h */
1011         /* If spy monitoring on */
1012         if (iface->spy_data.spy_number > 0) {
1013                 struct iw_quality wstats;
1014                 wstats.level = rx_stats->rssi;
1015                 wstats.noise = rx_stats->noise;
1016                 wstats.updated = 6;     /* No qual value */
1017                 /* Update spy records */
1018                 wireless_spy_update(dev, hdr->addr2, &wstats);
1019         }
1020 #endif /* IW_WIRELESS_SPY */
1021 #endif /* WIRELESS_EXT > 15 */
1022         hostap_update_rx_stats(local->ap, hdr, rx_stats);
1023 #endif
1024
1025 #if WIRELESS_EXT > 15
1026         if (ieee->iw_mode == IW_MODE_MONITOR) {
1027                 ieee80211_monitor_rx(ieee, skb, rx_stats);
1028                 stats->rx_packets++;
1029                 stats->rx_bytes += skb->len;
1030                 return 1;
1031         }
1032 #endif
1033         if (ieee->host_decrypt) {
1034                 int idx = 0;
1035                 if (skb->len >= hdrlen + 3)
1036                         idx = skb->data[hdrlen + 3] >> 6;
1037                 crypt = ieee->crypt[idx];
1038 #ifdef NOT_YET
1039                 sta = NULL;
1040
1041                 /* Use station specific key to override default keys if the
1042                  * receiver address is a unicast address ("individual RA"). If
1043                  * bcrx_sta_key parameter is set, station specific key is used
1044                  * even with broad/multicast targets (this is against IEEE
1045                  * 802.11, but makes it easier to use different keys with
1046                  * stations that do not support WEP key mapping). */
1047
1048                 if (!(hdr->addr1[0] & 0x01) || local->bcrx_sta_key)
1049                         (void) hostap_handle_sta_crypto(local, hdr, &crypt,
1050                                                         &sta);
1051 #endif
1052
1053                 /* allow NULL decrypt to indicate an station specific override
1054                  * for default encryption */
1055                 if (crypt && (crypt->ops == NULL ||
1056                               crypt->ops->decrypt_mpdu == NULL))
1057                         crypt = NULL;
1058
1059                 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
1060                         /* This seems to be triggered by some (multicast?)
1061                          * frames from other than current BSS, so just drop the
1062                          * frames silently instead of filling system log with
1063                          * these reports. */
1064                         IEEE80211_DEBUG_DROP("Decryption failed (not set)"
1065                                              " (SA=%pM)\n",
1066                                              hdr->addr2);
1067                         ieee->ieee_stats.rx_discards_undecryptable++;
1068                         goto rx_dropped;
1069                 }
1070         }
1071
1072         if (skb->len < IEEE80211_DATA_HDR3_LEN)
1073                 goto rx_dropped;
1074
1075         // if QoS enabled, should check the sequence for each of the AC
1076         if( (ieee->pHTInfo->bCurRxReorderEnable == false) || !ieee->current_network.qos_data.active|| !IsDataFrame(skb->data) || IsLegacyDataFrame(skb->data)){
1077                 if (is_duplicate_packet(ieee, hdr))
1078                 goto rx_dropped;
1079
1080         }
1081         else
1082         {
1083                 PRX_TS_RECORD pRxTS = NULL;
1084         #if 0
1085                 struct ieee80211_hdr_3addr *hdr;
1086                 u16 fc;
1087                 hdr = (struct ieee80211_hdr_3addr *)skb->data;
1088                 fc = le16_to_cpu(hdr->frame_ctl);
1089                 u8 tmp = (fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS);
1090
1091                 u8 tid = (*((u8*)skb->data + (((fc& IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))?30:24)))&0xf;
1092                 printk("====================>fc:%x, tid:%d, tmp:%d\n", fc, tid, tmp);
1093                 //u8 tid =  (u8)((frameqos*)(buf + ((fc & IEEE80211_FCTL_TODS)&&(fc & IEEE80211_FCTL_FROMDS))? 30 : 24))->field.tid;
1094         #endif
1095                         //IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): QOS ENABLE AND RECEIVE QOS DATA , we will get Ts, tid:%d\n",__FUNCTION__, tid);
1096 #if 1
1097                 if(GetTs(
1098                                 ieee,
1099                                 (PTS_COMMON_INFO*) &pRxTS,
1100                                 hdr->addr2,
1101                                 (u8)Frame_QoSTID((u8*)(skb->data)),
1102                                 RX_DIR,
1103                                 true))
1104                 {
1105
1106                 //      IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): pRxTS->RxLastFragNum is %d,frag is %d,pRxTS->RxLastSeqNum is %d,seq is %d\n",__FUNCTION__,pRxTS->RxLastFragNum,frag,pRxTS->RxLastSeqNum,WLAN_GET_SEQ_SEQ(sc));
1107                         if(     (fc & (1<<11))  &&
1108                                         (frag == pRxTS->RxLastFragNum) &&
1109                                         (WLAN_GET_SEQ_SEQ(sc) == pRxTS->RxLastSeqNum)   )
1110                         {
1111                                 goto rx_dropped;
1112                         }
1113                         else
1114                         {
1115                                 pRxTS->RxLastFragNum = frag;
1116                                 pRxTS->RxLastSeqNum = WLAN_GET_SEQ_SEQ(sc);
1117                         }
1118                 }
1119                 else
1120                 {
1121                         IEEE80211_DEBUG(IEEE80211_DL_ERR, "%s(): No TS!! Skip the check!!\n",__FUNCTION__);
1122                         goto rx_dropped;
1123                 }
1124         }
1125 #endif
1126         if (type == IEEE80211_FTYPE_MGMT) {
1127
1128         #if 0
1129                 if ( stype == IEEE80211_STYPE_AUTH &&
1130                     fc & IEEE80211_FCTL_WEP && ieee->host_decrypt &&
1131                     (keyidx = hostap_rx_frame_decrypt(ieee, skb, crypt)) < 0)
1132                 {
1133                         printk(KERN_DEBUG "%s: failed to decrypt mgmt::auth "
1134                                "from %pM\n", dev->name,
1135                                hdr->addr2);
1136                         /* TODO: could inform hostapd about this so that it
1137                          * could send auth failure report */
1138                         goto rx_dropped;
1139                 }
1140         #endif
1141
1142         //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
1143                 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
1144                         goto rx_dropped;
1145                 else
1146                         goto rx_exit;
1147         }
1148
1149         /* Data frame - extract src/dst addresses */
1150         switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
1151         case IEEE80211_FCTL_FROMDS:
1152                 memcpy(dst, hdr->addr1, ETH_ALEN);
1153                 memcpy(src, hdr->addr3, ETH_ALEN);
1154                 memcpy(bssid, hdr->addr2, ETH_ALEN);
1155                 break;
1156         case IEEE80211_FCTL_TODS:
1157                 memcpy(dst, hdr->addr3, ETH_ALEN);
1158                 memcpy(src, hdr->addr2, ETH_ALEN);
1159                 memcpy(bssid, hdr->addr1, ETH_ALEN);
1160                 break;
1161         case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
1162                 if (skb->len < IEEE80211_DATA_HDR4_LEN)
1163                         goto rx_dropped;
1164                 memcpy(dst, hdr->addr3, ETH_ALEN);
1165                 memcpy(src, hdr->addr4, ETH_ALEN);
1166                 memcpy(bssid, ieee->current_network.bssid, ETH_ALEN);
1167                 break;
1168         case 0:
1169                 memcpy(dst, hdr->addr1, ETH_ALEN);
1170                 memcpy(src, hdr->addr2, ETH_ALEN);
1171                 memcpy(bssid, hdr->addr3, ETH_ALEN);
1172                 break;
1173         }
1174
1175 #ifdef NOT_YET
1176         if (hostap_rx_frame_wds(ieee, hdr, fc, &wds))
1177                 goto rx_dropped;
1178         if (wds) {
1179                 skb->dev = dev = wds;
1180                 stats = hostap_get_stats(dev);
1181         }
1182
1183         if (ieee->iw_mode == IW_MODE_MASTER && !wds &&
1184             (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == IEEE80211_FCTL_FROMDS &&
1185             ieee->stadev &&
1186             memcmp(hdr->addr2, ieee->assoc_ap_addr, ETH_ALEN) == 0) {
1187                 /* Frame from BSSID of the AP for which we are a client */
1188                 skb->dev = dev = ieee->stadev;
1189                 stats = hostap_get_stats(dev);
1190                 from_assoc_ap = 1;
1191         }
1192 #endif
1193
1194         dev->last_rx = jiffies;
1195
1196 #ifdef NOT_YET
1197         if ((ieee->iw_mode == IW_MODE_MASTER ||
1198              ieee->iw_mode == IW_MODE_REPEAT) &&
1199             !from_assoc_ap) {
1200                 switch (hostap_handle_sta_rx(ieee, dev, skb, rx_stats,
1201                                              wds != NULL)) {
1202                 case AP_RX_CONTINUE_NOT_AUTHORIZED:
1203                         frame_authorized = 0;
1204                         break;
1205                 case AP_RX_CONTINUE:
1206                         frame_authorized = 1;
1207                         break;
1208                 case AP_RX_DROP:
1209                         goto rx_dropped;
1210                 case AP_RX_EXIT:
1211                         goto rx_exit;
1212                 }
1213         }
1214 #endif
1215         //IEEE80211_DEBUG_DATA(IEEE80211_DL_DATA, skb->data, skb->len);
1216         /* Nullfunc frames may have PS-bit set, so they must be passed to
1217          * hostap_handle_sta_rx() before being dropped here. */
1218         if (stype != IEEE80211_STYPE_DATA &&
1219             stype != IEEE80211_STYPE_DATA_CFACK &&
1220             stype != IEEE80211_STYPE_DATA_CFPOLL &&
1221             stype != IEEE80211_STYPE_DATA_CFACKPOLL&&
1222             stype != IEEE80211_STYPE_QOS_DATA//add by David,2006.8.4
1223             ) {
1224                 if (stype != IEEE80211_STYPE_NULLFUNC)
1225                         IEEE80211_DEBUG_DROP(
1226                                 "RX: dropped data frame "
1227                                 "with no data (type=0x%02x, "
1228                                 "subtype=0x%02x, len=%d)\n",
1229                                 type, stype, skb->len);
1230                 goto rx_dropped;
1231         }
1232         if (memcmp(bssid, ieee->current_network.bssid, ETH_ALEN))
1233                 goto rx_dropped;
1234
1235 #ifdef ENABLE_LPS
1236         if ((ieee->iw_mode == IW_MODE_INFRA)  && (ieee->sta_sleep == 1)
1237                 && (ieee->polling)) {
1238                 if (WLAN_FC_MORE_DATA(fc)) {
1239                         /* more data bit is set, let's request a new frame from the AP */
1240                         ieee80211_sta_ps_send_pspoll_frame(ieee);
1241                 } else {
1242                         ieee->polling =  false;
1243                 }
1244         }
1245 #endif
1246
1247         ieee->need_sw_enc = 0;
1248
1249         if((!rx_stats->Decrypted)){
1250                 ieee->need_sw_enc = 1;
1251         }
1252
1253         /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
1254
1255         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
1256             (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
1257         {
1258                 printk("decrypt frame error\n");
1259                 goto rx_dropped;
1260         }
1261
1262
1263         hdr = (struct ieee80211_hdr_4addr *) skb->data;
1264
1265         /* skb: hdr + (possibly fragmented) plaintext payload */
1266         // PR: FIXME: hostap has additional conditions in the "if" below:
1267         // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
1268         if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
1269                 int flen;
1270                 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
1271                 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
1272
1273                 if (!frag_skb) {
1274                         IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
1275                                         "Rx cannot get skb from fragment "
1276                                         "cache (morefrag=%d seq=%u frag=%u)\n",
1277                                         (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
1278                                         WLAN_GET_SEQ_SEQ(sc), frag);
1279                         goto rx_dropped;
1280                 }
1281                 flen = skb->len;
1282                 if (frag != 0)
1283                         flen -= hdrlen;
1284
1285                 if (frag_skb->tail + flen > frag_skb->end) {
1286                         printk(KERN_WARNING "%s: host decrypted and "
1287                                "reassembled frame did not fit skb\n",
1288                                dev->name);
1289                         ieee80211_frag_cache_invalidate(ieee, hdr);
1290                         goto rx_dropped;
1291                 }
1292
1293                 if (frag == 0) {
1294                         /* copy first fragment (including full headers) into
1295                          * beginning of the fragment cache skb */
1296                         memcpy(skb_put(frag_skb, flen), skb->data, flen);
1297                 } else {
1298                         /* append frame payload to the end of the fragment
1299                          * cache skb */
1300                         memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
1301                                flen);
1302                 }
1303                 dev_kfree_skb_any(skb);
1304                 skb = NULL;
1305
1306                 if (fc & IEEE80211_FCTL_MOREFRAGS) {
1307                         /* more fragments expected - leave the skb in fragment
1308                          * cache for now; it will be delivered to upper layers
1309                          * after all fragments have been received */
1310                         goto rx_exit;
1311                 }
1312
1313                 /* this was the last fragment and the frame will be
1314                  * delivered, so remove skb from fragment cache */
1315                 skb = frag_skb;
1316                 hdr = (struct ieee80211_hdr_4addr *) skb->data;
1317                 ieee80211_frag_cache_invalidate(ieee, hdr);
1318         }
1319
1320         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
1321          * encrypted/authenticated */
1322         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
1323             ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
1324         {
1325                 printk("==>decrypt msdu error\n");
1326                 goto rx_dropped;
1327         }
1328
1329         //added by amy for AP roaming
1330         ieee->LinkDetectInfo.NumRecvDataInPeriod++;
1331         ieee->LinkDetectInfo.NumRxOkInPeriod++;
1332
1333         hdr = (struct ieee80211_hdr_4addr *) skb->data;
1334         if((!is_multicast_ether_addr(hdr->addr1)) && (!is_broadcast_ether_addr(hdr->addr1)))
1335                 unicast_packet = true;
1336
1337         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
1338                 if (/*ieee->ieee802_1x &&*/
1339                     ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
1340
1341 #ifdef CONFIG_IEEE80211_DEBUG
1342                         /* pass unencrypted EAPOL frames even if encryption is
1343                          * configured */
1344                         struct eapol *eap = (struct eapol *)(skb->data +
1345                                 24);
1346                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
1347                                                 eap_get_type(eap->type));
1348 #endif
1349                 } else {
1350                         IEEE80211_DEBUG_DROP(
1351                                 "encryption configured, but RX "
1352                                 "frame not encrypted (SA=%pM)\n",
1353                                 hdr->addr2);
1354                         goto rx_dropped;
1355                 }
1356         }
1357
1358 #ifdef CONFIG_IEEE80211_DEBUG
1359         if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
1360             ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
1361                         struct eapol *eap = (struct eapol *)(skb->data +
1362                                 24);
1363                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
1364                                                 eap_get_type(eap->type));
1365         }
1366 #endif
1367
1368         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
1369             !ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
1370                 IEEE80211_DEBUG_DROP(
1371                         "dropped unencrypted RX data "
1372                         "frame from %pM"
1373                         " (drop_unencrypted=1)\n",
1374                         hdr->addr2);
1375                 goto rx_dropped;
1376         }
1377 /*
1378         if(ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
1379                 printk(KERN_WARNING "RX: IEEE802.1X EPAOL frame!\n");
1380         }
1381 */
1382 //added by amy for reorder
1383 #if 1
1384         if(ieee->current_network.qos_data.active && IsQoSDataFrame(skb->data)
1385                 && !is_multicast_ether_addr(hdr->addr1) && !is_broadcast_ether_addr(hdr->addr1))
1386         {
1387                 TID = Frame_QoSTID(skb->data);
1388                 SeqNum = WLAN_GET_SEQ_SEQ(sc);
1389                 GetTs(ieee,(PTS_COMMON_INFO*) &pTS,hdr->addr2,TID,RX_DIR,true);
1390                 if(TID !=0 && TID !=3)
1391                 {
1392                         ieee->bis_any_nonbepkts = true;
1393                 }
1394         }
1395 #endif
1396 //added by amy for reorder
1397         /* skb: hdr + (possible reassembled) full plaintext payload */
1398         payload = skb->data + hdrlen;
1399         //ethertype = (payload[6] << 8) | payload[7];
1400         rxb = kmalloc(sizeof(struct ieee80211_rxb), GFP_ATOMIC);
1401         if(rxb == NULL)
1402         {
1403                 IEEE80211_DEBUG(IEEE80211_DL_ERR,"%s(): kmalloc rxb error\n",__FUNCTION__);
1404                 goto rx_dropped;
1405         }
1406         /* to parse amsdu packets */
1407         /* qos data packets & reserved bit is 1 */
1408         if(parse_subframe(ieee, skb,rx_stats,rxb,src,dst) == 0) {
1409                 /* only to free rxb, and not submit the packets to upper layer */
1410                 for(i =0; i < rxb->nr_subframes; i++) {
1411                         dev_kfree_skb(rxb->subframes[i]);
1412                 }
1413                 kfree(rxb);
1414                 rxb = NULL;
1415                 goto rx_dropped;
1416         }
1417
1418 #ifdef ENABLE_LPS
1419                 if(unicast_packet)
1420                 {
1421                         if (type == IEEE80211_FTYPE_DATA)
1422                         {
1423
1424                                 if(ieee->bIsAggregateFrame)
1425                                         ieee->LinkDetectInfo.NumRxUnicastOkInPeriod+=rxb->nr_subframes;
1426                                 else
1427                                         ieee->LinkDetectInfo.NumRxUnicastOkInPeriod++;
1428
1429                                 // 2009.03.03 Leave DC mode immediately when detect high traffic
1430                                 // DbgPrint("ending Seq %d\n", Frame_SeqNum(pduOS));
1431                                 if((ieee->state == IEEE80211_LINKED) /*&& !MgntInitAdapterInProgress(pMgntInfo)*/)
1432                                 {
1433                                         if(     ((ieee->LinkDetectInfo.NumRxUnicastOkInPeriod +ieee->LinkDetectInfo.NumTxOkInPeriod) > 8 ) ||
1434                                                 (ieee->LinkDetectInfo.NumRxUnicastOkInPeriod > 2) )
1435                                         {
1436                                                 if(ieee->LeisurePSLeave)
1437                                                         ieee->LeisurePSLeave(dev);
1438                                         }
1439                                 }
1440                         }
1441                 }
1442 #endif
1443
1444         ieee->last_rx_ps_time = jiffies;
1445 //added by amy for reorder
1446         if(ieee->pHTInfo->bCurRxReorderEnable == false ||pTS == NULL){
1447 //added by amy for reorder
1448                 for(i = 0; i<rxb->nr_subframes; i++) {
1449                         struct sk_buff *sub_skb = rxb->subframes[i];
1450
1451                         if (sub_skb) {
1452                                 /* convert hdr + possible LLC headers into Ethernet header */
1453                                 ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
1454                                 if (sub_skb->len >= 8 &&
1455                                                 ((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
1456                                                   ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1457                                                  memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1458                                         /* remove RFC1042 or Bridge-Tunnel encapsulation and
1459                                          * replace EtherType */
1460                                         skb_pull(sub_skb, SNAP_SIZE);
1461                                         memcpy(skb_push(sub_skb, ETH_ALEN), src, ETH_ALEN);
1462                                         memcpy(skb_push(sub_skb, ETH_ALEN), dst, ETH_ALEN);
1463                                 } else {
1464                                         u16 len;
1465                                         /* Leave Ethernet header part of hdr and full payload */
1466                                         len = htons(sub_skb->len);
1467                                         memcpy(skb_push(sub_skb, 2), &len, 2);
1468                                         memcpy(skb_push(sub_skb, ETH_ALEN), src, ETH_ALEN);
1469                                         memcpy(skb_push(sub_skb, ETH_ALEN), dst, ETH_ALEN);
1470                                 }
1471
1472                                 stats->rx_packets++;
1473                                 stats->rx_bytes += sub_skb->len;
1474                                 if(is_multicast_ether_addr(dst)) {
1475                                         stats->multicast++;
1476                                 }
1477
1478                                 /* Indicat the packets to upper layer */
1479                                 //printk("0skb_len(%d)\n", skb->len);
1480                                 sub_skb->protocol = eth_type_trans(sub_skb, dev);
1481                                 memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
1482                                 sub_skb->dev = dev;
1483                                 sub_skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
1484                                 //skb->ip_summed = CHECKSUM_UNNECESSARY; /* 802.11 crc not sufficient */
1485                                 //printk("1skb_len(%d)\n", skb->len);
1486                                 netif_rx(sub_skb);
1487                         }
1488                 }
1489                 kfree(rxb);
1490                 rxb = NULL;
1491
1492         }
1493         else
1494         {
1495                 IEEE80211_DEBUG(IEEE80211_DL_REORDER,"%s(): REORDER ENABLE AND PTS not NULL, and we will enter RxReorderIndicatePacket()\n",__FUNCTION__);
1496                 RxReorderIndicatePacket(ieee, rxb, pTS, SeqNum);
1497         }
1498 #ifndef JOHN_NOCPY
1499         dev_kfree_skb(skb);
1500 #endif
1501
1502  rx_exit:
1503 #ifdef NOT_YET
1504         if (sta)
1505                 hostap_handle_sta_release(sta);
1506 #endif
1507         return 1;
1508
1509  rx_dropped:
1510         if (rxb != NULL)
1511         {
1512                 kfree(rxb);
1513                 rxb = NULL;
1514         }
1515         stats->rx_dropped++;
1516
1517         /* Returning 0 indicates to caller that we have not handled the SKB--
1518          * so it is still allocated and can be used again by underlying
1519          * hardware as a DMA target */
1520         return 0;
1521 }
1522
1523 #define MGMT_FRAME_FIXED_PART_LENGTH            0x24
1524
1525 static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
1526
1527 /*
1528 * Make ther structure we read from the beacon packet has
1529 * the right values
1530 */
1531 static int ieee80211_verify_qos_info(struct ieee80211_qos_information_element
1532                                      *info_element, int sub_type)
1533 {
1534
1535         if (info_element->qui_subtype != sub_type)
1536                 return -1;
1537         if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
1538                 return -1;
1539         if (info_element->qui_type != QOS_OUI_TYPE)
1540                 return -1;
1541         if (info_element->version != QOS_VERSION_1)
1542                 return -1;
1543
1544         return 0;
1545 }
1546
1547
1548 /*
1549  * Parse a QoS parameter element
1550  */
1551 static int ieee80211_read_qos_param_element(struct ieee80211_qos_parameter_info
1552                                             *element_param, struct ieee80211_info_element
1553                                             *info_element)
1554 {
1555         int ret = 0;
1556         u16 size = sizeof(struct ieee80211_qos_parameter_info) - 2;
1557
1558         if ((info_element == NULL) || (element_param == NULL))
1559                 return -1;
1560
1561         if (info_element->id == QOS_ELEMENT_ID && info_element->len == size) {
1562                 memcpy(element_param->info_element.qui, info_element->data,
1563                        info_element->len);
1564                 element_param->info_element.elementID = info_element->id;
1565                 element_param->info_element.length = info_element->len;
1566         } else
1567                 ret = -1;
1568         if (ret == 0)
1569                 ret = ieee80211_verify_qos_info(&element_param->info_element,
1570                                                 QOS_OUI_PARAM_SUB_TYPE);
1571         return ret;
1572 }
1573
1574 /*
1575  * Parse a QoS information element
1576  */
1577 static int ieee80211_read_qos_info_element(struct
1578                                            ieee80211_qos_information_element
1579                                            *element_info, struct ieee80211_info_element
1580                                            *info_element)
1581 {
1582         int ret = 0;
1583         u16 size = sizeof(struct ieee80211_qos_information_element) - 2;
1584
1585         if (element_info == NULL)
1586                 return -1;
1587         if (info_element == NULL)
1588                 return -1;
1589
1590         if ((info_element->id == QOS_ELEMENT_ID) && (info_element->len == size)) {
1591                 memcpy(element_info->qui, info_element->data,
1592                        info_element->len);
1593                 element_info->elementID = info_element->id;
1594                 element_info->length = info_element->len;
1595         } else
1596                 ret = -1;
1597
1598         if (ret == 0)
1599                 ret = ieee80211_verify_qos_info(element_info,
1600                                                 QOS_OUI_INFO_SUB_TYPE);
1601         return ret;
1602 }
1603
1604
1605 /*
1606  * Write QoS parameters from the ac parameters.
1607  */
1608 static int ieee80211_qos_convert_ac_to_parameters(struct
1609                                                   ieee80211_qos_parameter_info
1610                                                   *param_elm, struct
1611                                                   ieee80211_qos_parameters
1612                                                   *qos_param)
1613 {
1614         int rc = 0;
1615         int i;
1616         struct ieee80211_qos_ac_parameter *ac_params;
1617         u8 aci;
1618         //u8 cw_min;
1619         //u8 cw_max;
1620
1621         for (i = 0; i < QOS_QUEUE_NUM; i++) {
1622                 ac_params = &(param_elm->ac_params_record[i]);
1623
1624                 aci = (ac_params->aci_aifsn & 0x60) >> 5;
1625
1626                 if(aci >= QOS_QUEUE_NUM)
1627                         continue;
1628                 qos_param->aifs[aci] = (ac_params->aci_aifsn) & 0x0f;
1629
1630                 /* WMM spec P.11: The minimum value for AIFSN shall be 2 */
1631                 qos_param->aifs[aci] = (qos_param->aifs[aci] < 2) ? 2:qos_param->aifs[aci];
1632
1633                 qos_param->cw_min[aci] = ac_params->ecw_min_max & 0x0F;
1634
1635                 qos_param->cw_max[aci] = (ac_params->ecw_min_max & 0xF0) >> 4;
1636
1637                 qos_param->flag[aci] =
1638                     (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
1639                 qos_param->tx_op_limit[aci] = le16_to_cpu(ac_params->tx_op_limit);
1640         }
1641         return rc;
1642 }
1643
1644 /*
1645  * we have a generic data element which it may contain QoS information or
1646  * parameters element. check the information element length to decide
1647  * which type to read
1648  */
1649 static int ieee80211_parse_qos_info_param_IE(struct ieee80211_info_element
1650                                              *info_element,
1651                                              struct ieee80211_network *network)
1652 {
1653         int rc = 0;
1654         struct ieee80211_qos_parameters *qos_param = NULL;
1655         struct ieee80211_qos_information_element qos_info_element;
1656
1657         rc = ieee80211_read_qos_info_element(&qos_info_element, info_element);
1658
1659         if (rc == 0) {
1660                 network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
1661                 network->flags |= NETWORK_HAS_QOS_INFORMATION;
1662         } else {
1663                 struct ieee80211_qos_parameter_info param_element;
1664
1665                 rc = ieee80211_read_qos_param_element(&param_element,
1666                                                       info_element);
1667                 if (rc == 0) {
1668                         qos_param = &(network->qos_data.parameters);
1669                         ieee80211_qos_convert_ac_to_parameters(&param_element,
1670                                                                qos_param);
1671                         network->flags |= NETWORK_HAS_QOS_PARAMETERS;
1672                         network->qos_data.param_count =
1673                             param_element.info_element.ac_info & 0x0F;
1674                 }
1675         }
1676
1677         if (rc == 0) {
1678                 IEEE80211_DEBUG_QOS("QoS is supported\n");
1679                 network->qos_data.supported = 1;
1680         }
1681         return rc;
1682 }
1683
1684 #ifdef CONFIG_IEEE80211_DEBUG
1685 #define MFIE_STRING(x) case MFIE_TYPE_ ##x: return #x
1686
1687 static const char *get_info_element_string(u16 id)
1688 {
1689         switch (id) {
1690                 MFIE_STRING(SSID);
1691                 MFIE_STRING(RATES);
1692                 MFIE_STRING(FH_SET);
1693                 MFIE_STRING(DS_SET);
1694                 MFIE_STRING(CF_SET);
1695                 MFIE_STRING(TIM);
1696                 MFIE_STRING(IBSS_SET);
1697                 MFIE_STRING(COUNTRY);
1698                 MFIE_STRING(HOP_PARAMS);
1699                 MFIE_STRING(HOP_TABLE);
1700                 MFIE_STRING(REQUEST);
1701                 MFIE_STRING(CHALLENGE);
1702                 MFIE_STRING(POWER_CONSTRAINT);
1703                 MFIE_STRING(POWER_CAPABILITY);
1704                 MFIE_STRING(TPC_REQUEST);
1705                 MFIE_STRING(TPC_REPORT);
1706                 MFIE_STRING(SUPP_CHANNELS);
1707                 MFIE_STRING(CSA);
1708                 MFIE_STRING(MEASURE_REQUEST);
1709                 MFIE_STRING(MEASURE_REPORT);
1710                 MFIE_STRING(QUIET);
1711                 MFIE_STRING(IBSS_DFS);
1712                // MFIE_STRING(ERP_INFO);
1713                 MFIE_STRING(RSN);
1714                 MFIE_STRING(RATES_EX);
1715                 MFIE_STRING(GENERIC);
1716                 MFIE_STRING(QOS_PARAMETER);
1717         default:
1718                 return "UNKNOWN";
1719         }
1720 }
1721 #endif
1722
1723 #ifdef ENABLE_DOT11D
1724 static inline void ieee80211_extract_country_ie(
1725         struct ieee80211_device *ieee,
1726         struct ieee80211_info_element *info_element,
1727         struct ieee80211_network *network,
1728         u8 * addr2
1729 )
1730 {
1731         if(IS_DOT11D_ENABLE(ieee))
1732         {
1733                 if(info_element->len!= 0)
1734                 {
1735                         memcpy(network->CountryIeBuf, info_element->data, info_element->len);
1736                         network->CountryIeLen = info_element->len;
1737
1738                         if(!IS_COUNTRY_IE_VALID(ieee))
1739                         {
1740                                 Dot11d_UpdateCountryIe(ieee, addr2, info_element->len, info_element->data);
1741                         }
1742                 }
1743
1744                 //
1745                 // 070305, rcnjko: I update country IE watch dog here because
1746                 // some AP (e.g. Cisco 1242) don't include country IE in their
1747                 // probe response frame.
1748                 //
1749                 if(IS_EQUAL_CIE_SRC(ieee, addr2) )
1750                 {
1751                         UPDATE_CIE_WATCHDOG(ieee);
1752                 }
1753         }
1754
1755 }
1756 #endif
1757
1758 int ieee80211_parse_info_param(struct ieee80211_device *ieee,
1759                 struct ieee80211_info_element *info_element,
1760                 u16 length,
1761                 struct ieee80211_network *network,
1762                 struct ieee80211_rx_stats *stats)
1763 {
1764         u8 i;
1765         short offset;
1766         u16     tmp_htcap_len=0;
1767         u16     tmp_htinfo_len=0;
1768         u16 ht_realtek_agg_len=0;
1769         u8  ht_realtek_agg_buf[MAX_IE_LEN];
1770 //      u16 broadcom_len = 0;
1771 #ifdef CONFIG_IEEE80211_DEBUG
1772         char rates_str[64];
1773         char *p;
1774 #endif
1775
1776         while (length >= sizeof(*info_element)) {
1777                 if (sizeof(*info_element) + info_element->len > length) {
1778                         IEEE80211_DEBUG_MGMT("Info elem: parse failed: "
1779                                              "info_element->len + 2 > left : "
1780                                              "info_element->len+2=%zd left=%d, id=%d.\n",
1781                                              info_element->len +
1782                                              sizeof(*info_element),
1783                                              length, info_element->id);
1784                         /* We stop processing but don't return an error here
1785                          * because some misbehaviour APs break this rule. ie.
1786                          * Orinoco AP1000. */
1787                         break;
1788                 }
1789
1790                 switch (info_element->id) {
1791                 case MFIE_TYPE_SSID:
1792                         if (ieee80211_is_empty_essid(info_element->data,
1793                                                      info_element->len)) {
1794                                 network->flags |= NETWORK_EMPTY_ESSID;
1795                                 break;
1796                         }
1797
1798                         network->ssid_len = min(info_element->len,
1799                                                 (u8) IW_ESSID_MAX_SIZE);
1800                         memcpy(network->ssid, info_element->data, network->ssid_len);
1801                         if (network->ssid_len < IW_ESSID_MAX_SIZE)
1802                                 memset(network->ssid + network->ssid_len, 0,
1803                                        IW_ESSID_MAX_SIZE - network->ssid_len);
1804
1805                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_SSID: '%s' len=%d.\n",
1806                                              network->ssid, network->ssid_len);
1807                         break;
1808
1809                 case MFIE_TYPE_RATES:
1810 #ifdef CONFIG_IEEE80211_DEBUG
1811                         p = rates_str;
1812 #endif
1813                         network->rates_len = min(info_element->len,
1814                                                  MAX_RATES_LENGTH);
1815                         for (i = 0; i < network->rates_len; i++) {
1816                                 network->rates[i] = info_element->data[i];
1817 #ifdef CONFIG_IEEE80211_DEBUG
1818                                 p += snprintf(p, sizeof(rates_str) -
1819                                               (p - rates_str), "%02X ",
1820                                               network->rates[i]);
1821 #endif
1822                                 if (ieee80211_is_ofdm_rate
1823                                     (info_element->data[i])) {
1824                                         network->flags |= NETWORK_HAS_OFDM;
1825                                         if (info_element->data[i] &
1826                                             IEEE80211_BASIC_RATE_MASK)
1827                                                 network->flags &=
1828                                                     ~NETWORK_HAS_CCK;
1829                                 }
1830                         }
1831
1832                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES: '%s' (%d)\n",
1833                                              rates_str, network->rates_len);
1834                         break;
1835
1836                 case MFIE_TYPE_RATES_EX:
1837 #ifdef CONFIG_IEEE80211_DEBUG
1838                         p = rates_str;
1839 #endif
1840                         network->rates_ex_len = min(info_element->len,
1841                                                     MAX_RATES_EX_LENGTH);
1842                         for (i = 0; i < network->rates_ex_len; i++) {
1843                                 network->rates_ex[i] = info_element->data[i];
1844 #ifdef CONFIG_IEEE80211_DEBUG
1845                                 p += snprintf(p, sizeof(rates_str) -
1846                                               (p - rates_str), "%02X ",
1847                                               network->rates[i]);
1848 #endif
1849                                 if (ieee80211_is_ofdm_rate
1850                                     (info_element->data[i])) {
1851                                         network->flags |= NETWORK_HAS_OFDM;
1852                                         if (info_element->data[i] &
1853                                             IEEE80211_BASIC_RATE_MASK)
1854                                                 network->flags &=
1855                                                     ~NETWORK_HAS_CCK;
1856                                 }
1857                         }
1858
1859                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
1860                                              rates_str, network->rates_ex_len);
1861                         break;
1862
1863                 case MFIE_TYPE_DS_SET:
1864                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_DS_SET: %d\n",
1865                                              info_element->data[0]);
1866                         network->channel = info_element->data[0];
1867                         break;
1868
1869                 case MFIE_TYPE_FH_SET:
1870                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_FH_SET: ignored\n");
1871                         break;
1872
1873                 case MFIE_TYPE_CF_SET:
1874                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_CF_SET: ignored\n");
1875                         break;
1876
1877                 case MFIE_TYPE_TIM:
1878                         if(info_element->len < 4)
1879                                 break;
1880
1881                         network->tim.tim_count = info_element->data[0];
1882                         network->tim.tim_period = info_element->data[1];
1883
1884                         network->dtim_period = info_element->data[1];
1885                         if(ieee->state != IEEE80211_LINKED)
1886                                 break;
1887 #if 0
1888                         network->last_dtim_sta_time[0] = stats->mac_time[0];
1889 #else
1890                         //we use jiffies for legacy Power save
1891                         network->last_dtim_sta_time[0] = jiffies;
1892 #endif
1893                         network->last_dtim_sta_time[1] = stats->mac_time[1];
1894
1895                         network->dtim_data = IEEE80211_DTIM_VALID;
1896
1897                         if(info_element->data[0] != 0)
1898                                 break;
1899
1900                         if(info_element->data[2] & 1)
1901                                 network->dtim_data |= IEEE80211_DTIM_MBCAST;
1902
1903                         offset = (info_element->data[2] >> 1)*2;
1904
1905                         //printk("offset1:%x aid:%x\n",offset, ieee->assoc_id);
1906
1907                         if(ieee->assoc_id < 8*offset ||
1908                                 ieee->assoc_id > 8*(offset + info_element->len -3))
1909
1910                                 break;
1911
1912                         offset = (ieee->assoc_id / 8) - offset;// + ((aid % 8)? 0 : 1) ;
1913
1914                         if(info_element->data[3+offset] & (1<<(ieee->assoc_id%8)))
1915                                 network->dtim_data |= IEEE80211_DTIM_UCAST;
1916
1917                         //IEEE80211_DEBUG_MGMT("MFIE_TYPE_TIM: partially ignored\n");
1918                         break;
1919
1920                 case MFIE_TYPE_ERP:
1921                         network->erp_value = info_element->data[0];
1922                         network->flags |= NETWORK_HAS_ERP_VALUE;
1923                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_ERP_SET: %d\n",
1924                                              network->erp_value);
1925                         break;
1926                 case MFIE_TYPE_IBSS_SET:
1927                         network->atim_window = info_element->data[0];
1928                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_IBSS_SET: %d\n",
1929                                              network->atim_window);
1930                         break;
1931
1932                 case MFIE_TYPE_CHALLENGE:
1933                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_CHALLENGE: ignored\n");
1934                         break;
1935
1936                 case MFIE_TYPE_GENERIC:
1937                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_GENERIC: %d bytes\n",
1938                                              info_element->len);
1939                         if (!ieee80211_parse_qos_info_param_IE(info_element,
1940                                                                network))
1941                                 break;
1942
1943                         if (info_element->len >= 4 &&
1944                             info_element->data[0] == 0x00 &&
1945                             info_element->data[1] == 0x50 &&
1946                             info_element->data[2] == 0xf2 &&
1947                             info_element->data[3] == 0x01) {
1948                                 network->wpa_ie_len = min(info_element->len + 2,
1949                                                           MAX_WPA_IE_LEN);
1950                                 memcpy(network->wpa_ie, info_element,
1951                                        network->wpa_ie_len);
1952                                 break;
1953                         }
1954
1955 #ifdef THOMAS_TURBO
1956                         if (info_element->len == 7 &&
1957                             info_element->data[0] == 0x00 &&
1958                             info_element->data[1] == 0xe0 &&
1959                             info_element->data[2] == 0x4c &&
1960                             info_element->data[3] == 0x01 &&
1961                             info_element->data[4] == 0x02) {
1962                                 network->Turbo_Enable = 1;
1963                         }
1964 #endif
1965
1966                         //for HTcap and HTinfo parameters
1967                         if(tmp_htcap_len == 0){
1968                                 if(info_element->len >= 4 &&
1969                                    info_element->data[0] == 0x00 &&
1970                                    info_element->data[1] == 0x90 &&
1971                                    info_element->data[2] == 0x4c &&
1972                                    info_element->data[3] == 0x033){
1973
1974                                                 tmp_htcap_len = min(info_element->len,(u8)MAX_IE_LEN);
1975                                                 if(tmp_htcap_len != 0){
1976                                                         network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
1977                                                         network->bssht.bdHTCapLen = tmp_htcap_len > sizeof(network->bssht.bdHTCapBuf)?\
1978                                                                 sizeof(network->bssht.bdHTCapBuf):tmp_htcap_len;
1979                                                         memcpy(network->bssht.bdHTCapBuf,info_element->data,network->bssht.bdHTCapLen);
1980                                                 }
1981                                 }
1982                                 if(tmp_htcap_len != 0)
1983                                         network->bssht.bdSupportHT = true;
1984                                 else
1985                                         network->bssht.bdSupportHT = false;
1986                         }
1987
1988
1989                         if(tmp_htinfo_len == 0){
1990                                 if(info_element->len >= 4 &&
1991                                         info_element->data[0] == 0x00 &&
1992                                         info_element->data[1] == 0x90 &&
1993                                         info_element->data[2] == 0x4c &&
1994                                         info_element->data[3] == 0x034){
1995
1996                                                 tmp_htinfo_len = min(info_element->len,(u8)MAX_IE_LEN);
1997                                                 if(tmp_htinfo_len != 0){
1998                                                         network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
1999                                                         if(tmp_htinfo_len){
2000                                                                 network->bssht.bdHTInfoLen = tmp_htinfo_len > sizeof(network->bssht.bdHTInfoBuf)?\
2001                                                                         sizeof(network->bssht.bdHTInfoBuf):tmp_htinfo_len;
2002                                                                 memcpy(network->bssht.bdHTInfoBuf,info_element->data,network->bssht.bdHTInfoLen);
2003                                                         }
2004
2005                                                 }
2006
2007                                 }
2008                         }
2009
2010                         if(ieee->aggregation){
2011                                 if(network->bssht.bdSupportHT){
2012                                         if(info_element->len >= 4 &&
2013                                                 info_element->data[0] == 0x00 &&
2014                                                 info_element->data[1] == 0xe0 &&
2015                                                 info_element->data[2] == 0x4c &&
2016                                                 info_element->data[3] == 0x02){
2017
2018                                                 ht_realtek_agg_len = min(info_element->len,(u8)MAX_IE_LEN);
2019                                                 memcpy(ht_realtek_agg_buf,info_element->data,info_element->len);
2020
2021                                         }
2022                                         if(ht_realtek_agg_len >= 5){
2023                                                 network->bssht.bdRT2RTAggregation = true;
2024
2025                                                 if((ht_realtek_agg_buf[4] == 1) && (ht_realtek_agg_buf[5] & 0x02))
2026                                                 network->bssht.bdRT2RTLongSlotTime = true;
2027                                         }
2028                                 }
2029
2030                         }
2031
2032                         //if(tmp_htcap_len !=0  ||  tmp_htinfo_len != 0)
2033                         {
2034                                 if((info_element->len >= 3 &&
2035                                          info_element->data[0] == 0x00 &&
2036                                          info_element->data[1] == 0x05 &&
2037                                          info_element->data[2] == 0xb5) ||
2038                                          (info_element->len >= 3 &&
2039                                          info_element->data[0] == 0x00 &&
2040                                          info_element->data[1] == 0x0a &&
2041                                          info_element->data[2] == 0xf7) ||
2042                                          (info_element->len >= 3 &&
2043                                          info_element->data[0] == 0x00 &&
2044                                          info_element->data[1] == 0x10 &&
2045                                          info_element->data[2] == 0x18)){
2046
2047                                                 network->broadcom_cap_exist = true;
2048
2049                                 }
2050                         }
2051 #if 0
2052                         if (tmp_htcap_len !=0)
2053                                 {
2054                                         u16 cap_ext = ((PHT_CAPABILITY_ELE)&info_element->data[0])->ExtHTCapInfo;
2055                                         if ((cap_ext & 0x0c00) == 0x0c00)
2056                                                 {
2057                                                         network->ralink_cap_exist = true;
2058                                                 }
2059                                 }
2060 #endif
2061                         if(info_element->len >= 3 &&
2062                                 info_element->data[0] == 0x00 &&
2063                                 info_element->data[1] == 0x0c &&
2064                                 info_element->data[2] == 0x43)
2065                         {
2066                                 network->ralink_cap_exist = true;
2067                         }
2068                         else
2069                                 network->ralink_cap_exist = false;
2070                         //added by amy for atheros AP
2071                         if((info_element->len >= 3 &&
2072                                 info_element->data[0] == 0x00 &&
2073                                 info_element->data[1] == 0x03 &&
2074                                 info_element->data[2] == 0x7f) ||
2075                                 (info_element->len >= 3 &&
2076                                 info_element->data[0] == 0x00 &&
2077                                 info_element->data[1] == 0x13 &&
2078                                 info_element->data[2] == 0x74))
2079                         {
2080                                 //printk("========>%s(): athros AP is exist\n",__FUNCTION__);
2081                                 network->atheros_cap_exist = true;
2082                         }
2083                         else
2084                                 network->atheros_cap_exist = false;
2085
2086                         if ((info_element->len >= 3 &&
2087                                                 info_element->data[0] == 0x00 &&
2088                                                 info_element->data[1] == 0x50 &&
2089                                                 info_element->data[2] == 0x43) )
2090                         {
2091                                 network->marvell_cap_exist = true;
2092                                 //printk("========>%s(): marvel AP is exist\n",__FUNCTION__);
2093                         }
2094
2095
2096                         if(info_element->len >= 3 &&
2097                                 info_element->data[0] == 0x00 &&
2098                                 info_element->data[1] == 0x40 &&
2099                                 info_element->data[2] == 0x96)
2100                         {
2101                                 network->cisco_cap_exist = true;
2102                         }
2103                         else
2104                                 network->cisco_cap_exist = false;
2105                         //added by amy for LEAP of cisco
2106                         if(info_element->len > 4 &&
2107                                 info_element->data[0] == 0x00 &&
2108                                 info_element->data[1] == 0x40 &&
2109                                 info_element->data[2] == 0x96 &&
2110                                 info_element->data[3] == 0x01)
2111                         {
2112                                 if(info_element->len == 6)
2113                                 {
2114                                         memcpy(network->CcxRmState, &info_element[4], 2);
2115                                         if(network->CcxRmState[0] != 0)
2116                                         {
2117                                                 network->bCcxRmEnable = true;
2118                                         }
2119                                         else
2120                                                 network->bCcxRmEnable = false;
2121                                         //
2122                                         // CCXv4 Table 59-1 MBSSID Masks.
2123                                         //
2124                                         network->MBssidMask = network->CcxRmState[1] & 0x07;
2125                                         if(network->MBssidMask != 0)
2126                                         {
2127                                                 network->bMBssidValid = true;
2128                                                 network->MBssidMask = 0xff << (network->MBssidMask);
2129                                                 cpMacAddr(network->MBssid, network->bssid);
2130                                                 network->MBssid[5] &= network->MBssidMask;
2131                                         }
2132                                         else
2133                                         {
2134                                                 network->bMBssidValid = false;
2135                                         }
2136                                 }
2137                                 else
2138                                 {
2139                                         network->bCcxRmEnable = false;
2140                                 }
2141                         }
2142                         if(info_element->len > 4  &&
2143                                 info_element->data[0] == 0x00 &&
2144                                 info_element->data[1] == 0x40 &&
2145                                 info_element->data[2] == 0x96 &&
2146                                 info_element->data[3] == 0x03)
2147                         {
2148                                 if(info_element->len == 5)
2149                                 {
2150                                         network->bWithCcxVerNum = true;
2151                                         network->BssCcxVerNumber = info_element->data[4];
2152                                 }
2153                                 else
2154                                 {
2155                                         network->bWithCcxVerNum = false;
2156                                         network->BssCcxVerNumber = 0;
2157                                 }
2158                         }
2159                         break;
2160
2161                 case MFIE_TYPE_RSN:
2162                         IEEE80211_DEBUG_MGMT("MFIE_TYPE_RSN: %d bytes\n",
2163                                              info_element->len);
2164                         network->rsn_ie_len = min(info_element->len + 2,
2165                                                   MAX_WPA_IE_LEN);
2166                         memcpy(network->rsn_ie, info_element,
2167                                network->rsn_ie_len);
2168                         break;
2169
2170                         //HT related element.
2171                 case MFIE_TYPE_HT_CAP:
2172                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_HT_CAP: %d bytes\n",
2173                                              info_element->len);
2174                         tmp_htcap_len = min(info_element->len,(u8)MAX_IE_LEN);
2175                         if(tmp_htcap_len != 0){
2176                                 network->bssht.bdHTSpecVer = HT_SPEC_VER_EWC;
2177                                 network->bssht.bdHTCapLen = tmp_htcap_len > sizeof(network->bssht.bdHTCapBuf)?\
2178                                         sizeof(network->bssht.bdHTCapBuf):tmp_htcap_len;
2179                                 memcpy(network->bssht.bdHTCapBuf,info_element->data,network->bssht.bdHTCapLen);
2180
2181                                 //If peer is HT, but not WMM, call QosSetLegacyWMMParamWithHT()
2182                                 // windows driver will update WMM parameters each beacon received once connected
2183                                 // Linux driver is a bit different.
2184                                 network->bssht.bdSupportHT = true;
2185                         }
2186                         else
2187                                 network->bssht.bdSupportHT = false;
2188                         break;
2189
2190
2191                 case MFIE_TYPE_HT_INFO:
2192                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_HT_INFO: %d bytes\n",
2193                                              info_element->len);
2194                         tmp_htinfo_len = min(info_element->len,(u8)MAX_IE_LEN);
2195                         if(tmp_htinfo_len){
2196                                 network->bssht.bdHTSpecVer = HT_SPEC_VER_IEEE;
2197                                 network->bssht.bdHTInfoLen = tmp_htinfo_len > sizeof(network->bssht.bdHTInfoBuf)?\
2198                                         sizeof(network->bssht.bdHTInfoBuf):tmp_htinfo_len;
2199                                 memcpy(network->bssht.bdHTInfoBuf,info_element->data,network->bssht.bdHTInfoLen);
2200                         }
2201                         break;
2202
2203                 case MFIE_TYPE_AIRONET:
2204                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_AIRONET: %d bytes\n",
2205                                              info_element->len);
2206                         if(info_element->len >IE_CISCO_FLAG_POSITION)
2207                         {
2208                                 network->bWithAironetIE = true;
2209
2210                                 // CCX 1 spec v1.13, A01.1 CKIP Negotiation (page23):
2211                                 // "A Cisco access point advertises support for CKIP in beacon and probe response packets,
2212                                 //  by adding an Aironet element and setting one or both of the CKIP negotiation bits."
2213                                 if(     (info_element->data[IE_CISCO_FLAG_POSITION]&SUPPORT_CKIP_MIC)   ||
2214                                         (info_element->data[IE_CISCO_FLAG_POSITION]&SUPPORT_CKIP_PK)    )
2215                                 {
2216                                         network->bCkipSupported = true;
2217                                 }
2218                                 else
2219                                 {
2220                                         network->bCkipSupported = false;
2221                                 }
2222                         }
2223                         else
2224                         {
2225                                 network->bWithAironetIE = false;
2226                                 network->bCkipSupported = false;
2227                         }
2228                         break;
2229                 case MFIE_TYPE_QOS_PARAMETER:
2230                         printk(KERN_ERR
2231                                "QoS Error need to parse QOS_PARAMETER IE\n");
2232                         break;
2233
2234 #ifdef ENABLE_DOT11D
2235                 case MFIE_TYPE_COUNTRY:
2236                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_COUNTRY: %d bytes\n",
2237                                              info_element->len);
2238                         //printk("=====>Receive <%s> Country IE\n",network->ssid);
2239                         ieee80211_extract_country_ie(ieee, info_element, network, network->bssid);//addr2 is same as addr3 when from an AP
2240                         break;
2241 #endif
2242 /* TODO */
2243 #if 0
2244                         /* 802.11h */
2245                 case MFIE_TYPE_POWER_CONSTRAINT:
2246                         network->power_constraint = info_element->data[0];
2247                         network->flags |= NETWORK_HAS_POWER_CONSTRAINT;
2248                         break;
2249
2250                 case MFIE_TYPE_CSA:
2251                         network->power_constraint = info_element->data[0];
2252                         network->flags |= NETWORK_HAS_CSA;
2253                         break;
2254
2255                 case MFIE_TYPE_QUIET:
2256                         network->quiet.count = info_element->data[0];
2257                         network->quiet.period = info_element->data[1];
2258                         network->quiet.duration = info_element->data[2];
2259                         network->quiet.offset = info_element->data[3];
2260                         network->flags |= NETWORK_HAS_QUIET;
2261                         break;
2262
2263                 case MFIE_TYPE_IBSS_DFS:
2264                         if (network->ibss_dfs)
2265                                 break;
2266                         network->ibss_dfs = kmemdup(info_element->data,
2267                                                     info_element->len,
2268                                                     GFP_ATOMIC);
2269                         if (!network->ibss_dfs)
2270                                 return 1;
2271                         network->flags |= NETWORK_HAS_IBSS_DFS;
2272                         break;
2273
2274                 case MFIE_TYPE_TPC_REPORT:
2275                         network->tpc_report.transmit_power =
2276                             info_element->data[0];
2277                         network->tpc_report.link_margin = info_element->data[1];
2278                         network->flags |= NETWORK_HAS_TPC_REPORT;
2279                         break;
2280 #endif
2281                 default:
2282                         IEEE80211_DEBUG_MGMT
2283                             ("Unsupported info element: %s (%d)\n",
2284                              get_info_element_string(info_element->id),
2285                              info_element->id);
2286                         break;
2287                 }
2288
2289                 length -= sizeof(*info_element) + info_element->len;
2290                 info_element =
2291                     (struct ieee80211_info_element *)&info_element->
2292                     data[info_element->len];
2293         }
2294
2295         if(!network->atheros_cap_exist && !network->broadcom_cap_exist &&
2296                 !network->cisco_cap_exist && !network->ralink_cap_exist && !network->bssht.bdRT2RTAggregation &&
2297                 !network->marvell_cap_exist)
2298         {
2299                 network->unknown_cap_exist = true;
2300         }
2301         else
2302         {
2303                 network->unknown_cap_exist = false;
2304         }
2305         return 0;
2306 }
2307
2308 static inline u8 ieee80211_SignalStrengthTranslate(
2309         u8  CurrSS
2310         )
2311 {
2312         u8 RetSS;
2313
2314         // Step 1. Scale mapping.
2315         if(CurrSS >= 71 && CurrSS <= 100)
2316         {
2317                 RetSS = 90 + ((CurrSS - 70) / 3);
2318         }
2319         else if(CurrSS >= 41 && CurrSS <= 70)
2320         {
2321                 RetSS = 78 + ((CurrSS - 40) / 3);
2322         }
2323         else if(CurrSS >= 31 && CurrSS <= 40)
2324         {
2325                 RetSS = 66 + (CurrSS - 30);
2326         }
2327         else if(CurrSS >= 21 && CurrSS <= 30)
2328         {
2329                 RetSS = 54 + (CurrSS - 20);
2330         }
2331         else if(CurrSS >= 5 && CurrSS <= 20)
2332         {
2333                 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
2334         }
2335         else if(CurrSS == 4)
2336         {
2337                 RetSS = 36;
2338         }
2339         else if(CurrSS == 3)
2340         {
2341                 RetSS = 27;
2342         }
2343         else if(CurrSS == 2)
2344         {
2345                 RetSS = 18;
2346         }
2347         else if(CurrSS == 1)
2348         {
2349                 RetSS = 9;
2350         }
2351         else
2352         {
2353                 RetSS = CurrSS;
2354         }
2355         //RT_TRACE(COMP_DBG, DBG_LOUD, ("##### After Mapping:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2356
2357         // Step 2. Smoothing.
2358
2359         //RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$$$ After Smoothing:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
2360
2361         return RetSS;
2362 }
2363
2364 long ieee80211_translate_todbm(u8 signal_strength_index )// 0-100 index.
2365 {
2366         long    signal_power; // in dBm.
2367
2368         // Translate to dBm (x=0.5y-95).
2369         signal_power = (long)((signal_strength_index + 1) >> 1);
2370         signal_power -= 95;
2371
2372         return signal_power;
2373 }
2374
2375 static inline int ieee80211_network_init(
2376         struct ieee80211_device *ieee,
2377         struct ieee80211_probe_response *beacon,
2378         struct ieee80211_network *network,
2379         struct ieee80211_rx_stats *stats)
2380 {
2381 #ifdef CONFIG_IEEE80211_DEBUG
2382         //char rates_str[64];
2383         //char *p;
2384 #endif
2385
2386         network->qos_data.active = 0;
2387         network->qos_data.supported = 0;
2388         network->qos_data.param_count = 0;
2389         network->qos_data.old_param_count = 0;
2390
2391         /* Pull out fixed field data */
2392         memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
2393         network->capability = le16_to_cpu(beacon->capability);
2394         network->last_scanned = jiffies;
2395         network->time_stamp[0] = le32_to_cpu(beacon->time_stamp[0]);
2396         network->time_stamp[1] = le32_to_cpu(beacon->time_stamp[1]);
2397         network->beacon_interval = le32_to_cpu(beacon->beacon_interval);
2398         /* Where to pull this? beacon->listen_interval;*/
2399         network->listen_interval = 0x0A;
2400         network->rates_len = network->rates_ex_len = 0;
2401         network->last_associate = 0;
2402         network->ssid_len = 0;
2403         network->flags = 0;
2404         network->atim_window = 0;
2405         network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
2406             0x3 : 0x0;
2407         network->berp_info_valid = false;
2408         network->broadcom_cap_exist = false;
2409         network->ralink_cap_exist = false;
2410         network->atheros_cap_exist = false;
2411         network->marvell_cap_exist = false;
2412         network->cisco_cap_exist = false;
2413         network->unknown_cap_exist = false;
2414 #ifdef THOMAS_TURBO
2415         network->Turbo_Enable = 0;
2416 #endif
2417 #ifdef ENABLE_DOT11D
2418         network->CountryIeLen = 0;
2419         memset(network->CountryIeBuf, 0, MAX_IE_LEN);
2420 #endif
2421 //Initialize HT parameters
2422         //ieee80211_ht_initialize(&network->bssht);
2423         HTInitializeBssDesc(&network->bssht);
2424         if (stats->freq == IEEE80211_52GHZ_BAND) {
2425                 /* for A band (No DS info) */
2426                 network->channel = stats->received_channel;
2427         } else
2428                 network->flags |= NETWORK_HAS_CCK;
2429
2430         network->wpa_ie_len = 0;
2431         network->rsn_ie_len = 0;
2432
2433         if (ieee80211_parse_info_param
2434             (ieee,beacon->info_element, stats->len - sizeof(*beacon), network, stats))
2435                 return 1;
2436
2437         network->mode = 0;
2438         if (stats->freq == IEEE80211_52GHZ_BAND)
2439                 network->mode = IEEE_A;
2440         else {
2441                 if (network->flags & NETWORK_HAS_OFDM)
2442                         network->mode |= IEEE_G;
2443                 if (network->flags & NETWORK_HAS_CCK)
2444                         network->mode |= IEEE_B;
2445         }
2446
2447         if (network->mode == 0) {
2448                 IEEE80211_DEBUG_SCAN("Filtered out '%s (%pM)' "
2449                                      "network.\n",
2450                                      escape_essid(network->ssid,
2451                                                   network->ssid_len),
2452                                      network->bssid);
2453                 return 1;
2454         }
2455
2456         if(network->bssht.bdSupportHT){
2457                 if(network->mode == IEEE_A)
2458                         network->mode = IEEE_N_5G;
2459                 else if(network->mode & (IEEE_G | IEEE_B))
2460                         network->mode = IEEE_N_24G;
2461         }
2462         if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
2463                 network->flags |= NETWORK_EMPTY_ESSID;
2464
2465 #if 1
2466         stats->signal = 30 + (stats->SignalStrength * 70) / 100;
2467         //stats->signal = ieee80211_SignalStrengthTranslate(stats->signal);
2468         stats->noise = ieee80211_translate_todbm((u8)(100-stats->signal)) -25;
2469 #endif
2470
2471         memcpy(&network->stats, stats, sizeof(network->stats));
2472
2473         return 0;
2474 }
2475
2476 static inline int is_same_network(struct ieee80211_network *src,
2477                                   struct ieee80211_network *dst, struct ieee80211_device* ieee)
2478 {
2479         /* A network is only a duplicate if the channel, BSSID, ESSID
2480          * and the capability field (in particular IBSS and BSS) all match.
2481          * We treat all <hidden> with the same BSSID and channel
2482          * as one network */
2483         return //((src->ssid_len == dst->ssid_len) &&
2484                 (((src->ssid_len == dst->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) &&
2485                 (src->channel == dst->channel) &&
2486                 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
2487                 //!memcmp(src->ssid, dst->ssid, src->ssid_len) &&
2488                 (!memcmp(src->ssid, dst->ssid, src->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) &&
2489                 ((src->capability & WLAN_CAPABILITY_IBSS) ==
2490                 (dst->capability & WLAN_CAPABILITY_IBSS)) &&
2491                 ((src->capability & WLAN_CAPABILITY_BSS) ==
2492                 (dst->capability & WLAN_CAPABILITY_BSS)));
2493 }
2494
2495 static inline void update_network(struct ieee80211_network *dst,
2496                                   struct ieee80211_network *src)
2497 {
2498         int qos_active;
2499         u8 old_param;
2500
2501         memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
2502         dst->capability = src->capability;
2503         memcpy(dst->rates, src->rates, src->rates_len);
2504         dst->rates_len = src->rates_len;
2505         memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
2506         dst->rates_ex_len = src->rates_ex_len;
2507         if(src->ssid_len > 0)
2508         {
2509                 memset(dst->ssid, 0, dst->ssid_len);
2510                 dst->ssid_len = src->ssid_len;
2511                 memcpy(dst->ssid, src->ssid, src->ssid_len);
2512         }
2513         dst->mode = src->mode;
2514         dst->flags = src->flags;
2515         dst->time_stamp[0] = src->time_stamp[0];
2516         dst->time_stamp[1] = src->time_stamp[1];
2517         if (src->flags & NETWORK_HAS_ERP_VALUE)
2518         {
2519                 dst->erp_value = src->erp_value;
2520                 dst->berp_info_valid = src->berp_info_valid = true;
2521         }
2522         dst->beacon_interval = src->beacon_interval;
2523         dst->listen_interval = src->listen_interval;
2524         dst->atim_window = src->atim_window;
2525         dst->dtim_period = src->dtim_period;
2526         dst->dtim_data = src->dtim_data;
2527         dst->last_dtim_sta_time[0] = src->last_dtim_sta_time[0];
2528         dst->last_dtim_sta_time[1] = src->last_dtim_sta_time[1];
2529         memcpy(&dst->tim, &src->tim, sizeof(struct ieee80211_tim_parameters));
2530
2531         dst->bssht.bdSupportHT = src->bssht.bdSupportHT;
2532         dst->bssht.bdRT2RTAggregation = src->bssht.bdRT2RTAggregation;
2533         dst->bssht.bdHTCapLen= src->bssht.bdHTCapLen;
2534         memcpy(dst->bssht.bdHTCapBuf,src->bssht.bdHTCapBuf,src->bssht.bdHTCapLen);
2535         dst->bssht.bdHTInfoLen= src->bssht.bdHTInfoLen;
2536         memcpy(dst->bssht.bdHTInfoBuf,src->bssht.bdHTInfoBuf,src->bssht.bdHTInfoLen);
2537         dst->bssht.bdHTSpecVer = src->bssht.bdHTSpecVer;
2538         dst->bssht.bdRT2RTLongSlotTime = src->bssht.bdRT2RTLongSlotTime;
2539         dst->broadcom_cap_exist = src->broadcom_cap_exist;
2540         dst->ralink_cap_exist = src->ralink_cap_exist;
2541         dst->atheros_cap_exist = src->atheros_cap_exist;
2542         dst->marvell_cap_exist = src->marvell_cap_exist;
2543         dst->cisco_cap_exist = src->cisco_cap_exist;
2544         dst->unknown_cap_exist = src->unknown_cap_exist;
2545         memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
2546         dst->wpa_ie_len = src->wpa_ie_len;
2547         memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
2548         dst->rsn_ie_len = src->rsn_ie_len;
2549
2550         dst->last_scanned = jiffies;
2551         /* qos related parameters */
2552         //qos_active = src->qos_data.active;
2553         qos_active = dst->qos_data.active;
2554         //old_param = dst->qos_data.old_param_count;
2555         old_param = dst->qos_data.param_count;
2556         if(dst->flags & NETWORK_HAS_QOS_MASK){
2557         //not update QOS paramter in beacon, as most AP will set all these parameter to 0.//WB
2558         //      printk("====>%s(), aifs:%x, %x\n", __FUNCTION__, dst->qos_data.parameters.aifs[0], src->qos_data.parameters.aifs[0]);
2559         //      memcpy(&dst->qos_data, &src->qos_data,
2560         //              sizeof(struct ieee80211_qos_data));
2561         }
2562         else {
2563                 dst->qos_data.supported = src->qos_data.supported;
2564                 dst->qos_data.param_count = src->qos_data.param_count;
2565         }
2566
2567         if(dst->qos_data.supported == 1) {
2568                 dst->QoS_Enable = 1;
2569                 if(dst->ssid_len)
2570                         IEEE80211_DEBUG_QOS
2571                                 ("QoS the network %s is QoS supported\n",
2572                                 dst->ssid);
2573                 else
2574                         IEEE80211_DEBUG_QOS
2575                                 ("QoS the network is QoS supported\n");
2576         }
2577         dst->qos_data.active = qos_active;
2578         dst->qos_data.old_param_count = old_param;
2579
2580         /* dst->last_associate is not overwritten */
2581 #if 1
2582         dst->wmm_info = src->wmm_info; //sure to exist in beacon or probe response frame.
2583         if(src->wmm_param[0].ac_aci_acm_aifsn|| \
2584            src->wmm_param[1].ac_aci_acm_aifsn|| \
2585            src->wmm_param[2].ac_aci_acm_aifsn|| \
2586            src->wmm_param[1].ac_aci_acm_aifsn) {
2587           memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
2588         }
2589         //dst->QoS_Enable = src->QoS_Enable;
2590 #else
2591         dst->QoS_Enable = 1;//for Rtl8187 simulation
2592 #endif
2593 #ifdef THOMAS_TURBO
2594         dst->Turbo_Enable = src->Turbo_Enable;
2595 #endif
2596
2597 #ifdef ENABLE_DOT11D
2598         dst->CountryIeLen = src->CountryIeLen;
2599         memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
2600 #endif
2601
2602         //added by amy for LEAP
2603         dst->bWithAironetIE = src->bWithAironetIE;
2604         dst->bCkipSupported = src->bCkipSupported;
2605         memcpy(dst->CcxRmState,src->CcxRmState,2);
2606         dst->bCcxRmEnable = src->bCcxRmEnable;
2607         dst->MBssidMask = src->MBssidMask;
2608         dst->bMBssidValid = src->bMBssidValid;
2609         memcpy(dst->MBssid,src->MBssid,6);
2610         dst->bWithCcxVerNum = src->bWithCcxVerNum;
2611         dst->BssCcxVerNumber = src->BssCcxVerNumber;
2612
2613 }
2614
2615 static inline int is_beacon(__le16 fc)
2616 {
2617         return (WLAN_FC_GET_STYPE(le16_to_cpu(fc)) == IEEE80211_STYPE_BEACON);
2618 }
2619
2620 static inline void ieee80211_process_probe_response(
2621         struct ieee80211_device *ieee,
2622         struct ieee80211_probe_response *beacon,
2623         struct ieee80211_rx_stats *stats)
2624 {
2625         struct ieee80211_network network;
2626         struct ieee80211_network *target;
2627         struct ieee80211_network *oldest = NULL;
2628 #ifdef CONFIG_IEEE80211_DEBUG
2629         struct ieee80211_info_element *info_element = &beacon->info_element[0];
2630 #endif
2631         unsigned long flags;
2632         short renew;
2633         //u8 wmm_info;
2634
2635         memset(&network, 0, sizeof(struct ieee80211_network));
2636         IEEE80211_DEBUG_SCAN(
2637                 "'%s' (%pM): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
2638                 escape_essid(info_element->data, info_element->len),
2639                 beacon->header.addr3,
2640                 (beacon->capability & (1<<0xf)) ? '1' : '0',
2641                 (beacon->capability & (1<<0xe)) ? '1' : '0',
2642                 (beacon->capability & (1<<0xd)) ? '1' : '0',
2643                 (beacon->capability & (1<<0xc)) ? '1' : '0',
2644                 (beacon->capability & (1<<0xb)) ? '1' : '0',
2645                 (beacon->capability & (1<<0xa)) ? '1' : '0',
2646                 (beacon->capability & (1<<0x9)) ? '1' : '0',
2647                 (beacon->capability & (1<<0x8)) ? '1' : '0',
2648                 (beacon->capability & (1<<0x7)) ? '1' : '0',
2649                 (beacon->capability & (1<<0x6)) ? '1' : '0',
2650                 (beacon->capability & (1<<0x5)) ? '1' : '0',
2651                 (beacon->capability & (1<<0x4)) ? '1' : '0',
2652                 (beacon->capability & (1<<0x3)) ? '1' : '0',
2653                 (beacon->capability & (1<<0x2)) ? '1' : '0',
2654                 (beacon->capability & (1<<0x1)) ? '1' : '0',
2655                 (beacon->capability & (1<<0x0)) ? '1' : '0');
2656
2657         if (ieee80211_network_init(ieee, beacon, &network, stats)) {
2658                 IEEE80211_DEBUG_SCAN("Dropped '%s' (%pM) via %s.\n",
2659                                      escape_essid(info_element->data,
2660                                                   info_element->len),
2661                                      beacon->header.addr3,
2662                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
2663                                      IEEE80211_STYPE_PROBE_RESP ?
2664                                      "PROBE RESPONSE" : "BEACON");
2665                 return;
2666         }
2667
2668 #ifdef ENABLE_DOT11D
2669         // For Asus EeePc request,
2670         // (1) if wireless adapter receive get any 802.11d country code in AP beacon,
2671         //         wireless adapter should follow the country code.
2672         // (2)  If there is no any country code in beacon,
2673         //       then wireless adapter should do active scan from ch1~11 and
2674         //       passive scan from ch12~14
2675
2676         if( !IsLegalChannel(ieee, network.channel) )
2677                 return;
2678         if(ieee->bGlobalDomain)
2679         {
2680                 if (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_PROBE_RESP)
2681                 {
2682                         // Case 1: Country code
2683                         if(IS_COUNTRY_IE_VALID(ieee) )
2684                         {
2685                                 if( !IsLegalChannel(ieee, network.channel) )
2686                                 {
2687                                         printk("GetScanInfo(): For Country code, filter probe response at channel(%d).\n", network.channel);
2688                                         return;
2689                                 }
2690                         }
2691                         // Case 2: No any country code.
2692                         else
2693                         {
2694                                 // Filter over channel ch12~14
2695                                 if(network.channel > 11)
2696                                 {
2697                                         printk("GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n", network.channel);
2698                                         return;
2699                                 }
2700                         }
2701                 }
2702                 else
2703                 {
2704                         // Case 1: Country code
2705                         if(IS_COUNTRY_IE_VALID(ieee) )
2706                         {
2707                                 if( !IsLegalChannel(ieee, network.channel) )
2708                                 {
2709                                         printk("GetScanInfo(): For Country code, filter beacon at channel(%d).\n",network.channel);
2710                                         return;
2711                                 }
2712                         }
2713                         // Case 2: No any country code.
2714                         else
2715                         {
2716                                 // Filter over channel ch12~14
2717                                 if(network.channel > 14)
2718                                 {
2719                                         printk("GetScanInfo(): For Global Domain, filter beacon at channel(%d).\n",network.channel);
2720                                         return;
2721                                 }
2722                         }
2723                 }
2724         }
2725 #endif
2726
2727         /* The network parsed correctly -- so now we scan our known networks
2728          * to see if we can find it in our list.
2729          *
2730          * NOTE:  This search is definitely not optimized.  Once its doing
2731          *        the "right thing" we'll optimize it for efficiency if
2732          *        necessary */
2733
2734         /* Search for this entry in the list and update it if it is
2735          * already there. */
2736
2737         spin_lock_irqsave(&ieee->lock, flags);
2738
2739         if(is_same_network(&ieee->current_network, &network, ieee)) {
2740                 update_network(&ieee->current_network, &network);
2741                 if((ieee->current_network.mode == IEEE_N_24G || ieee->current_network.mode == IEEE_G)
2742                 && ieee->current_network.berp_info_valid){
2743                 if(ieee->current_network.erp_value& ERP_UseProtection)
2744                         ieee->current_network.buseprotection = true;
2745                 else
2746                         ieee->current_network.buseprotection = false;
2747                 }
2748                 if(is_beacon(beacon->header.frame_ctl))
2749                 {
2750                         if(ieee->state == IEEE80211_LINKED)
2751                                 ieee->LinkDetectInfo.NumRecvBcnInPeriod++;
2752                 }
2753                 else //hidden AP
2754                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & ieee->current_network.flags);
2755         }
2756
2757         list_for_each_entry(target, &ieee->network_list, list) {
2758                 if (is_same_network(target, &network, ieee))
2759                         break;
2760                 if ((oldest == NULL) ||
2761                     (target->last_scanned < oldest->last_scanned))
2762                         oldest = target;
2763         }
2764
2765         /* If we didn't find a match, then get a new network slot to initialize
2766          * with this beacon's information */
2767         if (&target->list == &ieee->network_list) {
2768                 if (list_empty(&ieee->network_free_list)) {
2769                         /* If there are no more slots, expire the oldest */
2770                         list_del(&oldest->list);
2771                         target = oldest;
2772                         IEEE80211_DEBUG_SCAN("Expired '%s' (%pM) from "
2773                                              "network list.\n",
2774                                              escape_essid(target->ssid,
2775                                                           target->ssid_len),
2776                                              target->bssid);
2777                 } else {
2778                         /* Otherwise just pull from the free list */
2779                         target = list_entry(ieee->network_free_list.next,
2780                                             struct ieee80211_network, list);
2781                         list_del(ieee->network_free_list.next);
2782                 }
2783
2784
2785 #ifdef CONFIG_IEEE80211_DEBUG
2786                 IEEE80211_DEBUG_SCAN("Adding '%s' (%pM) via %s.\n",
2787                                      escape_essid(network.ssid,
2788                                                   network.ssid_len),
2789                                      network.bssid,
2790                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
2791                                      IEEE80211_STYPE_PROBE_RESP ?
2792                                      "PROBE RESPONSE" : "BEACON");
2793 #endif
2794                 memcpy(target, &network, sizeof(*target));
2795                 list_add_tail(&target->list, &ieee->network_list);
2796                 if(ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
2797                         ieee80211_softmac_new_net(ieee,&network);
2798         } else {
2799                 IEEE80211_DEBUG_SCAN("Updating '%s' (%pM) via %s.\n",
2800                                      escape_essid(target->ssid,
2801                                                   target->ssid_len),
2802                                      target->bssid,
2803                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
2804                                      IEEE80211_STYPE_PROBE_RESP ?
2805                                      "PROBE RESPONSE" : "BEACON");
2806
2807                 /* we have an entry and we are going to update it. But this entry may
2808                  * be already expired. In this case we do the same as we found a new
2809                  * net and call the new_net handler
2810                  */
2811                 renew = !time_after(target->last_scanned + ieee->scan_age, jiffies);
2812                 //YJ,add,080819,for hidden ap
2813                 if(is_beacon(beacon->header.frame_ctl) == 0)
2814                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & target->flags);
2815                 //if(strncmp(network.ssid, "linksys-c",9) == 0)
2816                 //      printk("====>2 network.ssid=%s FLAG=%d target.ssid=%s FLAG=%d\n", network.ssid, network.flags, target->ssid, target->flags);
2817                 if(((network.flags & NETWORK_EMPTY_ESSID) == NETWORK_EMPTY_ESSID) \
2818                     && (((network.ssid_len > 0) && (strncmp(target->ssid, network.ssid, network.ssid_len)))\
2819                     ||((ieee->current_network.ssid_len == network.ssid_len)&&(strncmp(ieee->current_network.ssid, network.ssid, network.ssid_len) == 0)&&(ieee->state == IEEE80211_NOLINK))))
2820                         renew = 1;
2821                 //YJ,add,080819,for hidden ap,end
2822
2823                 update_network(target, &network);
2824                 if(renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
2825                         ieee80211_softmac_new_net(ieee,&network);
2826         }
2827
2828         spin_unlock_irqrestore(&ieee->lock, flags);
2829         if (is_beacon(beacon->header.frame_ctl)&&is_same_network(&ieee->current_network, &network, ieee)&&\
2830                 (ieee->state == IEEE80211_LINKED)) {
2831                 if(ieee->handle_beacon != NULL) {
2832                         ieee->handle_beacon(ieee->dev,beacon,&ieee->current_network);
2833                 }
2834         }
2835 }
2836
2837 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
2838                       struct ieee80211_hdr_4addr *header,
2839                       struct ieee80211_rx_stats *stats)
2840 {
2841 #if 0
2842         if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
2843                                 ieee->iw_mode == IW_MODE_INFRA &&
2844                                 ieee->state == IEEE80211_LINKED))
2845         {
2846                 tasklet_schedule(&ieee->ps_task);
2847         }
2848 #endif
2849
2850         if(WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_PROBE_RESP &&
2851                 WLAN_FC_GET_STYPE(header->frame_ctl) != IEEE80211_STYPE_BEACON)
2852                 ieee->last_rx_ps_time = jiffies;
2853
2854         switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
2855
2856         case IEEE80211_STYPE_BEACON:
2857                 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
2858                                      WLAN_FC_GET_STYPE(header->frame_ctl));
2859                 IEEE80211_DEBUG_SCAN("Beacon\n");
2860                 ieee80211_process_probe_response(
2861                         ieee, (struct ieee80211_probe_response *)header, stats);
2862
2863                 //printk("----------->%s()\n", __func__);
2864                 if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
2865                                         ieee->iw_mode == IW_MODE_INFRA &&
2866                                         ieee->state == IEEE80211_LINKED))
2867                 {
2868                         tasklet_schedule(&ieee->ps_task);
2869                 }
2870
2871                 break;
2872
2873         case IEEE80211_STYPE_PROBE_RESP:
2874                 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
2875                                      WLAN_FC_GET_STYPE(header->frame_ctl));
2876                 IEEE80211_DEBUG_SCAN("Probe response\n");
2877                 ieee80211_process_probe_response(
2878                         ieee, (struct ieee80211_probe_response *)header, stats);
2879                 break;
2880
2881         }
2882 }
2883
2884 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2885 //EXPORT_SYMBOL(ieee80211_rx_mgt);
2886 //EXPORT_SYMBOL(ieee80211_rx);
2887 #else
2888 EXPORT_SYMBOL_NOVERS(ieee80211_rx_mgt);
2889 EXPORT_SYMBOL_NOVERS(ieee80211_rx);
2890 #endif