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