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