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