OSDN Git Service

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