1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define _IEEE80211_C
8 
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <linux/of.h>
12 #include <asm/unaligned.h>
13 
14 u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
15 u16 RTW_WPA_VERSION = 1;
16 u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
17 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
18 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
19 u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
20 u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
21 u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
22 u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
23 u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
24 u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
25 
26 u16 RSN_VERSION_BSD = 1;
27 u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
28 u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
29 u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
30 u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
31 u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
32 u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
33 u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
34 u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
35 /*  */
36 /*  for adhoc-master to generate ie and provide supported-rate to fw */
37 /*  */
38 
39 static u8 WIFI_CCKRATES[] = {
40 		(IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
41 		(IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
42 		(IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
43 		(IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
44 };
45 
46 static u8 WIFI_OFDMRATES[] = {
47 		(IEEE80211_OFDM_RATE_6MB),
48 		(IEEE80211_OFDM_RATE_9MB),
49 		(IEEE80211_OFDM_RATE_12MB),
50 		(IEEE80211_OFDM_RATE_18MB),
51 		(IEEE80211_OFDM_RATE_24MB),
52 		IEEE80211_OFDM_RATE_36MB,
53 		IEEE80211_OFDM_RATE_48MB,
54 		IEEE80211_OFDM_RATE_54MB
55 };
56 
rtw_get_bit_value_from_ieee_value(u8 val)57 int rtw_get_bit_value_from_ieee_value(u8 val)
58 {
59 	unsigned char dot11_rate_table[] = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0}; /*  last element must be zero!! */
60 	int i = 0;
61 
62 	while (dot11_rate_table[i] != 0) {
63 		if (dot11_rate_table[i] == val)
64 			return BIT(i);
65 		i++;
66 	}
67 	return 0;
68 }
69 
rtw_is_cckrates_included(u8 * rate)70 bool rtw_is_cckrates_included(u8 *rate)
71 {
72 	while (*rate) {
73 		u8 r = *rate & 0x7f;
74 
75 		if (r == 2 || r == 4 || r == 11 || r == 22)
76 			return true;
77 		rate++;
78 	}
79 
80 	return false;
81 }
82 
rtw_is_cckratesonly_included(u8 * rate)83 bool rtw_is_cckratesonly_included(u8 *rate)
84 {
85 	while (*rate) {
86 		u8 r = *rate & 0x7f;
87 
88 		if (r != 2 && r != 4 && r != 11 && r != 22)
89 			return false;
90 		rate++;
91 	}
92 
93 	return true;
94 }
95 
rtw_check_network_type(unsigned char * rate,int ratelen,int channel)96 int rtw_check_network_type(unsigned char *rate, int ratelen, int channel)
97 {
98 	if (channel > 14) {
99 		if (rtw_is_cckrates_included(rate))
100 			return WIRELESS_INVALID;
101 		else
102 			return WIRELESS_11A;
103 	} else { /*  could be pure B, pure G, or B/G */
104 		if (rtw_is_cckratesonly_included(rate))
105 			return WIRELESS_11B;
106 		else if (rtw_is_cckrates_included(rate))
107 			return	WIRELESS_11BG;
108 		else
109 			return WIRELESS_11G;
110 	}
111 }
112 
rtw_set_fixed_ie(unsigned char * pbuf,unsigned int len,unsigned char * source,unsigned int * frlen)113 u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source,
114 				unsigned int *frlen)
115 {
116 	memcpy((void *)pbuf, (void *)source, len);
117 	*frlen = *frlen + len;
118 	return pbuf + len;
119 }
120 
121 /*  rtw_set_ie will update frame length */
rtw_set_ie(u8 * pbuf,sint index,uint len,u8 * source,uint * frlen)122 u8 *rtw_set_ie
123 (
124 	u8 *pbuf,
125 	sint index,
126 	uint len,
127 	u8 *source,
128 	uint *frlen /* frame length */
129 )
130 {
131 	*pbuf = (u8)index;
132 
133 	*(pbuf + 1) = (u8)len;
134 
135 	if (len > 0)
136 		memcpy((void *)(pbuf + 2), (void *)source, len);
137 
138 	*frlen = *frlen + (len + 2);
139 
140 	return pbuf + len + 2;
141 }
142 
143 /*----------------------------------------------------------------------------
144 index: the information element id index, limit is the limit for search
145 -----------------------------------------------------------------------------*/
rtw_get_ie(u8 * pbuf,sint index,sint * len,sint limit)146 u8 *rtw_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
147 {
148 	sint tmp, i;
149 	u8 *p;
150 
151 	if (limit < 1)
152 		return NULL;
153 
154 	p = pbuf;
155 	i = 0;
156 	*len = 0;
157 	while (1) {
158 		if (*p == index) {
159 			*len = *(p + 1);
160 			return p;
161 		} else {
162 			tmp = *(p + 1);
163 			p += (tmp + 2);
164 			i += (tmp + 2);
165 		}
166 		if (i >= limit)
167 			break;
168 	}
169 	return NULL;
170 }
171 
172 /**
173  * rtw_get_ie_ex - Search specific IE from a series of IEs
174  * @in_ie: Address of IEs to search
175  * @in_len: Length limit from in_ie
176  * @eid: Element ID to match
177  * @oui: OUI to match
178  * @oui_len: OUI length
179  * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE
180  * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE
181  *
182  * Returns: The address of the specific IE found, or NULL
183  */
rtw_get_ie_ex(u8 * in_ie,uint in_len,u8 eid,u8 * oui,u8 oui_len,u8 * ie,uint * ielen)184 u8 *rtw_get_ie_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len, u8 *ie, uint *ielen)
185 {
186 	uint cnt;
187 	u8 *target_ie = NULL;
188 
189 	if (ielen)
190 		*ielen = 0;
191 
192 	if (!in_ie || in_len <= 0)
193 		return target_ie;
194 
195 	cnt = 0;
196 
197 	while (cnt < in_len) {
198 		if (eid == in_ie[cnt]
199 			&& (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) {
200 			target_ie = &in_ie[cnt];
201 
202 			if (ie)
203 				memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2);
204 
205 			if (ielen)
206 				*ielen = in_ie[cnt+1]+2;
207 
208 			break;
209 		} else {
210 			cnt += in_ie[cnt+1]+2; /* goto next */
211 		}
212 	}
213 
214 	return target_ie;
215 }
216 
217 /**
218  * rtw_ies_remove_ie - Find matching IEs and remove
219  * @ies: Address of IEs to search
220  * @ies_len: Pointer of length of ies, will update to new length
221  * @offset: The offset to start search
222  * @eid: Element ID to match
223  * @oui: OUI to match
224  * @oui_len: OUI length
225  *
226  * Returns: _SUCCESS: ies is updated, _FAIL: not updated
227  */
rtw_ies_remove_ie(u8 * ies,uint * ies_len,uint offset,u8 eid,u8 * oui,u8 oui_len)228 int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len)
229 {
230 	int ret = _FAIL;
231 	u8 *target_ie;
232 	u32 target_ielen;
233 	u8 *start;
234 	uint search_len;
235 
236 	if (!ies || !ies_len || *ies_len <= offset)
237 		goto exit;
238 
239 	start = ies + offset;
240 	search_len = *ies_len - offset;
241 
242 	while (1) {
243 		target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen);
244 		if (target_ie && target_ielen) {
245 			u8 buf[MAX_IE_SZ] = {0};
246 			u8 *remain_ies = target_ie + target_ielen;
247 			uint remain_len = search_len - (remain_ies - start);
248 
249 			memcpy(buf, remain_ies, remain_len);
250 			memcpy(target_ie, buf, remain_len);
251 			*ies_len = *ies_len - target_ielen;
252 			ret = _SUCCESS;
253 
254 			start = target_ie;
255 			search_len = remain_len;
256 		} else {
257 			break;
258 		}
259 	}
260 exit:
261 	return ret;
262 }
263 
rtw_set_supported_rate(u8 * SupportedRates,uint mode)264 void rtw_set_supported_rate(u8 *SupportedRates, uint mode)
265 {
266 	memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
267 
268 	switch (mode) {
269 	case WIRELESS_11B:
270 		memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
271 		break;
272 
273 	case WIRELESS_11G:
274 	case WIRELESS_11A:
275 	case WIRELESS_11_5N:
276 	case WIRELESS_11A_5N:/* Todo: no basic rate for ofdm ? */
277 	case WIRELESS_11_5AC:
278 		memcpy(SupportedRates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
279 		break;
280 
281 	case WIRELESS_11BG:
282 	case WIRELESS_11G_24N:
283 	case WIRELESS_11_24N:
284 	case WIRELESS_11BG_24N:
285 		memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
286 		memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
287 		break;
288 	}
289 }
290 
rtw_get_rateset_len(u8 * rateset)291 uint rtw_get_rateset_len(u8 *rateset)
292 {
293 	uint i;
294 
295 	for (i = 0; i < 13; i++)
296 		if (rateset[i] == 0)
297 			break;
298 	return i;
299 }
300 
rtw_generate_ie(struct registry_priv * pregistrypriv)301 int rtw_generate_ie(struct registry_priv *pregistrypriv)
302 {
303 	u8 wireless_mode;
304 	int	sz = 0, rateLen;
305 	struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
306 	u8 *ie = pdev_network->IEs;
307 
308 	/* timestamp will be inserted by hardware */
309 	sz += 8;
310 	ie += sz;
311 
312 	/* beacon interval : 2bytes */
313 	*(__le16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);/* BCN_INTERVAL; */
314 	sz += 2;
315 	ie += 2;
316 
317 	/* capability info */
318 	*(u16 *)ie = 0;
319 
320 	*(__le16 *)ie |= cpu_to_le16(cap_IBSS);
321 
322 	if (pregistrypriv->preamble == PREAMBLE_SHORT)
323 		*(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
324 
325 	if (pdev_network->Privacy)
326 		*(__le16 *)ie |= cpu_to_le16(cap_Privacy);
327 
328 	sz += 2;
329 	ie += 2;
330 
331 	/* SSID */
332 	ie = rtw_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength, pdev_network->Ssid.Ssid, &sz);
333 
334 	/* supported rates */
335 	if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
336 		if (pdev_network->Configuration.DSConfig > 14)
337 			wireless_mode = WIRELESS_11A_5N;
338 		else
339 			wireless_mode = WIRELESS_11BG_24N;
340 	} else {
341 		wireless_mode = pregistrypriv->wireless_mode;
342 	}
343 
344 	rtw_set_supported_rate(pdev_network->SupportedRates, wireless_mode);
345 
346 	rateLen = rtw_get_rateset_len(pdev_network->SupportedRates);
347 
348 	if (rateLen > 8) {
349 		ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, pdev_network->SupportedRates, &sz);
350 		/* ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
351 	} else {
352 		ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, pdev_network->SupportedRates, &sz);
353 	}
354 
355 	/* DS parameter set */
356 	ie = rtw_set_ie(ie, _DSSET_IE_, 1, (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
357 
358 	/* IBSS Parameter Set */
359 
360 	ie = rtw_set_ie(ie, _IBSS_PARA_IE_, 2, (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
361 
362 	if (rateLen > 8) {
363 		ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz);
364 	}
365 
366 	/* HT Cap. */
367 	if (((pregistrypriv->wireless_mode&WIRELESS_11_5N) || (pregistrypriv->wireless_mode&WIRELESS_11_24N))
368 		&& (pregistrypriv->ht_enable == true)) {
369 		/* todo: */
370 	}
371 
372 	/* pdev_network->IELength =  sz; update IELength */
373 
374 	/* return _SUCCESS; */
375 
376 	return sz;
377 }
378 
rtw_get_wpa_ie(unsigned char * pie,int * wpa_ie_len,int limit)379 unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
380 {
381 	int len;
382 	u16 val16;
383 	unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
384 	u8 *pbuf = pie;
385 	int limit_new = limit;
386 	__le16 le_tmp;
387 
388 	while (1) {
389 		pbuf = rtw_get_ie(pbuf, _WPA_IE_ID_, &len, limit_new);
390 
391 		if (pbuf) {
392 			/* check if oui matches... */
393 			if (memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type))) {
394 				goto check_next_ie;
395 			}
396 
397 			/* check version... */
398 			memcpy((u8 *)&le_tmp, (pbuf + 6), sizeof(val16));
399 
400 			val16 = le16_to_cpu(le_tmp);
401 			if (val16 != 0x0001)
402 				goto check_next_ie;
403 
404 			*wpa_ie_len = *(pbuf + 1);
405 
406 			return pbuf;
407 
408 		} else {
409 			*wpa_ie_len = 0;
410 			return NULL;
411 		}
412 
413 check_next_ie:
414 
415 		limit_new = limit - (pbuf - pie) - 2 - len;
416 
417 		if (limit_new <= 0)
418 			break;
419 
420 		pbuf += (2 + len);
421 	}
422 
423 	*wpa_ie_len = 0;
424 
425 	return NULL;
426 }
427 
rtw_get_wpa2_ie(unsigned char * pie,int * rsn_ie_len,int limit)428 unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
429 {
430 	return rtw_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
431 }
432 
rtw_get_wpa_cipher_suite(u8 * s)433 int rtw_get_wpa_cipher_suite(u8 *s)
434 {
435 	if (!memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
436 		return WPA_CIPHER_NONE;
437 	if (!memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
438 		return WPA_CIPHER_WEP40;
439 	if (!memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
440 		return WPA_CIPHER_TKIP;
441 	if (!memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
442 		return WPA_CIPHER_CCMP;
443 	if (!memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
444 		return WPA_CIPHER_WEP104;
445 
446 	return 0;
447 }
448 
rtw_get_wpa2_cipher_suite(u8 * s)449 int rtw_get_wpa2_cipher_suite(u8 *s)
450 {
451 	if (!memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
452 		return WPA_CIPHER_NONE;
453 	if (!memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
454 		return WPA_CIPHER_WEP40;
455 	if (!memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
456 		return WPA_CIPHER_TKIP;
457 	if (!memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
458 		return WPA_CIPHER_CCMP;
459 	if (!memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
460 		return WPA_CIPHER_WEP104;
461 
462 	return 0;
463 }
464 
rtw_parse_wpa_ie(u8 * wpa_ie,int wpa_ie_len,int * group_cipher,int * pairwise_cipher,int * is_8021x)465 int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
466 {
467 	int i, ret = _SUCCESS;
468 	int left, count;
469 	u8 *pos;
470 	u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1};
471 
472 	if (wpa_ie_len <= 0) {
473 		/* No WPA IE - fail silently */
474 		return _FAIL;
475 	}
476 
477 	if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie+1) != (u8)(wpa_ie_len - 2)) ||
478 	   (memcmp(wpa_ie+2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN))) {
479 		return _FAIL;
480 	}
481 
482 	pos = wpa_ie;
483 
484 	pos += 8;
485 	left = wpa_ie_len - 8;
486 
487 	/* group_cipher */
488 	if (left >= WPA_SELECTOR_LEN) {
489 		*group_cipher = rtw_get_wpa_cipher_suite(pos);
490 
491 		pos += WPA_SELECTOR_LEN;
492 		left -= WPA_SELECTOR_LEN;
493 
494 	} else if (left > 0) {
495 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
496 
497 		return _FAIL;
498 	}
499 
500 	/* pairwise_cipher */
501 	if (left >= 2) {
502 		/* count = le16_to_cpu(*(u16*)pos); */
503 		count = get_unaligned_le16(pos);
504 		pos += 2;
505 		left -= 2;
506 
507 		if (count == 0 || left < count * WPA_SELECTOR_LEN) {
508 			RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
509 						"count %u left %u", __func__, count, left));
510 			return _FAIL;
511 		}
512 
513 		for (i = 0; i < count; i++) {
514 			*pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
515 
516 			pos += WPA_SELECTOR_LEN;
517 			left -= WPA_SELECTOR_LEN;
518 		}
519 
520 	} else if (left == 1) {
521 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)",   __func__));
522 		return _FAIL;
523 	}
524 
525 	if (is_8021x) {
526 		if (left >= 6) {
527 			pos += 2;
528 			if (!memcmp(pos, SUITE_1X, 4)) {
529 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s : there has 802.1x auth\n", __func__));
530 				*is_8021x = 1;
531 			}
532 		}
533 	}
534 
535 	return ret;
536 }
537 
rtw_parse_wpa2_ie(u8 * rsn_ie,int rsn_ie_len,int * group_cipher,int * pairwise_cipher,int * is_8021x)538 int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
539 {
540 	int i, ret = _SUCCESS;
541 	int left, count;
542 	u8 *pos;
543 	u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
544 
545 	if (rsn_ie_len <= 0) {
546 		/* No RSN IE - fail silently */
547 		return _FAIL;
548 	}
549 
550 	if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2))) {
551 		return _FAIL;
552 	}
553 
554 	pos = rsn_ie;
555 	pos += 4;
556 	left = rsn_ie_len - 4;
557 
558 	/* group_cipher */
559 	if (left >= RSN_SELECTOR_LEN) {
560 		*group_cipher = rtw_get_wpa2_cipher_suite(pos);
561 
562 		pos += RSN_SELECTOR_LEN;
563 		left -= RSN_SELECTOR_LEN;
564 
565 	} else if (left > 0) {
566 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
567 		return _FAIL;
568 	}
569 
570 	/* pairwise_cipher */
571 	if (left >= 2) {
572 	  /* count = le16_to_cpu(*(u16*)pos); */
573 		count = get_unaligned_le16(pos);
574 		pos += 2;
575 		left -= 2;
576 
577 		if (count == 0 || left < count * RSN_SELECTOR_LEN) {
578 			RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
579 						 "count %u left %u", __func__, count, left));
580 			return _FAIL;
581 		}
582 
583 		for (i = 0; i < count; i++) {
584 			*pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos);
585 
586 			pos += RSN_SELECTOR_LEN;
587 			left -= RSN_SELECTOR_LEN;
588 		}
589 
590 	} else if (left == 1) {
591 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)",  __func__));
592 
593 		return _FAIL;
594 	}
595 
596 	if (is_8021x) {
597 		if (left >= 6) {
598 			pos += 2;
599 			if (!memcmp(pos, SUITE_1X, 4)) {
600 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s (): there has 802.1x auth\n", __func__));
601 				*is_8021x = 1;
602 			}
603 		}
604 	}
605 
606 	return ret;
607 }
608 
609 /* ifdef CONFIG_WAPI_SUPPORT */
rtw_get_wapi_ie(u8 * in_ie,uint in_len,u8 * wapi_ie,u16 * wapi_len)610 int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len)
611 {
612 	int len = 0;
613 	u8 authmode, i;
614 	uint	cnt;
615 	u8 wapi_oui1[4] = {0x0, 0x14, 0x72, 0x01};
616 	u8 wapi_oui2[4] = {0x0, 0x14, 0x72, 0x02};
617 
618 	if (wapi_len)
619 		*wapi_len = 0;
620 
621 	if (!in_ie || in_len <= 0)
622 		return len;
623 
624 	cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
625 
626 	while (cnt < in_len) {
627 		authmode = in_ie[cnt];
628 
629 		/* if (authmode == _WAPI_IE_) */
630 		if (authmode == _WAPI_IE_ && (!memcmp(&in_ie[cnt+6], wapi_oui1, 4) ||
631 					!memcmp(&in_ie[cnt+6], wapi_oui2, 4))) {
632 			if (wapi_ie) {
633 				memcpy(wapi_ie, &in_ie[cnt], in_ie[cnt+1]+2);
634 
635 				for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
636 					RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
637 								wapi_ie[i], wapi_ie[i+1], wapi_ie[i+2], wapi_ie[i+3], wapi_ie[i+4],
638 								wapi_ie[i+5], wapi_ie[i+6], wapi_ie[i+7]));
639 				}
640 			}
641 
642 			if (wapi_len)
643 				*wapi_len = in_ie[cnt+1]+2;
644 
645 			cnt += in_ie[cnt+1]+2;  /* get next */
646 		} else {
647 			cnt += in_ie[cnt+1]+2;   /* get next */
648 		}
649 	}
650 
651 	if (wapi_len)
652 		len = *wapi_len;
653 
654 	return len;
655 }
656 /* endif */
657 
rtw_get_sec_ie(u8 * in_ie,uint in_len,u8 * rsn_ie,u16 * rsn_len,u8 * wpa_ie,u16 * wpa_len)658 void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len)
659 {
660 	u8 authmode, sec_idx, i;
661 	u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
662 	uint	cnt;
663 
664 	/* Search required WPA or WPA2 IE and copy to sec_ie[ ] */
665 
666 	cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
667 
668 	sec_idx = 0;
669 
670 	while (cnt < in_len) {
671 		authmode = in_ie[cnt];
672 
673 		if ((authmode == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], &wpa_oui[0], 4))) {
674 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n rtw_get_wpa_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n", sec_idx, in_ie[cnt+1]+2));
675 
676 				if (wpa_ie) {
677 				memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt+1]+2);
678 
679 				for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
680 						RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
681 									wpa_ie[i], wpa_ie[i+1], wpa_ie[i+2], wpa_ie[i+3], wpa_ie[i+4],
682 									wpa_ie[i+5], wpa_ie[i+6], wpa_ie[i+7]));
683 					}
684 				}
685 
686 				*wpa_len = in_ie[cnt+1]+2;
687 				cnt += in_ie[cnt+1]+2;  /* get next */
688 		} else {
689 			if (authmode == _WPA2_IE_ID_) {
690 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n get_rsn_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n", sec_idx, in_ie[cnt+1]+2));
691 
692 				if (rsn_ie) {
693 				memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt+1]+2);
694 
695 				for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
696 						RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
697 									rsn_ie[i], rsn_ie[i+1], rsn_ie[i+2], rsn_ie[i+3], rsn_ie[i+4],
698 									rsn_ie[i+5], rsn_ie[i+6], rsn_ie[i+7]));
699 					}
700 				}
701 
702 				*rsn_len = in_ie[cnt+1]+2;
703 				cnt += in_ie[cnt+1]+2;  /* get next */
704 			} else {
705 				cnt += in_ie[cnt+1]+2;   /* get next */
706 			}
707 		}
708 	}
709 }
710 
rtw_is_wps_ie(u8 * ie_ptr,uint * wps_ielen)711 u8 rtw_is_wps_ie(u8 *ie_ptr, uint *wps_ielen)
712 {
713 	u8 match = false;
714 	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
715 
716 	if (!ie_ptr)
717 		return match;
718 
719 	eid = ie_ptr[0];
720 
721 	if ((eid == _WPA_IE_ID_) && (!memcmp(&ie_ptr[2], wps_oui, 4))) {
722 		/* DBG_8192C("==> found WPS_IE.....\n"); */
723 		*wps_ielen = ie_ptr[1]+2;
724 		match = true;
725 	}
726 	return match;
727 }
728 
729 /**
730  * rtw_get_wps_ie - Search WPS IE from a series of IEs
731  * @in_ie: Address of IEs to search
732  * @in_len: Length limit from in_ie
733  * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie
734  * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE
735  *
736  * Returns: The address of the WPS IE found, or NULL
737  */
rtw_get_wps_ie(u8 * in_ie,uint in_len,u8 * wps_ie,uint * wps_ielen)738 u8 *rtw_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
739 {
740 	uint cnt;
741 	u8 *wpsie_ptr = NULL;
742 	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
743 
744 	if (wps_ielen)
745 		*wps_ielen = 0;
746 
747 	if (!in_ie || in_len <= 0)
748 		return wpsie_ptr;
749 
750 	cnt = 0;
751 
752 	while (cnt < in_len) {
753 		eid = in_ie[cnt];
754 
755 		if ((eid == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
756 			wpsie_ptr = &in_ie[cnt];
757 
758 			if (wps_ie)
759 				memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
760 
761 			if (wps_ielen)
762 				*wps_ielen = in_ie[cnt+1]+2;
763 
764 			cnt += in_ie[cnt+1]+2;
765 
766 			break;
767 		} else {
768 			cnt += in_ie[cnt+1]+2; /* goto next */
769 		}
770 	}
771 
772 	return wpsie_ptr;
773 }
774 
775 /**
776  * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE
777  * @wps_ie: Address of WPS IE to search
778  * @wps_ielen: Length limit from wps_ie
779  * @target_attr_id: The attribute ID of WPS attribute to search
780  * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr
781  * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute
782  *
783  * Returns: the address of the specific WPS attribute found, or NULL
784  */
rtw_get_wps_attr(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_attr,u32 * len_attr)785 u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_attr, u32 *len_attr)
786 {
787 	u8 *attr_ptr = NULL;
788 	u8 *target_attr_ptr = NULL;
789 	u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
790 
791 	if (len_attr)
792 		*len_attr = 0;
793 
794 	if ((wps_ie[0] != _VENDOR_SPECIFIC_IE_) ||
795 		(memcmp(wps_ie + 2, wps_oui, 4))) {
796 		return attr_ptr;
797 	}
798 
799 	/*  6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
800 	attr_ptr = wps_ie + 6; /* goto first attr */
801 
802 	while (attr_ptr - wps_ie < wps_ielen) {
803 		/*  4 = 2(Attribute ID) + 2(Length) */
804 		u16 attr_id = get_unaligned_be16(attr_ptr);
805 		u16 attr_data_len = get_unaligned_be16(attr_ptr + 2);
806 		u16 attr_len = attr_data_len + 4;
807 
808 		/* DBG_871X("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len); */
809 		if (attr_id == target_attr_id) {
810 			target_attr_ptr = attr_ptr;
811 
812 			if (buf_attr)
813 				memcpy(buf_attr, attr_ptr, attr_len);
814 
815 			if (len_attr)
816 				*len_attr = attr_len;
817 
818 			break;
819 		} else {
820 			attr_ptr += attr_len; /* goto next */
821 		}
822 	}
823 
824 	return target_attr_ptr;
825 }
826 
827 /**
828  * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE
829  * @wps_ie: Address of WPS IE to search
830  * @wps_ielen: Length limit from wps_ie
831  * @target_attr_id: The attribute ID of WPS attribute to search
832  * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content
833  * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content
834  *
835  * Returns: the address of the specific WPS attribute content found, or NULL
836  */
rtw_get_wps_attr_content(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_content,uint * len_content)837 u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_content, uint *len_content)
838 {
839 	u8 *attr_ptr;
840 	u32 attr_len;
841 
842 	if (len_content)
843 		*len_content = 0;
844 
845 	attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len);
846 
847 	if (attr_ptr && attr_len) {
848 		if (buf_content)
849 			memcpy(buf_content, attr_ptr+4, attr_len-4);
850 
851 		if (len_content)
852 			*len_content = attr_len-4;
853 
854 		return attr_ptr+4;
855 	}
856 
857 	return NULL;
858 }
859 
rtw_ieee802_11_parse_vendor_specific(u8 * pos,uint elen,struct rtw_ieee802_11_elems * elems,int show_errors)860 static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen,
861 					    struct rtw_ieee802_11_elems *elems,
862 					    int show_errors)
863 {
864 	unsigned int oui;
865 
866 	/* first 3 bytes in vendor specific information element are the IEEE
867 	 * OUI of the vendor. The following byte is used a vendor specific
868 	 * sub-type. */
869 	if (elen < 4) {
870 		if (show_errors) {
871 			DBG_871X("short vendor specific "
872 				   "information element ignored (len =%lu)\n",
873 				   (unsigned long) elen);
874 		}
875 		return -1;
876 	}
877 
878 	oui = get_unaligned_be24(pos);
879 	switch (oui) {
880 	case OUI_MICROSOFT:
881 		/* Microsoft/Wi-Fi information elements are further typed and
882 		 * subtyped */
883 		switch (pos[3]) {
884 		case 1:
885 			/* Microsoft OUI (00:50:F2) with OUI Type 1:
886 			 * real WPA information element */
887 			elems->wpa_ie = pos;
888 			elems->wpa_ie_len = elen;
889 			break;
890 		case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
891 			if (elen < 5) {
892 				DBG_871X("short WME "
893 					   "information element ignored "
894 					   "(len =%lu)\n",
895 					   (unsigned long) elen);
896 				return -1;
897 			}
898 			switch (pos[4]) {
899 			case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
900 			case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
901 				elems->wme = pos;
902 				elems->wme_len = elen;
903 				break;
904 			case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
905 				elems->wme_tspec = pos;
906 				elems->wme_tspec_len = elen;
907 				break;
908 			default:
909 				DBG_871X("unknown WME "
910 					   "information element ignored "
911 					   "(subtype =%d len =%lu)\n",
912 					   pos[4], (unsigned long) elen);
913 				return -1;
914 			}
915 			break;
916 		case 4:
917 			/* Wi-Fi Protected Setup (WPS) IE */
918 			elems->wps_ie = pos;
919 			elems->wps_ie_len = elen;
920 			break;
921 		default:
922 			DBG_871X("Unknown Microsoft "
923 				   "information element ignored "
924 				   "(type =%d len =%lu)\n",
925 				   pos[3], (unsigned long) elen);
926 			return -1;
927 		}
928 		break;
929 
930 	case OUI_BROADCOM:
931 		switch (pos[3]) {
932 		case VENDOR_HT_CAPAB_OUI_TYPE:
933 			elems->vendor_ht_cap = pos;
934 			elems->vendor_ht_cap_len = elen;
935 			break;
936 		default:
937 			DBG_871X("Unknown Broadcom "
938 				   "information element ignored "
939 				   "(type =%d len =%lu)\n",
940 				   pos[3], (unsigned long) elen);
941 			return -1;
942 		}
943 		break;
944 
945 	default:
946 		DBG_871X("unknown vendor specific information "
947 			   "element ignored (vendor OUI %02x:%02x:%02x "
948 			   "len =%lu)\n",
949 			   pos[0], pos[1], pos[2], (unsigned long) elen);
950 		return -1;
951 	}
952 
953 	return 0;
954 }
955 
956 /**
957  * ieee802_11_parse_elems - Parse information elements in management frames
958  * @start: Pointer to the start of IEs
959  * @len: Length of IE buffer in octets
960  * @elems: Data structure for parsed elements
961  * @show_errors: Whether to show parsing errors in debug log
962  * Returns: Parsing result
963  */
rtw_ieee802_11_parse_elems(u8 * start,uint len,struct rtw_ieee802_11_elems * elems,int show_errors)964 ParseRes rtw_ieee802_11_parse_elems(u8 *start, uint len,
965 				struct rtw_ieee802_11_elems *elems,
966 				int show_errors)
967 {
968 	uint left = len;
969 	u8 *pos = start;
970 	int unknown = 0;
971 
972 	memset(elems, 0, sizeof(*elems));
973 
974 	while (left >= 2) {
975 		u8 id, elen;
976 
977 		id = *pos++;
978 		elen = *pos++;
979 		left -= 2;
980 
981 		if (elen > left) {
982 			if (show_errors) {
983 				DBG_871X("IEEE 802.11 element "
984 					   "parse failed (id =%d elen =%d "
985 					   "left =%lu)\n",
986 					   id, elen, (unsigned long) left);
987 			}
988 			return ParseFailed;
989 		}
990 
991 		switch (id) {
992 		case WLAN_EID_SSID:
993 			elems->ssid = pos;
994 			elems->ssid_len = elen;
995 			break;
996 		case WLAN_EID_SUPP_RATES:
997 			elems->supp_rates = pos;
998 			elems->supp_rates_len = elen;
999 			break;
1000 		case WLAN_EID_FH_PARAMS:
1001 			elems->fh_params = pos;
1002 			elems->fh_params_len = elen;
1003 			break;
1004 		case WLAN_EID_DS_PARAMS:
1005 			elems->ds_params = pos;
1006 			elems->ds_params_len = elen;
1007 			break;
1008 		case WLAN_EID_CF_PARAMS:
1009 			elems->cf_params = pos;
1010 			elems->cf_params_len = elen;
1011 			break;
1012 		case WLAN_EID_TIM:
1013 			elems->tim = pos;
1014 			elems->tim_len = elen;
1015 			break;
1016 		case WLAN_EID_IBSS_PARAMS:
1017 			elems->ibss_params = pos;
1018 			elems->ibss_params_len = elen;
1019 			break;
1020 		case WLAN_EID_CHALLENGE:
1021 			elems->challenge = pos;
1022 			elems->challenge_len = elen;
1023 			break;
1024 		case WLAN_EID_ERP_INFO:
1025 			elems->erp_info = pos;
1026 			elems->erp_info_len = elen;
1027 			break;
1028 		case WLAN_EID_EXT_SUPP_RATES:
1029 			elems->ext_supp_rates = pos;
1030 			elems->ext_supp_rates_len = elen;
1031 			break;
1032 		case WLAN_EID_VENDOR_SPECIFIC:
1033 			if (rtw_ieee802_11_parse_vendor_specific(pos, elen,
1034 							     elems,
1035 							     show_errors))
1036 				unknown++;
1037 			break;
1038 		case WLAN_EID_RSN:
1039 			elems->rsn_ie = pos;
1040 			elems->rsn_ie_len = elen;
1041 			break;
1042 		case WLAN_EID_PWR_CAPABILITY:
1043 			elems->power_cap = pos;
1044 			elems->power_cap_len = elen;
1045 			break;
1046 		case WLAN_EID_SUPPORTED_CHANNELS:
1047 			elems->supp_channels = pos;
1048 			elems->supp_channels_len = elen;
1049 			break;
1050 		case WLAN_EID_MOBILITY_DOMAIN:
1051 			elems->mdie = pos;
1052 			elems->mdie_len = elen;
1053 			break;
1054 		case WLAN_EID_FAST_BSS_TRANSITION:
1055 			elems->ftie = pos;
1056 			elems->ftie_len = elen;
1057 			break;
1058 		case WLAN_EID_TIMEOUT_INTERVAL:
1059 			elems->timeout_int = pos;
1060 			elems->timeout_int_len = elen;
1061 			break;
1062 		case WLAN_EID_HT_CAP:
1063 			elems->ht_capabilities = pos;
1064 			elems->ht_capabilities_len = elen;
1065 			break;
1066 		case WLAN_EID_HT_OPERATION:
1067 			elems->ht_operation = pos;
1068 			elems->ht_operation_len = elen;
1069 			break;
1070 		case WLAN_EID_VHT_CAPABILITY:
1071 			elems->vht_capabilities = pos;
1072 			elems->vht_capabilities_len = elen;
1073 			break;
1074 		case WLAN_EID_VHT_OPERATION:
1075 			elems->vht_operation = pos;
1076 			elems->vht_operation_len = elen;
1077 			break;
1078 		case WLAN_EID_VHT_OP_MODE_NOTIFY:
1079 			elems->vht_op_mode_notify = pos;
1080 			elems->vht_op_mode_notify_len = elen;
1081 			break;
1082 		default:
1083 			unknown++;
1084 			if (!show_errors)
1085 				break;
1086 			DBG_871X("IEEE 802.11 element parse "
1087 				   "ignored unknown element (id =%d elen =%d)\n",
1088 				   id, elen);
1089 			break;
1090 		}
1091 
1092 		left -= elen;
1093 		pos += elen;
1094 	}
1095 
1096 	if (left)
1097 		return ParseFailed;
1098 
1099 	return unknown ? ParseUnknown : ParseOK;
1100 }
1101 
rtw_macaddr_cfg(struct device * dev,u8 * mac_addr)1102 void rtw_macaddr_cfg(struct device *dev, u8 *mac_addr)
1103 {
1104 	u8 mac[ETH_ALEN];
1105 	struct device_node *np = dev->of_node;
1106 	const unsigned char *addr;
1107 	int len;
1108 
1109 	if (!mac_addr)
1110 		return;
1111 
1112 	if (rtw_initmac && mac_pton(rtw_initmac, mac)) {
1113 		/* Users specify the mac address */
1114 		ether_addr_copy(mac_addr, mac);
1115 	} else {
1116 		/* Use the mac address stored in the Efuse */
1117 		ether_addr_copy(mac, mac_addr);
1118 	}
1119 
1120 	if (is_broadcast_ether_addr(mac) || is_zero_ether_addr(mac)) {
1121 		if ((addr = of_get_property(np, "local-mac-address", &len)) &&
1122 		    len == ETH_ALEN) {
1123 			ether_addr_copy(mac_addr, addr);
1124 		} else {
1125 			eth_random_addr(mac_addr);
1126 			DBG_871X("MAC Address from efuse error, assign random one !!!\n");
1127 		}
1128 	}
1129 
1130 	DBG_871X("rtw_macaddr_cfg MAC Address  = "MAC_FMT"\n", MAC_ARG(mac_addr));
1131 }
1132 
rtw_get_cipher_info(struct wlan_network * pnetwork)1133 static int rtw_get_cipher_info(struct wlan_network *pnetwork)
1134 {
1135 	u32 wpa_ielen;
1136 	unsigned char *pbuf;
1137 	int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
1138 	int ret = _FAIL;
1139 
1140 	pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1141 
1142 	if (pbuf && (wpa_ielen > 0)) {
1143 		RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_cipher_info: wpa_ielen: %d", wpa_ielen));
1144 		if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1145 			pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1146 			pnetwork->BcnInfo.group_cipher = group_cipher;
1147 			pnetwork->BcnInfo.is_8021x = is8021x;
1148 			RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d, is_8021x is %d",
1149 						__func__, pnetwork->BcnInfo.pairwise_cipher, pnetwork->BcnInfo.is_8021x));
1150 			ret = _SUCCESS;
1151 		}
1152 	} else {
1153 		pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1154 
1155 		if (pbuf && (wpa_ielen > 0)) {
1156 			RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE\n"));
1157 			if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1158 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE  OK!!!\n"));
1159 				pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1160 				pnetwork->BcnInfo.group_cipher = group_cipher;
1161 				pnetwork->BcnInfo.is_8021x = is8021x;
1162 				RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d,"
1163 							"pnetwork->group_cipher is %d, is_8021x is %d",	__func__, pnetwork->BcnInfo.pairwise_cipher,
1164 							pnetwork->BcnInfo.group_cipher, pnetwork->BcnInfo.is_8021x));
1165 				ret = _SUCCESS;
1166 			}
1167 		}
1168 	}
1169 
1170 	return ret;
1171 }
1172 
rtw_get_bcn_info(struct wlan_network * pnetwork)1173 void rtw_get_bcn_info(struct wlan_network *pnetwork)
1174 {
1175 	unsigned short cap = 0;
1176 	u8 bencrypt = 0;
1177 	/* u8 wpa_ie[255], rsn_ie[255]; */
1178 	u16 wpa_len = 0, rsn_len = 0;
1179 	struct HT_info_element *pht_info = NULL;
1180 	struct ieee80211_ht_cap *pht_cap = NULL;
1181 	unsigned int		len;
1182 	unsigned char 	*p;
1183 	__le16 le_cap;
1184 
1185 	memcpy((u8 *)&le_cap, rtw_get_capability_from_ie(pnetwork->network.IEs), 2);
1186 	cap = le16_to_cpu(le_cap);
1187 	if (cap & WLAN_CAPABILITY_PRIVACY) {
1188 		bencrypt = 1;
1189 		pnetwork->network.Privacy = 1;
1190 	} else {
1191 		pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
1192 	}
1193 	rtw_get_sec_ie(pnetwork->network.IEs, pnetwork->network.IELength, NULL, &rsn_len, NULL, &wpa_len);
1194 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1195 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1196 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1197 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1198 
1199 	if (rsn_len > 0) {
1200 		pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
1201 	} else if (wpa_len > 0) {
1202 		pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
1203 	} else {
1204 		if (bencrypt)
1205 			pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
1206 	}
1207 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1208 				pnetwork->BcnInfo.encryp_protocol));
1209 	RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1210 				pnetwork->BcnInfo.encryp_protocol));
1211 	rtw_get_cipher_info(pnetwork);
1212 
1213 	/* get bwmode and ch_offset */
1214 	/* parsing HT_CAP_IE */
1215 	p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1216 	if (p && len > 0) {
1217 			pht_cap = (struct ieee80211_ht_cap *)(p + 2);
1218 			pnetwork->BcnInfo.ht_cap_info = le16_to_cpu(pht_cap->cap_info);
1219 	} else {
1220 			pnetwork->BcnInfo.ht_cap_info = 0;
1221 	}
1222 	/* parsing HT_INFO_IE */
1223 	p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1224 	if (p && len > 0) {
1225 			pht_info = (struct HT_info_element *)(p + 2);
1226 			pnetwork->BcnInfo.ht_info_infos_0 = pht_info->infos[0];
1227 	} else {
1228 			pnetwork->BcnInfo.ht_info_infos_0 = 0;
1229 	}
1230 }
1231 
1232 /* show MCS rate, unit: 100Kbps */
rtw_mcs_rate(u8 rf_type,u8 bw_40MHz,u8 short_GI,unsigned char * MCS_rate)1233 u16 rtw_mcs_rate(u8 rf_type, u8 bw_40MHz, u8 short_GI, unsigned char *MCS_rate)
1234 {
1235 	u16 max_rate = 0;
1236 
1237 	if (rf_type == RF_1T1R) {
1238 		if (MCS_rate[0] & BIT(7))
1239 			max_rate = (bw_40MHz) ? ((short_GI)?1500:1350):((short_GI)?722:650);
1240 		else if (MCS_rate[0] & BIT(6))
1241 			max_rate = (bw_40MHz) ? ((short_GI)?1350:1215):((short_GI)?650:585);
1242 		else if (MCS_rate[0] & BIT(5))
1243 			max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1244 		else if (MCS_rate[0] & BIT(4))
1245 			max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1246 		else if (MCS_rate[0] & BIT(3))
1247 			max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1248 		else if (MCS_rate[0] & BIT(2))
1249 			max_rate = (bw_40MHz) ? ((short_GI)?450:405):((short_GI)?217:195);
1250 		else if (MCS_rate[0] & BIT(1))
1251 			max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1252 		else if (MCS_rate[0] & BIT(0))
1253 			max_rate = (bw_40MHz) ? ((short_GI)?150:135):((short_GI)?72:65);
1254 	} else {
1255 		if (MCS_rate[1]) {
1256 			if (MCS_rate[1] & BIT(7))
1257 				max_rate = (bw_40MHz) ? ((short_GI)?3000:2700):((short_GI)?1444:1300);
1258 			else if (MCS_rate[1] & BIT(6))
1259 				max_rate = (bw_40MHz) ? ((short_GI)?2700:2430):((short_GI)?1300:1170);
1260 			else if (MCS_rate[1] & BIT(5))
1261 				max_rate = (bw_40MHz) ? ((short_GI)?2400:2160):((short_GI)?1156:1040);
1262 			else if (MCS_rate[1] & BIT(4))
1263 				max_rate = (bw_40MHz) ? ((short_GI)?1800:1620):((short_GI)?867:780);
1264 			else if (MCS_rate[1] & BIT(3))
1265 				max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1266 			else if (MCS_rate[1] & BIT(2))
1267 				max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1268 			else if (MCS_rate[1] & BIT(1))
1269 				max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1270 			else if (MCS_rate[1] & BIT(0))
1271 				max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1272 		} else {
1273 			if (MCS_rate[0] & BIT(7))
1274 				max_rate = (bw_40MHz) ? ((short_GI)?1500:1350):((short_GI)?722:650);
1275 			else if (MCS_rate[0] & BIT(6))
1276 				max_rate = (bw_40MHz) ? ((short_GI)?1350:1215):((short_GI)?650:585);
1277 			else if (MCS_rate[0] & BIT(5))
1278 				max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1279 			else if (MCS_rate[0] & BIT(4))
1280 				max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1281 			else if (MCS_rate[0] & BIT(3))
1282 				max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1283 			else if (MCS_rate[0] & BIT(2))
1284 				max_rate = (bw_40MHz) ? ((short_GI)?450:405):((short_GI)?217:195);
1285 			else if (MCS_rate[0] & BIT(1))
1286 				max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1287 			else if (MCS_rate[0] & BIT(0))
1288 				max_rate = (bw_40MHz) ? ((short_GI)?150:135):((short_GI)?72:65);
1289 		}
1290 	}
1291 	return max_rate;
1292 }
1293 
rtw_action_frame_parse(const u8 * frame,u32 frame_len,u8 * category,u8 * action)1294 int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action)
1295 {
1296 	const u8 *frame_body = frame + sizeof(struct ieee80211_hdr_3addr);
1297 	u16 fc;
1298 	u8 c;
1299 	u8 a = ACT_PUBLIC_MAX;
1300 
1301 	fc = le16_to_cpu(((struct ieee80211_hdr_3addr *)frame)->frame_control);
1302 
1303 	if ((fc & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE))
1304 		!= (IEEE80211_FTYPE_MGMT|IEEE80211_STYPE_ACTION)
1305 	) {
1306 		return false;
1307 	}
1308 
1309 	c = frame_body[0];
1310 
1311 	switch (c) {
1312 	case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */
1313 		break;
1314 	default:
1315 		a = frame_body[1];
1316 	}
1317 
1318 	if (category)
1319 		*category = c;
1320 	if (action)
1321 		*action = a;
1322 
1323 	return true;
1324 }
1325 
1326 static const char *_action_public_str[] = {
1327 	"ACT_PUB_BSSCOEXIST",
1328 	"ACT_PUB_DSE_ENABLE",
1329 	"ACT_PUB_DSE_DEENABLE",
1330 	"ACT_PUB_DSE_REG_LOCATION",
1331 	"ACT_PUB_EXT_CHL_SWITCH",
1332 	"ACT_PUB_DSE_MSR_REQ",
1333 	"ACT_PUB_DSE_MSR_RPRT",
1334 	"ACT_PUB_MP",
1335 	"ACT_PUB_DSE_PWR_CONSTRAINT",
1336 	"ACT_PUB_VENDOR",
1337 	"ACT_PUB_GAS_INITIAL_REQ",
1338 	"ACT_PUB_GAS_INITIAL_RSP",
1339 	"ACT_PUB_GAS_COMEBACK_REQ",
1340 	"ACT_PUB_GAS_COMEBACK_RSP",
1341 	"ACT_PUB_TDLS_DISCOVERY_RSP",
1342 	"ACT_PUB_LOCATION_TRACK",
1343 	"ACT_PUB_RSVD",
1344 };
1345 
action_public_str(u8 action)1346 const char *action_public_str(u8 action)
1347 {
1348 	action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action;
1349 	return _action_public_str[action];
1350 }
1351