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