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