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