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