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