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