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