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