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