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