xref: /linux/drivers/staging/rtl8192e/rtllib_rx.c (revision e91c37f1)
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_FlushRxTsPendingPkts(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 	if (RTLLIB_QOS_HAS_SEQ(fc))
869 		rx_stats->bIsQosData = true;
870 
871 	return hdrlen;
872 }
873 
874 static int rtllib_rx_check_duplicate(struct rtllib_device *ieee,
875 				     struct sk_buff *skb, u8 multicast)
876 {
877 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
878 	u16 fc, sc;
879 	u8 frag;
880 
881 	fc = le16_to_cpu(hdr->frame_control);
882 	sc = le16_to_cpu(hdr->seq_ctrl);
883 	frag = WLAN_GET_SEQ_FRAG(sc);
884 
885 	if (!ieee->ht_info->cur_rx_reorder_enable ||
886 		!ieee->current_network.qos_data.active ||
887 		!IsDataFrame(skb->data) ||
888 		IsLegacyDataFrame(skb->data)) {
889 		if (!ieee80211_is_beacon(hdr->frame_control)) {
890 			if (is_duplicate_packet(ieee, hdr))
891 				return -1;
892 		}
893 	} else {
894 		struct rx_ts_record *ts = NULL;
895 
896 		if (rtllib_get_ts(ieee, (struct ts_common_info **)&ts, hdr->addr2,
897 			(u8)Frame_QoSTID((u8 *)(skb->data)), RX_DIR, true)) {
898 			if ((fc & (1 << 11)) && (frag == ts->rx_last_frag_num) &&
899 			    (WLAN_GET_SEQ_SEQ(sc) == ts->rx_last_seq_num))
900 				return -1;
901 			ts->rx_last_frag_num = frag;
902 			ts->rx_last_seq_num = WLAN_GET_SEQ_SEQ(sc);
903 		} else {
904 			netdev_warn(ieee->dev, "%s(): No TS! Skip the check!\n",
905 				    __func__);
906 			return -1;
907 		}
908 	}
909 
910 	return 0;
911 }
912 
913 static void rtllib_rx_extract_addr(struct rtllib_device *ieee,
914 				   struct ieee80211_hdr *hdr, u8 *dst,
915 				   u8 *src, u8 *bssid)
916 {
917 	u16 fc = le16_to_cpu(hdr->frame_control);
918 
919 	switch (fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
920 	case IEEE80211_FCTL_FROMDS:
921 		ether_addr_copy(dst, hdr->addr1);
922 		ether_addr_copy(src, hdr->addr3);
923 		ether_addr_copy(bssid, hdr->addr2);
924 		break;
925 	case IEEE80211_FCTL_TODS:
926 		ether_addr_copy(dst, hdr->addr3);
927 		ether_addr_copy(src, hdr->addr2);
928 		ether_addr_copy(bssid, hdr->addr1);
929 		break;
930 	case IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS:
931 		ether_addr_copy(dst, hdr->addr3);
932 		ether_addr_copy(src, hdr->addr4);
933 		ether_addr_copy(bssid, ieee->current_network.bssid);
934 		break;
935 	default:
936 		ether_addr_copy(dst, hdr->addr1);
937 		ether_addr_copy(src, hdr->addr2);
938 		ether_addr_copy(bssid, hdr->addr3);
939 		break;
940 	}
941 }
942 
943 static int rtllib_rx_data_filter(struct rtllib_device *ieee, struct ieee80211_hdr *hdr,
944 				 u8 *dst, u8 *src, u8 *bssid, u8 *addr2)
945 {
946 	u8 type, stype;
947 	u16 fc = le16_to_cpu(hdr->frame_control);
948 	type = WLAN_FC_GET_TYPE(fc);
949 	stype = WLAN_FC_GET_STYPE(fc);
950 
951 	/* Filter frames from different BSS */
952 	if (ieee80211_has_a4(hdr->frame_control) &&
953 	    !ether_addr_equal(ieee->current_network.bssid, bssid) &&
954 	    !is_zero_ether_addr(ieee->current_network.bssid)) {
955 		return -1;
956 	}
957 
958 	/* Nullfunc frames may have PS-bit set, so they must be passed to
959 	 * hostap_handle_sta_rx() before being dropped here.
960 	 */
961 	if (stype != IEEE80211_STYPE_DATA &&
962 	    stype != IEEE80211_STYPE_DATA_CFACK &&
963 	    stype != IEEE80211_STYPE_DATA_CFPOLL &&
964 	    stype != IEEE80211_STYPE_DATA_CFACKPOLL &&
965 	    stype != IEEE80211_STYPE_QOS_DATA) {
966 		if (stype != IEEE80211_STYPE_NULLFUNC)
967 			netdev_dbg(ieee->dev,
968 				   "RX: dropped data frame with no data (type=0x%02x, subtype=0x%02x)\n",
969 				   type, stype);
970 		return -1;
971 	}
972 
973 	/* packets from our adapter are dropped (echo) */
974 	if (!memcmp(src, ieee->dev->dev_addr, ETH_ALEN))
975 		return -1;
976 
977 	/* {broad,multi}cast packets to our BSS go through */
978 	if (is_multicast_ether_addr(dst)) {
979 		if (memcmp(bssid, ieee->current_network.bssid,
980 			   ETH_ALEN))
981 			return -1;
982 	}
983 	return 0;
984 }
985 
986 static int rtllib_rx_get_crypt(struct rtllib_device *ieee, struct sk_buff *skb,
987 			struct lib80211_crypt_data **crypt, size_t hdrlen)
988 {
989 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
990 	u16 fc = le16_to_cpu(hdr->frame_control);
991 	int idx = 0;
992 
993 	if (skb->len >= hdrlen + 3)
994 		idx = skb->data[hdrlen + 3] >> 6;
995 
996 	*crypt = ieee->crypt_info.crypt[idx];
997 	/* allow NULL decrypt to indicate an station specific override
998 	 * for default encryption
999 	 */
1000 	if (*crypt && (!(*crypt)->ops || !(*crypt)->ops->decrypt_mpdu))
1001 		*crypt = NULL;
1002 
1003 	if (!*crypt && (fc & IEEE80211_FCTL_PROTECTED)) {
1004 		/* This seems to be triggered by some (multicast?)
1005 		 * frames from other than current BSS, so just drop the
1006 		 * frames silently instead of filling system log with
1007 		 * these reports.
1008 		 */
1009 		netdev_dbg(ieee->dev,
1010 			   "Decryption failed (not set) (SA= %pM)\n",
1011 			   hdr->addr2);
1012 		return -1;
1013 	}
1014 
1015 	return 0;
1016 }
1017 
1018 static int rtllib_rx_decrypt(struct rtllib_device *ieee, struct sk_buff *skb,
1019 		      struct rtllib_rx_stats *rx_stats,
1020 		      struct lib80211_crypt_data *crypt, size_t hdrlen)
1021 {
1022 	struct ieee80211_hdr *hdr;
1023 	int keyidx = 0;
1024 	u16 fc, sc;
1025 	u8 frag;
1026 
1027 	hdr = (struct ieee80211_hdr *)skb->data;
1028 	fc = le16_to_cpu(hdr->frame_control);
1029 	sc = le16_to_cpu(hdr->seq_ctrl);
1030 	frag = WLAN_GET_SEQ_FRAG(sc);
1031 
1032 	if ((!rx_stats->Decrypted))
1033 		ieee->need_sw_enc = 1;
1034 	else
1035 		ieee->need_sw_enc = 0;
1036 
1037 	keyidx = rtllib_rx_frame_decrypt(ieee, skb, crypt);
1038 	if ((fc & IEEE80211_FCTL_PROTECTED) && (keyidx < 0)) {
1039 		netdev_info(ieee->dev, "%s: decrypt frame error\n", __func__);
1040 		return -1;
1041 	}
1042 
1043 	hdr = (struct ieee80211_hdr *)skb->data;
1044 	if ((frag != 0 || (fc & IEEE80211_FCTL_MOREFRAGS))) {
1045 		int flen;
1046 		struct sk_buff *frag_skb = rtllib_frag_cache_get(ieee, hdr);
1047 
1048 		netdev_dbg(ieee->dev, "Rx Fragment received (%u)\n", frag);
1049 
1050 		if (!frag_skb) {
1051 			netdev_dbg(ieee->dev,
1052 				   "Rx cannot get skb from fragment cache (morefrag=%d seq=%u frag=%u)\n",
1053 				   (fc & IEEE80211_FCTL_MOREFRAGS) != 0,
1054 				   WLAN_GET_SEQ_SEQ(sc), frag);
1055 			return -1;
1056 		}
1057 		flen = skb->len;
1058 		if (frag != 0)
1059 			flen -= hdrlen;
1060 
1061 		if (frag_skb->tail + flen > frag_skb->end) {
1062 			netdev_warn(ieee->dev,
1063 				    "%s: host decrypted and reassembled frame did not fit skb\n",
1064 				    __func__);
1065 			rtllib_frag_cache_invalidate(ieee, hdr);
1066 			return -1;
1067 		}
1068 
1069 		if (frag == 0) {
1070 			/* copy first fragment (including full headers) into
1071 			 * beginning of the fragment cache skb
1072 			 */
1073 			skb_put_data(frag_skb, skb->data, flen);
1074 		} else {
1075 			/* append frame payload to the end of the fragment
1076 			 * cache skb
1077 			 */
1078 			skb_put_data(frag_skb, skb->data + hdrlen, flen);
1079 		}
1080 		dev_kfree_skb_any(skb);
1081 		skb = NULL;
1082 
1083 		if (fc & IEEE80211_FCTL_MOREFRAGS) {
1084 			/* more fragments expected - leave the skb in fragment
1085 			 * cache for now; it will be delivered to upper layers
1086 			 * after all fragments have been received
1087 			 */
1088 			return -2;
1089 		}
1090 
1091 		/* this was the last fragment and the frame will be
1092 		 * delivered, so remove skb from fragment cache
1093 		 */
1094 		skb = frag_skb;
1095 		hdr = (struct ieee80211_hdr *)skb->data;
1096 		rtllib_frag_cache_invalidate(ieee, hdr);
1097 	}
1098 
1099 	/* skb: hdr + (possible reassembled) full MSDU payload; possibly still
1100 	 * encrypted/authenticated
1101 	 */
1102 	if ((fc & IEEE80211_FCTL_PROTECTED) &&
1103 		rtllib_rx_frame_decrypt_msdu(ieee, skb, keyidx, crypt)) {
1104 		netdev_info(ieee->dev, "%s: ==>decrypt msdu error\n", __func__);
1105 		return -1;
1106 	}
1107 
1108 	hdr = (struct ieee80211_hdr *)skb->data;
1109 	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep) {
1110 		if (/*ieee->ieee802_1x &&*/
1111 		    rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1112 			/* pass unencrypted EAPOL frames even if encryption is
1113 			 * configured
1114 			 */
1115 			struct eapol *eap = (struct eapol *)(skb->data +
1116 				24);
1117 			netdev_dbg(ieee->dev,
1118 				   "RX: IEEE 802.1X EAPOL frame: %s\n",
1119 				   eap_get_type(eap->type));
1120 		} else {
1121 			netdev_dbg(ieee->dev,
1122 				   "encryption configured, but RX frame not encrypted (SA= %pM)\n",
1123 				   hdr->addr2);
1124 			return -1;
1125 		}
1126 	}
1127 
1128 	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) &&
1129 	    rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1130 		struct eapol *eap = (struct eapol *)(skb->data + 24);
1131 
1132 		netdev_dbg(ieee->dev, "RX: IEEE 802.1X EAPOL frame: %s\n",
1133 			   eap_get_type(eap->type));
1134 	}
1135 
1136 	if (crypt && !(fc & IEEE80211_FCTL_PROTECTED) && !ieee->open_wep &&
1137 	    !rtllib_is_eapol_frame(ieee, skb, hdrlen)) {
1138 		netdev_dbg(ieee->dev,
1139 			   "dropped unencrypted RX data frame from %pM (drop_unencrypted=1)\n",
1140 			   hdr->addr2);
1141 		return -1;
1142 	}
1143 
1144 	return 0;
1145 }
1146 
1147 static void rtllib_rx_check_leave_lps(struct rtllib_device *ieee, u8 unicast,
1148 				      u8 nr_subframes)
1149 {
1150 	if (unicast) {
1151 		if (ieee->link_state == MAC80211_LINKED) {
1152 			if (((ieee->link_detect_info.NumRxUnicastOkInPeriod +
1153 			    ieee->link_detect_info.num_tx_ok_in_period) > 8) ||
1154 			    (ieee->link_detect_info.NumRxUnicastOkInPeriod > 2)) {
1155 				ieee->leisure_ps_leave(ieee->dev);
1156 			}
1157 		}
1158 	}
1159 	ieee->last_rx_ps_time = jiffies;
1160 }
1161 
1162 static void rtllib_rx_indicate_pkt_legacy(struct rtllib_device *ieee,
1163 		struct rtllib_rx_stats *rx_stats,
1164 		struct rtllib_rxb *rxb,
1165 		u8 *dst,
1166 		u8 *src)
1167 {
1168 	struct net_device *dev = ieee->dev;
1169 	u16 ethertype;
1170 	int i = 0;
1171 
1172 	if (!rxb) {
1173 		netdev_info(dev, "%s: rxb is NULL!!\n", __func__);
1174 		return;
1175 	}
1176 
1177 	for (i = 0; i < rxb->nr_subframes; i++) {
1178 		struct sk_buff *sub_skb = rxb->subframes[i];
1179 
1180 		if (sub_skb) {
1181 			/* convert hdr + possible LLC headers
1182 			 * into Ethernet header
1183 			 */
1184 			ethertype = (sub_skb->data[6] << 8) | sub_skb->data[7];
1185 			if (sub_skb->len >= 8 &&
1186 				((memcmp(sub_skb->data, rfc1042_header, SNAP_SIZE) == 0 &&
1187 				ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
1188 				memcmp(sub_skb->data, bridge_tunnel_header, SNAP_SIZE) == 0)) {
1189 				/* remove RFC1042 or Bridge-Tunnel encapsulation
1190 				 * and replace EtherType
1191 				 */
1192 				skb_pull(sub_skb, SNAP_SIZE);
1193 				ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1194 						src);
1195 				ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1196 						dst);
1197 			} else {
1198 				u16 len;
1199 				/* Leave Ethernet header part of hdr
1200 				 * and full payload
1201 				 */
1202 				len = sub_skb->len;
1203 				memcpy(skb_push(sub_skb, 2), &len, 2);
1204 				ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1205 						src);
1206 				ether_addr_copy(skb_push(sub_skb, ETH_ALEN),
1207 						dst);
1208 			}
1209 
1210 			ieee->stats.rx_packets++;
1211 			ieee->stats.rx_bytes += sub_skb->len;
1212 
1213 			if (is_multicast_ether_addr(dst))
1214 				ieee->stats.multicast++;
1215 
1216 			/* Indicate the packets to upper layer */
1217 			memset(sub_skb->cb, 0, sizeof(sub_skb->cb));
1218 			sub_skb->protocol = eth_type_trans(sub_skb, dev);
1219 			sub_skb->dev = dev;
1220 			sub_skb->dev->stats.rx_packets++;
1221 			sub_skb->dev->stats.rx_bytes += sub_skb->len;
1222 			/* 802.11 crc not sufficient */
1223 			sub_skb->ip_summed = CHECKSUM_NONE;
1224 			netif_rx(sub_skb);
1225 		}
1226 	}
1227 	kfree(rxb);
1228 }
1229 
1230 static int rtllib_rx_InfraAdhoc(struct rtllib_device *ieee, struct sk_buff *skb,
1231 		 struct rtllib_rx_stats *rx_stats)
1232 {
1233 	struct net_device *dev = ieee->dev;
1234 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1235 	struct lib80211_crypt_data *crypt = NULL;
1236 	struct rtllib_rxb *rxb = NULL;
1237 	struct rx_ts_record *ts = NULL;
1238 	u16 fc, sc, SeqNum = 0;
1239 	u8 type, stype, multicast = 0, unicast = 0, nr_subframes = 0, TID = 0;
1240 	u8 dst[ETH_ALEN];
1241 	u8 src[ETH_ALEN];
1242 	u8 bssid[ETH_ALEN] = {0};
1243 
1244 	size_t hdrlen = 0;
1245 	int ret = 0, i = 0;
1246 
1247 	fc = le16_to_cpu(hdr->frame_control);
1248 	type = WLAN_FC_GET_TYPE(fc);
1249 	stype = WLAN_FC_GET_STYPE(fc);
1250 	sc = le16_to_cpu(hdr->seq_ctrl);
1251 
1252 	/*Filter pkt not to me*/
1253 	multicast = is_multicast_ether_addr(hdr->addr1);
1254 	unicast = !multicast;
1255 	if (unicast && !ether_addr_equal(dev->dev_addr, hdr->addr1))
1256 		goto rx_dropped;
1257 
1258 	/*Filter pkt has too small length */
1259 	hdrlen = rtllib_rx_get_hdrlen(ieee, skb, rx_stats);
1260 	if (skb->len < hdrlen) {
1261 		netdev_info(dev,
1262 			    "%s():ERR!!! skb->len is smaller than hdrlen\n",
1263 			    __func__);
1264 		goto rx_dropped;
1265 	}
1266 
1267 	/* Filter Duplicate pkt */
1268 	ret = rtllib_rx_check_duplicate(ieee, skb, multicast);
1269 	if (ret < 0)
1270 		goto rx_dropped;
1271 
1272 	/* Filter CTRL Frame */
1273 	if (type == RTLLIB_FTYPE_CTL)
1274 		goto rx_dropped;
1275 
1276 	/* Filter MGNT Frame */
1277 	if (type == RTLLIB_FTYPE_MGMT) {
1278 		if (rtllib_rx_frame_mgmt(ieee, skb, rx_stats, type, stype))
1279 			goto rx_dropped;
1280 		else
1281 			goto rx_exit;
1282 	}
1283 
1284 	/* Filter WAPI DATA Frame */
1285 
1286 	/* Update statstics for AP roaming */
1287 	ieee->link_detect_info.NumRecvDataInPeriod++;
1288 	ieee->link_detect_info.num_rx_ok_in_period++;
1289 
1290 	/* Data frame - extract src/dst addresses */
1291 	rtllib_rx_extract_addr(ieee, hdr, dst, src, bssid);
1292 
1293 	/* Filter Data frames */
1294 	ret = rtllib_rx_data_filter(ieee, hdr, dst, src, bssid, hdr->addr2);
1295 	if (ret < 0)
1296 		goto rx_dropped;
1297 
1298 	if (skb->len == hdrlen)
1299 		goto rx_dropped;
1300 
1301 	/* Send pspoll based on moredata */
1302 	if ((ieee->iw_mode == IW_MODE_INFRA)  &&
1303 	    (ieee->sta_sleep == LPS_IS_SLEEP) &&
1304 	    (ieee->polling)) {
1305 		if (WLAN_FC_MORE_DATA(fc)) {
1306 			/* more data bit is set, let's request a new frame
1307 			 * from the AP
1308 			 */
1309 			rtllib_sta_ps_send_pspoll_frame(ieee);
1310 		} else {
1311 			ieee->polling =  false;
1312 		}
1313 	}
1314 
1315 	/* Get crypt if encrypted */
1316 	ret = rtllib_rx_get_crypt(ieee, skb, &crypt, hdrlen);
1317 	if (ret == -1)
1318 		goto rx_dropped;
1319 
1320 	/* Decrypt data frame (including reassemble) */
1321 	ret = rtllib_rx_decrypt(ieee, skb, rx_stats, crypt, hdrlen);
1322 	if (ret == -1)
1323 		goto rx_dropped;
1324 	else if (ret == -2)
1325 		goto rx_exit;
1326 
1327 	/* Get TS for Rx Reorder  */
1328 	hdr = (struct ieee80211_hdr *)skb->data;
1329 	if (ieee->current_network.qos_data.active && IsQoSDataFrame(skb->data)
1330 		&& !is_multicast_ether_addr(hdr->addr1)) {
1331 		TID = Frame_QoSTID(skb->data);
1332 		SeqNum = WLAN_GET_SEQ_SEQ(sc);
1333 		rtllib_get_ts(ieee, (struct ts_common_info **)&ts, hdr->addr2, TID,
1334 		      RX_DIR, true);
1335 		if (TID != 0 && TID != 3)
1336 			ieee->bis_any_nonbepkts = true;
1337 	}
1338 
1339 	/* Parse rx data frame (For AMSDU) */
1340 	/* skb: hdr + (possible reassembled) full plaintext payload */
1341 	rxb = kmalloc(sizeof(struct rtllib_rxb), GFP_ATOMIC);
1342 	if (!rxb)
1343 		goto rx_dropped;
1344 
1345 	/* to parse amsdu packets */
1346 	/* qos data packets & reserved bit is 1 */
1347 	if (parse_subframe(ieee, skb, rx_stats, rxb, src, dst) == 0) {
1348 		/* only to free rxb, and not submit the packets
1349 		 * to upper layer
1350 		 */
1351 		for (i = 0; i < rxb->nr_subframes; i++)
1352 			dev_kfree_skb(rxb->subframes[i]);
1353 		kfree(rxb);
1354 		rxb = NULL;
1355 		goto rx_dropped;
1356 	}
1357 
1358 	/* Update WAPI PN */
1359 
1360 	/* Check if leave LPS */
1361 	if (ieee->is_aggregate_frame)
1362 		nr_subframes = rxb->nr_subframes;
1363 	else
1364 		nr_subframes = 1;
1365 	if (unicast)
1366 		ieee->link_detect_info.NumRxUnicastOkInPeriod += nr_subframes;
1367 	rtllib_rx_check_leave_lps(ieee, unicast, nr_subframes);
1368 
1369 	/* Indicate packets to upper layer or Rx Reorder */
1370 	if (!ieee->ht_info->cur_rx_reorder_enable || !ts)
1371 		rtllib_rx_indicate_pkt_legacy(ieee, rx_stats, rxb, dst, src);
1372 	else
1373 		RxReorderIndicatePacket(ieee, rxb, ts, SeqNum);
1374 
1375 	dev_kfree_skb(skb);
1376 
1377  rx_exit:
1378 	return 1;
1379 
1380  rx_dropped:
1381 	ieee->stats.rx_dropped++;
1382 
1383 	/* Returning 0 indicates to caller that we have not handled the SKB--
1384 	 * so it is still allocated and can be used again by underlying
1385 	 * hardware as a DMA target
1386 	 */
1387 	return 0;
1388 }
1389 
1390 static int rtllib_rx_Monitor(struct rtllib_device *ieee, struct sk_buff *skb,
1391 		 struct rtllib_rx_stats *rx_stats)
1392 {
1393 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1394 	u16 fc = le16_to_cpu(hdr->frame_control);
1395 	size_t hdrlen = rtllib_get_hdrlen(fc);
1396 
1397 	if (skb->len < hdrlen) {
1398 		netdev_info(ieee->dev,
1399 			    "%s():ERR!!! skb->len is smaller than hdrlen\n",
1400 			    __func__);
1401 		return 0;
1402 	}
1403 
1404 	if (ht_c_check(ieee, skb->data)) {
1405 		if (net_ratelimit())
1406 			netdev_info(ieee->dev, "%s: Find HTCControl!\n",
1407 				    __func__);
1408 		hdrlen += 4;
1409 	}
1410 
1411 	ieee->stats.rx_packets++;
1412 	ieee->stats.rx_bytes += skb->len;
1413 	rtllib_monitor_rx(ieee, skb, rx_stats, hdrlen);
1414 
1415 	return 1;
1416 }
1417 
1418 /* All received frames are sent to this function. @skb contains the frame in
1419  * IEEE 802.11 format, i.e., in the format it was sent over air.
1420  * This function is called only as a tasklet (software IRQ).
1421  */
1422 int rtllib_rx(struct rtllib_device *ieee, struct sk_buff *skb,
1423 		 struct rtllib_rx_stats *rx_stats)
1424 {
1425 	int ret = 0;
1426 
1427 	if (!ieee || !skb || !rx_stats) {
1428 		pr_info("%s: Input parameters NULL!\n", __func__);
1429 		goto rx_dropped;
1430 	}
1431 	if (skb->len < 10) {
1432 		netdev_info(ieee->dev, "%s: SKB length < 10\n", __func__);
1433 		goto rx_dropped;
1434 	}
1435 
1436 	switch (ieee->iw_mode) {
1437 	case IW_MODE_INFRA:
1438 		ret = rtllib_rx_InfraAdhoc(ieee, skb, rx_stats);
1439 		break;
1440 	case IW_MODE_MONITOR:
1441 		ret = rtllib_rx_Monitor(ieee, skb, rx_stats);
1442 		break;
1443 	default:
1444 		netdev_info(ieee->dev, "%s: ERR iw mode!!!\n", __func__);
1445 		break;
1446 	}
1447 
1448 	return ret;
1449 
1450  rx_dropped:
1451 	if (ieee)
1452 		ieee->stats.rx_dropped++;
1453 	return 0;
1454 }
1455 EXPORT_SYMBOL(rtllib_rx);
1456 
1457 static u8 qos_oui[QOS_OUI_LEN] = { 0x00, 0x50, 0xF2 };
1458 
1459 /* Make ther structure we read from the beacon packet has the right values */
1460 static int rtllib_verify_qos_info(struct rtllib_qos_information_element
1461 				     *info_element, int sub_type)
1462 {
1463 	if (info_element->elementID != QOS_ELEMENT_ID)
1464 		return -1;
1465 	if (info_element->qui_subtype != sub_type)
1466 		return -1;
1467 	if (memcmp(info_element->qui, qos_oui, QOS_OUI_LEN))
1468 		return -1;
1469 	if (info_element->qui_type != QOS_OUI_TYPE)
1470 		return -1;
1471 	if (info_element->version != QOS_VERSION_1)
1472 		return -1;
1473 
1474 	return 0;
1475 }
1476 
1477 /* Parse a QoS parameter element */
1478 static int rtllib_read_qos_param_element(
1479 			struct rtllib_qos_parameter_info *element_param,
1480 			struct rtllib_info_element *info_element)
1481 {
1482 	size_t size = sizeof(*element_param);
1483 
1484 	if (!element_param || !info_element || info_element->len != size - 2)
1485 		return -1;
1486 
1487 	memcpy(element_param, info_element, size);
1488 	return rtllib_verify_qos_info(&element_param->info_element,
1489 				      QOS_OUI_PARAM_SUB_TYPE);
1490 }
1491 
1492 /* Parse a QoS information element */
1493 static int rtllib_read_qos_info_element(
1494 			struct rtllib_qos_information_element *element_info,
1495 			struct rtllib_info_element *info_element)
1496 {
1497 	size_t size = sizeof(*element_info);
1498 
1499 	if (!element_info || !info_element || info_element->len != size - 2)
1500 		return -1;
1501 
1502 	memcpy(element_info, info_element, size);
1503 	return rtllib_verify_qos_info(element_info, QOS_OUI_INFO_SUB_TYPE);
1504 }
1505 
1506 /* Write QoS parameters from the ac parameters. */
1507 static int rtllib_qos_convert_ac_to_parameters(struct rtllib_qos_parameter_info *param_elm,
1508 					       struct rtllib_qos_data *qos_data)
1509 {
1510 	struct rtllib_qos_ac_parameter *ac_params;
1511 	struct rtllib_qos_parameters *qos_param = &(qos_data->parameters);
1512 	int i;
1513 	u8 aci;
1514 	u8 acm;
1515 
1516 	qos_data->wmm_acm = 0;
1517 	for (i = 0; i < QOS_QUEUE_NUM; i++) {
1518 		ac_params = &(param_elm->ac_params_record[i]);
1519 
1520 		aci = (ac_params->aci_aifsn & 0x60) >> 5;
1521 		acm = (ac_params->aci_aifsn & 0x10) >> 4;
1522 
1523 		if (aci >= QOS_QUEUE_NUM)
1524 			continue;
1525 		switch (aci) {
1526 		case 1:
1527 			/* BIT(0) | BIT(3) */
1528 			if (acm)
1529 				qos_data->wmm_acm |= (0x01 << 0) | (0x01 << 3);
1530 			break;
1531 		case 2:
1532 			/* BIT(4) | BIT(5) */
1533 			if (acm)
1534 				qos_data->wmm_acm |= (0x01 << 4) | (0x01 << 5);
1535 			break;
1536 		case 3:
1537 			/* BIT(6) | BIT(7) */
1538 			if (acm)
1539 				qos_data->wmm_acm |= (0x01 << 6) | (0x01 << 7);
1540 			break;
1541 		case 0:
1542 		default:
1543 			/* BIT(1) | BIT(2) */
1544 			if (acm)
1545 				qos_data->wmm_acm |= (0x01 << 1) | (0x01 << 2);
1546 			break;
1547 		}
1548 
1549 		qos_param->aifs[aci] = (ac_params->aci_aifsn) & 0x0f;
1550 
1551 		/* WMM spec P.11: The minimum value for AIFSN shall be 2 */
1552 		qos_param->aifs[aci] = max_t(u8, qos_param->aifs[aci], 2);
1553 
1554 		qos_param->cw_min[aci] = cpu_to_le16(ac_params->ecw_min_max &
1555 						     0x0F);
1556 
1557 		qos_param->cw_max[aci] = cpu_to_le16((ac_params->ecw_min_max &
1558 						      0xF0) >> 4);
1559 
1560 		qos_param->flag[aci] =
1561 		    (ac_params->aci_aifsn & 0x10) ? 0x01 : 0x00;
1562 		qos_param->tx_op_limit[aci] = ac_params->tx_op_limit;
1563 	}
1564 	return 0;
1565 }
1566 
1567 /* we have a generic data element which it may contain QoS information or
1568  * parameters element. check the information element length to decide
1569  * which type to read
1570  */
1571 static int rtllib_parse_qos_info_param_IE(struct rtllib_device *ieee,
1572 					  struct rtllib_info_element
1573 					     *info_element,
1574 					  struct rtllib_network *network)
1575 {
1576 	int rc = 0;
1577 	struct rtllib_qos_information_element qos_info_element;
1578 
1579 	rc = rtllib_read_qos_info_element(&qos_info_element, info_element);
1580 
1581 	if (rc == 0) {
1582 		network->qos_data.param_count = qos_info_element.ac_info & 0x0F;
1583 		network->flags |= NETWORK_HAS_QOS_INFORMATION;
1584 	} else {
1585 		struct rtllib_qos_parameter_info param_element;
1586 
1587 		rc = rtllib_read_qos_param_element(&param_element,
1588 						      info_element);
1589 		if (rc == 0) {
1590 			rtllib_qos_convert_ac_to_parameters(&param_element,
1591 							       &(network->qos_data));
1592 			network->flags |= NETWORK_HAS_QOS_PARAMETERS;
1593 			network->qos_data.param_count =
1594 			    param_element.info_element.ac_info & 0x0F;
1595 		}
1596 	}
1597 
1598 	if (rc == 0) {
1599 		netdev_dbg(ieee->dev, "QoS is supported\n");
1600 		network->qos_data.supported = 1;
1601 	}
1602 	return rc;
1603 }
1604 
1605 static const char *get_info_element_string(u16 id)
1606 {
1607 	switch (id) {
1608 	case MFIE_TYPE_SSID:
1609 		return "SSID";
1610 	case MFIE_TYPE_RATES:
1611 		return "RATES";
1612 	case MFIE_TYPE_FH_SET:
1613 		return "FH_SET";
1614 	case MFIE_TYPE_DS_SET:
1615 		return "DS_SET";
1616 	case MFIE_TYPE_CF_SET:
1617 		return "CF_SET";
1618 	case MFIE_TYPE_TIM:
1619 		return "TIM";
1620 	case MFIE_TYPE_IBSS_SET:
1621 		return "IBSS_SET";
1622 	case MFIE_TYPE_COUNTRY:
1623 		return "COUNTRY";
1624 	case MFIE_TYPE_HOP_PARAMS:
1625 		return "HOP_PARAMS";
1626 	case MFIE_TYPE_HOP_TABLE:
1627 		return "HOP_TABLE";
1628 	case MFIE_TYPE_REQUEST:
1629 		return "REQUEST";
1630 	case MFIE_TYPE_CHALLENGE:
1631 		return "CHALLENGE";
1632 	case MFIE_TYPE_POWER_CONSTRAINT:
1633 		return "POWER_CONSTRAINT";
1634 	case MFIE_TYPE_POWER_CAPABILITY:
1635 		return "POWER_CAPABILITY";
1636 	case MFIE_TYPE_TPC_REQUEST:
1637 		return "TPC_REQUEST";
1638 	case MFIE_TYPE_TPC_REPORT:
1639 		return "TPC_REPORT";
1640 	case MFIE_TYPE_SUPP_CHANNELS:
1641 		return "SUPP_CHANNELS";
1642 	case MFIE_TYPE_CSA:
1643 		return "CSA";
1644 	case MFIE_TYPE_MEASURE_REQUEST:
1645 		return "MEASURE_REQUEST";
1646 	case MFIE_TYPE_MEASURE_REPORT:
1647 		return "MEASURE_REPORT";
1648 	case MFIE_TYPE_QUIET:
1649 		return "QUIET";
1650 	case MFIE_TYPE_IBSS_DFS:
1651 		return "IBSS_DFS";
1652 	case MFIE_TYPE_RSN:
1653 		return "RSN";
1654 	case MFIE_TYPE_RATES_EX:
1655 		return "RATES_EX";
1656 	case MFIE_TYPE_GENERIC:
1657 		return "GENERIC";
1658 	case MFIE_TYPE_QOS_PARAMETER:
1659 		return "QOS_PARAMETER";
1660 	default:
1661 		return "UNKNOWN";
1662 	}
1663 }
1664 
1665 static void rtllib_parse_mife_generic(struct rtllib_device *ieee,
1666 				      struct rtllib_info_element *info_element,
1667 				      struct rtllib_network *network,
1668 				      u16 *tmp_htcap_len,
1669 				      u16 *tmp_htinfo_len)
1670 {
1671 	u16 ht_realtek_agg_len = 0;
1672 	u8  ht_realtek_agg_buf[MAX_IE_LEN];
1673 
1674 	if (!rtllib_parse_qos_info_param_IE(ieee, info_element, network))
1675 		return;
1676 	if (info_element->len >= 4 &&
1677 	    info_element->data[0] == 0x00 &&
1678 	    info_element->data[1] == 0x50 &&
1679 	    info_element->data[2] == 0xf2 &&
1680 	    info_element->data[3] == 0x01) {
1681 		network->wpa_ie_len = min(info_element->len + 2,
1682 					  MAX_WPA_IE_LEN);
1683 		memcpy(network->wpa_ie, info_element, network->wpa_ie_len);
1684 		return;
1685 	}
1686 	if (info_element->len == 7 &&
1687 	    info_element->data[0] == 0x00 &&
1688 	    info_element->data[1] == 0xe0 &&
1689 	    info_element->data[2] == 0x4c &&
1690 	    info_element->data[3] == 0x01 &&
1691 	    info_element->data[4] == 0x02)
1692 		network->Turbo_Enable = 1;
1693 
1694 	if (*tmp_htcap_len == 0) {
1695 		if (info_element->len >= 4 &&
1696 		    info_element->data[0] == 0x00 &&
1697 		    info_element->data[1] == 0x90 &&
1698 		    info_element->data[2] == 0x4c &&
1699 		    info_element->data[3] == 0x033) {
1700 			*tmp_htcap_len = min_t(u8, info_element->len,
1701 					       MAX_IE_LEN);
1702 			if (*tmp_htcap_len != 0) {
1703 				network->bssht.bd_ht_spec_ver = HT_SPEC_VER_EWC;
1704 				network->bssht.bd_ht_cap_len = min_t(u16, *tmp_htcap_len,
1705 								  sizeof(network->bssht.bd_ht_cap_buf));
1706 				memcpy(network->bssht.bd_ht_cap_buf,
1707 				       info_element->data,
1708 				       network->bssht.bd_ht_cap_len);
1709 			}
1710 		}
1711 		if (*tmp_htcap_len != 0) {
1712 			network->bssht.bd_support_ht = true;
1713 			network->bssht.bd_ht_1r = ((((struct ht_capab_ele *)(network->bssht.bd_ht_cap_buf))->MCS[1]) == 0);
1714 		} else {
1715 			network->bssht.bd_support_ht = false;
1716 			network->bssht.bd_ht_1r = false;
1717 		}
1718 	}
1719 
1720 	if (*tmp_htinfo_len == 0) {
1721 		if (info_element->len >= 4 &&
1722 		    info_element->data[0] == 0x00 &&
1723 		    info_element->data[1] == 0x90 &&
1724 		    info_element->data[2] == 0x4c &&
1725 		    info_element->data[3] == 0x034) {
1726 			*tmp_htinfo_len = min_t(u8, info_element->len,
1727 						MAX_IE_LEN);
1728 			if (*tmp_htinfo_len != 0) {
1729 				network->bssht.bd_ht_spec_ver = HT_SPEC_VER_EWC;
1730 				network->bssht.bd_ht_info_len = min_t(u16, *tmp_htinfo_len,
1731 								      sizeof(network->bssht.bd_ht_info_buf));
1732 				memcpy(network->bssht.bd_ht_info_buf,
1733 				       info_element->data,
1734 				       network->bssht.bd_ht_info_len);
1735 			}
1736 		}
1737 	}
1738 
1739 	if (network->bssht.bd_support_ht) {
1740 		if (info_element->len >= 4 &&
1741 		    info_element->data[0] == 0x00 &&
1742 		    info_element->data[1] == 0xe0 &&
1743 		    info_element->data[2] == 0x4c &&
1744 		    info_element->data[3] == 0x02) {
1745 			ht_realtek_agg_len = min_t(u8, info_element->len,
1746 						   MAX_IE_LEN);
1747 			memcpy(ht_realtek_agg_buf, info_element->data,
1748 			       info_element->len);
1749 		}
1750 		if (ht_realtek_agg_len >= 5) {
1751 			network->realtek_cap_exit = true;
1752 			network->bssht.bd_rt2rt_aggregation = true;
1753 
1754 			if ((ht_realtek_agg_buf[4] == 1) &&
1755 			    (ht_realtek_agg_buf[5] & 0x02))
1756 				network->bssht.bd_rt2rt_long_slot_time = true;
1757 
1758 			if ((ht_realtek_agg_buf[4] == 1) &&
1759 			    (ht_realtek_agg_buf[5] & RT_HT_CAP_USE_92SE))
1760 				network->bssht.rt2rt_ht_mode |= RT_HT_CAP_USE_92SE;
1761 		}
1762 	}
1763 	if (ht_realtek_agg_len >= 5) {
1764 		if ((ht_realtek_agg_buf[5] & RT_HT_CAP_USE_SOFTAP))
1765 			network->bssht.rt2rt_ht_mode |= RT_HT_CAP_USE_SOFTAP;
1766 	}
1767 
1768 	if ((info_element->len >= 3 &&
1769 	     info_element->data[0] == 0x00 &&
1770 	     info_element->data[1] == 0x05 &&
1771 	     info_element->data[2] == 0xb5) ||
1772 	     (info_element->len >= 3 &&
1773 	     info_element->data[0] == 0x00 &&
1774 	     info_element->data[1] == 0x0a &&
1775 	     info_element->data[2] == 0xf7) ||
1776 	     (info_element->len >= 3 &&
1777 	     info_element->data[0] == 0x00 &&
1778 	     info_element->data[1] == 0x10 &&
1779 	     info_element->data[2] == 0x18)) {
1780 		network->broadcom_cap_exist = true;
1781 	}
1782 	if (info_element->len >= 3 &&
1783 	    info_element->data[0] == 0x00 &&
1784 	    info_element->data[1] == 0x0c &&
1785 	    info_element->data[2] == 0x43)
1786 		network->ralink_cap_exist = true;
1787 	if ((info_element->len >= 3 &&
1788 	     info_element->data[0] == 0x00 &&
1789 	     info_element->data[1] == 0x03 &&
1790 	     info_element->data[2] == 0x7f) ||
1791 	     (info_element->len >= 3 &&
1792 	     info_element->data[0] == 0x00 &&
1793 	     info_element->data[1] == 0x13 &&
1794 	     info_element->data[2] == 0x74))
1795 		network->atheros_cap_exist = true;
1796 
1797 	if ((info_element->len >= 3 &&
1798 	     info_element->data[0] == 0x00 &&
1799 	     info_element->data[1] == 0x50 &&
1800 	     info_element->data[2] == 0x43))
1801 		network->marvell_cap_exist = true;
1802 	if (info_element->len >= 3 &&
1803 	    info_element->data[0] == 0x00 &&
1804 	    info_element->data[1] == 0x40 &&
1805 	    info_element->data[2] == 0x96)
1806 		network->cisco_cap_exist = true;
1807 
1808 	if (info_element->len >= 3 &&
1809 	    info_element->data[0] == 0x00 &&
1810 	    info_element->data[1] == 0x0a &&
1811 	    info_element->data[2] == 0xf5)
1812 		network->airgo_cap_exist = true;
1813 
1814 	if (info_element->len > 4 &&
1815 	    info_element->data[0] == 0x00 &&
1816 	    info_element->data[1] == 0x40 &&
1817 	    info_element->data[2] == 0x96 &&
1818 	    info_element->data[3] == 0x01) {
1819 		if (info_element->len == 6) {
1820 			memcpy(network->CcxRmState, &info_element->data[4], 2);
1821 			if (network->CcxRmState[0] != 0)
1822 				network->bCcxRmEnable = true;
1823 			else
1824 				network->bCcxRmEnable = false;
1825 			network->MBssidMask = network->CcxRmState[1] & 0x07;
1826 			if (network->MBssidMask != 0) {
1827 				network->bMBssidValid = true;
1828 				network->MBssidMask = 0xff <<
1829 						      (network->MBssidMask);
1830 				ether_addr_copy(network->MBssid,
1831 						network->bssid);
1832 				network->MBssid[5] &= network->MBssidMask;
1833 			} else {
1834 				network->bMBssidValid = false;
1835 			}
1836 		} else {
1837 			network->bCcxRmEnable = false;
1838 		}
1839 	}
1840 	if (info_element->len > 4  &&
1841 	    info_element->data[0] == 0x00 &&
1842 	    info_element->data[1] == 0x40 &&
1843 	    info_element->data[2] == 0x96 &&
1844 	    info_element->data[3] == 0x03) {
1845 		if (info_element->len == 5) {
1846 			network->bWithCcxVerNum = true;
1847 			network->BssCcxVerNumber = info_element->data[4];
1848 		} else {
1849 			network->bWithCcxVerNum = false;
1850 			network->BssCcxVerNumber = 0;
1851 		}
1852 	}
1853 	if (info_element->len > 4  &&
1854 	    info_element->data[0] == 0x00 &&
1855 	    info_element->data[1] == 0x50 &&
1856 	    info_element->data[2] == 0xf2 &&
1857 	    info_element->data[3] == 0x04) {
1858 		netdev_dbg(ieee->dev, "MFIE_TYPE_WZC: %d bytes\n",
1859 			   info_element->len);
1860 		network->wzc_ie_len = min(info_element->len + 2, MAX_WZC_IE_LEN);
1861 		memcpy(network->wzc_ie, info_element, network->wzc_ie_len);
1862 	}
1863 }
1864 
1865 static void rtllib_parse_mfie_ht_cap(struct rtllib_info_element *info_element,
1866 				     struct rtllib_network *network,
1867 				     u16 *tmp_htcap_len)
1868 {
1869 	struct bss_ht *ht = &network->bssht;
1870 
1871 	*tmp_htcap_len = min_t(u8, info_element->len, MAX_IE_LEN);
1872 	if (*tmp_htcap_len != 0) {
1873 		ht->bd_ht_spec_ver = HT_SPEC_VER_EWC;
1874 		ht->bd_ht_cap_len = min_t(u16, *tmp_htcap_len,
1875 				       sizeof(ht->bd_ht_cap_buf));
1876 		memcpy(ht->bd_ht_cap_buf, info_element->data, ht->bd_ht_cap_len);
1877 
1878 		ht->bd_support_ht = true;
1879 		ht->bd_ht_1r = ((((struct ht_capab_ele *)
1880 				ht->bd_ht_cap_buf))->MCS[1]) == 0;
1881 
1882 		ht->bd_bandwidth = (enum ht_channel_width)
1883 					     (((struct ht_capab_ele *)
1884 					     (ht->bd_ht_cap_buf))->ChlWidth);
1885 	} else {
1886 		ht->bd_support_ht = false;
1887 		ht->bd_ht_1r = false;
1888 		ht->bd_bandwidth = HT_CHANNEL_WIDTH_20;
1889 	}
1890 }
1891 
1892 int rtllib_parse_info_param(struct rtllib_device *ieee,
1893 		struct rtllib_info_element *info_element,
1894 		u16 length,
1895 		struct rtllib_network *network,
1896 		struct rtllib_rx_stats *stats)
1897 {
1898 	u8 i;
1899 	short offset;
1900 	u16	tmp_htcap_len = 0;
1901 	u16	tmp_htinfo_len = 0;
1902 	char rates_str[64];
1903 	char *p;
1904 
1905 	while (length >= sizeof(*info_element)) {
1906 		if (sizeof(*info_element) + info_element->len > length) {
1907 			netdev_dbg(ieee->dev,
1908 				   "Info elem: parse failed: info_element->len + 2 > left : info_element->len+2=%zd left=%d, id=%d.\n",
1909 				   info_element->len + sizeof(*info_element),
1910 				   length, info_element->id);
1911 			/* We stop processing but don't return an error here
1912 			 * because some misbehaviour APs break this rule. ie.
1913 			 * Orinoco AP1000.
1914 			 */
1915 			break;
1916 		}
1917 
1918 		switch (info_element->id) {
1919 		case MFIE_TYPE_SSID:
1920 			if (rtllib_is_empty_essid(info_element->data,
1921 						     info_element->len)) {
1922 				network->flags |= NETWORK_EMPTY_ESSID;
1923 				break;
1924 			}
1925 
1926 			network->ssid_len = min(info_element->len,
1927 						(u8)IW_ESSID_MAX_SIZE);
1928 			memcpy(network->ssid, info_element->data,
1929 			       network->ssid_len);
1930 			if (network->ssid_len < IW_ESSID_MAX_SIZE)
1931 				memset(network->ssid + network->ssid_len, 0,
1932 				       IW_ESSID_MAX_SIZE - network->ssid_len);
1933 
1934 			netdev_dbg(ieee->dev, "MFIE_TYPE_SSID: '%s' len=%d.\n",
1935 				   network->ssid, network->ssid_len);
1936 			break;
1937 
1938 		case MFIE_TYPE_RATES:
1939 			p = rates_str;
1940 			network->rates_len = min(info_element->len,
1941 						 MAX_RATES_LENGTH);
1942 			for (i = 0; i < network->rates_len; i++) {
1943 				network->rates[i] = info_element->data[i];
1944 				p += scnprintf(p, sizeof(rates_str) -
1945 					      (p - rates_str), "%02X ",
1946 					      network->rates[i]);
1947 				if (rtllib_is_ofdm_rate
1948 				    (info_element->data[i])) {
1949 					network->flags |= NETWORK_HAS_OFDM;
1950 					if (info_element->data[i] &
1951 					    RTLLIB_BASIC_RATE_MASK)
1952 						network->flags &=
1953 						    ~NETWORK_HAS_CCK;
1954 				}
1955 
1956 				if (rtllib_is_cck_rate
1957 				    (info_element->data[i])) {
1958 					network->flags |= NETWORK_HAS_CCK;
1959 				}
1960 			}
1961 
1962 			netdev_dbg(ieee->dev, "MFIE_TYPE_RATES: '%s' (%d)\n",
1963 				   rates_str, network->rates_len);
1964 			break;
1965 
1966 		case MFIE_TYPE_RATES_EX:
1967 			p = rates_str;
1968 			network->rates_ex_len = min(info_element->len,
1969 						    MAX_RATES_EX_LENGTH);
1970 			for (i = 0; i < network->rates_ex_len; i++) {
1971 				network->rates_ex[i] = info_element->data[i];
1972 				p += scnprintf(p, sizeof(rates_str) -
1973 					      (p - rates_str), "%02X ",
1974 					      network->rates_ex[i]);
1975 				if (rtllib_is_ofdm_rate
1976 				    (info_element->data[i])) {
1977 					network->flags |= NETWORK_HAS_OFDM;
1978 					if (info_element->data[i] &
1979 					    RTLLIB_BASIC_RATE_MASK)
1980 						network->flags &=
1981 						    ~NETWORK_HAS_CCK;
1982 				}
1983 			}
1984 
1985 			netdev_dbg(ieee->dev, "MFIE_TYPE_RATES_EX: '%s' (%d)\n",
1986 				   rates_str, network->rates_ex_len);
1987 			break;
1988 
1989 		case MFIE_TYPE_DS_SET:
1990 			netdev_dbg(ieee->dev, "MFIE_TYPE_DS_SET: %d\n",
1991 				   info_element->data[0]);
1992 			network->channel = info_element->data[0];
1993 			break;
1994 
1995 		case MFIE_TYPE_FH_SET:
1996 			netdev_dbg(ieee->dev, "MFIE_TYPE_FH_SET: ignored\n");
1997 			break;
1998 
1999 		case MFIE_TYPE_CF_SET:
2000 			netdev_dbg(ieee->dev, "MFIE_TYPE_CF_SET: ignored\n");
2001 			break;
2002 
2003 		case MFIE_TYPE_TIM:
2004 			if (info_element->len < 4)
2005 				break;
2006 
2007 			network->tim.tim_count = info_element->data[0];
2008 			network->tim.tim_period = info_element->data[1];
2009 
2010 			network->dtim_period = info_element->data[1];
2011 			if (ieee->link_state != MAC80211_LINKED)
2012 				break;
2013 			network->last_dtim_sta_time = jiffies;
2014 
2015 			network->dtim_data = RTLLIB_DTIM_VALID;
2016 
2017 			if (info_element->data[2] & 1)
2018 				network->dtim_data |= RTLLIB_DTIM_MBCAST;
2019 
2020 			offset = (info_element->data[2] >> 1) * 2;
2021 
2022 			if (ieee->assoc_id < 8 * offset ||
2023 			    ieee->assoc_id > 8 * (offset + info_element->len - 3))
2024 				break;
2025 
2026 			offset = (ieee->assoc_id / 8) - offset;
2027 			if (info_element->data[3 + offset] &
2028 			   (1 << (ieee->assoc_id % 8)))
2029 				network->dtim_data |= RTLLIB_DTIM_UCAST;
2030 
2031 			network->listen_interval = network->dtim_period;
2032 			break;
2033 
2034 		case MFIE_TYPE_ERP:
2035 			network->erp_value = info_element->data[0];
2036 			network->flags |= NETWORK_HAS_ERP_VALUE;
2037 			netdev_dbg(ieee->dev, "MFIE_TYPE_ERP_SET: %d\n",
2038 				   network->erp_value);
2039 			break;
2040 		case MFIE_TYPE_IBSS_SET:
2041 			network->atim_window = info_element->data[0];
2042 			netdev_dbg(ieee->dev, "MFIE_TYPE_IBSS_SET: %d\n",
2043 				   network->atim_window);
2044 			break;
2045 
2046 		case MFIE_TYPE_CHALLENGE:
2047 			netdev_dbg(ieee->dev, "MFIE_TYPE_CHALLENGE: ignored\n");
2048 			break;
2049 
2050 		case MFIE_TYPE_GENERIC:
2051 			netdev_dbg(ieee->dev, "MFIE_TYPE_GENERIC: %d bytes\n",
2052 				   info_element->len);
2053 
2054 			rtllib_parse_mife_generic(ieee, info_element, network,
2055 						  &tmp_htcap_len,
2056 						  &tmp_htinfo_len);
2057 			break;
2058 
2059 		case MFIE_TYPE_RSN:
2060 			netdev_dbg(ieee->dev, "MFIE_TYPE_RSN: %d bytes\n",
2061 				   info_element->len);
2062 			network->rsn_ie_len = min(info_element->len + 2,
2063 						  MAX_WPA_IE_LEN);
2064 			memcpy(network->rsn_ie, info_element,
2065 			       network->rsn_ie_len);
2066 			break;
2067 
2068 		case MFIE_TYPE_HT_CAP:
2069 			netdev_dbg(ieee->dev, "MFIE_TYPE_HT_CAP: %d bytes\n",
2070 				   info_element->len);
2071 
2072 			rtllib_parse_mfie_ht_cap(info_element, network,
2073 						 &tmp_htcap_len);
2074 			break;
2075 
2076 		case MFIE_TYPE_HT_INFO:
2077 			netdev_dbg(ieee->dev, "MFIE_TYPE_HT_INFO: %d bytes\n",
2078 				   info_element->len);
2079 			tmp_htinfo_len = min_t(u8, info_element->len,
2080 					       MAX_IE_LEN);
2081 			if (tmp_htinfo_len) {
2082 				network->bssht.bd_ht_spec_ver = HT_SPEC_VER_IEEE;
2083 				network->bssht.bd_ht_info_len = tmp_htinfo_len >
2084 					sizeof(network->bssht.bd_ht_info_buf) ?
2085 					sizeof(network->bssht.bd_ht_info_buf) :
2086 					tmp_htinfo_len;
2087 				memcpy(network->bssht.bd_ht_info_buf,
2088 				       info_element->data,
2089 				       network->bssht.bd_ht_info_len);
2090 			}
2091 			break;
2092 
2093 		case MFIE_TYPE_AIRONET:
2094 			netdev_dbg(ieee->dev, "MFIE_TYPE_AIRONET: %d bytes\n",
2095 				   info_element->len);
2096 			if (info_element->len > IE_CISCO_FLAG_POSITION) {
2097 				network->bWithAironetIE = true;
2098 
2099 				if ((info_element->data[IE_CISCO_FLAG_POSITION]
2100 				     & SUPPORT_CKIP_MIC) ||
2101 				     (info_element->data[IE_CISCO_FLAG_POSITION]
2102 				     & SUPPORT_CKIP_PK))
2103 					network->bCkipSupported = true;
2104 				else
2105 					network->bCkipSupported = false;
2106 			} else {
2107 				network->bWithAironetIE = false;
2108 				network->bCkipSupported = false;
2109 			}
2110 			break;
2111 		case MFIE_TYPE_QOS_PARAMETER:
2112 			netdev_err(ieee->dev,
2113 				   "QoS Error need to parse QOS_PARAMETER IE\n");
2114 			break;
2115 
2116 		case MFIE_TYPE_COUNTRY:
2117 			netdev_dbg(ieee->dev, "MFIE_TYPE_COUNTRY: %d bytes\n",
2118 				   info_element->len);
2119 			break;
2120 /* TODO */
2121 		default:
2122 			netdev_dbg(ieee->dev,
2123 				   "Unsupported info element: %s (%d)\n",
2124 				   get_info_element_string(info_element->id),
2125 				   info_element->id);
2126 			break;
2127 		}
2128 
2129 		length -= sizeof(*info_element) + info_element->len;
2130 		info_element =
2131 		    (struct rtllib_info_element *)&info_element->data[info_element->len];
2132 	}
2133 
2134 	if (!network->atheros_cap_exist && !network->broadcom_cap_exist &&
2135 	    !network->cisco_cap_exist && !network->ralink_cap_exist &&
2136 	    !network->bssht.bd_rt2rt_aggregation)
2137 		network->unknown_cap_exist = true;
2138 	else
2139 		network->unknown_cap_exist = false;
2140 	return 0;
2141 }
2142 
2143 static long rtllib_translate_todbm(u8 signal_strength_index)
2144 {
2145 	long	signal_power;
2146 
2147 	signal_power = (long)((signal_strength_index + 1) >> 1);
2148 	signal_power -= 95;
2149 
2150 	return signal_power;
2151 }
2152 
2153 static inline int rtllib_network_init(
2154 	struct rtllib_device *ieee,
2155 	struct rtllib_probe_response *beacon,
2156 	struct rtllib_network *network,
2157 	struct rtllib_rx_stats *stats)
2158 {
2159 	memset(&network->qos_data, 0, sizeof(struct rtllib_qos_data));
2160 
2161 	/* Pull out fixed field data */
2162 	ether_addr_copy(network->bssid, beacon->header.addr3);
2163 	network->capability = le16_to_cpu(beacon->capability);
2164 	network->last_scanned = jiffies;
2165 	network->time_stamp[0] = beacon->time_stamp[0];
2166 	network->time_stamp[1] = beacon->time_stamp[1];
2167 	network->beacon_interval = le16_to_cpu(beacon->beacon_interval);
2168 	/* Where to pull this? beacon->listen_interval;*/
2169 	network->listen_interval = 0x0A;
2170 	network->rates_len = network->rates_ex_len = 0;
2171 	network->ssid_len = 0;
2172 	network->hidden_ssid_len = 0;
2173 	memset(network->hidden_ssid, 0, sizeof(network->hidden_ssid));
2174 	network->flags = 0;
2175 	network->atim_window = 0;
2176 	network->erp_value = (network->capability & WLAN_CAPABILITY_IBSS) ?
2177 	    0x3 : 0x0;
2178 	network->berp_info_valid = false;
2179 	network->broadcom_cap_exist = false;
2180 	network->ralink_cap_exist = false;
2181 	network->atheros_cap_exist = false;
2182 	network->cisco_cap_exist = false;
2183 	network->unknown_cap_exist = false;
2184 	network->realtek_cap_exit = false;
2185 	network->marvell_cap_exist = false;
2186 	network->airgo_cap_exist = false;
2187 	network->Turbo_Enable = 0;
2188 	network->SignalStrength = stats->SignalStrength;
2189 	network->RSSI = stats->SignalStrength;
2190 	network->CountryIeLen = 0;
2191 	memset(network->CountryIeBuf, 0, MAX_IE_LEN);
2192 	ht_initialize_bss_desc(&network->bssht);
2193 	network->flags |= NETWORK_HAS_CCK;
2194 
2195 	network->wpa_ie_len = 0;
2196 	network->rsn_ie_len = 0;
2197 	network->wzc_ie_len = 0;
2198 
2199 	if (rtllib_parse_info_param(ieee,
2200 			beacon->info_element,
2201 			(stats->len - sizeof(*beacon)),
2202 			network,
2203 			stats))
2204 		return 1;
2205 
2206 	network->mode = 0;
2207 
2208 	if (network->flags & NETWORK_HAS_OFDM)
2209 		network->mode |= WIRELESS_MODE_G;
2210 	if (network->flags & NETWORK_HAS_CCK)
2211 		network->mode |= WIRELESS_MODE_B;
2212 
2213 	if (network->mode == 0) {
2214 		netdev_dbg(ieee->dev, "Filtered out '%s (%pM)' network.\n",
2215 			   escape_essid(network->ssid, network->ssid_len),
2216 			   network->bssid);
2217 		return 1;
2218 	}
2219 
2220 	if (network->bssht.bd_support_ht) {
2221 		if (network->mode & (WIRELESS_MODE_G | WIRELESS_MODE_B))
2222 			network->mode = WIRELESS_MODE_N_24G;
2223 	}
2224 	if (rtllib_is_empty_essid(network->ssid, network->ssid_len))
2225 		network->flags |= NETWORK_EMPTY_ESSID;
2226 	stats->signal = 30 + (stats->SignalStrength * 70) / 100;
2227 	stats->noise = rtllib_translate_todbm((u8)(100 - stats->signal)) - 25;
2228 
2229 	memcpy(&network->stats, stats, sizeof(network->stats));
2230 
2231 	return 0;
2232 }
2233 
2234 static inline int is_same_network(struct rtllib_network *src,
2235 				  struct rtllib_network *dst, u8 ssidbroad)
2236 {
2237 	/* A network is only a duplicate if the channel, BSSID, ESSID
2238 	 * and the capability field (in particular IBSS and BSS) all match.
2239 	 * We treat all <hidden> with the same BSSID and channel
2240 	 * as one network
2241 	 */
2242 	return (((src->ssid_len == dst->ssid_len) || (!ssidbroad)) &&
2243 		(src->channel == dst->channel) &&
2244 		!memcmp(src->bssid, dst->bssid, ETH_ALEN) &&
2245 		(!memcmp(src->ssid, dst->ssid, src->ssid_len) ||
2246 		(!ssidbroad)) &&
2247 		((src->capability & WLAN_CAPABILITY_IBSS) ==
2248 		(dst->capability & WLAN_CAPABILITY_IBSS)) &&
2249 		((src->capability & WLAN_CAPABILITY_ESS) ==
2250 		(dst->capability & WLAN_CAPABILITY_ESS)));
2251 }
2252 
2253 static inline void update_network(struct rtllib_device *ieee,
2254 				  struct rtllib_network *dst,
2255 				  struct rtllib_network *src)
2256 {
2257 	int qos_active;
2258 	u8 old_param;
2259 
2260 	memcpy(&dst->stats, &src->stats, sizeof(struct rtllib_rx_stats));
2261 	dst->capability = src->capability;
2262 	memcpy(dst->rates, src->rates, src->rates_len);
2263 	dst->rates_len = src->rates_len;
2264 	memcpy(dst->rates_ex, src->rates_ex, src->rates_ex_len);
2265 	dst->rates_ex_len = src->rates_ex_len;
2266 	if (src->ssid_len > 0) {
2267 		if (dst->ssid_len == 0) {
2268 			memset(dst->hidden_ssid, 0, sizeof(dst->hidden_ssid));
2269 			dst->hidden_ssid_len = src->ssid_len;
2270 			memcpy(dst->hidden_ssid, src->ssid, src->ssid_len);
2271 		} else {
2272 			memset(dst->ssid, 0, dst->ssid_len);
2273 			dst->ssid_len = src->ssid_len;
2274 			memcpy(dst->ssid, src->ssid, src->ssid_len);
2275 		}
2276 	}
2277 	dst->mode = src->mode;
2278 	dst->flags = src->flags;
2279 	dst->time_stamp[0] = src->time_stamp[0];
2280 	dst->time_stamp[1] = src->time_stamp[1];
2281 	if (src->flags & NETWORK_HAS_ERP_VALUE) {
2282 		dst->erp_value = src->erp_value;
2283 		dst->berp_info_valid = src->berp_info_valid = true;
2284 	}
2285 	dst->beacon_interval = src->beacon_interval;
2286 	dst->listen_interval = src->listen_interval;
2287 	dst->atim_window = src->atim_window;
2288 	dst->dtim_period = src->dtim_period;
2289 	dst->dtim_data = src->dtim_data;
2290 	dst->last_dtim_sta_time = src->last_dtim_sta_time;
2291 	memcpy(&dst->tim, &src->tim, sizeof(struct rtllib_tim_parameters));
2292 
2293 	dst->bssht.bd_support_ht = src->bssht.bd_support_ht;
2294 	dst->bssht.bd_rt2rt_aggregation = src->bssht.bd_rt2rt_aggregation;
2295 	dst->bssht.bd_ht_cap_len = src->bssht.bd_ht_cap_len;
2296 	memcpy(dst->bssht.bd_ht_cap_buf, src->bssht.bd_ht_cap_buf,
2297 	       src->bssht.bd_ht_cap_len);
2298 	dst->bssht.bd_ht_info_len = src->bssht.bd_ht_info_len;
2299 	memcpy(dst->bssht.bd_ht_info_buf, src->bssht.bd_ht_info_buf,
2300 	       src->bssht.bd_ht_info_len);
2301 	dst->bssht.bd_ht_spec_ver = src->bssht.bd_ht_spec_ver;
2302 	dst->bssht.bd_rt2rt_long_slot_time = src->bssht.bd_rt2rt_long_slot_time;
2303 	dst->broadcom_cap_exist = src->broadcom_cap_exist;
2304 	dst->ralink_cap_exist = src->ralink_cap_exist;
2305 	dst->atheros_cap_exist = src->atheros_cap_exist;
2306 	dst->realtek_cap_exit = src->realtek_cap_exit;
2307 	dst->marvell_cap_exist = src->marvell_cap_exist;
2308 	dst->cisco_cap_exist = src->cisco_cap_exist;
2309 	dst->airgo_cap_exist = src->airgo_cap_exist;
2310 	dst->unknown_cap_exist = src->unknown_cap_exist;
2311 	memcpy(dst->wpa_ie, src->wpa_ie, src->wpa_ie_len);
2312 	dst->wpa_ie_len = src->wpa_ie_len;
2313 	memcpy(dst->rsn_ie, src->rsn_ie, src->rsn_ie_len);
2314 	dst->rsn_ie_len = src->rsn_ie_len;
2315 	memcpy(dst->wzc_ie, src->wzc_ie, src->wzc_ie_len);
2316 	dst->wzc_ie_len = src->wzc_ie_len;
2317 
2318 	dst->last_scanned = jiffies;
2319 	/* qos related parameters */
2320 	qos_active = dst->qos_data.active;
2321 	old_param = dst->qos_data.param_count;
2322 	dst->qos_data.supported = src->qos_data.supported;
2323 	if (dst->flags & NETWORK_HAS_QOS_PARAMETERS)
2324 		memcpy(&dst->qos_data, &src->qos_data,
2325 		       sizeof(struct rtllib_qos_data));
2326 	if (dst->qos_data.supported == 1) {
2327 		if (dst->ssid_len)
2328 			netdev_dbg(ieee->dev,
2329 				   "QoS the network %s is QoS supported\n",
2330 				   dst->ssid);
2331 		else
2332 			netdev_dbg(ieee->dev,
2333 				   "QoS the network is QoS supported\n");
2334 	}
2335 	dst->qos_data.active = qos_active;
2336 	dst->qos_data.old_param_count = old_param;
2337 
2338 	dst->wmm_info = src->wmm_info;
2339 	if (src->wmm_param[0].ac_aci_acm_aifsn ||
2340 	   src->wmm_param[1].ac_aci_acm_aifsn ||
2341 	   src->wmm_param[2].ac_aci_acm_aifsn ||
2342 	   src->wmm_param[3].ac_aci_acm_aifsn)
2343 		memcpy(dst->wmm_param, src->wmm_param, WME_AC_PRAM_LEN);
2344 
2345 	dst->SignalStrength = src->SignalStrength;
2346 	dst->RSSI = src->RSSI;
2347 	dst->Turbo_Enable = src->Turbo_Enable;
2348 
2349 	dst->CountryIeLen = src->CountryIeLen;
2350 	memcpy(dst->CountryIeBuf, src->CountryIeBuf, src->CountryIeLen);
2351 
2352 	dst->bWithAironetIE = src->bWithAironetIE;
2353 	dst->bCkipSupported = src->bCkipSupported;
2354 	memcpy(dst->CcxRmState, src->CcxRmState, 2);
2355 	dst->bCcxRmEnable = src->bCcxRmEnable;
2356 	dst->MBssidMask = src->MBssidMask;
2357 	dst->bMBssidValid = src->bMBssidValid;
2358 	memcpy(dst->MBssid, src->MBssid, 6);
2359 	dst->bWithCcxVerNum = src->bWithCcxVerNum;
2360 	dst->BssCcxVerNumber = src->BssCcxVerNumber;
2361 }
2362 
2363 static int IsPassiveChannel(struct rtllib_device *rtllib, u8 channel)
2364 {
2365 	if (channel > MAX_CHANNEL_NUMBER) {
2366 		netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
2367 		return 0;
2368 	}
2369 
2370 	if (rtllib->active_channel_map[channel] == 2)
2371 		return 1;
2372 
2373 	return 0;
2374 }
2375 
2376 int rtllib_legal_channel(struct rtllib_device *rtllib, u8 channel)
2377 {
2378 	if (channel > MAX_CHANNEL_NUMBER) {
2379 		netdev_info(rtllib->dev, "%s(): Invalid Channel\n", __func__);
2380 		return 0;
2381 	}
2382 	if (rtllib->active_channel_map[channel] > 0)
2383 		return 1;
2384 
2385 	return 0;
2386 }
2387 EXPORT_SYMBOL(rtllib_legal_channel);
2388 
2389 static inline void rtllib_process_probe_response(
2390 	struct rtllib_device *ieee,
2391 	struct rtllib_probe_response *beacon,
2392 	struct rtllib_rx_stats *stats)
2393 {
2394 	struct rtllib_network *target;
2395 	struct rtllib_network *oldest = NULL;
2396 	struct rtllib_info_element *info_element = &beacon->info_element[0];
2397 	unsigned long flags;
2398 	short renew;
2399 	struct rtllib_network *network = kzalloc(sizeof(struct rtllib_network),
2400 						 GFP_ATOMIC);
2401 	__le16 frame_ctl = beacon->header.frame_control;
2402 
2403 	if (!network)
2404 		return;
2405 
2406 	netdev_dbg(ieee->dev,
2407 		   "'%s' ( %pM ): %c%c%c%c %c%c%c%c-%c%c%c%c %c%c%c%c\n",
2408 		   escape_essid(info_element->data, info_element->len),
2409 		   beacon->header.addr3,
2410 		   (le16_to_cpu(beacon->capability) & (1 << 0xf)) ? '1' : '0',
2411 		   (le16_to_cpu(beacon->capability) & (1 << 0xe)) ? '1' : '0',
2412 		   (le16_to_cpu(beacon->capability) & (1 << 0xd)) ? '1' : '0',
2413 		   (le16_to_cpu(beacon->capability) & (1 << 0xc)) ? '1' : '0',
2414 		   (le16_to_cpu(beacon->capability) & (1 << 0xb)) ? '1' : '0',
2415 		   (le16_to_cpu(beacon->capability) & (1 << 0xa)) ? '1' : '0',
2416 		   (le16_to_cpu(beacon->capability) & (1 << 0x9)) ? '1' : '0',
2417 		   (le16_to_cpu(beacon->capability) & (1 << 0x8)) ? '1' : '0',
2418 		   (le16_to_cpu(beacon->capability) & (1 << 0x7)) ? '1' : '0',
2419 		   (le16_to_cpu(beacon->capability) & (1 << 0x6)) ? '1' : '0',
2420 		   (le16_to_cpu(beacon->capability) & (1 << 0x5)) ? '1' : '0',
2421 		   (le16_to_cpu(beacon->capability) & (1 << 0x4)) ? '1' : '0',
2422 		   (le16_to_cpu(beacon->capability) & (1 << 0x3)) ? '1' : '0',
2423 		   (le16_to_cpu(beacon->capability) & (1 << 0x2)) ? '1' : '0',
2424 		   (le16_to_cpu(beacon->capability) & (1 << 0x1)) ? '1' : '0',
2425 		   (le16_to_cpu(beacon->capability) & (1 << 0x0)) ? '1' : '0');
2426 
2427 	if (rtllib_network_init(ieee, beacon, network, stats)) {
2428 		netdev_dbg(ieee->dev, "Dropped '%s' ( %pM) via %s.\n",
2429 			   escape_essid(info_element->data, info_element->len),
2430 			   beacon->header.addr3,
2431 			   ieee80211_is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2432 		goto free_network;
2433 	}
2434 
2435 	if (!rtllib_legal_channel(ieee, network->channel))
2436 		goto free_network;
2437 
2438 	if (ieee80211_is_probe_resp(frame_ctl)) {
2439 		if (IsPassiveChannel(ieee, network->channel)) {
2440 			netdev_info(ieee->dev,
2441 				    "GetScanInfo(): For Global Domain, filter probe response at channel(%d).\n",
2442 				    network->channel);
2443 			goto free_network;
2444 		}
2445 	}
2446 
2447 	/* The network parsed correctly -- so now we scan our known networks
2448 	 * to see if we can find it in our list.
2449 	 *
2450 	 * NOTE:  This search is definitely not optimized.  Once its doing
2451 	 *	the "right thing" we'll optimize it for efficiency if
2452 	 *	necessary
2453 	 */
2454 
2455 	/* Search for this entry in the list and update it if it is
2456 	 * already there.
2457 	 */
2458 
2459 	spin_lock_irqsave(&ieee->lock, flags);
2460 	if (is_same_network(&ieee->current_network, network,
2461 	   (network->ssid_len ? 1 : 0))) {
2462 		update_network(ieee, &ieee->current_network, network);
2463 		if ((ieee->current_network.mode == WIRELESS_MODE_N_24G ||
2464 		     ieee->current_network.mode == WIRELESS_MODE_G) &&
2465 		    ieee->current_network.berp_info_valid) {
2466 			if (ieee->current_network.erp_value & ERP_UseProtection)
2467 				ieee->current_network.buseprotection = true;
2468 			else
2469 				ieee->current_network.buseprotection = false;
2470 		}
2471 		if (ieee80211_is_beacon(frame_ctl)) {
2472 			if (ieee->link_state >= MAC80211_LINKED)
2473 				ieee->link_detect_info.NumRecvBcnInPeriod++;
2474 		}
2475 	}
2476 	list_for_each_entry(target, &ieee->network_list, list) {
2477 		if (is_same_network(target, network,
2478 		   (target->ssid_len ? 1 : 0)))
2479 			break;
2480 		if (!oldest || (target->last_scanned < oldest->last_scanned))
2481 			oldest = target;
2482 	}
2483 
2484 	/* If we didn't find a match, then get a new network slot to initialize
2485 	 * with this beacon's information
2486 	 */
2487 	if (&target->list == &ieee->network_list) {
2488 		if (list_empty(&ieee->network_free_list)) {
2489 			/* If there are no more slots, expire the oldest */
2490 			list_del(&oldest->list);
2491 			target = oldest;
2492 			netdev_dbg(ieee->dev,
2493 				   "Expired '%s' ( %pM) from network list.\n",
2494 				   escape_essid(target->ssid, target->ssid_len),
2495 				   target->bssid);
2496 		} else {
2497 			/* Otherwise just pull from the free list */
2498 			target = list_entry(ieee->network_free_list.next,
2499 					    struct rtllib_network, list);
2500 			list_del(ieee->network_free_list.next);
2501 		}
2502 
2503 		netdev_dbg(ieee->dev, "Adding '%s' ( %pM) via %s.\n",
2504 			   escape_essid(network->ssid, network->ssid_len),
2505 			   network->bssid,
2506 			   ieee80211_is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2507 
2508 		memcpy(target, network, sizeof(*target));
2509 		list_add_tail(&target->list, &ieee->network_list);
2510 		if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE)
2511 			rtllib_softmac_new_net(ieee, network);
2512 	} else {
2513 		netdev_dbg(ieee->dev, "Updating '%s' ( %pM) via %s.\n",
2514 			   escape_essid(target->ssid, target->ssid_len),
2515 			   target->bssid,
2516 			   ieee80211_is_beacon(frame_ctl) ? "BEACON" : "PROBE RESPONSE");
2517 
2518 		/* we have an entry and we are going to update it. But this
2519 		 *  entry may be already expired. In this case we do the same
2520 		 * as we found a new net and call the new_net handler
2521 		 */
2522 		renew = !time_after(target->last_scanned + ieee->scan_age,
2523 				    jiffies);
2524 		if ((!target->ssid_len) &&
2525 		    (((network->ssid_len > 0) && (target->hidden_ssid_len == 0))
2526 		    || ((ieee->current_network.ssid_len == network->ssid_len) &&
2527 		    (strncmp(ieee->current_network.ssid, network->ssid,
2528 		    network->ssid_len) == 0) &&
2529 		    (ieee->link_state == MAC80211_NOLINK))))
2530 			renew = 1;
2531 		update_network(ieee, target, network);
2532 		if (renew && (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE))
2533 			rtllib_softmac_new_net(ieee, network);
2534 	}
2535 
2536 	spin_unlock_irqrestore(&ieee->lock, flags);
2537 	if (ieee80211_is_beacon(frame_ctl) &&
2538 	    is_same_network(&ieee->current_network, network,
2539 	    (network->ssid_len ? 1 : 0)) &&
2540 	    (ieee->link_state == MAC80211_LINKED)) {
2541 		ieee->handle_beacon(ieee->dev, beacon, &ieee->current_network);
2542 	}
2543 free_network:
2544 	kfree(network);
2545 }
2546 
2547 static void rtllib_rx_mgt(struct rtllib_device *ieee,
2548 			  struct sk_buff *skb,
2549 			  struct rtllib_rx_stats *stats)
2550 {
2551 	struct ieee80211_hdr *header = (struct ieee80211_hdr *)skb->data;
2552 
2553 	if (!ieee80211_is_probe_resp(header->frame_control) &&
2554 	    (!ieee80211_is_beacon(header->frame_control)))
2555 		ieee->last_rx_ps_time = jiffies;
2556 
2557 	if (ieee80211_is_beacon(header->frame_control)) {
2558 		netdev_dbg(ieee->dev, "received BEACON\n");
2559 		rtllib_process_probe_response(
2560 				ieee, (struct rtllib_probe_response *)header,
2561 				stats);
2562 
2563 		if (ieee->sta_sleep || (ieee->ps != RTLLIB_PS_DISABLED &&
2564 		    ieee->iw_mode == IW_MODE_INFRA &&
2565 		    ieee->link_state == MAC80211_LINKED))
2566 			schedule_work(&ieee->ps_task);
2567 	} else if (ieee80211_is_probe_resp(header->frame_control)) {
2568 		netdev_dbg(ieee->dev, "received PROBE RESPONSE\n");
2569 		rtllib_process_probe_response(ieee,
2570 			      (struct rtllib_probe_response *)header, stats);
2571 	}
2572 }
2573