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