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