367ee47a4b0298ab898f9a748317208c183e8127
[safe/jmp/linux-2.6] / drivers / staging / rtl8187se / 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 #include "dot11d.h"
48 static inline void ieee80211_monitor_rx(struct ieee80211_device *ieee,
49                                         struct sk_buff *skb,
50                                         struct ieee80211_rx_stats *rx_stats)
51 {
52         struct ieee80211_hdr_4addr *hdr =
53                 (struct ieee80211_hdr_4addr *)skb->data;
54         u16 fc = le16_to_cpu(hdr->frame_ctl);
55
56         skb->dev = ieee->dev;
57         skb_reset_mac_header(skb);
58         skb_pull(skb, ieee80211_get_hdrlen(fc));
59         skb->pkt_type = PACKET_OTHERHOST;
60         skb->protocol = __constant_htons(ETH_P_80211_RAW);
61         memset(skb->cb, 0, sizeof(skb->cb));
62         netif_rx(skb);
63 }
64
65
66 /* Called only as a tasklet (software IRQ) */
67 static struct ieee80211_frag_entry *
68 ieee80211_frag_cache_find(struct ieee80211_device *ieee, unsigned int seq,
69                           unsigned int frag, u8 tid,u8 *src, u8 *dst)
70 {
71         struct ieee80211_frag_entry *entry;
72         int i;
73
74         for (i = 0; i < IEEE80211_FRAG_CACHE_LEN; i++) {
75                 entry = &ieee->frag_cache[tid][i];
76                 if (entry->skb != NULL &&
77                     time_after(jiffies, entry->first_frag_time + 2 * HZ)) {
78                         IEEE80211_DEBUG_FRAG(
79                                 "expiring fragment cache entry "
80                                 "seq=%u last_frag=%u\n",
81                                 entry->seq, entry->last_frag);
82                         dev_kfree_skb_any(entry->skb);
83                         entry->skb = NULL;
84                 }
85
86                 if (entry->skb != NULL && entry->seq == seq &&
87                     (entry->last_frag + 1 == frag || frag == -1) &&
88                     memcmp(entry->src_addr, src, ETH_ALEN) == 0 &&
89                     memcmp(entry->dst_addr, dst, ETH_ALEN) == 0)
90                         return entry;
91         }
92
93         return NULL;
94 }
95
96 /* Called only as a tasklet (software IRQ) */
97 static struct sk_buff *
98 ieee80211_frag_cache_get(struct ieee80211_device *ieee,
99                          struct ieee80211_hdr_4addr *hdr)
100 {
101         struct sk_buff *skb = NULL;
102         u16 fc = le16_to_cpu(hdr->frame_ctl);
103         u16 sc = le16_to_cpu(hdr->seq_ctl);
104         unsigned int frag = WLAN_GET_SEQ_FRAG(sc);
105         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
106         struct ieee80211_frag_entry *entry;
107         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
108         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
109         u8 tid;
110
111         if (((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
112           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)hdr;
113           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
114           tid = UP2AC(tid);
115           tid ++;
116         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
117           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS *)hdr;
118           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
119           tid = UP2AC(tid);
120           tid ++;
121         } else {
122           tid = 0;
123         }
124
125         if (frag == 0) {
126                 /* Reserve enough space to fit maximum frame length */
127                 skb = dev_alloc_skb(ieee->dev->mtu +
128                                     sizeof(struct ieee80211_hdr_4addr) +
129                                     8 /* LLC */ +
130                                     2 /* alignment */ +
131                                     8 /* WEP */ +
132                                     ETH_ALEN /* WDS */ +
133                                     (IEEE80211_QOS_HAS_SEQ(fc)?2:0) /* QOS Control */);
134                 if (skb == NULL)
135                         return NULL;
136
137                 entry = &ieee->frag_cache[tid][ieee->frag_next_idx[tid]];
138                 ieee->frag_next_idx[tid]++;
139                 if (ieee->frag_next_idx[tid] >= IEEE80211_FRAG_CACHE_LEN)
140                         ieee->frag_next_idx[tid] = 0;
141
142                 if (entry->skb != NULL)
143                         dev_kfree_skb_any(entry->skb);
144
145                 entry->first_frag_time = jiffies;
146                 entry->seq = seq;
147                 entry->last_frag = frag;
148                 entry->skb = skb;
149                 memcpy(entry->src_addr, hdr->addr2, ETH_ALEN);
150                 memcpy(entry->dst_addr, hdr->addr1, ETH_ALEN);
151         } else {
152                 /* received a fragment of a frame for which the head fragment
153                  * should have already been received */
154                 entry = ieee80211_frag_cache_find(ieee, seq, frag, tid,hdr->addr2,
155                                                   hdr->addr1);
156                 if (entry != NULL) {
157                         entry->last_frag = frag;
158                         skb = entry->skb;
159                 }
160         }
161
162         return skb;
163 }
164
165
166 /* Called only as a tasklet (software IRQ) */
167 static int ieee80211_frag_cache_invalidate(struct ieee80211_device *ieee,
168                                            struct ieee80211_hdr_4addr *hdr)
169 {
170         u16 fc = le16_to_cpu(hdr->frame_ctl);
171         u16 sc = le16_to_cpu(hdr->seq_ctl);
172         unsigned int seq = WLAN_GET_SEQ_SEQ(sc);
173         struct ieee80211_frag_entry *entry;
174         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
175         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
176         u8 tid;
177
178         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
179           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)hdr;
180           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
181           tid = UP2AC(tid);
182           tid ++;
183         } else if (IEEE80211_QOS_HAS_SEQ(fc)) {
184           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS *)hdr;
185           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
186           tid = UP2AC(tid);
187           tid ++;
188         } else {
189           tid = 0;
190         }
191
192         entry = ieee80211_frag_cache_find(ieee, seq, -1, tid,hdr->addr2,
193                                           hdr->addr1);
194
195         if (entry == NULL) {
196                 IEEE80211_DEBUG_FRAG(
197                         "could not invalidate fragment cache "
198                         "entry (seq=%u)\n", seq);
199                 return -1;
200         }
201
202         entry->skb = NULL;
203         return 0;
204 }
205
206
207
208 /* ieee80211_rx_frame_mgtmt
209  *
210  * Responsible for handling management control frames
211  *
212  * Called by ieee80211_rx */
213 static inline int
214 ieee80211_rx_frame_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb,
215                         struct ieee80211_rx_stats *rx_stats, u16 type,
216                         u16 stype)
217 {
218         struct ieee80211_hdr_4addr *hdr;
219
220         // cheat the the hdr type
221         hdr = (struct ieee80211_hdr_4addr *)skb->data;
222
223         /* On the struct stats definition there is written that
224          * this is not mandatory.... but seems that the probe
225          * response parser uses it
226          */
227         rx_stats->len = skb->len;
228         ieee80211_rx_mgt(ieee, (struct ieee80211_hdr_4addr *)skb->data,
229                          rx_stats);
230
231         if((ieee->state == IEEE80211_LINKED)&&(memcmp(hdr->addr3,ieee->current_network.bssid,ETH_ALEN))) {
232                 dev_kfree_skb_any(skb);
233                 return 0;
234         }
235
236         ieee80211_rx_frame_softmac(ieee, skb, rx_stats, type, stype);
237
238         dev_kfree_skb_any(skb);
239
240         return 0;
241
242 }
243
244
245
246 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
247 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
248 static unsigned char rfc1042_header[] =
249 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
250 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
251 static unsigned char bridge_tunnel_header[] =
252 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
253 /* No encapsulation header if EtherType < 0x600 (=length) */
254
255 /* Called by ieee80211_rx_frame_decrypt */
256 static int ieee80211_is_eapol_frame(struct ieee80211_device *ieee,
257                                     struct sk_buff *skb, size_t hdrlen)
258 {
259         struct net_device *dev = ieee->dev;
260         u16 fc, ethertype;
261         struct ieee80211_hdr_4addr *hdr;
262         u8 *pos;
263
264         if (skb->len < 24)
265                 return 0;
266
267         hdr = (struct ieee80211_hdr_4addr *)skb->data;
268         fc = le16_to_cpu(hdr->frame_ctl);
269
270         /* check that the frame is unicast frame to us */
271         if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
272             IEEE80211_FCTL_TODS &&
273             memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0 &&
274             memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0) {
275                 /* ToDS frame with own addr BSSID and DA */
276         } else if ((fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
277                    IEEE80211_FCTL_FROMDS &&
278                    memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) == 0) {
279                 /* FromDS frame with own addr as DA */
280         } else
281                 return 0;
282
283         if (skb->len < 24 + 8)
284                 return 0;
285
286         /* check for port access entity Ethernet type */
287 //      pos = skb->data + 24;
288         pos = skb->data + hdrlen;
289         ethertype = (pos[6] << 8) | pos[7];
290         if (ethertype == ETH_P_PAE)
291                 return 1;
292
293         return 0;
294 }
295
296 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
297 static inline int
298 ieee80211_rx_frame_decrypt(struct ieee80211_device* ieee, struct sk_buff *skb,
299                            struct ieee80211_crypt_data *crypt)
300 {
301         struct ieee80211_hdr_4addr *hdr;
302         int res, hdrlen;
303
304         if (crypt == NULL || crypt->ops->decrypt_mpdu == NULL)
305                 return 0;
306
307         hdr = (struct ieee80211_hdr_4addr *)skb->data;
308         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
309
310 #ifdef CONFIG_IEEE80211_CRYPT_TKIP
311         if (ieee->tkip_countermeasures &&
312             strcmp(crypt->ops->name, "TKIP") == 0) {
313                 if (net_ratelimit()) {
314                         printk(KERN_DEBUG "%s: TKIP countermeasures: dropped "
315                                "received packet from " MAC_FMT "\n",
316                                ieee->dev->name, MAC_ARG(hdr->addr2));
317                 }
318                 return -1;
319         }
320 #endif
321
322         atomic_inc(&crypt->refcnt);
323         res = crypt->ops->decrypt_mpdu(skb, hdrlen, crypt->priv);
324         atomic_dec(&crypt->refcnt);
325         if (res < 0) {
326                 IEEE80211_DEBUG_DROP(
327                         "decryption failed (SA=" MAC_FMT
328                         ") res=%d\n", MAC_ARG(hdr->addr2), res);
329                 if (res == -2)
330                         IEEE80211_DEBUG_DROP("Decryption failed ICV "
331                                              "mismatch (key %d)\n",
332                                              skb->data[hdrlen + 3] >> 6);
333                 ieee->ieee_stats.rx_discards_undecryptable++;
334                 return -1;
335         }
336
337         return res;
338 }
339
340
341 /* Called only as a tasklet (software IRQ), by ieee80211_rx */
342 static inline int
343 ieee80211_rx_frame_decrypt_msdu(struct ieee80211_device* ieee, struct sk_buff *skb,
344                              int keyidx, struct ieee80211_crypt_data *crypt)
345 {
346         struct ieee80211_hdr_4addr *hdr;
347         int res, hdrlen;
348
349         if (crypt == NULL || crypt->ops->decrypt_msdu == NULL)
350                 return 0;
351
352         hdr = (struct ieee80211_hdr_4addr *)skb->data;
353         hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_ctl));
354
355         atomic_inc(&crypt->refcnt);
356         res = crypt->ops->decrypt_msdu(skb, keyidx, hdrlen, crypt->priv);
357         atomic_dec(&crypt->refcnt);
358         if (res < 0) {
359                 printk(KERN_DEBUG "%s: MSDU decryption/MIC verification failed"
360                        " (SA=" MAC_FMT " keyidx=%d)\n",
361                        ieee->dev->name, MAC_ARG(hdr->addr2), keyidx);
362                 return -1;
363         }
364
365         return 0;
366 }
367
368
369 /* this function is stolen from ipw2200 driver*/
370 #define IEEE_PACKET_RETRY_TIME (5*HZ)
371 static int is_duplicate_packet(struct ieee80211_device *ieee,
372                                       struct ieee80211_hdr_4addr *header)
373 {
374         u16 fc = le16_to_cpu(header->frame_ctl);
375         u16 sc = le16_to_cpu(header->seq_ctl);
376         u16 seq = WLAN_GET_SEQ_SEQ(sc);
377         u16 frag = WLAN_GET_SEQ_FRAG(sc);
378         u16 *last_seq, *last_frag;
379         unsigned long *last_time;
380         struct ieee80211_hdr_3addr_QOS *hdr_3addr_QoS;
381         struct ieee80211_hdr_QOS *hdr_4addr_QoS;
382         u8 tid;
383
384         //TO2DS and QoS
385         if(((fc & IEEE80211_FCTL_DSTODS) == IEEE80211_FCTL_DSTODS)&&IEEE80211_QOS_HAS_SEQ(fc)) {
386           hdr_4addr_QoS = (struct ieee80211_hdr_QOS *)header;
387           tid = le16_to_cpu(hdr_4addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
388           tid = UP2AC(tid);
389           tid ++;
390         } else if(IEEE80211_QOS_HAS_SEQ(fc)) { //QoS
391           hdr_3addr_QoS = (struct ieee80211_hdr_3addr_QOS*)header;
392           tid = le16_to_cpu(hdr_3addr_QoS->QOS_ctl) & IEEE80211_QOS_TID;
393           tid = UP2AC(tid);
394           tid ++;
395         } else { // no QoS
396           tid = 0;
397         }
398         switch (ieee->iw_mode) {
399         case IW_MODE_ADHOC:
400         {
401                 struct list_head *p;
402                 struct ieee_ibss_seq *entry = NULL;
403                 u8 *mac = header->addr2;
404                 int index = mac[5] % IEEE_IBSS_MAC_HASH_SIZE;
405                 //for (pos = (head)->next; pos != (head); pos = pos->next)
406                 __list_for_each(p, &ieee->ibss_mac_hash[index]) {
407                         entry = list_entry(p, struct ieee_ibss_seq, list);
408                         if (!memcmp(entry->mac, mac, ETH_ALEN))
409                                 break;
410                 }
411         //      if (memcmp(entry->mac, mac, ETH_ALEN)){
412                 if (p == &ieee->ibss_mac_hash[index]) {
413                         entry = kmalloc(sizeof(struct ieee_ibss_seq), GFP_ATOMIC);
414                         if (!entry) {
415                                 printk(KERN_WARNING "Cannot malloc new mac entry\n");
416                                 return 0;
417                         }
418                         memcpy(entry->mac, mac, ETH_ALEN);
419                         entry->seq_num[tid] = seq;
420                         entry->frag_num[tid] = frag;
421                         entry->packet_time[tid] = jiffies;
422                         list_add(&entry->list, &ieee->ibss_mac_hash[index]);
423                         return 0;
424                 }
425                 last_seq = &entry->seq_num[tid];
426                 last_frag = &entry->frag_num[tid];
427                 last_time = &entry->packet_time[tid];
428                 break;
429         }
430
431         case IW_MODE_INFRA:
432                 last_seq = &ieee->last_rxseq_num[tid];
433                 last_frag = &ieee->last_rxfrag_num[tid];
434                 last_time = &ieee->last_packet_time[tid];
435
436                 break;
437         default:
438                 return 0;
439         }
440
441 //      if(tid != 0) {
442 //              printk(KERN_WARNING ":)))))))))))%x %x %x, fc(%x)\n", tid, *last_seq, seq, header->frame_ctl);
443 //      }
444         if ((*last_seq == seq) &&
445             time_after(*last_time + IEEE_PACKET_RETRY_TIME, jiffies)) {
446                 if (*last_frag == frag){
447                         //printk(KERN_WARNING "[1] go drop!\n");
448                         goto drop;
449
450                 }
451                 if (*last_frag + 1 != frag)
452                         /* out-of-order fragment */
453                         //printk(KERN_WARNING "[2] go drop!\n");
454                         goto drop;
455         } else
456                 *last_seq = seq;
457
458         *last_frag = frag;
459         *last_time = jiffies;
460         return 0;
461
462 drop:
463 //      BUG_ON(!(fc & IEEE80211_FCTL_RETRY));
464 //      printk("DUP\n");
465
466         return 1;
467 }
468
469
470 /* All received frames are sent to this function. @skb contains the frame in
471  * IEEE 802.11 format, i.e., in the format it was sent over air.
472  * This function is called only as a tasklet (software IRQ). */
473 int ieee80211_rx(struct ieee80211_device *ieee, struct sk_buff *skb,
474                  struct ieee80211_rx_stats *rx_stats)
475 {
476         struct net_device *dev = ieee->dev;
477         //struct r8180_priv *priv = (struct r8180_priv *)ieee80211_priv(dev);
478         struct ieee80211_hdr_4addr *hdr;
479         //struct ieee80211_hdr_3addr_QOS *hdr;
480
481         size_t hdrlen;
482         u16 fc, type, stype, sc;
483         struct net_device_stats *stats;
484         unsigned int frag;
485         u8 *payload;
486         u16 ethertype;
487 //      u16 QOS_ctl = 0;
488         u8 dst[ETH_ALEN];
489         u8 src[ETH_ALEN];
490         u8 bssid[ETH_ALEN];
491         struct ieee80211_crypt_data *crypt = NULL;
492         int keyidx = 0;
493
494         // cheat the the hdr type
495         hdr = (struct ieee80211_hdr_4addr *)skb->data;
496         stats = &ieee->stats;
497
498         if (skb->len < 10) {
499                 printk(KERN_INFO "%s: SKB length < 10\n",
500                        dev->name);
501                 goto rx_dropped;
502         }
503
504         fc = le16_to_cpu(hdr->frame_ctl);
505         type = WLAN_FC_GET_TYPE(fc);
506         stype = WLAN_FC_GET_STYPE(fc);
507         sc = le16_to_cpu(hdr->seq_ctl);
508
509         frag = WLAN_GET_SEQ_FRAG(sc);
510
511 //YJ,add,080828,for keep alive
512         if((fc & IEEE80211_FCTL_TODS) != IEEE80211_FCTL_TODS)
513         {
514                 if(!memcmp(hdr->addr1,dev->dev_addr, ETH_ALEN))
515                 {
516                         ieee->NumRxUnicast++;
517                 }
518         }
519         else
520         {
521                 if(!memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN))
522                 {
523                         ieee->NumRxUnicast++;
524                 }
525         }
526 //YJ,add,080828,for keep alive,end
527
528         hdrlen = ieee80211_get_hdrlen(fc);
529
530
531         if (ieee->iw_mode == IW_MODE_MONITOR) {
532                 ieee80211_monitor_rx(ieee, skb, rx_stats);
533                 stats->rx_packets++;
534                 stats->rx_bytes += skb->len;
535                 return 1;
536         }
537
538         if (ieee->host_decrypt) {
539                 int idx = 0;
540                 if (skb->len >= hdrlen + 3)
541                         idx = skb->data[hdrlen + 3] >> 6;
542                 crypt = ieee->crypt[idx];
543
544                 /* allow NULL decrypt to indicate an station specific override
545                  * for default encryption */
546                 if (crypt && (crypt->ops == NULL ||
547                               crypt->ops->decrypt_mpdu == NULL))
548                         crypt = NULL;
549
550                 if (!crypt && (fc & IEEE80211_FCTL_WEP)) {
551                         /* This seems to be triggered by some (multicast?)
552                          * frames from other than current BSS, so just drop the
553                          * frames silently instead of filling system log with
554                          * these reports. */
555                         IEEE80211_DEBUG_DROP("Decryption failed (not set)"
556                                              " (SA=" MAC_FMT ")\n",
557                                              MAC_ARG(hdr->addr2));
558                         ieee->ieee_stats.rx_discards_undecryptable++;
559                         goto rx_dropped;
560                 }
561         }
562
563         if (skb->len < IEEE80211_DATA_HDR3_LEN)
564                 goto rx_dropped;
565
566         // if QoS enabled, should check the sequence for each of the AC
567         if (is_duplicate_packet(ieee, hdr))
568                 goto rx_dropped;
569
570
571         if (type == IEEE80211_FTYPE_MGMT) {
572                 if (ieee80211_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
573                         goto rx_dropped;
574                 else
575                         goto rx_exit;
576         }
577
578         /* Data frame - extract src/dst addresses */
579         switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
580         case IEEE80211_FCTL_FROMDS:
581                 memcpy(dst, hdr->addr1, ETH_ALEN);
582                 memcpy(src, hdr->addr3, ETH_ALEN);
583                 memcpy(bssid,hdr->addr2,ETH_ALEN);
584                 break;
585         case IEEE80211_FCTL_TODS:
586                 memcpy(dst, hdr->addr3, ETH_ALEN);
587                 memcpy(src, hdr->addr2, ETH_ALEN);
588                 memcpy(bssid,hdr->addr1,ETH_ALEN);
589                 break;
590         case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
591                 if (skb->len < IEEE80211_DATA_HDR4_LEN)
592                         goto rx_dropped;
593                 memcpy(dst, hdr->addr3, ETH_ALEN);
594                 memcpy(src, hdr->addr4, ETH_ALEN);
595                 memcpy(bssid, ieee->current_network.bssid, ETH_ALEN);
596                 break;
597         case 0:
598                 memcpy(dst, hdr->addr1, ETH_ALEN);
599                 memcpy(src, hdr->addr2, ETH_ALEN);
600                 memcpy(bssid,hdr->addr3,ETH_ALEN);
601                 break;
602         }
603
604
605         dev->last_rx = jiffies;
606
607
608         /* Nullfunc frames may have PS-bit set, so they must be passed to
609          * hostap_handle_sta_rx() before being dropped here. */
610         if (stype != IEEE80211_STYPE_DATA &&
611             stype != IEEE80211_STYPE_DATA_CFACK &&
612             stype != IEEE80211_STYPE_DATA_CFPOLL &&
613             stype != IEEE80211_STYPE_DATA_CFACKPOLL&&
614             stype != IEEE80211_STYPE_QOS_DATA//add by David,2006.8.4
615             ) {
616                 if (stype != IEEE80211_STYPE_NULLFUNC)
617                         IEEE80211_DEBUG_DROP(
618                                 "RX: dropped data frame "
619                                 "with no data (type=0x%02x, "
620                                 "subtype=0x%02x, len=%d)\n",
621                                 type, stype, skb->len);
622                 goto rx_dropped;
623         }
624         if(memcmp(bssid,ieee->current_network.bssid,ETH_ALEN)) {
625                 goto rx_dropped;
626         }
627
628         ieee->NumRxDataInPeriod++;
629         ieee->NumRxOkTotal++;
630         /* skb: hdr + (possibly fragmented, possibly encrypted) payload */
631
632         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
633             (keyidx = ieee80211_rx_frame_decrypt(ieee, skb, crypt)) < 0)
634                 goto rx_dropped;
635
636         hdr = (struct ieee80211_hdr_4addr *)skb->data;
637
638         /* skb: hdr + (possibly fragmented) plaintext payload */
639         // PR: FIXME: hostap has additional conditions in the "if" below:
640         // ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
641         if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
642                 int flen;
643                 struct sk_buff *frag_skb = ieee80211_frag_cache_get(ieee, hdr);
644                 IEEE80211_DEBUG_FRAG("Rx Fragment received (%u)\n", frag);
645
646                 if (!frag_skb) {
647                         IEEE80211_DEBUG(IEEE80211_DL_RX | IEEE80211_DL_FRAG,
648                                         "Rx cannot get skb from fragment "
649                                         "cache (morefrag=%d seq=%u frag=%u)\n",
650                                         (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
651                                         WLAN_GET_SEQ_SEQ(sc), frag);
652                         goto rx_dropped;
653                 }
654                 flen = skb->len;
655                 if (frag != 0)
656                         flen -= hdrlen;
657
658                 if (frag_skb->tail + flen > frag_skb->end) {
659                         printk(KERN_WARNING "%s: host decrypted and "
660                                "reassembled frame did not fit skb\n",
661                                dev->name);
662                         ieee80211_frag_cache_invalidate(ieee, hdr);
663                         goto rx_dropped;
664                 }
665
666                 if (frag == 0) {
667                         /* copy first fragment (including full headers) into
668                          * beginning of the fragment cache skb */
669                         memcpy(skb_put(frag_skb, flen), skb->data, flen);
670                 } else {
671                         /* append frame payload to the end of the fragment
672                          * cache skb */
673                         memcpy(skb_put(frag_skb, flen), skb->data + hdrlen,
674                                flen);
675                 }
676                 dev_kfree_skb_any(skb);
677                 skb = NULL;
678
679                 if (fc & IEEE80211_FCTL_MOREFRAGS) {
680                         /* more fragments expected - leave the skb in fragment
681                          * cache for now; it will be delivered to upper layers
682                          * after all fragments have been received */
683                         goto rx_exit;
684                 }
685
686                 /* this was the last fragment and the frame will be
687                  * delivered, so remove skb from fragment cache */
688                 skb = frag_skb;
689                 hdr = (struct ieee80211_hdr_4addr *)skb->data;
690                 ieee80211_frag_cache_invalidate(ieee, hdr);
691         }
692
693         /* skb: hdr + (possible reassembled) full MSDU payload; possibly still
694          * encrypted/authenticated */
695         if (ieee->host_decrypt && (fc & IEEE80211_FCTL_WEP) &&
696             ieee80211_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt))
697                 goto rx_dropped;
698
699         hdr = (struct ieee80211_hdr_4addr *)skb->data;
700         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep) {
701                 if (/*ieee->ieee802_1x &&*/
702                     ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
703
704 #ifdef CONFIG_IEEE80211_DEBUG
705                         /* pass unencrypted EAPOL frames even if encryption is
706                          * configured */
707                         struct eapol *eap = (struct eapol *)(skb->data +
708                                 24);
709                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
710                                                 eap_get_type(eap->type));
711 #endif
712                 } else {
713                         IEEE80211_DEBUG_DROP(
714                                 "encryption configured, but RX "
715                                 "frame not encrypted (SA=" MAC_FMT ")\n",
716                                 MAC_ARG(hdr->addr2));
717                         goto rx_dropped;
718                 }
719         }
720
721 #ifdef CONFIG_IEEE80211_DEBUG
722         if (crypt && !(fc & IEEE80211_FCTL_WEP) &&
723             ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
724                         struct eapol *eap = (struct eapol *)(skb->data +
725                                 24);
726                         IEEE80211_DEBUG_EAP("RX: IEEE 802.1X EAPOL frame: %s\n",
727                                                 eap_get_type(eap->type));
728         }
729 #endif
730
731         if (crypt && !(fc & IEEE80211_FCTL_WEP) && !ieee->open_wep &&
732             !ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
733                 IEEE80211_DEBUG_DROP(
734                         "dropped unencrypted RX data "
735                         "frame from " MAC_FMT
736                         " (drop_unencrypted=1)\n",
737                         MAC_ARG(hdr->addr2));
738                 goto rx_dropped;
739         }
740 /*
741         if(ieee80211_is_eapol_frame(ieee, skb, hdrlen)) {
742                 printk(KERN_WARNING "RX: IEEE802.1X EPAOL frame!\n");
743         }
744 */
745         /* skb: hdr + (possible reassembled) full plaintext payload */
746         payload = skb->data + hdrlen;
747         ethertype = (payload[6] << 8) | payload[7];
748
749
750         /* convert hdr + possible LLC headers into Ethernet header */
751         if (skb->len - hdrlen >= 8 &&
752             ((memcmp(payload, rfc1042_header, SNAP_SIZE) == 0 &&
753               ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
754              memcmp(payload, bridge_tunnel_header, SNAP_SIZE) == 0)) {
755                 /* remove RFC1042 or Bridge-Tunnel encapsulation and
756                  * replace EtherType */
757                 skb_pull(skb, hdrlen + SNAP_SIZE);
758                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
759                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
760         } else {
761                 u16 len;
762                 /* Leave Ethernet header part of hdr and full payload */
763                 skb_pull(skb, hdrlen);
764                 len = htons(skb->len);
765                 memcpy(skb_push(skb, 2), &len, 2);
766                 memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
767                 memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
768         }
769
770
771         stats->rx_packets++;
772         stats->rx_bytes += skb->len;
773
774         if (skb) {
775                 skb->protocol = eth_type_trans(skb, dev);
776                 memset(skb->cb, 0, sizeof(skb->cb));
777                 skb->dev = dev;
778                 skb->ip_summed = CHECKSUM_NONE; /* 802.11 crc not sufficient */
779                 ieee->last_rx_ps_time = jiffies;
780                 netif_rx(skb);
781         }
782
783  rx_exit:
784         return 1;
785
786  rx_dropped:
787         stats->rx_dropped++;
788
789         /* Returning 0 indicates to caller that we have not handled the SKB--
790          * so it is still allocated and can be used again by underlying
791          * hardware as a DMA target */
792         return 0;
793 }
794
795 #define MGMT_FRAME_FIXED_PART_LENGTH            0x24
796
797 static inline int ieee80211_is_ofdm_rate(u8 rate)
798 {
799         switch (rate & ~IEEE80211_BASIC_RATE_MASK) {
800         case IEEE80211_OFDM_RATE_6MB:
801         case IEEE80211_OFDM_RATE_9MB:
802         case IEEE80211_OFDM_RATE_12MB:
803         case IEEE80211_OFDM_RATE_18MB:
804         case IEEE80211_OFDM_RATE_24MB:
805         case IEEE80211_OFDM_RATE_36MB:
806         case IEEE80211_OFDM_RATE_48MB:
807         case IEEE80211_OFDM_RATE_54MB:
808                 return 1;
809         }
810         return 0;
811 }
812
813 static inline int ieee80211_SignalStrengthTranslate(
814         int  CurrSS
815         )
816 {
817         int RetSS;
818
819         // Step 1. Scale mapping.
820         if(CurrSS >= 71 && CurrSS <= 100)
821         {
822                 RetSS = 90 + ((CurrSS - 70) / 3);
823         }
824         else if(CurrSS >= 41 && CurrSS <= 70)
825         {
826                 RetSS = 78 + ((CurrSS - 40) / 3);
827         }
828         else if(CurrSS >= 31 && CurrSS <= 40)
829         {
830                 RetSS = 66 + (CurrSS - 30);
831         }
832         else if(CurrSS >= 21 && CurrSS <= 30)
833         {
834                 RetSS = 54 + (CurrSS - 20);
835         }
836         else if(CurrSS >= 5 && CurrSS <= 20)
837         {
838                 RetSS = 42 + (((CurrSS - 5) * 2) / 3);
839         }
840         else if(CurrSS == 4)
841         {
842                 RetSS = 36;
843         }
844         else if(CurrSS == 3)
845         {
846                 RetSS = 27;
847         }
848         else if(CurrSS == 2)
849         {
850                 RetSS = 18;
851         }
852         else if(CurrSS == 1)
853         {
854                 RetSS = 9;
855         }
856         else
857         {
858                 RetSS = CurrSS;
859         }
860         //RT_TRACE(COMP_DBG, DBG_LOUD, ("##### After Mapping:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
861
862         // Step 2. Smoothing.
863
864         //RT_TRACE(COMP_DBG, DBG_LOUD, ("$$$$$ After Smoothing:  LastSS: %d, CurrSS: %d, RetSS: %d\n", LastSS, CurrSS, RetSS));
865
866         return RetSS;
867 }
868
869 static inline void ieee80211_extract_country_ie(
870         struct ieee80211_device *ieee,
871         struct ieee80211_info_element *info_element,
872         struct ieee80211_network *network,
873         u8 * addr2
874 )
875 {
876         if(IS_DOT11D_ENABLE(ieee))
877         {
878                 if(info_element->len!= 0)
879                 {
880                         memcpy(network->CountryIeBuf, info_element->data, info_element->len);
881                         network->CountryIeLen = info_element->len;
882
883                         if(!IS_COUNTRY_IE_VALID(ieee))
884                         {
885                                 Dot11d_UpdateCountryIe(ieee, addr2, info_element->len, info_element->data);
886                         }
887                 }
888
889                 //
890                 // 070305, rcnjko: I update country IE watch dog here because
891                 // some AP (e.g. Cisco 1242) don't include country IE in their
892                 // probe response frame.
893                 //
894                 if(IS_EQUAL_CIE_SRC(ieee, addr2) )
895                 {
896                         UPDATE_CIE_WATCHDOG(ieee);
897                 }
898         }
899
900 }
901
902 int
903 ieee80211_TranslateToDbm(
904         unsigned char SignalStrengthIndex       // 0-100 index.
905         )
906 {
907         unsigned char SignalPower; // in dBm.
908
909         // Translate to dBm (x=0.5y-95).
910         SignalPower = (int)SignalStrengthIndex * 7 / 10;
911         SignalPower -= 95;
912
913         return SignalPower;
914 }
915 inline int ieee80211_network_init(
916         struct ieee80211_device *ieee,
917         struct ieee80211_probe_response *beacon,
918         struct ieee80211_network *network,
919         struct ieee80211_rx_stats *stats)
920 {
921 #ifdef CONFIG_IEEE80211_DEBUG
922         char rates_str[64];
923         char *p;
924 #endif
925         struct ieee80211_info_element *info_element;
926         u16 left;
927         u8 i;
928         short offset;
929         u8 curRate = 0,hOpRate = 0,curRate_ex = 0;
930
931         /* Pull out fixed field data */
932         memcpy(network->bssid, beacon->header.addr3, ETH_ALEN);
933         network->capability = beacon->capability;
934         network->last_scanned = jiffies;
935         network->time_stamp[0] = beacon->time_stamp[0];
936         network->time_stamp[1] = beacon->time_stamp[1];
937         network->beacon_interval = beacon->beacon_interval;
938         /* Where to pull this? beacon->listen_interval;*/
939         network->listen_interval = 0x0A;
940         network->rates_len = network->rates_ex_len = 0;
941         network->last_associate = 0;
942         network->ssid_len = 0;
943         network->flags = 0;
944         network->atim_window = 0;
945         network->QoS_Enable = 0;
946 //by amy 080312
947         network->HighestOperaRate = 0;
948 //by amy 080312
949         network->Turbo_Enable = 0;
950         network->CountryIeLen = 0;
951         memset(network->CountryIeBuf, 0, MAX_IE_LEN);
952
953         if (stats->freq == IEEE80211_52GHZ_BAND) {
954                 /* for A band (No DS info) */
955                 network->channel = stats->received_channel;
956         } else
957                 network->flags |= NETWORK_HAS_CCK;
958
959         network->wpa_ie_len = 0;
960         network->rsn_ie_len = 0;
961
962         info_element = &beacon->info_element;
963         left = stats->len - ((void *)info_element - (void *)beacon);
964         while (left >= sizeof(struct ieee80211_info_element_hdr)) {
965                 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
966                         IEEE80211_DEBUG_SCAN("SCAN: parse failed: info_element->len + 2 > left : info_element->len+2=%d left=%d.\n",
967                                              info_element->len + sizeof(struct ieee80211_info_element),
968                                              left);
969                         return 1;
970                 }
971
972                 switch (info_element->id) {
973                 case MFIE_TYPE_SSID:
974                         if (ieee80211_is_empty_essid(info_element->data,
975                                                      info_element->len)) {
976                                 network->flags |= NETWORK_EMPTY_ESSID;
977                                 break;
978                         }
979
980                         network->ssid_len = min(info_element->len,
981                                                 (u8)IW_ESSID_MAX_SIZE);
982                         memcpy(network->ssid, info_element->data, network->ssid_len);
983                         if (network->ssid_len < IW_ESSID_MAX_SIZE)
984                                 memset(network->ssid + network->ssid_len, 0,
985                                        IW_ESSID_MAX_SIZE - network->ssid_len);
986
987                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_SSID: '%s' len=%d.\n",
988                                              network->ssid, network->ssid_len);
989                         break;
990
991                 case MFIE_TYPE_RATES:
992 #ifdef CONFIG_IEEE80211_DEBUG
993                         p = rates_str;
994 #endif
995                         network->rates_len = min(info_element->len, MAX_RATES_LENGTH);
996                         for (i = 0; i < network->rates_len; i++) {
997                                 network->rates[i] = info_element->data[i];
998                                 curRate = network->rates[i] & 0x7f;
999                                 if( hOpRate < curRate )
1000                                         hOpRate = curRate;
1001 #ifdef CONFIG_IEEE80211_DEBUG
1002                                 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
1003 #endif
1004                                 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
1005                                         network->flags |= NETWORK_HAS_OFDM;
1006                                         if (info_element->data[i] &
1007                                             IEEE80211_BASIC_RATE_MASK)
1008                                                 network->flags &=
1009                                                         ~NETWORK_HAS_CCK;
1010                                 }
1011                         }
1012
1013                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES: '%s' (%d)\n",
1014                                              rates_str, network->rates_len);
1015                         break;
1016
1017                 case MFIE_TYPE_RATES_EX:
1018 #ifdef CONFIG_IEEE80211_DEBUG
1019                         p = rates_str;
1020 #endif
1021                         network->rates_ex_len = min(info_element->len, MAX_RATES_EX_LENGTH);
1022                         for (i = 0; i < network->rates_ex_len; i++) {
1023                                 network->rates_ex[i] = info_element->data[i];
1024                                 curRate_ex = network->rates_ex[i] & 0x7f;
1025                                 if( hOpRate < curRate_ex )
1026                                         hOpRate = curRate_ex;
1027 #ifdef CONFIG_IEEE80211_DEBUG
1028                                 p += snprintf(p, sizeof(rates_str) - (p - rates_str), "%02X ", network->rates[i]);
1029 #endif
1030                                 if (ieee80211_is_ofdm_rate(info_element->data[i])) {
1031                                         network->flags |= NETWORK_HAS_OFDM;
1032                                         if (info_element->data[i] &
1033                                             IEEE80211_BASIC_RATE_MASK)
1034                                                 network->flags &=
1035                                                         ~NETWORK_HAS_CCK;
1036                                 }
1037                         }
1038
1039                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RATES_EX: '%s' (%d)\n",
1040                                              rates_str, network->rates_ex_len);
1041                         break;
1042
1043                 case MFIE_TYPE_DS_SET:
1044                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_DS_SET: %d\n",
1045                                              info_element->data[0]);
1046                         if (stats->freq == IEEE80211_24GHZ_BAND)
1047                                 network->channel = info_element->data[0];
1048                         break;
1049
1050                 case MFIE_TYPE_FH_SET:
1051                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_FH_SET: ignored\n");
1052                         break;
1053
1054                 case MFIE_TYPE_CF_SET:
1055                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_CF_SET: ignored\n");
1056                         break;
1057
1058                 case MFIE_TYPE_TIM:
1059
1060                         if(info_element->len < 4)
1061                                 break;
1062
1063                         network->dtim_period = info_element->data[1];
1064
1065                         if(ieee->state != IEEE80211_LINKED)
1066                                 break;
1067
1068                         network->last_dtim_sta_time[0] = jiffies;
1069                         network->last_dtim_sta_time[1] = stats->mac_time[1];
1070
1071                         network->dtim_data = IEEE80211_DTIM_VALID;
1072
1073                         if(info_element->data[0] != 0)
1074                                 break;
1075
1076                         if(info_element->data[2] & 1)
1077                                 network->dtim_data |= IEEE80211_DTIM_MBCAST;
1078
1079                         offset = (info_element->data[2] >> 1)*2;
1080
1081                         //printk("offset1:%x aid:%x\n",offset, ieee->assoc_id);
1082
1083                         /* add and modified for ps 2008.1.22 */
1084                         if(ieee->assoc_id < 8*offset ||
1085                                 ieee->assoc_id > 8*(offset + info_element->len -3)) {
1086                                 break;
1087                         }
1088
1089                         offset = (ieee->assoc_id/8) - offset;// + ((aid % 8)? 0 : 1) ;
1090
1091                 //      printk("offset:%x data:%x, ucast:%d\n", offset,
1092                         //      info_element->data[3+offset] ,
1093                         //      info_element->data[3+offset] & (1<<(ieee->assoc_id%8)));
1094
1095                         if(info_element->data[3+offset] & (1<<(ieee->assoc_id%8))) {
1096                                 network->dtim_data |= IEEE80211_DTIM_UCAST;
1097                         }
1098                         break;
1099
1100                 case MFIE_TYPE_IBSS_SET:
1101                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_IBSS_SET: ignored\n");
1102                         break;
1103
1104                 case MFIE_TYPE_CHALLENGE:
1105                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_CHALLENGE: ignored\n");
1106                         break;
1107
1108                 case MFIE_TYPE_GENERIC:
1109                         //nic is 87B
1110                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n",
1111                                              info_element->len);
1112                         if (info_element->len >= 4  &&
1113                             info_element->data[0] == 0x00 &&
1114                             info_element->data[1] == 0x50 &&
1115                             info_element->data[2] == 0xf2 &&
1116                             info_element->data[3] == 0x01) {
1117                                 network->wpa_ie_len = min(info_element->len + 2,
1118                                                          MAX_WPA_IE_LEN);
1119                                 memcpy(network->wpa_ie, info_element,
1120                                        network->wpa_ie_len);
1121                         }
1122
1123                         if (info_element->len == 7 &&
1124                             info_element->data[0] == 0x00 &&
1125                             info_element->data[1] == 0xe0 &&
1126                             info_element->data[2] == 0x4c &&
1127                             info_element->data[3] == 0x01 &&
1128                             info_element->data[4] == 0x02) {
1129                                 network->Turbo_Enable = 1;
1130                         }
1131                         if (1 == stats->nic_type) {//nic 87
1132                                 break;
1133                         }
1134
1135                         if (info_element->len >= 5  &&
1136                             info_element->data[0] == 0x00 &&
1137                             info_element->data[1] == 0x50 &&
1138                             info_element->data[2] == 0xf2 &&
1139                             info_element->data[3] == 0x02 &&
1140                             info_element->data[4] == 0x00) {
1141                                 //printk(KERN_WARNING "wmm info updated: %x\n", info_element->data[6]);
1142                                 //WMM Information Element
1143                                 network->wmm_info = info_element->data[6];
1144                                 network->QoS_Enable = 1;
1145                         }
1146
1147                         if (info_element->len >= 8  &&
1148                             info_element->data[0] == 0x00 &&
1149                             info_element->data[1] == 0x50 &&
1150                             info_element->data[2] == 0xf2 &&
1151                             info_element->data[3] == 0x02 &&
1152                             info_element->data[4] == 0x01) {
1153                                 // Not care about version at present.
1154                                 //WMM Information Element
1155                                 //printk(KERN_WARNING "wmm info&param updated: %x\n", info_element->data[6]);
1156                                 network->wmm_info = info_element->data[6];
1157                                 //WMM Parameter Element
1158                                 memcpy(network->wmm_param, (u8 *)(info_element->data + 8),(info_element->len - 8));
1159                                 network->QoS_Enable = 1;
1160                         }
1161                         break;
1162
1163                 case MFIE_TYPE_RSN:
1164                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_RSN: %d bytes\n",
1165                                              info_element->len);
1166                         network->rsn_ie_len = min(info_element->len + 2,
1167                                                  MAX_WPA_IE_LEN);
1168                         memcpy(network->rsn_ie, info_element,
1169                                network->rsn_ie_len);
1170                         break;
1171                 case MFIE_TYPE_COUNTRY:
1172                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_COUNTRY: %d bytes\n",
1173                                              info_element->len);
1174 //                      printk("=====>Receive <%s> Country IE\n",network->ssid);
1175                         ieee80211_extract_country_ie(ieee, info_element, network, beacon->header.addr2);
1176                         break;
1177                 default:
1178                         IEEE80211_DEBUG_SCAN("unsupported IE %d\n",
1179                                              info_element->id);
1180                         break;
1181                 }
1182
1183                 left -= sizeof(struct ieee80211_info_element_hdr) +
1184                         info_element->len;
1185                 info_element = (struct ieee80211_info_element *)
1186                         &info_element->data[info_element->len];
1187         }
1188 //by amy 080312
1189         network->HighestOperaRate = hOpRate;
1190 //by amy 080312
1191         network->mode = 0;
1192         if (stats->freq == IEEE80211_52GHZ_BAND)
1193                 network->mode = IEEE_A;
1194         else {
1195                 if (network->flags & NETWORK_HAS_OFDM)
1196                         network->mode |= IEEE_G;
1197                 if (network->flags & NETWORK_HAS_CCK)
1198                         network->mode |= IEEE_B;
1199         }
1200
1201         if (network->mode == 0) {
1202                 IEEE80211_DEBUG_SCAN("Filtered out '%s (" MAC_FMT ")' "
1203                                      "network.\n",
1204                                      escape_essid(network->ssid,
1205                                                   network->ssid_len),
1206                                      MAC_ARG(network->bssid));
1207                 return 1;
1208         }
1209
1210         if (ieee80211_is_empty_essid(network->ssid, network->ssid_len))
1211                 network->flags |= NETWORK_EMPTY_ESSID;
1212
1213         stats->signal = ieee80211_TranslateToDbm(stats->signalstrength);
1214         //stats->noise = stats->signal - stats->noise;
1215         stats->noise = ieee80211_TranslateToDbm(100 - stats->signalstrength) - 25;
1216         memcpy(&network->stats, stats, sizeof(network->stats));
1217
1218         return 0;
1219 }
1220
1221 static inline int is_same_network(struct ieee80211_network *src,
1222                                   struct ieee80211_network *dst,
1223                                   struct ieee80211_device * ieee)
1224 {
1225         /* A network is only a duplicate if the channel, BSSID, ESSID
1226          * and the capability field (in particular IBSS and BSS) all match.
1227          * We treat all <hidden> with the same BSSID and channel
1228          * as one network */
1229         return (((src->ssid_len == dst->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) &&  //YJ,mod,080819,for hidden ap
1230                 //((src->ssid_len == dst->ssid_len) &&
1231                 (src->channel == dst->channel) &&
1232                 !memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
1233                 (!memcmp(src->ssid, dst->ssid, src->ssid_len) || (ieee->iw_mode == IW_MODE_INFRA)) && //YJ,mod,080819,for hidden ap
1234                 //!memcmp(src->ssid, dst->ssid, src->ssid_len) &&
1235                 ((src->capability & WLAN_CAPABILITY_IBSS) ==
1236                 (dst->capability & WLAN_CAPABILITY_IBSS)) &&
1237                 ((src->capability & WLAN_CAPABILITY_BSS) ==
1238                 (dst->capability & WLAN_CAPABILITY_BSS)));
1239 }
1240
1241 inline void update_network(struct ieee80211_network *dst,
1242                                   struct ieee80211_network *src)
1243 {
1244         unsigned char quality = src->stats.signalstrength;
1245         unsigned char signal = 0;
1246         unsigned char noise = 0;
1247         if(dst->stats.signalstrength > 0) {
1248                 quality = (dst->stats.signalstrength * 5 + src->stats.signalstrength + 5)/6;
1249         }
1250         signal = ieee80211_TranslateToDbm(quality);
1251         //noise = signal - src->stats.noise;
1252         if(dst->stats.noise > 0)
1253                 noise = (dst->stats.noise * 5 + src->stats.noise)/6;
1254         //if(strcmp(dst->ssid, "linksys_lzm000") == 0)
1255 //      printk("ssid:%s, quality:%d, signal:%d\n", dst->ssid, quality, signal);
1256         memcpy(&dst->stats, &src->stats, sizeof(struct ieee80211_rx_stats));
1257         dst->stats.signalstrength = quality;
1258         dst->stats.signal = signal;
1259 //      printk("==================>stats.signal is %d\n",dst->stats.signal);
1260         dst->stats.noise = noise;
1261
1262
1263         dst->capability = src->capability;
1264         memcpy(dst->rates, src->rates, src->rates_len);
1265         dst->rates_len = src->rates_len;
1266         memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
1267         dst->rates_ex_len = src->rates_ex_len;
1268         dst->HighestOperaRate= src->HighestOperaRate;
1269         //printk("==========>in %s: src->ssid is %s,chan is %d\n",__func__,src->ssid,src->channel);
1270
1271         //YJ,add,080819,for hidden ap
1272         if(src->ssid_len > 0)
1273         {
1274                 //if(src->ssid_len == 13)
1275                 //      printk("=====================>>>>>>>> Dst ssid: %s Src ssid: %s\n", dst->ssid, src->ssid);
1276                 memset(dst->ssid, 0, dst->ssid_len);
1277                 dst->ssid_len = src->ssid_len;
1278                 memcpy(dst->ssid, src->ssid, src->ssid_len);
1279         }
1280         //YJ,add,080819,for hidden ap,end
1281
1282         dst->channel = src->channel;
1283         dst->mode = src->mode;
1284         dst->flags = src->flags;
1285         dst->time_stamp[0] = src->time_stamp[0];
1286         dst->time_stamp[1] = src->time_stamp[1];
1287
1288         dst->beacon_interval = src->beacon_interval;
1289         dst->listen_interval = src->listen_interval;
1290         dst->atim_window = src->atim_window;
1291         dst->dtim_period = src->dtim_period;
1292         dst->dtim_data = src->dtim_data;
1293         dst->last_dtim_sta_time[0] = src->last_dtim_sta_time[0];
1294         dst->last_dtim_sta_time[1] = src->last_dtim_sta_time[1];
1295 //      printk("update:%s, dtim_period:%x, dtim_data:%x\n", src->ssid, src->dtim_period, src->dtim_data);
1296         memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
1297         dst->wpa_ie_len = src->wpa_ie_len;
1298         memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
1299         dst->rsn_ie_len = src->rsn_ie_len;
1300
1301         dst->last_scanned = jiffies;
1302         /* dst->last_associate is not overwritten */
1303 // disable QoS process now, added by David 2006/7/25
1304 #if 1
1305         dst->wmm_info = src->wmm_info; //sure to exist in beacon or probe response frame.
1306 /*
1307         if((dst->wmm_info^src->wmm_info)&0x0f) {//Param Set Count change, update Parameter
1308           memcpy(dst->wmm_param, src->wmm_param, IEEE80211_AC_PRAM_LEN);
1309         }
1310 */
1311         if(src->wmm_param[0].ac_aci_acm_aifsn|| \
1312            src->wmm_param[1].ac_aci_acm_aifsn|| \
1313            src->wmm_param[2].ac_aci_acm_aifsn|| \
1314            src->wmm_param[3].ac_aci_acm_aifsn) {
1315           memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
1316         }
1317         dst->QoS_Enable = src->QoS_Enable;
1318 #else
1319         dst->QoS_Enable = 1;//for Rtl8187 simulation
1320 #endif
1321         dst->SignalStrength = src->SignalStrength;
1322         dst->Turbo_Enable = src->Turbo_Enable;
1323         dst->CountryIeLen = src->CountryIeLen;
1324         memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
1325 }
1326
1327
1328 inline void ieee80211_process_probe_response(
1329         struct ieee80211_device *ieee,
1330         struct ieee80211_probe_response *beacon,
1331         struct ieee80211_rx_stats *stats)
1332 {
1333         struct ieee80211_network network;
1334         struct ieee80211_network *target;
1335         struct ieee80211_network *oldest = NULL;
1336 #ifdef CONFIG_IEEE80211_DEBUG
1337         struct ieee80211_info_element *info_element = &beacon->info_element;
1338 #endif
1339         unsigned long flags;
1340         short renew;
1341         u8 wmm_info;
1342         u8 is_beacon = (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_BEACON)? 1:0;  //YJ,add,080819,for hidden ap
1343
1344         memset(&network, 0, sizeof(struct ieee80211_network));
1345
1346         IEEE80211_DEBUG_SCAN(
1347                 "'%s' (" MAC_FMT "): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
1348                 escape_essid(info_element->data, info_element->len),
1349                 MAC_ARG(beacon->header.addr3),
1350                 (beacon->capability & (1<<0xf)) ? '1' : '0',
1351                 (beacon->capability & (1<<0xe)) ? '1' : '0',
1352                 (beacon->capability & (1<<0xd)) ? '1' : '0',
1353                 (beacon->capability & (1<<0xc)) ? '1' : '0',
1354                 (beacon->capability & (1<<0xb)) ? '1' : '0',
1355                 (beacon->capability & (1<<0xa)) ? '1' : '0',
1356                 (beacon->capability & (1<<0x9)) ? '1' : '0',
1357                 (beacon->capability & (1<<0x8)) ? '1' : '0',
1358                 (beacon->capability & (1<<0x7)) ? '1' : '0',
1359                 (beacon->capability & (1<<0x6)) ? '1' : '0',
1360                 (beacon->capability & (1<<0x5)) ? '1' : '0',
1361                 (beacon->capability & (1<<0x4)) ? '1' : '0',
1362                 (beacon->capability & (1<<0x3)) ? '1' : '0',
1363                 (beacon->capability & (1<<0x2)) ? '1' : '0',
1364                 (beacon->capability & (1<<0x1)) ? '1' : '0',
1365                 (beacon->capability & (1<<0x0)) ? '1' : '0');
1366
1367         if (ieee80211_network_init(ieee, beacon, &network, stats)) {
1368                 IEEE80211_DEBUG_SCAN("Dropped '%s' (" MAC_FMT ") via %s.\n",
1369                                      escape_essid(info_element->data,
1370                                                   info_element->len),
1371                                      MAC_ARG(beacon->header.addr3),
1372                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1373                                      IEEE80211_STYPE_PROBE_RESP ?
1374                                      "PROBE RESPONSE" : "BEACON");
1375                 return;
1376         }
1377
1378         // For Asus EeePc request,
1379         // (1) if wireless adapter receive get any 802.11d country code in AP beacon,
1380         //         wireless adapter should follow the country code.
1381         // (2)  If there is no any country code in beacon,
1382         //       then wireless adapter should do active scan from ch1~11 and
1383         //       passive scan from ch12~14
1384         if(ieee->bGlobalDomain)
1385         {
1386                 if (WLAN_FC_GET_STYPE(beacon->header.frame_ctl) == IEEE80211_STYPE_PROBE_RESP)
1387                 {
1388                         // Case 1: Country code
1389                         if(IS_COUNTRY_IE_VALID(ieee) )
1390                         {
1391                                 if( !IsLegalChannel(ieee, network.channel) )
1392                                 {
1393                                         printk("GetScanInfo(): For Country code, filter probe response at channel(%d).\n", network.channel);
1394                                         return;
1395                                 }
1396                         }
1397                         // Case 2: No any country code.
1398                         else
1399                         {
1400                                 // Filter over channel ch12~14
1401                                 if(network.channel > 11)
1402                                 {
1403                                         printk("GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n", network.channel);
1404                                         return;
1405                                 }
1406                         }
1407                 }
1408                 else
1409                 {
1410                         // Case 1: Country code
1411                         if(IS_COUNTRY_IE_VALID(ieee) )
1412                         {
1413                                 if( !IsLegalChannel(ieee, network.channel) )
1414                                 {
1415                                         printk("GetScanInfo(): For Country code, filter beacon at channel(%d).\n",network.channel);
1416                                         return;
1417                                 }
1418                         }
1419                         // Case 2: No any country code.
1420                         else
1421                         {
1422                                 // Filter over channel ch12~14
1423                                 if(network.channel > 14)
1424                                 {
1425                                         printk("GetScanInfo(): For Global Domain, filter beacon at channel(%d).\n",network.channel);
1426                                         return;
1427                                 }
1428                         }
1429                 }
1430         }
1431         /* The network parsed correctly -- so now we scan our known networks
1432          * to see if we can find it in our list.
1433          *
1434          * NOTE:  This search is definitely not optimized.  Once its doing
1435          *        the "right thing" we'll optimize it for efficiency if
1436          *        necessary */
1437
1438         /* Search for this entry in the list and update it if it is
1439          * already there. */
1440
1441         spin_lock_irqsave(&ieee->lock, flags);
1442
1443         if(is_same_network(&ieee->current_network, &network, ieee)) {
1444                 wmm_info = ieee->current_network.wmm_info;
1445                 //YJ,add,080819,for hidden ap
1446                 if(is_beacon == 0)
1447                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & ieee->current_network.flags);
1448                 else if(ieee->state == IEEE80211_LINKED)
1449                         ieee->NumRxBcnInPeriod++;
1450                 //YJ,add,080819,for hidden ap,end
1451                 //printk("====>network.ssid=%s cur_ssid=%s\n", network.ssid, ieee->current_network.ssid);
1452                 update_network(&ieee->current_network, &network);
1453         }
1454
1455         list_for_each_entry(target, &ieee->network_list, list) {
1456                 if (is_same_network(target, &network, ieee))
1457                         break;
1458                 if ((oldest == NULL) ||
1459                     (target->last_scanned < oldest->last_scanned))
1460                         oldest = target;
1461         }
1462
1463         /* If we didn't find a match, then get a new network slot to initialize
1464          * with this beacon's information */
1465         if (&target->list == &ieee->network_list) {
1466                 if (list_empty(&ieee->network_free_list)) {
1467                         /* If there are no more slots, expire the oldest */
1468                         list_del(&oldest->list);
1469                         target = oldest;
1470                         IEEE80211_DEBUG_SCAN("Expired '%s' (" MAC_FMT ") from "
1471                                              "network list.\n",
1472                                              escape_essid(target->ssid,
1473                                                           target->ssid_len),
1474                                              MAC_ARG(target->bssid));
1475                 } else {
1476                         /* Otherwise just pull from the free list */
1477                         target = list_entry(ieee->network_free_list.next,
1478                                             struct ieee80211_network, list);
1479                         list_del(ieee->network_free_list.next);
1480                 }
1481
1482
1483 #ifdef CONFIG_IEEE80211_DEBUG
1484                 IEEE80211_DEBUG_SCAN("Adding '%s' (" MAC_FMT ") via %s.\n",
1485                                      escape_essid(network.ssid,
1486                                                   network.ssid_len),
1487                                      MAC_ARG(network.bssid),
1488                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1489                                      IEEE80211_STYPE_PROBE_RESP ?
1490                                      "PROBE RESPONSE" : "BEACON");
1491 #endif
1492
1493                 memcpy(target, &network, sizeof(*target));
1494                 list_add_tail(&target->list, &ieee->network_list);
1495                 if(ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
1496                         ieee80211_softmac_new_net(ieee,&network);
1497         } else {
1498                 IEEE80211_DEBUG_SCAN("Updating '%s' (" MAC_FMT ") via %s.\n",
1499                                      escape_essid(target->ssid,
1500                                                   target->ssid_len),
1501                                      MAC_ARG(target->bssid),
1502                                      WLAN_FC_GET_STYPE(beacon->header.frame_ctl) ==
1503                                      IEEE80211_STYPE_PROBE_RESP ?
1504                                      "PROBE RESPONSE" : "BEACON");
1505
1506                 /* we have an entry and we are going to update it. But this entry may
1507                  * be already expired. In this case we do the same as we found a new
1508                  * net and call the new_net handler
1509                  */
1510                 renew = !time_after(target->last_scanned + ieee->scan_age, jiffies);
1511                 //YJ,add,080819,for hidden ap
1512                 if(is_beacon == 0)
1513                         network.flags = (~NETWORK_EMPTY_ESSID & network.flags)|(NETWORK_EMPTY_ESSID & target->flags);
1514                 //if(strncmp(network.ssid, "linksys-c",9) == 0)
1515                 //      printk("====>2 network.ssid=%s FLAG=%d target.ssid=%s FLAG=%d\n", network.ssid, network.flags, target->ssid, target->flags);
1516                 if(((network.flags & NETWORK_EMPTY_ESSID) == NETWORK_EMPTY_ESSID) \
1517                     && (((network.ssid_len > 0) && (strncmp(target->ssid, network.ssid, network.ssid_len)))\
1518                     ||((ieee->current_network.ssid_len == network.ssid_len)&&(strncmp(ieee->current_network.ssid, network.ssid, network.ssid_len) == 0)&&(ieee->state == IEEE80211_NOLINK))))
1519                         renew = 1;
1520                 //YJ,add,080819,for hidden ap,end
1521                 update_network(target, &network);
1522                 if(renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
1523                         ieee80211_softmac_new_net(ieee,&network);
1524         }
1525
1526         spin_unlock_irqrestore(&ieee->lock, flags);
1527 }
1528
1529 void ieee80211_rx_mgt(struct ieee80211_device *ieee,
1530                       struct ieee80211_hdr_4addr *header,
1531                       struct ieee80211_rx_stats *stats)
1532 {
1533         switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
1534
1535         case IEEE80211_STYPE_BEACON:
1536                 IEEE80211_DEBUG_MGMT("received BEACON (%d)\n",
1537                                      WLAN_FC_GET_STYPE(header->frame_ctl));
1538                 IEEE80211_DEBUG_SCAN("Beacon\n");
1539                 ieee80211_process_probe_response(
1540                         ieee, (struct ieee80211_probe_response *)header, stats);
1541                 break;
1542
1543         case IEEE80211_STYPE_PROBE_RESP:
1544                 IEEE80211_DEBUG_MGMT("received PROBE RESPONSE (%d)\n",
1545                                      WLAN_FC_GET_STYPE(header->frame_ctl));
1546                 IEEE80211_DEBUG_SCAN("Probe response\n");
1547                 ieee80211_process_probe_response(
1548                         ieee, (struct ieee80211_probe_response *)header, stats);
1549                 break;
1550         }
1551 }