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