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