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