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