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