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