xref: /OK3568_Linux_fs/external/rkwifibt/drivers/rtl8723ds/core/rtw_ieee80211.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2017 Realtek Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  *****************************************************************************/
15 #define _IEEE80211_C
16 
17 #ifdef CONFIG_PLATFORM_INTEL_BYT
18 	#include <linux/fs.h>
19 #endif
20 #include <drv_types.h>
21 
22 
23 u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
24 u16 RTW_WPA_VERSION = 1;
25 u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
26 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
27 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
28 u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
29 u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
30 u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
31 u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
32 u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
33 u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
34 
35 u16 RSN_VERSION_BSD = 1;
36 u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
37 u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
38 u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
39 u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
40 u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
41 u8 RSN_CIPHER_SUITE_AES_128_CMAC[] = { 0x00, 0x0f, 0xac, 6 };
42 u8 RSN_CIPHER_SUITE_GCMP[] = { 0x00, 0x0f, 0xac, 8 };
43 u8 RSN_CIPHER_SUITE_GCMP_256[] = { 0x00, 0x0f, 0xac, 9 };
44 u8 RSN_CIPHER_SUITE_CCMP_256[] = { 0x00, 0x0f, 0xac, 10 };
45 u8 RSN_CIPHER_SUITE_BIP_GMAC_128[] = { 0x00, 0x0f, 0xac, 11 };
46 u8 RSN_CIPHER_SUITE_BIP_GMAC_256[] = { 0x00, 0x0f, 0xac, 12 };
47 u8 RSN_CIPHER_SUITE_BIP_CMAC_256[] = { 0x00, 0x0f, 0xac, 13 };
48 u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
49 
50 u8 WLAN_AKM_8021X[] = {0x00, 0x0f, 0xac, 1};
51 u8 WLAN_AKM_PSK[] = {0x00, 0x0f, 0xac, 2};
52 u8 WLAN_AKM_FT_8021X[] = {0x00, 0x0f, 0xac, 3};
53 u8 WLAN_AKM_FT_PSK[] = {0x00, 0x0f, 0xac, 4};
54 u8 WLAN_AKM_8021X_SHA256[] = {0x00, 0x0f, 0xac, 5};
55 u8 WLAN_AKM_PSK_SHA256[] = {0x00, 0x0f, 0xac, 6};
56 u8 WLAN_AKM_TDLS[] = {0x00, 0x0f, 0xac, 7};
57 u8 WLAN_AKM_SAE[] = {0x00, 0x0f, 0xac, 8};
58 u8 WLAN_AKM_FT_OVER_SAE[] = {0x00, 0x0f, 0xac, 9};
59 u8 WLAN_AKM_8021X_SUITE_B[] = {0x00, 0x0f, 0xac, 11};
60 u8 WLAN_AKM_8021X_SUITE_B_192[] = {0x00, 0x0f, 0xac, 12};
61 u8 WLAN_AKM_FILS_SHA256[] = {0x00, 0x0f, 0xac, 14};
62 u8 WLAN_AKM_FILS_SHA384[] = {0x00, 0x0f, 0xac, 15};
63 u8 WLAN_AKM_FT_FILS_SHA256[] = {0x00, 0x0f, 0xac, 16};
64 u8 WLAN_AKM_FT_FILS_SHA384[] = {0x00, 0x0f, 0xac, 17};
65 /* -----------------------------------------------------------
66  * for adhoc-master to generate ie and provide supported-rate to fw
67  * ----------------------------------------------------------- */
68 
69 u8	WIFI_CCKRATES[] = {
70 	(IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
71 	(IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
72 	(IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
73 	(IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
74 };
75 
76 u8	WIFI_OFDMRATES[] = {
77 	(IEEE80211_OFDM_RATE_6MB),
78 	(IEEE80211_OFDM_RATE_9MB),
79 	(IEEE80211_OFDM_RATE_12MB),
80 	(IEEE80211_OFDM_RATE_18MB),
81 	(IEEE80211_OFDM_RATE_24MB),
82 	IEEE80211_OFDM_RATE_36MB,
83 	IEEE80211_OFDM_RATE_48MB,
84 	IEEE80211_OFDM_RATE_54MB
85 };
86 
MGN_RATE_STR(enum MGN_RATE rate)87 const char *MGN_RATE_STR(enum MGN_RATE rate)
88 {
89 	u8 hw_rate;
90 
91 	if (rate == MGN_MCS32)
92 		return "MCS32";
93 
94 	hw_rate = MRateToHwRate(rate);
95 	if (hw_rate == DESC_RATE1M && rate != MGN_1M)
96 		hw_rate = DESC_RATE_NUM; /* invalid case */
97 
98 	return HDATA_RATE(hw_rate);
99 }
100 
101 u8 mgn_rates_cck[4] = {MGN_1M, MGN_2M, MGN_5_5M, MGN_11M};
102 u8 mgn_rates_ofdm[8] = {MGN_6M, MGN_9M, MGN_12M, MGN_18M, MGN_24M, MGN_36M, MGN_48M, MGN_54M};
103 u8 mgn_rates_mcs0_7[8] = {MGN_MCS0, MGN_MCS1, MGN_MCS2, MGN_MCS3, MGN_MCS4, MGN_MCS5, MGN_MCS6, MGN_MCS7};
104 u8 mgn_rates_mcs8_15[8] = {MGN_MCS8, MGN_MCS9, MGN_MCS10, MGN_MCS11, MGN_MCS12, MGN_MCS13, MGN_MCS14, MGN_MCS15};
105 u8 mgn_rates_mcs16_23[8] = {MGN_MCS16, MGN_MCS17, MGN_MCS18, MGN_MCS19, MGN_MCS20, MGN_MCS21, MGN_MCS22, MGN_MCS23};
106 u8 mgn_rates_mcs24_31[8] = {MGN_MCS24, MGN_MCS25, MGN_MCS26, MGN_MCS27, MGN_MCS28, MGN_MCS29, MGN_MCS30, MGN_MCS31};
107 u8 mgn_rates_vht1ss[10] = {MGN_VHT1SS_MCS0, MGN_VHT1SS_MCS1, MGN_VHT1SS_MCS2, MGN_VHT1SS_MCS3, MGN_VHT1SS_MCS4
108 	, MGN_VHT1SS_MCS5, MGN_VHT1SS_MCS6, MGN_VHT1SS_MCS7, MGN_VHT1SS_MCS8, MGN_VHT1SS_MCS9
109 			  };
110 u8 mgn_rates_vht2ss[10] = {MGN_VHT2SS_MCS0, MGN_VHT2SS_MCS1, MGN_VHT2SS_MCS2, MGN_VHT2SS_MCS3, MGN_VHT2SS_MCS4
111 	, MGN_VHT2SS_MCS5, MGN_VHT2SS_MCS6, MGN_VHT2SS_MCS7, MGN_VHT2SS_MCS8, MGN_VHT2SS_MCS9
112 			  };
113 u8 mgn_rates_vht3ss[10] = {MGN_VHT3SS_MCS0, MGN_VHT3SS_MCS1, MGN_VHT3SS_MCS2, MGN_VHT3SS_MCS3, MGN_VHT3SS_MCS4
114 	, MGN_VHT3SS_MCS5, MGN_VHT3SS_MCS6, MGN_VHT3SS_MCS7, MGN_VHT3SS_MCS8, MGN_VHT3SS_MCS9
115 			  };
116 u8 mgn_rates_vht4ss[10] = {MGN_VHT4SS_MCS0, MGN_VHT4SS_MCS1, MGN_VHT4SS_MCS2, MGN_VHT4SS_MCS3, MGN_VHT4SS_MCS4
117 	, MGN_VHT4SS_MCS5, MGN_VHT4SS_MCS6, MGN_VHT4SS_MCS7, MGN_VHT4SS_MCS8, MGN_VHT4SS_MCS9
118 			  };
119 
mgn_rate_to_rs(enum MGN_RATE rate)120 RATE_SECTION mgn_rate_to_rs(enum MGN_RATE rate)
121 {
122 	RATE_SECTION rs = RATE_SECTION_NUM;
123 
124 	if (IS_CCK_RATE(rate))
125 		rs = CCK;
126 	else if (IS_OFDM_RATE(rate))
127 		rs = OFDM;
128 	else if (IS_HT1SS_RATE(rate))
129 		rs = HT_1SS;
130 	else if (IS_HT2SS_RATE(rate))
131 		rs = HT_2SS;
132 	else if (IS_HT3SS_RATE(rate))
133 		rs = HT_3SS;
134 	else if (IS_HT4SS_RATE(rate))
135 		rs = HT_4SS;
136 	else if (IS_VHT1SS_RATE(rate))
137 		rs = VHT_1SS;
138 	else if (IS_VHT2SS_RATE(rate))
139 		rs = VHT_2SS;
140 	else if (IS_VHT3SS_RATE(rate))
141 		rs = VHT_3SS;
142 	else if (IS_VHT4SS_RATE(rate))
143 		rs = VHT_4SS;
144 
145 	return rs;
146 }
147 
148 static const char *const _rate_section_str[] = {
149 	"CCK",
150 	"OFDM",
151 	"HT_1SS",
152 	"HT_2SS",
153 	"HT_3SS",
154 	"HT_4SS",
155 	"VHT_1SS",
156 	"VHT_2SS",
157 	"VHT_3SS",
158 	"VHT_4SS",
159 	"RATE_SECTION_UNKNOWN",
160 };
161 
rate_section_str(u8 section)162 const char *rate_section_str(u8 section)
163 {
164 	section = (section >= RATE_SECTION_NUM) ? RATE_SECTION_NUM : section;
165 	return _rate_section_str[section];
166 }
167 
168 struct rate_section_ent rates_by_sections[RATE_SECTION_NUM] = {
169 	{RF_1TX, 4, mgn_rates_cck},
170 	{RF_1TX, 8, mgn_rates_ofdm},
171 	{RF_1TX, 8, mgn_rates_mcs0_7},
172 	{RF_2TX, 8, mgn_rates_mcs8_15},
173 	{RF_3TX, 8, mgn_rates_mcs16_23},
174 	{RF_4TX, 8, mgn_rates_mcs24_31},
175 	{RF_1TX, 10, mgn_rates_vht1ss},
176 	{RF_2TX, 10, mgn_rates_vht2ss},
177 	{RF_3TX, 10, mgn_rates_vht3ss},
178 	{RF_4TX, 10, mgn_rates_vht4ss},
179 };
180 
rtw_get_bit_value_from_ieee_value(u8 val)181 int rtw_get_bit_value_from_ieee_value(u8 val)
182 {
183 	unsigned char dot11_rate_table[] = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0}; /* last element must be zero!! */
184 
185 	int i = 0;
186 	while (dot11_rate_table[i] != 0) {
187 		if (dot11_rate_table[i] == val)
188 			return BIT(i);
189 		i++;
190 	}
191 	return 0;
192 }
rtw_get_cckrate_size(u8 * rate,u32 rate_length)193 uint rtw_get_cckrate_size(u8 *rate, u32 rate_length)
194 {
195 	int i = 0;
196 	while(i < rate_length){
197 		RTW_DBG("%s, rate[%d]=%u\n", __FUNCTION__, i, rate[i]);
198 		if (((rate[i] & 0x7f) == 2) || ((rate[i] & 0x7f) == 4) ||
199 			((rate[i] & 0x7f) == 11)  || ((rate[i] & 0x7f) == 22))
200 			i++;
201 		else
202 			break;
203 	}
204 	return i;
205 }
206 
rtw_is_cckrates_included(u8 * rate)207 uint	rtw_is_cckrates_included(u8 *rate)
208 {
209 	u32	i = 0;
210 
211 	while (rate[i] != 0) {
212 		if ((((rate[i]) & 0x7f) == 2)	|| (((rate[i]) & 0x7f) == 4) ||
213 		    (((rate[i]) & 0x7f) == 11)  || (((rate[i]) & 0x7f) == 22))
214 			return _TRUE;
215 		i++;
216 	}
217 
218 	return _FALSE;
219 }
220 
rtw_is_cckratesonly_included(u8 * rate)221 uint	rtw_is_cckratesonly_included(u8 *rate)
222 {
223 	u32 i = 0;
224 
225 
226 	while (rate[i] != 0) {
227 		if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
228 		    (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
229 			return _FALSE;
230 
231 		i++;
232 	}
233 
234 	return _TRUE;
235 
236 }
237 
rtw_check_network_type(unsigned char * rate,int ratelen,int channel)238 int rtw_check_network_type(unsigned char *rate, int ratelen, int channel)
239 {
240 	if (channel > 14) {
241 		if ((rtw_is_cckrates_included(rate)) == _TRUE)
242 			return WIRELESS_INVALID;
243 		else
244 			return WIRELESS_11A;
245 	} else { /* could be pure B, pure G, or B/G */
246 		if ((rtw_is_cckratesonly_included(rate)) == _TRUE)
247 			return WIRELESS_11B;
248 		else if ((rtw_is_cckrates_included(rate)) == _TRUE)
249 			return	WIRELESS_11BG;
250 		else
251 			return WIRELESS_11G;
252 	}
253 
254 }
255 
rtw_set_fixed_ie(unsigned char * pbuf,unsigned int len,unsigned char * source,unsigned int * frlen)256 u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source,
257 		     unsigned int *frlen)
258 {
259 	_rtw_memcpy((void *)pbuf, (void *)source, len);
260 	*frlen = *frlen + len;
261 	return pbuf + len;
262 }
263 
264 /* rtw_set_ie will update frame length */
rtw_set_ie(u8 * pbuf,sint index,uint len,const u8 * source,uint * frlen)265 u8 *rtw_set_ie
266 (
267 	u8 *pbuf,
268 	sint index,
269 	uint len, /* IE content length, not entire IE length */
270 	const u8 *source,
271 	uint *frlen /* frame length */
272 )
273 {
274 	*pbuf = (u8)index;
275 
276 	*(pbuf + 1) = (u8)len;
277 
278 	if (len > 0)
279 		_rtw_memcpy((void *)(pbuf + 2), (void *)source, len);
280 
281 	if (frlen)
282 		*frlen = *frlen + (len + 2);
283 
284 	return pbuf + len + 2;
285 }
286 
rtw_set_ie_tpc_report(u8 * buf,u32 * buf_len,u8 tx_power,u8 link_margin)287 u8 *rtw_set_ie_tpc_report(u8 *buf, u32 *buf_len, u8 tx_power, u8 link_margin)
288 {
289 	u8 ie_data[2];
290 
291 	ie_data[0] = tx_power;
292 	ie_data[1] = link_margin;
293 	return rtw_set_ie(buf, WLAN_EID_TPC_REPORT,  2, ie_data, buf_len);
294 }
295 
rtw_set_ie_ch_switch(u8 * buf,u32 * buf_len,u8 ch_switch_mode,u8 new_ch,u8 ch_switch_cnt)296 inline u8 *rtw_set_ie_ch_switch(u8 *buf, u32 *buf_len, u8 ch_switch_mode,
297 				u8 new_ch, u8 ch_switch_cnt)
298 {
299 	u8 ie_data[3];
300 
301 	ie_data[0] = ch_switch_mode;
302 	ie_data[1] = new_ch;
303 	ie_data[2] = ch_switch_cnt;
304 	return rtw_set_ie(buf, WLAN_EID_CHANNEL_SWITCH,  3, ie_data, buf_len);
305 }
306 
secondary_ch_offset_to_hal_ch_offset(u8 ch_offset)307 inline u8 secondary_ch_offset_to_hal_ch_offset(u8 ch_offset)
308 {
309 	if (ch_offset == SCN)
310 		return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
311 	else if (ch_offset == SCA)
312 		return HAL_PRIME_CHNL_OFFSET_LOWER;
313 	else if (ch_offset == SCB)
314 		return HAL_PRIME_CHNL_OFFSET_UPPER;
315 
316 	return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
317 }
318 
hal_ch_offset_to_secondary_ch_offset(u8 ch_offset)319 inline u8 hal_ch_offset_to_secondary_ch_offset(u8 ch_offset)
320 {
321 	if (ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
322 		return SCN;
323 	else if (ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
324 		return SCA;
325 	else if (ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
326 		return SCB;
327 
328 	return SCN;
329 }
330 
rtw_set_ie_secondary_ch_offset(u8 * buf,u32 * buf_len,u8 secondary_ch_offset)331 inline u8 *rtw_set_ie_secondary_ch_offset(u8 *buf, u32 *buf_len, u8 secondary_ch_offset)
332 {
333 	return rtw_set_ie(buf, WLAN_EID_SECONDARY_CHANNEL_OFFSET,  1, &secondary_ch_offset, buf_len);
334 }
335 
rtw_set_ie_mesh_ch_switch_parm(u8 * buf,u32 * buf_len,u8 ttl,u8 flags,u16 reason,u16 precedence)336 inline u8 *rtw_set_ie_mesh_ch_switch_parm(u8 *buf, u32 *buf_len, u8 ttl,
337 		u8 flags, u16 reason, u16 precedence)
338 {
339 	u8 ie_data[6];
340 
341 	ie_data[0] = ttl;
342 	ie_data[1] = flags;
343 	RTW_PUT_LE16((u8 *)&ie_data[2], reason);
344 	RTW_PUT_LE16((u8 *)&ie_data[4], precedence);
345 
346 	return rtw_set_ie(buf, 0x118,  6, ie_data, buf_len);
347 }
348 
349 /*----------------------------------------------------------------------------
350 index: the information element id index, limit is the limit for search
351 -----------------------------------------------------------------------------*/
rtw_get_ie(const u8 * pbuf,sint index,sint * len,sint limit)352 u8 *rtw_get_ie(const u8 *pbuf, sint index, sint *len, sint limit)
353 {
354 	sint tmp, i;
355 	const u8 *p;
356 	if (limit < 1) {
357 		return NULL;
358 	}
359 
360 	p = pbuf;
361 	i = 0;
362 	*len = 0;
363 	while (1) {
364 		if (*p == index) {
365 			*len = *(p + 1);
366 			return (u8 *)p;
367 		} else {
368 			tmp = *(p + 1);
369 			p += (tmp + 2);
370 			i += (tmp + 2);
371 		}
372 		if (i >= limit)
373 			break;
374 	}
375 	return NULL;
376 }
377 
378 /**
379  * rtw_get_ie_ex - Search specific IE from a series of IEs
380  * @in_ie: Address of IEs to search
381  * @in_len: Length limit from in_ie
382  * @eid: Element ID to match
383  * @oui: OUI to match
384  * @oui_len: OUI length
385  * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE
386  * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE
387  *
388  * Returns: The address of the specific IE found, or NULL
389  */
rtw_get_ie_ex(const u8 * in_ie,uint in_len,u8 eid,const u8 * oui,u8 oui_len,u8 * ie,uint * ielen)390 u8 *rtw_get_ie_ex(const u8 *in_ie, uint in_len, u8 eid, const u8 *oui, u8 oui_len, u8 *ie, uint *ielen)
391 {
392 	uint cnt;
393 	const u8 *target_ie = NULL;
394 
395 
396 	if (ielen)
397 		*ielen = 0;
398 
399 	if (!in_ie || in_len <= 0)
400 		return (u8 *)target_ie;
401 
402 	cnt = 0;
403 
404 	while (cnt < in_len) {
405 		if (eid == in_ie[cnt]
406 		    && (!oui || _rtw_memcmp(&in_ie[cnt + 2], oui, oui_len) == _TRUE)) {
407 			target_ie = &in_ie[cnt];
408 
409 			if (ie)
410 				_rtw_memcpy(ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
411 
412 			if (ielen)
413 				*ielen = in_ie[cnt + 1] + 2;
414 
415 			break;
416 		} else {
417 			cnt += in_ie[cnt + 1] + 2; /* goto next	 */
418 		}
419 
420 	}
421 
422 	return (u8 *)target_ie;
423 }
424 
425 /**
426  * rtw_ies_update_ie - Find matching IEs and update it
427  *
428  * @ies: address of IEs to search
429  * @ies_len: address of length of ies, will update to new length
430  * @offset: the offset to start scarch
431  * @eid: element ID to match
432  * @content: new content will update to matching element
433  * @content_len: length of new content
434  * Returns: _SUCCESS: ies is updated, _FAIL: not updated
435  */
rtw_ies_update_ie(u8 * ies,uint * ies_len,uint ies_offset,u8 eid,const u8 * content,u8 content_len)436 u8 rtw_ies_update_ie(u8 *ies, uint *ies_len, uint ies_offset, u8 eid, const u8 *content, u8 content_len)
437 {
438 	u8 ret = _FAIL;
439 	u8 *target_ie;
440 	u32 target_ielen;
441 	u8 *start, *remain_ies = NULL, *backup_ies = NULL;
442 	uint search_len, remain_len = 0;
443 	sint offset;
444 
445 	if (ies == NULL || *ies_len == 0 || *ies_len <= ies_offset)
446 		goto exit;
447 
448 	start = ies + ies_offset;
449 	search_len = *ies_len - ies_offset;
450 
451 	target_ie = rtw_get_ie(start, eid, &target_ielen, search_len);
452 	if (target_ie && target_ielen) {
453 		if (target_ielen != content_len) {
454 			remain_ies = target_ie + 2 + target_ielen;
455 			remain_len = search_len - (remain_ies - start);
456 
457 			backup_ies = rtw_malloc(remain_len);
458 			if (!backup_ies)
459 				goto exit;
460 
461 			_rtw_memcpy(backup_ies, remain_ies, remain_len);
462 		}
463 
464 		_rtw_memcpy(target_ie + 2, content, content_len);
465 		*(target_ie + 1) = content_len;
466 		ret = _SUCCESS;
467 
468 		if (target_ielen != content_len) {
469 			remain_ies = target_ie + 2 + content_len;
470 			_rtw_memcpy(remain_ies, backup_ies, remain_len);
471 			offset = content_len - target_ielen;
472 			*ies_len = *ies_len + offset;
473 		}
474 	}
475 exit:
476 	return ret;
477 }
478 
479 /**
480  * rtw_ies_remove_ie - Find matching IEs and remove
481  * @ies: Address of IEs to search
482  * @ies_len: Pointer of length of ies, will update to new length
483  * @offset: The offset to start scarch
484  * @eid: Element ID to match
485  * @oui: OUI to match
486  * @oui_len: OUI length
487  *
488  * Returns: _SUCCESS: ies is updated, _FAIL: not updated
489  */
rtw_ies_remove_ie(u8 * ies,uint * ies_len,uint offset,u8 eid,u8 * oui,u8 oui_len)490 int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len)
491 {
492 	int ret = _FAIL;
493 	u8 *target_ie;
494 	u32 target_ielen;
495 	u8 *start;
496 	uint search_len;
497 
498 	if (!ies || !ies_len || *ies_len <= offset)
499 		goto exit;
500 
501 	start = ies + offset;
502 	search_len = *ies_len - offset;
503 
504 	while (1) {
505 		target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen);
506 		if (target_ie && target_ielen) {
507 			u8 *remain_ies = target_ie + target_ielen;
508 			uint remain_len = search_len - (remain_ies - start);
509 
510 			_rtw_memmove(target_ie, remain_ies, remain_len);
511 			*ies_len = *ies_len - target_ielen;
512 			ret = _SUCCESS;
513 
514 			start = target_ie;
515 			search_len = remain_len;
516 		} else
517 			break;
518 	}
519 exit:
520 	return ret;
521 }
522 
rtw_set_supported_rate(u8 * SupportedRates,uint mode)523 void rtw_set_supported_rate(u8 *SupportedRates, uint mode)
524 {
525 
526 	_rtw_memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
527 
528 	switch (mode) {
529 	case WIRELESS_11B:
530 		_rtw_memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
531 		break;
532 
533 	case WIRELESS_11G:
534 	case WIRELESS_11A:
535 	case WIRELESS_11_5N:
536 	case WIRELESS_11A_5N: /* Todo: no basic rate for ofdm ? */
537 	case WIRELESS_11_5AC:
538 		_rtw_memcpy(SupportedRates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
539 		break;
540 
541 	case WIRELESS_11BG:
542 	case WIRELESS_11G_24N:
543 	case WIRELESS_11_24N:
544 	case WIRELESS_11BG_24N:
545 		_rtw_memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
546 		_rtw_memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
547 		break;
548 
549 	}
550 }
551 
rtw_filter_suppport_rateie(WLAN_BSSID_EX * pbss_network,u8 keep)552 void rtw_filter_suppport_rateie(WLAN_BSSID_EX *pbss_network, u8 keep)
553 {
554 	u8 i, idx = 0, new_rate[NDIS_802_11_LENGTH_RATES_EX], *p;
555 	uint iscck, isofdm, ie_orilen = 0, remain_len;
556 	u8 *remain_ies;
557 
558 	p = rtw_get_ie(pbss_network->IEs + _BEACON_IE_OFFSET_, _SUPPORTEDRATES_IE_, &ie_orilen, (pbss_network->IELength - _BEACON_IE_OFFSET_));
559 	if (!p)
560 		return;
561 
562 	_rtw_memset(new_rate, 0, NDIS_802_11_LENGTH_RATES_EX);
563 	for (i=0; i < ie_orilen; i++) {
564 		iscck = rtw_is_cck_rate(p[i+2]);
565 		isofdm= rtw_is_ofdm_rate(p[i+2]);
566 		if (((keep == CCK) && iscck)
567 			|| ((keep == OFDM) && isofdm))
568 			new_rate[idx++]= rtw_is_basic_rate_ofdm(p[i+2]) ? p[i+2]|IEEE80211_BASIC_RATE_MASK : p[i+2];
569 	}
570 	/*	update rate ie	*/
571 	p[1] = idx;
572 	_rtw_memcpy(p+2, new_rate, idx);
573 	/*	update remain ie & IELength*/
574 	remain_ies = p + 2 + ie_orilen;
575 	remain_len = pbss_network->IELength - (remain_ies - pbss_network->IEs);
576 	_rtw_memmove(p+2+idx, remain_ies, remain_len);
577 	pbss_network->IELength -= (ie_orilen - idx);
578 }
579 
580 
581 /*
582 	Adjust those items by given wireless_mode
583 		1. pbss_network->IELength
584 		2. pbss_network->IE (SUPPORTRATE & EXT_SUPPORTRATE)
585 		3. pbss_network->SupportedRates
586 */
587 
rtw_update_rate_bymode(WLAN_BSSID_EX * pbss_network,u32 mode)588 u8 rtw_update_rate_bymode(WLAN_BSSID_EX *pbss_network, u32 mode)
589 {
590 	u8 network_type, *p, *ie = pbss_network->IEs;
591 	sint ie_len;
592 	uint network_ielen = pbss_network->IELength;
593 
594 	if (mode == WIRELESS_11B) {
595 		/*only keep CCK in support_rate IE and remove whole ext_support_rate IE*/
596 		rtw_filter_suppport_rateie(pbss_network, CCK);
597 		p = rtw_get_ie(ie + _BEACON_IE_OFFSET_, _EXT_SUPPORTEDRATES_IE_, &ie_len, pbss_network->IELength - _BEACON_IE_OFFSET_);
598 		if (p) {
599 			rtw_ies_remove_ie(ie , &network_ielen, _BEACON_IE_OFFSET_, _EXT_SUPPORTEDRATES_IE_, NULL, 0);
600 			pbss_network->IELength -= ie_len;
601 		}
602 		network_type = WIRELESS_11B;
603 	} else {
604 		if (pbss_network->Configuration.DSConfig > 14) {
605 			/* Remove CCK in support_rate IE */
606 			rtw_filter_suppport_rateie(pbss_network, OFDM);
607 			network_type = WIRELESS_11A;
608 		} else {
609 			if ((mode & WIRELESS_11B) == 0) {
610 				/* Remove CCK in support_rate IE */
611 				rtw_filter_suppport_rateie(pbss_network, OFDM);
612 				network_type = WIRELESS_11G;
613 			} else {
614 				network_type = WIRELESS_11BG;
615 			}
616 		}
617 	}
618 
619 	rtw_set_supported_rate(pbss_network->SupportedRates, network_type);
620 
621 	return network_type;
622 }
623 
rtw_get_rateset_len(u8 * rateset)624 uint	rtw_get_rateset_len(u8	*rateset)
625 {
626 	uint i = 0;
627 	while (1) {
628 		if ((rateset[i]) == 0)
629 			break;
630 
631 		if (i > 12)
632 			break;
633 
634 		i++;
635 	}
636 	return i;
637 }
638 
rtw_generate_ie(struct registry_priv * pregistrypriv)639 int rtw_generate_ie(struct registry_priv *pregistrypriv)
640 {
641 	u8	wireless_mode;
642 	int	sz = 0, rateLen;
643 	WLAN_BSSID_EX	*pdev_network = &pregistrypriv->dev_network;
644 	u8	*ie = pdev_network->IEs;
645 
646 
647 	/* timestamp will be inserted by hardware */
648 	sz += 8;
649 	ie += sz;
650 
651 	/* beacon interval : 2bytes */
652 	*(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod); /* BCN_INTERVAL; */
653 	sz += 2;
654 	ie += 2;
655 
656 	/* capability info */
657 	*(u16 *)ie = 0;
658 
659 	*(u16 *)ie |= cpu_to_le16(cap_IBSS);
660 
661 	if (pregistrypriv->preamble == PREAMBLE_SHORT)
662 		*(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
663 
664 	if (pdev_network->Privacy)
665 		*(u16 *)ie |= cpu_to_le16(cap_Privacy);
666 
667 	sz += 2;
668 	ie += 2;
669 
670 	/* SSID */
671 	ie = rtw_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength, pdev_network->Ssid.Ssid, &sz);
672 
673 	/* supported rates */
674 	if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
675 		if (pdev_network->Configuration.DSConfig > 14)
676 			wireless_mode = WIRELESS_11A_5N;
677 		else
678 			wireless_mode = WIRELESS_11BG_24N;
679 	} else if (pregistrypriv->wireless_mode == WIRELESS_MODE_MAX) { /* WIRELESS_11ABGN | WIRELESS_11AC */
680 		if (pdev_network->Configuration.DSConfig > 14)
681 			wireless_mode = WIRELESS_11_5AC;
682 		else
683 			wireless_mode = WIRELESS_11BG_24N;
684 	} else
685 		wireless_mode = pregistrypriv->wireless_mode;
686 
687 	rtw_set_supported_rate(pdev_network->SupportedRates, wireless_mode) ;
688 
689 	rateLen = rtw_get_rateset_len(pdev_network->SupportedRates);
690 
691 	if (rateLen > 8) {
692 		ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, pdev_network->SupportedRates, &sz);
693 		/* ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
694 	} else
695 		ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, pdev_network->SupportedRates, &sz);
696 
697 	/* DS parameter set */
698 	ie = rtw_set_ie(ie, _DSSET_IE_, 1, (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
699 
700 
701 	/* IBSS Parameter Set */
702 
703 	ie = rtw_set_ie(ie, _IBSS_PARA_IE_, 2, (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
704 
705 	if (rateLen > 8)
706 		ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz);
707 
708 #ifdef CONFIG_80211N_HT
709 	/* HT Cap. */
710 	if (is_supported_ht(pregistrypriv->wireless_mode)
711 	    && (pregistrypriv->ht_enable == _TRUE)) {
712 		/* todo: */
713 	}
714 #endif /* CONFIG_80211N_HT */
715 
716 	/* pdev_network->IELength =  sz; */ /* update IELength */
717 
718 
719 	/* return _SUCCESS; */
720 
721 	return sz;
722 
723 }
724 
rtw_get_wpa_ie(unsigned char * pie,int * wpa_ie_len,int limit)725 unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
726 {
727 	int len;
728 	u16 val16;
729 	unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
730 	u8 *pbuf = pie;
731 	int limit_new = limit;
732 
733 	while (1) {
734 		pbuf = rtw_get_ie(pbuf, _WPA_IE_ID_, &len, limit_new);
735 
736 		if (pbuf) {
737 
738 			/* check if oui matches... */
739 			if (_rtw_memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type)) == _FALSE)
740 
741 				goto check_next_ie;
742 
743 			/* check version... */
744 			_rtw_memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
745 
746 			val16 = le16_to_cpu(val16);
747 			if (val16 != 0x0001)
748 				goto check_next_ie;
749 
750 			*wpa_ie_len = *(pbuf + 1);
751 
752 			return pbuf;
753 
754 		} else {
755 
756 			*wpa_ie_len = 0;
757 			return NULL;
758 		}
759 
760 check_next_ie:
761 
762 		limit_new = limit - (pbuf - pie) - 2 - len;
763 
764 		if (limit_new <= 0)
765 			break;
766 
767 		pbuf += (2 + len);
768 
769 	}
770 
771 	*wpa_ie_len = 0;
772 
773 	return NULL;
774 
775 }
776 
rtw_get_wpa2_ie(unsigned char * pie,int * rsn_ie_len,int limit)777 unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
778 {
779 
780 	return rtw_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
781 
782 }
783 
rtw_get_wpa_cipher_suite(u8 * s)784 int rtw_get_wpa_cipher_suite(u8 *s)
785 {
786 	if (_rtw_memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN) == _TRUE)
787 		return WPA_CIPHER_NONE;
788 	if (_rtw_memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN) == _TRUE)
789 		return WPA_CIPHER_WEP40;
790 	if (_rtw_memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN) == _TRUE)
791 		return WPA_CIPHER_TKIP;
792 	if (_rtw_memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN) == _TRUE)
793 		return WPA_CIPHER_CCMP;
794 	if (_rtw_memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN) == _TRUE)
795 		return WPA_CIPHER_WEP104;
796 
797 	return 0;
798 }
799 
rtw_get_rsn_cipher_suite(u8 * s)800 int rtw_get_rsn_cipher_suite(u8 *s)
801 {
802 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN) == _TRUE)
803 		return WPA_CIPHER_NONE;
804 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN) == _TRUE)
805 		return WPA_CIPHER_WEP40;
806 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN) == _TRUE)
807 		return WPA_CIPHER_TKIP;
808 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN) == _TRUE)
809 		return WPA_CIPHER_CCMP;
810 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_GCMP, RSN_SELECTOR_LEN) == _TRUE)
811 		return WPA_CIPHER_GCMP;
812 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_GCMP_256, RSN_SELECTOR_LEN) == _TRUE)
813 		return WPA_CIPHER_GCMP_256;
814 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_CCMP_256, RSN_SELECTOR_LEN) == _TRUE)
815 		return WPA_CIPHER_CCMP_256;
816 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN) == _TRUE)
817 		return WPA_CIPHER_WEP104;
818 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_AES_128_CMAC, RSN_SELECTOR_LEN) == _TRUE)
819 		return WPA_CIPHER_BIP_CMAC_128;
820 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_BIP_GMAC_128, RSN_SELECTOR_LEN) == _TRUE)
821 		return WPA_CIPHER_BIP_GMAC_128;
822 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_BIP_GMAC_256, RSN_SELECTOR_LEN) == _TRUE)
823 		return WPA_CIPHER_BIP_GMAC_256;
824 	if (_rtw_memcmp(s, RSN_CIPHER_SUITE_BIP_CMAC_256, RSN_SELECTOR_LEN) == _TRUE)
825 		return WPA_CIPHER_BIP_CMAC_256;
826 	return 0;
827 }
828 
rtw_get_akm_suite_bitmap(u8 * s)829 u32 rtw_get_akm_suite_bitmap(u8 *s)
830 {
831 	if (_rtw_memcmp(s, WLAN_AKM_8021X, RSN_SELECTOR_LEN) == _TRUE)
832 		return WLAN_AKM_TYPE_8021X;
833 	if (_rtw_memcmp(s, WLAN_AKM_PSK, RSN_SELECTOR_LEN) == _TRUE)
834 		return WLAN_AKM_TYPE_PSK;
835 	if (_rtw_memcmp(s, WLAN_AKM_FT_8021X, RSN_SELECTOR_LEN) == _TRUE)
836 		return WLAN_AKM_TYPE_FT_8021X;
837 	if (_rtw_memcmp(s, WLAN_AKM_FT_PSK, RSN_SELECTOR_LEN) == _TRUE)
838 		return WLAN_AKM_TYPE_FT_PSK;
839 	if (_rtw_memcmp(s, WLAN_AKM_8021X_SHA256, RSN_SELECTOR_LEN) == _TRUE)
840 		return WLAN_AKM_TYPE_8021X_SHA256;
841 	if (_rtw_memcmp(s, WLAN_AKM_PSK_SHA256, RSN_SELECTOR_LEN) == _TRUE)
842 		return WLAN_AKM_TYPE_PSK_SHA256;
843 	if (_rtw_memcmp(s, WLAN_AKM_TDLS, RSN_SELECTOR_LEN) == _TRUE)
844 		return WLAN_AKM_TYPE_TDLS;
845 	if (_rtw_memcmp(s, WLAN_AKM_SAE, RSN_SELECTOR_LEN) == _TRUE)
846 		return WLAN_AKM_TYPE_SAE;
847 	if (_rtw_memcmp(s, WLAN_AKM_FT_OVER_SAE, RSN_SELECTOR_LEN) == _TRUE)
848 		return WLAN_AKM_TYPE_FT_OVER_SAE;
849 	if (_rtw_memcmp(s, WLAN_AKM_8021X_SUITE_B, RSN_SELECTOR_LEN) == _TRUE)
850 		return WLAN_AKM_TYPE_8021X_SUITE_B;
851 	if (_rtw_memcmp(s, WLAN_AKM_8021X_SUITE_B_192, RSN_SELECTOR_LEN) == _TRUE)
852 		return WLAN_AKM_TYPE_8021X_SUITE_B_192;
853 	if (_rtw_memcmp(s, WLAN_AKM_FILS_SHA256, RSN_SELECTOR_LEN) == _TRUE)
854 		return WLAN_AKM_TYPE_FILS_SHA256;
855 	if (_rtw_memcmp(s, WLAN_AKM_FILS_SHA384, RSN_SELECTOR_LEN) == _TRUE)
856 		return WLAN_AKM_TYPE_FILS_SHA384;
857 	if (_rtw_memcmp(s, WLAN_AKM_FT_FILS_SHA256, RSN_SELECTOR_LEN) == _TRUE)
858 		return WLAN_AKM_TYPE_FT_FILS_SHA256;
859 	if (_rtw_memcmp(s, WLAN_AKM_FT_FILS_SHA384, RSN_SELECTOR_LEN) == _TRUE)
860 		return WLAN_AKM_TYPE_FT_FILS_SHA384;
861 
862 	return 0;
863 }
864 
rtw_parse_wpa_ie(u8 * wpa_ie,int wpa_ie_len,int * group_cipher,int * pairwise_cipher,u32 * akm)865 int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
866 	int *pairwise_cipher, u32 *akm)
867 {
868 	int i, ret = _SUCCESS;
869 	int left, count;
870 	u8 *pos;
871 	u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1};
872 
873 	if (wpa_ie_len <= 0) {
874 		/* No WPA IE - fail silently */
875 		return _FAIL;
876 	}
877 
878 
879 	if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
880 	    (_rtw_memcmp(wpa_ie + 2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN) != _TRUE))
881 		return _FAIL;
882 
883 	pos = wpa_ie;
884 
885 	pos += 8;
886 	left = wpa_ie_len - 8;
887 
888 
889 	/* group_cipher */
890 	if (left >= WPA_SELECTOR_LEN) {
891 
892 		*group_cipher = rtw_get_wpa_cipher_suite(pos);
893 
894 		pos += WPA_SELECTOR_LEN;
895 		left -= WPA_SELECTOR_LEN;
896 
897 	} else if (left > 0) {
898 
899 		return _FAIL;
900 	}
901 
902 
903 	/* pairwise_cipher */
904 	if (left >= 2) {
905 		/* count = le16_to_cpu(*(u16*)pos);	 */
906 		count = RTW_GET_LE16(pos);
907 		pos += 2;
908 		left -= 2;
909 
910 		if (count == 0 || left < count * WPA_SELECTOR_LEN) {
911 			return _FAIL;
912 		}
913 
914 		for (i = 0; i < count; i++) {
915 			*pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
916 
917 			pos += WPA_SELECTOR_LEN;
918 			left -= WPA_SELECTOR_LEN;
919 		}
920 
921 	} else if (left == 1) {
922 		return _FAIL;
923 	}
924 
925 	if (akm) {
926 		if (left >= 6) {
927 			pos += 2;
928 			if (_rtw_memcmp(pos, SUITE_1X, 4) == 1) {
929 				*akm = WLAN_AKM_TYPE_8021X;
930 			}
931 		}
932 	}
933 
934 	return ret;
935 
936 }
937 
rtw_rsne_info_parse(const u8 * ie,uint ie_len,struct rsne_info * info)938 int rtw_rsne_info_parse(const u8 *ie, uint ie_len, struct rsne_info *info)
939 {
940 	const u8 *pos = ie;
941 	u16 ver;
942 	u16 cnt;
943 
944 	_rtw_memset(info, 0, sizeof(struct rsne_info));
945 
946 	if (ie + ie_len < pos + 4)
947 		goto err;
948 
949 	if (*ie != WLAN_EID_RSN || *(ie + 1) != ie_len - 2)
950 		goto err;
951 	pos += 2;
952 
953 	/* Version */
954 	ver = RTW_GET_LE16(pos);
955 	if(1 != ver)
956 		goto err;
957 	pos += 2;
958 
959 	/* Group CS */
960 	if (ie + ie_len < pos + 4) {
961 		if (ie + ie_len != pos)
962 			goto err;
963 		goto exit;
964 	}
965 	info->gcs = (u8 *)pos;
966 	pos += 4;
967 
968 	/* Pairwise CS */
969 	if (ie + ie_len < pos + 2) {
970 		if (ie + ie_len != pos)
971 			goto err;
972 		goto exit;
973 	}
974 	cnt = RTW_GET_LE16(pos);
975 	pos += 2;
976 	if (ie + ie_len < pos + 4 * cnt) {
977 		if (ie + ie_len != pos)
978 			goto err;
979 		goto exit;
980 	}
981 	info->pcs_cnt = cnt;
982 	info->pcs_list = (u8 *)pos;
983 	pos += 4 * cnt;
984 
985 	/* AKM */
986 	if (ie + ie_len < pos + 2) {
987 		if (ie + ie_len != pos)
988 			goto err;
989 		goto exit;
990 	}
991 	cnt = RTW_GET_LE16(pos);
992 	pos += 2;
993 	if (ie + ie_len < pos + 4 * cnt) {
994 		if (ie + ie_len != pos)
995 			goto err;
996 		goto exit;
997 	}
998 	info->akm_cnt = cnt;
999 	info->akm_list = (u8 *)pos;
1000 	pos += 4 * cnt;
1001 
1002 	/* RSN cap */
1003 	if (ie + ie_len < pos + 2) {
1004 		if (ie + ie_len != pos)
1005 			goto err;
1006 		goto exit;
1007 	}
1008 	info->cap = (u8 *)pos;
1009 	pos += 2;
1010 
1011 	/* PMKID */
1012 	if (ie + ie_len < pos + 2) {
1013 		if (ie + ie_len != pos)
1014 			goto err;
1015 		goto exit;
1016 	}
1017 	cnt = RTW_GET_LE16(pos);
1018 	pos += 2;
1019 	if (ie + ie_len < pos + 16 * cnt)
1020 		goto err;
1021 	info->pmkid_cnt = cnt;
1022 	info->pmkid_list = (u8 *)pos;
1023 	pos += 16 * cnt;
1024 
1025 	/* Group Mgmt CS */
1026 	if (ie + ie_len < pos + 4) {
1027 		if (ie + ie_len != pos)
1028 			goto err;
1029 		goto exit;
1030 	}
1031 	info->gmcs = (u8 *)pos;
1032 
1033 exit:
1034 	return _SUCCESS;
1035 
1036 err:
1037 	info->err = 1;
1038 	return _FAIL;
1039 }
1040 
rtw_parse_wpa2_ie(u8 * rsn_ie,int rsn_ie_len,int * group_cipher,int * pairwise_cipher,int * gmcs,u32 * akm,u8 * mfp_opt,u8 * spp_opt)1041 int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
1042 	int *pairwise_cipher, int *gmcs, u32 *akm, u8 *mfp_opt, u8 *spp_opt)
1043 {
1044 	struct rsne_info info;
1045 	int i, ret = _SUCCESS;
1046 
1047 	ret = rtw_rsne_info_parse(rsn_ie, rsn_ie_len, &info);
1048 	if (ret != _SUCCESS)
1049 		goto exit;
1050 
1051 	if (group_cipher) {
1052 		if (info.gcs)
1053 			*group_cipher = rtw_get_rsn_cipher_suite(info.gcs);
1054 		else
1055 			*group_cipher = 0;
1056 	}
1057 
1058 	if (pairwise_cipher) {
1059 		*pairwise_cipher = 0;
1060 		if (info.pcs_list) {
1061 			for (i = 0; i < info.pcs_cnt; i++)
1062 				*pairwise_cipher |= rtw_get_rsn_cipher_suite(info.pcs_list + 4 * i);
1063 		}
1064 	}
1065 
1066 	if (gmcs) {
1067 		if (info.gmcs)
1068 			*gmcs = rtw_get_rsn_cipher_suite(info.gmcs);
1069 		else
1070 			*gmcs = WPA_CIPHER_BIP_CMAC_128; /* default value when absent */
1071 	}
1072 
1073 	if (akm) {
1074 		*akm = 0;
1075 		if (info.akm_list) {
1076 			for (i = 0; i < info.akm_cnt; i++)
1077 				*akm |= rtw_get_akm_suite_bitmap(info.akm_list + 4 * i);
1078 		}
1079 	}
1080 
1081 	if (mfp_opt) {
1082 		*mfp_opt = MFP_NO;
1083 		if (info.cap)
1084 			*mfp_opt = GET_RSN_CAP_MFP_OPTION(info.cap);
1085 	}
1086 
1087 	if (spp_opt) {
1088 		*spp_opt = 0;
1089 		if (info.cap)
1090 			*spp_opt = GET_RSN_CAP_SPP_OPT(info.cap);
1091 	}
1092 
1093 exit:
1094 	return ret;
1095 }
1096 
1097 /* #ifdef CONFIG_WAPI_SUPPORT */
rtw_get_wapi_ie(u8 * in_ie,uint in_len,u8 * wapi_ie,u16 * wapi_len)1098 int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len)
1099 {
1100 	int len = 0;
1101 	u8 authmode;
1102 	uint	cnt;
1103 	u8 wapi_oui1[4] = {0x0, 0x14, 0x72, 0x01};
1104 	u8 wapi_oui2[4] = {0x0, 0x14, 0x72, 0x02};
1105 
1106 
1107 	if (wapi_len)
1108 		*wapi_len = 0;
1109 
1110 	if (!in_ie || in_len <= 0)
1111 		return len;
1112 
1113 	cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
1114 
1115 	while (cnt < in_len) {
1116 		authmode = in_ie[cnt];
1117 
1118 		/* if(authmode==_WAPI_IE_) */
1119 		if (authmode == _WAPI_IE_ && (_rtw_memcmp(&in_ie[cnt + 6], wapi_oui1, 4) == _TRUE ||
1120 			_rtw_memcmp(&in_ie[cnt + 6], wapi_oui2, 4) == _TRUE)) {
1121 			if (wapi_ie)
1122 				_rtw_memcpy(wapi_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1123 
1124 			if (wapi_len)
1125 				*wapi_len = in_ie[cnt + 1] + 2;
1126 
1127 			cnt += in_ie[cnt + 1] + 2; /* get next */
1128 		} else {
1129 			cnt += in_ie[cnt + 1] + 2; /* get next */
1130 		}
1131 	}
1132 
1133 	if (wapi_len)
1134 		len = *wapi_len;
1135 
1136 
1137 	return len;
1138 
1139 }
1140 /* #endif */
1141 
rtw_get_sec_ie(u8 * in_ie,uint in_len,u8 * rsn_ie,u16 * rsn_len,u8 * wpa_ie,u16 * wpa_len)1142 int rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len)
1143 {
1144 	u8 authmode, sec_idx;
1145 	u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
1146 	uint	cnt;
1147 
1148 
1149 	/* Search required WPA or WPA2 IE and copy to sec_ie[ ] */
1150 
1151 	cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
1152 
1153 	sec_idx = 0;
1154 
1155 	while (cnt < in_len) {
1156 		authmode = in_ie[cnt];
1157 
1158 		if ((authmode == _WPA_IE_ID_) && (_rtw_memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4) == _TRUE)) {
1159 
1160 			if (wpa_ie)
1161 				_rtw_memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1162 
1163 			*wpa_len = in_ie[cnt + 1] + 2;
1164 			cnt += in_ie[cnt + 1] + 2; /* get next */
1165 		} else {
1166 			if (authmode == _WPA2_IE_ID_) {
1167 
1168 				if (rsn_ie)
1169 					_rtw_memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1170 
1171 				*rsn_len = in_ie[cnt + 1] + 2;
1172 				cnt += in_ie[cnt + 1] + 2; /* get next */
1173 			} else {
1174 				cnt += in_ie[cnt + 1] + 2; /* get next */
1175 			}
1176 		}
1177 
1178 	}
1179 
1180 
1181 	return *rsn_len + *wpa_len;
1182 
1183 }
1184 
rtw_is_wps_ie(u8 * ie_ptr,uint * wps_ielen)1185 u8 rtw_is_wps_ie(u8 *ie_ptr, uint *wps_ielen)
1186 {
1187 	u8 match = _FALSE;
1188 	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
1189 
1190 	if (ie_ptr == NULL)
1191 		return match;
1192 
1193 	eid = ie_ptr[0];
1194 
1195 	if ((eid == _WPA_IE_ID_) && (_rtw_memcmp(&ie_ptr[2], wps_oui, 4) == _TRUE)) {
1196 		/* RTW_INFO("==> found WPS_IE.....\n"); */
1197 		*wps_ielen = ie_ptr[1] + 2;
1198 		match = _TRUE;
1199 	}
1200 	return match;
1201 }
1202 
rtw_get_wps_ie_from_scan_queue(u8 * in_ie,uint in_len,u8 * wps_ie,uint * wps_ielen,enum bss_type frame_type)1203 u8 *rtw_get_wps_ie_from_scan_queue(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen, enum bss_type frame_type)
1204 {
1205 	u8	*wps = NULL;
1206 
1207 	RTW_INFO("[%s] frame_type = %d\n", __FUNCTION__, frame_type);
1208 	switch (frame_type) {
1209 	case BSS_TYPE_BCN:
1210 	case BSS_TYPE_PROB_RSP: {
1211 		/*	Beacon or Probe Response */
1212 		wps = rtw_get_wps_ie(in_ie + _PROBERSP_IE_OFFSET_, in_len - _PROBERSP_IE_OFFSET_, wps_ie, wps_ielen);
1213 		break;
1214 	}
1215 	case BSS_TYPE_PROB_REQ: {
1216 		/*	Probe Request */
1217 		wps = rtw_get_wps_ie(in_ie + _PROBEREQ_IE_OFFSET_ , in_len - _PROBEREQ_IE_OFFSET_ , wps_ie, wps_ielen);
1218 		break;
1219 	}
1220 	default:
1221 	case BSS_TYPE_UNDEF:
1222 		break;
1223 	}
1224 	return wps;
1225 }
1226 
1227 /**
1228  * rtw_get_wps_ie - Search WPS IE from a series of IEs
1229  * @in_ie: Address of IEs to search
1230  * @in_len: Length limit from in_ie
1231  * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie
1232  * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE
1233  *
1234  * Returns: The address of the WPS IE found, or NULL
1235  */
rtw_get_wps_ie(const u8 * in_ie,uint in_len,u8 * wps_ie,uint * wps_ielen)1236 u8 *rtw_get_wps_ie(const u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
1237 {
1238 	uint cnt;
1239 	const u8 *wpsie_ptr = NULL;
1240 	u8 eid, wps_oui[4] = {0x00, 0x50, 0xf2, 0x04};
1241 
1242 	if (wps_ielen)
1243 		*wps_ielen = 0;
1244 
1245 	if (!in_ie) {
1246 		rtw_warn_on(1);
1247 		return (u8 *)wpsie_ptr;
1248 	}
1249 
1250 	if (in_len <= 0)
1251 		return (u8 *)wpsie_ptr;
1252 
1253 	cnt = 0;
1254 
1255 	while (cnt + 1 + 4 < in_len) {
1256 		eid = in_ie[cnt];
1257 
1258 		if (cnt + 1 + 4 >= MAX_IE_SZ) {
1259 			rtw_warn_on(1);
1260 			return NULL;
1261 		}
1262 
1263 		if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], wps_oui, 4) == _TRUE) {
1264 			wpsie_ptr = in_ie + cnt;
1265 
1266 			if (wps_ie)
1267 				_rtw_memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1268 
1269 			if (wps_ielen)
1270 				*wps_ielen = in_ie[cnt + 1] + 2;
1271 
1272 			break;
1273 		} else
1274 			cnt += in_ie[cnt + 1] + 2;
1275 
1276 	}
1277 
1278 	return (u8 *)wpsie_ptr;
1279 }
1280 
1281 /**
1282  * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE
1283  * @wps_ie: Address of WPS IE to search
1284  * @wps_ielen: Length limit from wps_ie
1285  * @target_attr_id: The attribute ID of WPS attribute to search
1286  * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr
1287  * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute
1288  *
1289  * Returns: the address of the specific WPS attribute found, or NULL
1290  */
rtw_get_wps_attr(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_attr,u32 * len_attr)1291 u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_attr, u32 *len_attr)
1292 {
1293 	u8 *attr_ptr = NULL;
1294 	u8 *target_attr_ptr = NULL;
1295 	u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
1296 
1297 	if (len_attr)
1298 		*len_attr = 0;
1299 
1300 	if ((wps_ie[0] != _VENDOR_SPECIFIC_IE_) ||
1301 	    (_rtw_memcmp(wps_ie + 2, wps_oui , 4) != _TRUE))
1302 		return attr_ptr;
1303 
1304 	/* 6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
1305 	attr_ptr = wps_ie + 6; /* goto first attr */
1306 
1307 	while (attr_ptr - wps_ie < wps_ielen) {
1308 		/* 4 = 2(Attribute ID) + 2(Length) */
1309 		u16 attr_id = RTW_GET_BE16(attr_ptr);
1310 		u16 attr_data_len = RTW_GET_BE16(attr_ptr + 2);
1311 		u16 attr_len = attr_data_len + 4;
1312 
1313 		/* RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __FUNCTION__, attr_ptr, attr_id, attr_data_len); */
1314 		if (attr_id == target_attr_id) {
1315 			target_attr_ptr = attr_ptr;
1316 
1317 			if (buf_attr)
1318 				_rtw_memcpy(buf_attr, attr_ptr, attr_len);
1319 
1320 			if (len_attr)
1321 				*len_attr = attr_len;
1322 
1323 			break;
1324 		} else {
1325 			attr_ptr += attr_len; /* goto next */
1326 		}
1327 
1328 	}
1329 
1330 	return target_attr_ptr;
1331 }
1332 
1333 /**
1334  * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE
1335  * @wps_ie: Address of WPS IE to search
1336  * @wps_ielen: Length limit from wps_ie
1337  * @target_attr_id: The attribute ID of WPS attribute to search
1338  * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content
1339  *               If len_content is NULL, only copy one byte.
1340  * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content
1341  *
1342  * Returns: the address of the specific WPS attribute content found, or NULL
1343  */
rtw_get_wps_attr_content(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_content,uint * len_content)1344 u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_content, uint *len_content)
1345 {
1346 	u8 *attr_ptr;
1347 	u32 attr_len;
1348 
1349 	attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len);
1350 
1351 	if (attr_ptr && attr_len) {
1352 		if (len_content) {
1353 			if ((buf_content && (*len_content > (attr_len - 4))) || !buf_content)
1354 				*len_content = attr_len - 4;
1355 		}
1356 
1357 		if (len_content && buf_content) {
1358 			_rtw_memcpy(buf_content, attr_ptr + 4, *len_content);
1359 		} else if (buf_content) {
1360 			_rtw_memcpy(buf_content, attr_ptr + 4, 1);
1361 		}
1362 
1363 		return attr_ptr + 4;
1364 	}
1365 
1366 	if (len_content)
1367 		*len_content = 0;
1368 
1369 	return NULL;
1370 }
1371 
1372 /* OWE */
1373 
1374 /**
1375  * rtw_get_OWE_ie - Search OWE IE from a series of IEs
1376  * @in_ie: Address of IEs to search
1377  * @in_len: Length limit from in_ie
1378  * @wps_ie: If not NULL and OWE IE is found, OWE IE will be copied to the buf starting from owe_ie
1379  * @wps_ielen: If not NULL and OWE IE is found, will set to the length of the entire OWE IE
1380  *
1381  * Returns: The address of the OWE IE found, or NULL
1382  */
rtw_get_owe_ie(const u8 * in_ie,uint in_len,u8 * owe_ie,uint * owe_ielen)1383 u8 *rtw_get_owe_ie(const u8 *in_ie, uint in_len, u8 *owe_ie, uint *owe_ielen)
1384 {
1385 	uint cnt;
1386 	const u8 *oweie_ptr = NULL;
1387 	u8 eid;
1388 
1389 	if (owe_ielen)
1390 		*owe_ielen = 0;
1391 
1392 	if (!in_ie) {
1393 		rtw_warn_on(1);
1394 		return (u8 *)oweie_ptr;
1395 	}
1396 
1397 	if (in_len <= 0)
1398 		return (u8 *)oweie_ptr;
1399 
1400 	cnt = 0;
1401 
1402 	while (cnt + 1 + 4 < in_len) {
1403 		eid = in_ie[cnt];
1404 
1405 		if (cnt + 1 + 4 >= MAX_IE_SZ) {
1406 			rtw_warn_on(1);
1407 			return NULL;
1408 		}
1409 
1410 		if ((eid == WLAN_EID_EXTENSION) && (in_ie[cnt + 2] == WLAN_EID_EXT_OWE_DH_PARAM)) {
1411 			oweie_ptr = in_ie + cnt;
1412 
1413 			if (owe_ie)
1414 				_rtw_memcpy(owe_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1415 
1416 			if (owe_ielen)
1417 				*owe_ielen = in_ie[cnt + 1] + 2;
1418 
1419 			break;
1420 		} else
1421 			cnt += in_ie[cnt + 1] + 2;
1422 
1423 	}
1424 
1425 	return (u8 *)oweie_ptr;
1426 }
1427 
1428 /* Add extended capabilities element infomation into ext_cap_data of driver */
rtw_add_ext_cap_info(u8 * ext_cap_data,u8 * ext_cap_data_len,u8 cap_info)1429 void rtw_add_ext_cap_info(u8 *ext_cap_data, u8 *ext_cap_data_len, u8 cap_info)
1430 {
1431 	u8 byte_offset = cap_info >> 3;
1432 	u8 bit_offset = cap_info % 8;
1433 
1434 	ext_cap_data[byte_offset] |= BIT(bit_offset);
1435 
1436 	/* Enlarge the length of EXT_CAP_IE */
1437 	if (byte_offset + 1 > *ext_cap_data_len)
1438 		*ext_cap_data_len = byte_offset + 1;
1439 
1440 	#ifdef DBG_EXT_CAP_IE
1441 	RTW_INFO("%s : cap_info = %u, byte_offset = %u, bit_offset = %u, ext_cap_data_len = %u\n", \
1442 			__func__, cap_info, byte_offset, bit_offset, *ext_cap_data_len);
1443 	#endif
1444 }
1445 
1446 /* Remvoe extended capabilities element infomation from ext_cap_data of driver */
rtw_remove_ext_cap_info(u8 * ext_cap_data,u8 * ext_cap_data_len,u8 cap_info)1447 void rtw_remove_ext_cap_info(u8 *ext_cap_data, u8 *ext_cap_data_len, u8 cap_info)
1448 {
1449 	u8 byte_offset = cap_info >> 3;
1450 	u8 bit_offset = cap_info % 8;
1451 	u8 i, max_len = 0;
1452 
1453 	ext_cap_data[byte_offset] &= (~BIT(bit_offset));
1454 
1455 	/* Reduce the length of EXT_CAP_IE */
1456 	for (i = 0; i < WLAN_EID_EXT_CAP_MAX_LEN; i++) {
1457 		if (ext_cap_data[i] != 0x0)
1458 			max_len = i + 1;
1459 	}
1460 	*ext_cap_data_len = max_len;
1461 
1462 	#ifdef DBG_EXT_CAP_IE
1463 	RTW_INFO("%s : cap_info = %u, byte_offset = %u, bit_offset = %u, ext_cap_data_len = %u\n", \
1464 			__func__, cap_info, byte_offset, bit_offset, *ext_cap_data_len);
1465 	#endif
1466 }
1467 
1468 /**
1469  * rtw_update_ext_cap_ie - add/update/remove the extended capabilities element of frame
1470  *
1471  * @ext_cap_data: from &(mlme_priv->ext_capab_ie_data)
1472  * @ext_cap_data_len: length of ext_cap_data
1473  * @ies: address of ies, e.g. pnetwork->IEs
1474  * @ies_len: address of length of ies, e.g. &(pnetwork->IELength)
1475  * @ies_offset: offset of ies, e.g. _BEACON_IE_OFFSET_
1476  */
rtw_update_ext_cap_ie(u8 * ext_cap_data,u8 ext_cap_data_len,u8 * ies,u32 * ies_len,u8 ies_offset)1477 u8 rtw_update_ext_cap_ie(u8 *ext_cap_data, u8 ext_cap_data_len, u8 *ies, u32 *ies_len, u8 ies_offset)
1478 {
1479 	u8 *extcap_ie;
1480 	uint extcap_len_field = 0;
1481 	uint ie_len = 0;
1482 
1483 	if (ext_cap_data_len != 0) {
1484 		extcap_ie = rtw_get_ie(ies + ies_offset, WLAN_EID_EXT_CAP, &extcap_len_field, *ies_len - ies_offset);
1485 
1486 		if (extcap_ie == NULL) {
1487 			rtw_set_ie(ies + *ies_len, WLAN_EID_EXT_CAP, ext_cap_data_len, ext_cap_data, &ie_len);
1488 			*ies_len += ie_len;
1489 		} else {
1490 			rtw_ies_update_ie(ies, ies_len, ies_offset, WLAN_EID_EXT_CAP, ext_cap_data, ext_cap_data_len);
1491 		}
1492 	} else {
1493 		rtw_ies_remove_ie(ies, ies_len, ies_offset, WLAN_EID_EXT_CAP, NULL, 0);
1494 	}
1495 
1496 	return _SUCCESS;
1497 }
1498 
rtw_parse_ext_cap_ie(u8 * ext_cap_data,u8 * ext_cap_data_len,u8 * ies,u32 ies_len,u8 ies_offset)1499 void rtw_parse_ext_cap_ie(u8 *ext_cap_data, u8 *ext_cap_data_len, u8 *ies, u32 ies_len, u8 ies_offset)
1500 {
1501 	u8 *extcap_ie;
1502 	uint extcap_len_field = 0;
1503 	u8 i;
1504 
1505 	extcap_ie = rtw_get_ie(ies + ies_offset, WLAN_EID_EXT_CAP, &extcap_len_field, ies_len - ies_offset);
1506 
1507 	if (extcap_ie != NULL) {
1508 		extcap_ie = extcap_ie + 2; /* element id and length filed */
1509 		if (*ext_cap_data_len == 0) {
1510 			_rtw_memcpy(ext_cap_data, extcap_ie, extcap_len_field);
1511 			*ext_cap_data_len = extcap_len_field;
1512 		} else {
1513 			for (i = 0; i < extcap_len_field; i++)
1514 				ext_cap_data[i] |= extcap_ie[i];
1515 		}
1516 
1517 		#ifdef DBG_EXT_CAP_IE
1518 		for (i = 0; i < extcap_len_field; i++)
1519 			RTW_INFO("%s : Parse extended capabilties[%u] = 0x%x\n", __func__, i, extcap_ie[i]);
1520 		#endif
1521 	}
1522 }
1523 
rtw_ieee802_11_parse_vendor_specific(u8 * pos,uint elen,struct rtw_ieee802_11_elems * elems,int show_errors)1524 static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen,
1525 		struct rtw_ieee802_11_elems *elems,
1526 		int show_errors)
1527 {
1528 	unsigned int oui;
1529 
1530 	/* first 3 bytes in vendor specific information element are the IEEE
1531 	 * OUI of the vendor. The following byte is used a vendor specific
1532 	 * sub-type. */
1533 	if (elen < 4) {
1534 		if (show_errors) {
1535 			RTW_INFO("short vendor specific "
1536 				 "information element ignored (len=%lu)\n",
1537 				 (unsigned long) elen);
1538 		}
1539 		return -1;
1540 	}
1541 
1542 	oui = RTW_GET_BE24(pos);
1543 	switch (oui) {
1544 	case OUI_MICROSOFT:
1545 		/* Microsoft/Wi-Fi information elements are further typed and
1546 		 * subtyped */
1547 		switch (pos[3]) {
1548 		case 1:
1549 			/* Microsoft OUI (00:50:F2) with OUI Type 1:
1550 			 * real WPA information element */
1551 			elems->wpa_ie = pos;
1552 			elems->wpa_ie_len = elen;
1553 			break;
1554 		case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
1555 			if (elen < 5) {
1556 				RTW_DBG("short WME "
1557 					"information element ignored "
1558 					"(len=%lu)\n",
1559 					(unsigned long) elen);
1560 				return -1;
1561 			}
1562 			switch (pos[4]) {
1563 			case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
1564 			case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
1565 				elems->wme = pos;
1566 				elems->wme_len = elen;
1567 				break;
1568 			case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
1569 				elems->wme_tspec = pos;
1570 				elems->wme_tspec_len = elen;
1571 				break;
1572 			default:
1573 				RTW_DBG("unknown WME "
1574 					"information element ignored "
1575 					"(subtype=%d len=%lu)\n",
1576 					pos[4], (unsigned long) elen);
1577 				return -1;
1578 			}
1579 			break;
1580 		case 4:
1581 			/* Wi-Fi Protected Setup (WPS) IE */
1582 			elems->wps_ie = pos;
1583 			elems->wps_ie_len = elen;
1584 			break;
1585 		default:
1586 			RTW_DBG("Unknown Microsoft "
1587 				"information element ignored "
1588 				"(type=%d len=%lu)\n",
1589 				pos[3], (unsigned long) elen);
1590 			return -1;
1591 		}
1592 		break;
1593 
1594 	case OUI_BROADCOM:
1595 		switch (pos[3]) {
1596 		case VENDOR_HT_CAPAB_OUI_TYPE:
1597 			elems->vendor_ht_cap = pos;
1598 			elems->vendor_ht_cap_len = elen;
1599 			break;
1600 		default:
1601 			RTW_DBG("Unknown Broadcom "
1602 				"information element ignored "
1603 				"(type=%d len=%lu)\n",
1604 				pos[3], (unsigned long) elen);
1605 			return -1;
1606 		}
1607 		break;
1608 #ifdef CONFIG_RTW_TOKEN_BASED_XMIT
1609 	case OUI_REALTEK:
1610 		if (elen == 8) {  // TBTX capable IE length is 8
1611 			elems->tbtx_cap = pos;
1612 			elems->tbtx_cap_len = elen;
1613 		}
1614 		break;
1615 #endif
1616 	default:
1617 		RTW_DBG("unknown vendor specific information "
1618 			"element ignored (vendor OUI %02x:%02x:%02x "
1619 			"len=%lu)\n",
1620 			pos[0], pos[1], pos[2], (unsigned long) elen);
1621 		return -1;
1622 	}
1623 
1624 	return 0;
1625 
1626 }
1627 
1628 /**
1629  * ieee802_11_parse_elems - Parse information elements in management frames
1630  * @start: Pointer to the start of IEs
1631  * @len: Length of IE buffer in octets
1632  * @elems: Data structure for parsed elements
1633  * @show_errors: Whether to show parsing errors in debug log
1634  * Returns: Parsing result
1635  */
rtw_ieee802_11_parse_elems(u8 * start,uint len,struct rtw_ieee802_11_elems * elems,int show_errors)1636 ParseRes rtw_ieee802_11_parse_elems(u8 *start, uint len,
1637 				    struct rtw_ieee802_11_elems *elems,
1638 				    int show_errors)
1639 {
1640 	uint left = len;
1641 	u8 *pos = start;
1642 	int unknown = 0;
1643 
1644 	_rtw_memset(elems, 0, sizeof(*elems));
1645 
1646 	while (left >= 2) {
1647 		u8 id, elen;
1648 
1649 		id = *pos++;
1650 		elen = *pos++;
1651 		left -= 2;
1652 
1653 		if (elen > left) {
1654 			if (show_errors) {
1655 				RTW_INFO("IEEE 802.11 element "
1656 					 "parse failed (id=%d elen=%d "
1657 					 "left=%lu)\n",
1658 					 id, elen, (unsigned long) left);
1659 			}
1660 			return ParseFailed;
1661 		}
1662 
1663 		switch (id) {
1664 		case WLAN_EID_SSID:
1665 			elems->ssid = pos;
1666 			elems->ssid_len = elen;
1667 			break;
1668 		case WLAN_EID_SUPP_RATES:
1669 			elems->supp_rates = pos;
1670 			elems->supp_rates_len = elen;
1671 			break;
1672 		case WLAN_EID_FH_PARAMS:
1673 			elems->fh_params = pos;
1674 			elems->fh_params_len = elen;
1675 			break;
1676 		case WLAN_EID_DS_PARAMS:
1677 			elems->ds_params = pos;
1678 			elems->ds_params_len = elen;
1679 			break;
1680 		case WLAN_EID_CF_PARAMS:
1681 			elems->cf_params = pos;
1682 			elems->cf_params_len = elen;
1683 			break;
1684 		case WLAN_EID_TIM:
1685 			elems->tim = pos;
1686 			elems->tim_len = elen;
1687 			break;
1688 		case WLAN_EID_IBSS_PARAMS:
1689 			elems->ibss_params = pos;
1690 			elems->ibss_params_len = elen;
1691 			break;
1692 		case WLAN_EID_CHALLENGE:
1693 			elems->challenge = pos;
1694 			elems->challenge_len = elen;
1695 			break;
1696 		case WLAN_EID_ERP_INFO:
1697 			elems->erp_info = pos;
1698 			elems->erp_info_len = elen;
1699 			break;
1700 		case WLAN_EID_EXT_SUPP_RATES:
1701 			elems->ext_supp_rates = pos;
1702 			elems->ext_supp_rates_len = elen;
1703 			break;
1704 		case WLAN_EID_VENDOR_SPECIFIC:
1705 			if (rtw_ieee802_11_parse_vendor_specific(pos, elen,
1706 					elems,
1707 					show_errors))
1708 				unknown++;
1709 			break;
1710 		case WLAN_EID_RSN:
1711 			elems->rsn_ie = pos;
1712 			elems->rsn_ie_len = elen;
1713 			break;
1714 		case WLAN_EID_PWR_CAPABILITY:
1715 			elems->power_cap = pos;
1716 			elems->power_cap_len = elen;
1717 			break;
1718 		case WLAN_EID_SUPPORTED_CHANNELS:
1719 			elems->supp_channels = pos;
1720 			elems->supp_channels_len = elen;
1721 			break;
1722 		case WLAN_EID_MOBILITY_DOMAIN:
1723 			elems->mdie = pos;
1724 			elems->mdie_len = elen;
1725 			break;
1726 		case WLAN_EID_FAST_BSS_TRANSITION:
1727 			elems->ftie = pos;
1728 			elems->ftie_len = elen;
1729 			break;
1730 		case WLAN_EID_TIMEOUT_INTERVAL:
1731 			elems->timeout_int = pos;
1732 			elems->timeout_int_len = elen;
1733 			break;
1734 		case WLAN_EID_HT_CAP:
1735 			elems->ht_capabilities = pos;
1736 			elems->ht_capabilities_len = elen;
1737 			break;
1738 		case WLAN_EID_HT_OPERATION:
1739 			elems->ht_operation = pos;
1740 			elems->ht_operation_len = elen;
1741 			break;
1742 		case WLAN_EID_VHT_CAPABILITY:
1743 			elems->vht_capabilities = pos;
1744 			elems->vht_capabilities_len = elen;
1745 			break;
1746 		case WLAN_EID_VHT_OPERATION:
1747 			elems->vht_operation = pos;
1748 			elems->vht_operation_len = elen;
1749 			break;
1750 		case WLAN_EID_VHT_OP_MODE_NOTIFY:
1751 			elems->vht_op_mode_notify = pos;
1752 			elems->vht_op_mode_notify_len = elen;
1753 			break;
1754 		case _EID_RRM_EN_CAP_IE_:
1755 			elems->rm_en_cap = pos;
1756 			elems->rm_en_cap_len = elen;
1757 			break;
1758 #ifdef CONFIG_RTW_MESH
1759 		case WLAN_EID_PREQ:
1760 			elems->preq = pos;
1761 			elems->preq_len = elen;
1762 			break;
1763 		case WLAN_EID_PREP:
1764 			elems->prep = pos;
1765 			elems->prep_len = elen;
1766 			break;
1767 		case WLAN_EID_PERR:
1768 			elems->perr = pos;
1769 			elems->perr_len = elen;
1770 			break;
1771 		case WLAN_EID_RANN:
1772 			elems->rann = pos;
1773 			elems->rann_len = elen;
1774 			break;
1775 #endif
1776 		default:
1777 			unknown++;
1778 			if (!show_errors)
1779 				break;
1780 			RTW_DBG("IEEE 802.11 element parse "
1781 				"ignored unknown element (id=%d elen=%d)\n",
1782 				id, elen);
1783 			break;
1784 		}
1785 
1786 		left -= elen;
1787 		pos += elen;
1788 	}
1789 
1790 	if (left)
1791 		return ParseFailed;
1792 
1793 	return unknown ? ParseUnknown : ParseOK;
1794 
1795 }
1796 
1797 static u8 key_char2num(u8 ch);
key_char2num(u8 ch)1798 static u8 key_char2num(u8 ch)
1799 {
1800 	if ((ch >= '0') && (ch <= '9'))
1801 		return ch - '0';
1802 	else if ((ch >= 'a') && (ch <= 'f'))
1803 		return ch - 'a' + 10;
1804 	else if ((ch >= 'A') && (ch <= 'F'))
1805 		return ch - 'A' + 10;
1806 	else
1807 		return 0xff;
1808 }
1809 
1810 u8 str_2char2num(u8 hch, u8 lch);
str_2char2num(u8 hch,u8 lch)1811 u8 str_2char2num(u8 hch, u8 lch)
1812 {
1813 	return (key_char2num(hch) * 10) + key_char2num(lch);
1814 }
1815 
1816 u8 key_2char2num(u8 hch, u8 lch);
key_2char2num(u8 hch,u8 lch)1817 u8 key_2char2num(u8 hch, u8 lch)
1818 {
1819 	return (key_char2num(hch) << 4) | key_char2num(lch);
1820 }
1821 
1822 void macstr2num(u8 *dst, u8 *src);
macstr2num(u8 * dst,u8 * src)1823 void macstr2num(u8 *dst, u8 *src)
1824 {
1825 	int	jj, kk;
1826 	for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
1827 		dst[jj] = key_2char2num(src[kk], src[kk + 1]);
1828 }
1829 
convert_ip_addr(u8 hch,u8 mch,u8 lch)1830 u8 convert_ip_addr(u8 hch, u8 mch, u8 lch)
1831 {
1832 	return (key_char2num(hch) * 100) + (key_char2num(mch) * 10) + key_char2num(lch);
1833 }
1834 
1835 #ifdef CONFIG_PLATFORM_INTEL_BYT
1836 #define MAC_ADDRESS_LEN 12
1837 
rtw_get_mac_addr_intel(unsigned char * buf)1838 int rtw_get_mac_addr_intel(unsigned char *buf)
1839 {
1840 	int ret = 0;
1841 	int i;
1842 	struct file *fp = NULL;
1843 	mm_segment_t oldfs;
1844 	unsigned char c_mac[MAC_ADDRESS_LEN];
1845 	char fname[] = "/config/wifi/mac.txt";
1846 	int jj, kk;
1847 
1848 	RTW_INFO("%s Enter\n", __FUNCTION__);
1849 
1850 	ret = rtw_retrieve_from_file(fname, c_mac, MAC_ADDRESS_LEN);
1851 	if (ret < MAC_ADDRESS_LEN)
1852 		return -1;
1853 
1854 	for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 2)
1855 		buf[jj] = key_2char2num(c_mac[kk], c_mac[kk + 1]);
1856 
1857 	RTW_INFO("%s: read from file mac address: "MAC_FMT"\n",
1858 		 __FUNCTION__, MAC_ARG(buf));
1859 
1860 	return 0;
1861 }
1862 #endif /* CONFIG_PLATFORM_INTEL_BYT */
1863 
1864 /*
1865  * Description:
1866  * rtw_check_invalid_mac_address:
1867  * This is only used for checking mac address valid or not.
1868  *
1869  * Input:
1870  * adapter: mac_address pointer.
1871  * check_local_bit: check locally bit or not.
1872  *
1873  * Output:
1874  * _TRUE: The mac address is invalid.
1875  * _FALSE: The mac address is valid.
1876  *
1877  * Auther: Isaac.Li
1878  */
rtw_check_invalid_mac_address(u8 * mac_addr,u8 check_local_bit)1879 u8 rtw_check_invalid_mac_address(u8 *mac_addr, u8 check_local_bit)
1880 {
1881 	u8 null_mac_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1882 	u8 multi_mac_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1883 	u8 res = _FALSE;
1884 
1885 	if (_rtw_memcmp(mac_addr, null_mac_addr, ETH_ALEN)) {
1886 		res = _TRUE;
1887 		goto func_exit;
1888 	}
1889 
1890 	if (_rtw_memcmp(mac_addr, multi_mac_addr, ETH_ALEN)) {
1891 		res = _TRUE;
1892 		goto func_exit;
1893 	}
1894 
1895 	if (mac_addr[0] & BIT0) {
1896 		res = _TRUE;
1897 		goto func_exit;
1898 	}
1899 
1900 	if (check_local_bit == _TRUE) {
1901 		if (mac_addr[0] & BIT1) {
1902 			res = _TRUE;
1903 			goto func_exit;
1904 		}
1905 	}
1906 
1907 func_exit:
1908 	return res;
1909 }
1910 
1911 extern char *rtw_initmac;
1912 /**
1913  * rtw_macaddr_cfg - Decide the mac address used
1914  * @out: buf to store mac address decided
1915  * @hw_mac_addr: mac address from efuse/epprom
1916  */
rtw_macaddr_cfg(u8 * out,const u8 * hw_mac_addr)1917 void rtw_macaddr_cfg(u8 *out, const u8 *hw_mac_addr)
1918 {
1919 #define DEFAULT_RANDOM_MACADDR 1
1920 	u8 mac[ETH_ALEN];
1921 
1922 	if (out == NULL) {
1923 		rtw_warn_on(1);
1924 		return;
1925 	}
1926 
1927 	/* Users specify the mac address */
1928 	if (rtw_initmac) {
1929 		int jj, kk;
1930 
1931 		for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
1932 			mac[jj] = key_2char2num(rtw_initmac[kk], rtw_initmac[kk + 1]);
1933 
1934 		goto err_chk;
1935 	}
1936 
1937 	/* platform specified */
1938 #ifdef CONFIG_PLATFORM_INTEL_BYT
1939 	if (rtw_get_mac_addr_intel(mac) == 0)
1940 		goto err_chk;
1941 #endif
1942 
1943 	/* Use the mac address stored in the Efuse */
1944 	if (hw_mac_addr) {
1945 		_rtw_memcpy(mac, hw_mac_addr, ETH_ALEN);
1946 		goto err_chk;
1947 	}
1948 
1949 err_chk:
1950 	if (rtw_check_invalid_mac_address(mac, _TRUE) == _TRUE) {
1951 #if DEFAULT_RANDOM_MACADDR
1952 		RTW_ERR("invalid mac addr:"MAC_FMT", assign random MAC\n", MAC_ARG(mac));
1953 		*((u32 *)(&mac[2])) = rtw_random32();
1954 		mac[0] = 0x00;
1955 		mac[1] = 0xe0;
1956 		mac[2] = 0x4c;
1957 #else
1958 		RTW_ERR("invalid mac addr:"MAC_FMT", assign default one\n", MAC_ARG(mac));
1959 		mac[0] = 0x00;
1960 		mac[1] = 0xe0;
1961 		mac[2] = 0x4c;
1962 		mac[3] = 0x87;
1963 		mac[4] = 0x00;
1964 		mac[5] = 0x00;
1965 #endif
1966 	}
1967 
1968 	_rtw_memcpy(out, mac, ETH_ALEN);
1969 	RTW_INFO("%s mac addr:"MAC_FMT"\n", __func__, MAC_ARG(out));
1970 }
1971 
1972 #ifdef CONFIG_RTW_DEBUG
1973 #ifdef CONFIG_80211N_HT
dump_ht_cap_ie_content(void * sel,const u8 * buf,u32 buf_len)1974 void dump_ht_cap_ie_content(void *sel, const u8 *buf, u32 buf_len)
1975 {
1976 	if (buf_len != HT_CAP_IE_LEN) {
1977 		RTW_PRINT_SEL(sel, "Invalid HT capability IE len:%d != %d\n", buf_len, HT_CAP_IE_LEN);
1978 		return;
1979 	}
1980 
1981 	RTW_PRINT_SEL(sel, "cap_info:%02x%02x:%s\n", *(buf), *(buf + 1)
1982 		, GET_HT_CAP_ELE_CHL_WIDTH(buf) ? " 40MHz" : " 20MHz");
1983 	RTW_PRINT_SEL(sel, "A-MPDU Parameters:"HT_AMPDU_PARA_FMT"\n"
1984 		      , HT_AMPDU_PARA_ARG(HT_CAP_ELE_AMPDU_PARA(buf)));
1985 	RTW_PRINT_SEL(sel, "Supported MCS Set:"HT_SUP_MCS_SET_FMT"\n"
1986 		      , HT_SUP_MCS_SET_ARG(HT_CAP_ELE_SUP_MCS_SET(buf)));
1987 }
1988 
dump_ht_cap_ie(void * sel,const u8 * ie,u32 ie_len)1989 void dump_ht_cap_ie(void *sel, const u8 *ie, u32 ie_len)
1990 {
1991 	const u8 *ht_cap_ie;
1992 	sint ht_cap_ielen;
1993 
1994 	ht_cap_ie = rtw_get_ie(ie, WLAN_EID_HT_CAP, &ht_cap_ielen, ie_len);
1995 	if (!ie || ht_cap_ie != ie)
1996 		return;
1997 
1998 	dump_ht_cap_ie_content(sel, ht_cap_ie + 2, ht_cap_ielen);
1999 }
2000 
2001 const char *const _ht_sc_offset_str[] = {
2002 	"SCN",
2003 	"SCA",
2004 	"SC-RSVD",
2005 	"SCB",
2006 };
2007 
dump_ht_op_ie_content(void * sel,const u8 * buf,u32 buf_len)2008 void dump_ht_op_ie_content(void *sel, const u8 *buf, u32 buf_len)
2009 {
2010 	if (buf_len != HT_OP_IE_LEN) {
2011 		RTW_PRINT_SEL(sel, "Invalid HT operation IE len:%d != %d\n", buf_len, HT_OP_IE_LEN);
2012 		return;
2013 	}
2014 
2015 	RTW_PRINT_SEL(sel, "ch:%u%s %s\n"
2016 		, GET_HT_OP_ELE_PRI_CHL(buf)
2017 		, GET_HT_OP_ELE_STA_CHL_WIDTH(buf) ? "" : " 20MHz only"
2018 		, ht_sc_offset_str(GET_HT_OP_ELE_2ND_CHL_OFFSET(buf))
2019 	);
2020 }
2021 
dump_ht_op_ie(void * sel,const u8 * ie,u32 ie_len)2022 void dump_ht_op_ie(void *sel, const u8 *ie, u32 ie_len)
2023 {
2024 	const u8 *ht_op_ie;
2025 	sint ht_op_ielen;
2026 
2027 	ht_op_ie = rtw_get_ie(ie, WLAN_EID_HT_OPERATION, &ht_op_ielen, ie_len);
2028 	if (!ie || ht_op_ie != ie)
2029 		return;
2030 
2031 	dump_ht_op_ie_content(sel, ht_op_ie + 2, ht_op_ielen);
2032 }
2033 #endif /* CONFIG_80211N_HT */
2034 
dump_wps_ie(void * sel,const u8 * ie,u32 ie_len)2035 void dump_wps_ie(void *sel, const u8 *ie, u32 ie_len)
2036 {
2037 	const u8 *pos = ie;
2038 	u16 id;
2039 	u16 len;
2040 
2041 	const u8 *wps_ie;
2042 	uint wps_ielen;
2043 
2044 	wps_ie = rtw_get_wps_ie(ie, ie_len, NULL, &wps_ielen);
2045 	if (wps_ie != ie || wps_ielen == 0)
2046 		return;
2047 
2048 	pos += 6;
2049 	while (pos - ie + 4 <= ie_len) {
2050 		id = RTW_GET_BE16(pos);
2051 		len = RTW_GET_BE16(pos + 2);
2052 
2053 		RTW_PRINT_SEL(sel, "%s ID:0x%04x, LEN:%u%s\n", __func__, id, len
2054 			, ((pos - ie + 4 + len) <= ie_len) ? "" : "(exceed ie_len)");
2055 
2056 		pos += (4 + len);
2057 	}
2058 }
2059 #endif	/*	CONFIG_RTW_DEBUG	*/
dump_ies(void * sel,const u8 * buf,u32 buf_len)2060 void dump_ies(void *sel, const u8 *buf, u32 buf_len)
2061 {
2062 #ifdef CONFIG_RTW_DEBUG
2063 	const u8 *pos = buf;
2064 	u8 id, len;
2065 
2066 	while (pos - buf + 1 < buf_len) {
2067 		id = *pos;
2068 		len = *(pos + 1);
2069 
2070 		RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u\n", __FUNCTION__, id, len);
2071 #ifdef CONFIG_80211N_HT
2072 		dump_ht_cap_ie(sel, pos, len + 2);
2073 		dump_ht_op_ie(sel, pos, len + 2);
2074 #endif
2075 #ifdef CONFIG_80211AC_VHT
2076 		dump_vht_cap_ie(sel, pos, len + 2);
2077 		dump_vht_op_ie(sel, pos, len + 2);
2078 #endif
2079 		dump_wps_ie(sel, pos, len + 2);
2080 #ifdef CONFIG_P2P
2081 		dump_p2p_ie(sel, pos, len + 2);
2082 #ifdef CONFIG_WFD
2083 		dump_wfd_ie(sel, pos, len + 2);
2084 #endif
2085 #endif
2086 #ifdef CONFIG_RTW_MULTI_AP
2087 		dump_multi_ap_ie(sel, pos, len + 2);
2088 #endif
2089 
2090 		pos += (2 + len);
2091 	}
2092 #endif	/*	CONFIG_RTW_DEBUG	*/
2093 }
2094 
2095 /**
2096  * rtw_ies_get_chbw - get operation ch, bw, offset from IEs of BSS.
2097  * @ies: pointer of the first tlv IE
2098  * @ies_len: length of @ies
2099  * @ch: pointer of ch, used as output
2100  * @bw: pointer of bw, used as output
2101  * @offset: pointer of offset, used as output
2102  * @ht: check HT IEs
2103  * @vht: check VHT IEs, if true imply ht is true
2104  */
rtw_ies_get_chbw(u8 * ies,int ies_len,u8 * ch,u8 * bw,u8 * offset,u8 ht,u8 vht)2105 void rtw_ies_get_chbw(u8 *ies, int ies_len, u8 *ch, u8 *bw, u8 *offset, u8 ht, u8 vht)
2106 {
2107 	u8 *p;
2108 	int	ie_len;
2109 
2110 	*ch = 0;
2111 	*bw = CHANNEL_WIDTH_20;
2112 	*offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2113 
2114 	p = rtw_get_ie(ies, _DSSET_IE_, &ie_len, ies_len);
2115 	if (p && ie_len > 0)
2116 		*ch = *(p + 2);
2117 
2118 #ifdef CONFIG_80211N_HT
2119 	if (ht || vht) {
2120 		u8 *ht_cap_ie, *ht_op_ie;
2121 		int ht_cap_ielen, ht_op_ielen;
2122 
2123 		ht_cap_ie = rtw_get_ie(ies, EID_HTCapability, &ht_cap_ielen, ies_len);
2124 		if (ht_cap_ie && ht_cap_ielen) {
2125 			if (GET_HT_CAP_ELE_CHL_WIDTH(ht_cap_ie + 2))
2126 				*bw = CHANNEL_WIDTH_40;
2127 		}
2128 
2129 		ht_op_ie = rtw_get_ie(ies, EID_HTInfo, &ht_op_ielen, ies_len);
2130 		if (ht_op_ie && ht_op_ielen) {
2131 			if (*ch == 0)
2132 				*ch = GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2);
2133 			else if (*ch != 0 && *ch != GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2)) {
2134 				RTW_INFO("%s ch inconsistent, DSSS:%u, HT primary:%u\n"
2135 					, __func__, *ch, GET_HT_OP_ELE_PRI_CHL(ht_op_ie + 2));
2136 			}
2137 
2138 			if (!GET_HT_OP_ELE_STA_CHL_WIDTH(ht_op_ie + 2))
2139 				*bw = CHANNEL_WIDTH_20;
2140 
2141 			if (*bw == CHANNEL_WIDTH_40) {
2142 				switch (GET_HT_OP_ELE_2ND_CHL_OFFSET(ht_op_ie + 2)) {
2143 				case SCA:
2144 					*offset = HAL_PRIME_CHNL_OFFSET_LOWER;
2145 					break;
2146 				case SCB:
2147 					*offset = HAL_PRIME_CHNL_OFFSET_UPPER;
2148 					break;
2149 				}
2150 			}
2151 		}
2152 
2153 #ifdef CONFIG_80211AC_VHT
2154 		if (vht) {
2155 			u8 *vht_op_ie;
2156 			int vht_op_ielen;
2157 
2158 			vht_op_ie = rtw_get_ie(ies, EID_VHTOperation, &vht_op_ielen, ies_len);
2159 			if (vht_op_ie && vht_op_ielen) {
2160 				if (GET_VHT_OPERATION_ELE_CHL_WIDTH(vht_op_ie + 2) >= 1)
2161 					*bw = CHANNEL_WIDTH_80;
2162 			}
2163 		}
2164 #endif /* CONFIG_80211AC_VHT */
2165 
2166 	}
2167 #endif /* CONFIG_80211N_HT */
2168 }
2169 
rtw_bss_get_chbw(WLAN_BSSID_EX * bss,u8 * ch,u8 * bw,u8 * offset,u8 ht,u8 vht)2170 void rtw_bss_get_chbw(WLAN_BSSID_EX *bss, u8 *ch, u8 *bw, u8 *offset, u8 ht, u8 vht)
2171 {
2172 	rtw_ies_get_chbw(bss->IEs + sizeof(NDIS_802_11_FIXED_IEs)
2173 		, bss->IELength - sizeof(NDIS_802_11_FIXED_IEs)
2174 		, ch, bw, offset, ht, vht);
2175 
2176 	if (*ch == 0)
2177 		*ch = bss->Configuration.DSConfig;
2178 	else if (*ch != bss->Configuration.DSConfig) {
2179 		RTW_INFO("inconsistent ch - ies:%u bss->Configuration.DSConfig:%u\n"
2180 			 , *ch, bss->Configuration.DSConfig);
2181 		*ch = bss->Configuration.DSConfig;
2182 		rtw_warn_on(1);
2183 	}
2184 }
2185 
2186 /**
2187  * rtw_is_chbw_grouped - test if the two ch settings can be grouped together
2188  * @ch_a: ch of set a
2189  * @bw_a: bw of set a
2190  * @offset_a: offset of set a
2191  * @ch_b: ch of set b
2192  * @bw_b: bw of set b
2193  * @offset_b: offset of set b
2194  */
rtw_is_chbw_grouped(u8 ch_a,u8 bw_a,u8 offset_a,u8 ch_b,u8 bw_b,u8 offset_b)2195 bool rtw_is_chbw_grouped(u8 ch_a, u8 bw_a, u8 offset_a
2196 			 , u8 ch_b, u8 bw_b, u8 offset_b)
2197 {
2198 	bool is_grouped = _FALSE;
2199 
2200 	if (ch_a != ch_b) {
2201 		/* ch is different */
2202 		goto exit;
2203 	} else if ((bw_a == CHANNEL_WIDTH_40 || bw_a == CHANNEL_WIDTH_80)
2204 		   && (bw_b == CHANNEL_WIDTH_40 || bw_b == CHANNEL_WIDTH_80)
2205 		  ) {
2206 		if (offset_a != offset_b)
2207 			goto exit;
2208 	}
2209 
2210 	is_grouped = _TRUE;
2211 
2212 exit:
2213 	return is_grouped;
2214 }
2215 
2216 /**
2217  * rtw_sync_chbw - obey g_ch, adjust g_bw, g_offset, bw, offset
2218  * @req_ch: pointer of the request ch, may be modified further
2219  * @req_bw: pointer of the request bw, may be modified further
2220  * @req_offset: pointer of the request offset, may be modified further
2221  * @g_ch: pointer of the ongoing group ch
2222  * @g_bw: pointer of the ongoing group bw, may be modified further
2223  * @g_offset: pointer of the ongoing group offset, may be modified further
2224  */
rtw_sync_chbw(u8 * req_ch,u8 * req_bw,u8 * req_offset,u8 * g_ch,u8 * g_bw,u8 * g_offset)2225 void rtw_sync_chbw(u8 *req_ch, u8 *req_bw, u8 *req_offset
2226 		   , u8 *g_ch, u8 *g_bw, u8 *g_offset)
2227 {
2228 
2229 	*req_ch = *g_ch;
2230 
2231 	if (*req_bw == CHANNEL_WIDTH_80 && *g_ch <= 14) {
2232 		/*2.4G ch, downgrade to 40Mhz */
2233 		*req_bw = CHANNEL_WIDTH_40;
2234 	}
2235 
2236 	switch (*req_bw) {
2237 	case CHANNEL_WIDTH_80:
2238 		if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
2239 			*req_offset = *g_offset;
2240 		else if (*g_bw == CHANNEL_WIDTH_20)
2241 			rtw_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
2242 
2243 		if (*req_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE) {
2244 			RTW_ERR("%s req 80MHz BW without offset, down to 20MHz\n", __func__);
2245 			rtw_warn_on(1);
2246 			*req_bw = CHANNEL_WIDTH_20;
2247 		}
2248 		break;
2249 	case CHANNEL_WIDTH_40:
2250 		if (*g_bw == CHANNEL_WIDTH_40 || *g_bw == CHANNEL_WIDTH_80)
2251 			*req_offset = *g_offset;
2252 		else if (*g_bw == CHANNEL_WIDTH_20)
2253 			rtw_get_offset_by_chbw(*req_ch, *req_bw, req_offset);
2254 
2255 		if (*req_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE) {
2256 			RTW_ERR("%s req 40MHz BW without offset, down to 20MHz\n", __func__);
2257 			rtw_warn_on(1);
2258 			*req_bw = CHANNEL_WIDTH_20;
2259 		}
2260 		break;
2261 	case CHANNEL_WIDTH_20:
2262 		*req_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2263 		break;
2264 	default:
2265 		RTW_ERR("%s req unsupported BW:%u\n", __func__, *req_bw);
2266 		rtw_warn_on(1);
2267 	}
2268 
2269 	if (*req_bw > *g_bw) {
2270 		*g_bw = *req_bw;
2271 		*g_offset = *req_offset;
2272 	}
2273 }
2274 
2275 #ifdef CONFIG_P2P
2276 /**
2277  * rtw_get_p2p_merged_len - Get merged ie length from muitiple p2p ies.
2278  * @in_ie: Pointer of the first p2p ie
2279  * @in_len: Total len of muiltiple p2p ies
2280  * Returns: Length of merged p2p ie length
2281  */
rtw_get_p2p_merged_ies_len(u8 * in_ie,u32 in_len)2282 u32 rtw_get_p2p_merged_ies_len(u8 *in_ie, u32 in_len)
2283 {
2284 	PNDIS_802_11_VARIABLE_IEs	pIE;
2285 	u8 OUI[4] = { 0x50, 0x6f, 0x9a, 0x09 };
2286 	int i = 0;
2287 	int len = 0;
2288 
2289 	while (i < in_len) {
2290 		pIE = (PNDIS_802_11_VARIABLE_IEs)(in_ie + i);
2291 
2292 		if (pIE->ElementID == _VENDOR_SPECIFIC_IE_ && _rtw_memcmp(pIE->data, OUI, 4)) {
2293 			len += pIE->Length - 4; /* 4 is P2P OUI length, don't count it in this loop */
2294 		}
2295 
2296 		i += (pIE->Length + 2);
2297 	}
2298 
2299 	return len + 4;	/* Append P2P OUI length at last. */
2300 }
2301 
2302 /**
2303  * rtw_p2p_merge_ies - Merge muitiple p2p ies into one
2304  * @in_ie: Pointer of the first p2p ie
2305  * @in_len: Total len of muiltiple p2p ies
2306  * @merge_ie: Pointer of merged ie
2307  * Returns: Length of merged p2p ie
2308  */
rtw_p2p_merge_ies(u8 * in_ie,u32 in_len,u8 * merge_ie)2309 int rtw_p2p_merge_ies(u8 *in_ie, u32 in_len, u8 *merge_ie)
2310 {
2311 	PNDIS_802_11_VARIABLE_IEs	pIE;
2312 	u8 len = 0;
2313 	u8 OUI[4] = { 0x50, 0x6f, 0x9a, 0x09 };
2314 	u8 ELOUI[6] = { 0xDD, 0x00, 0x50, 0x6f, 0x9a, 0x09 };	/* EID;Len;OUI, Len would copy at the end of function */
2315 	int i = 0;
2316 
2317 	if (merge_ie != NULL) {
2318 		/* Set first P2P OUI */
2319 		_rtw_memcpy(merge_ie, ELOUI, 6);
2320 		merge_ie += 6;
2321 
2322 		while (i < in_len) {
2323 			pIE = (PNDIS_802_11_VARIABLE_IEs)(in_ie + i);
2324 
2325 			/* Take out the rest of P2P OUIs */
2326 			if (pIE->ElementID == _VENDOR_SPECIFIC_IE_ && _rtw_memcmp(pIE->data, OUI, 4)) {
2327 				_rtw_memcpy(merge_ie, pIE->data + 4, pIE->Length - 4);
2328 				len += pIE->Length - 4;
2329 				merge_ie += pIE->Length - 4;
2330 			}
2331 
2332 			i += (pIE->Length + 2);
2333 		}
2334 
2335 		return len + 4;	/* 4 is for P2P OUI */
2336 
2337 	}
2338 
2339 	return 0;
2340 }
2341 
dump_p2p_ie(void * sel,const u8 * ie,u32 ie_len)2342 void dump_p2p_ie(void *sel, const u8 *ie, u32 ie_len)
2343 {
2344 	const u8 *pos = ie;
2345 	u8 id;
2346 	u16 len;
2347 
2348 	const u8 *p2p_ie;
2349 	uint p2p_ielen;
2350 
2351 	p2p_ie = rtw_get_p2p_ie(ie, ie_len, NULL, &p2p_ielen);
2352 	if (p2p_ie != ie || p2p_ielen == 0)
2353 		return;
2354 
2355 	pos += 6;
2356 	while (pos - ie + 3 <= ie_len) {
2357 		id = *pos;
2358 		len = RTW_GET_LE16(pos + 1);
2359 
2360 		RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u%s\n", __func__, id, len
2361 			, ((pos - ie + 3 + len) <= ie_len) ? "" : "(exceed ie_len)");
2362 
2363 		pos += (3 + len);
2364 	}
2365 }
2366 
2367 /**
2368  * rtw_get_p2p_ie - Search P2P IE from a series of IEs
2369  * @in_ie: Address of IEs to search
2370  * @in_len: Length limit from in_ie
2371  * @p2p_ie: If not NULL and P2P IE is found, P2P IE will be copied to the buf starting from p2p_ie
2372  * @p2p_ielen: If not NULL and P2P IE is found, will set to the length of the entire P2P IE
2373  *
2374  * Returns: The address of the P2P IE found, or NULL
2375  */
rtw_get_p2p_ie(const u8 * in_ie,int in_len,u8 * p2p_ie,uint * p2p_ielen)2376 u8 *rtw_get_p2p_ie(const u8 *in_ie, int in_len, u8 *p2p_ie, uint *p2p_ielen)
2377 {
2378 	uint cnt;
2379 	const u8 *p2p_ie_ptr = NULL;
2380 	u8 eid, p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
2381 
2382 	if (p2p_ielen)
2383 		*p2p_ielen = 0;
2384 
2385 	if (!in_ie || in_len < 0) {
2386 		rtw_warn_on(1);
2387 		return (u8 *)p2p_ie_ptr;
2388 	}
2389 
2390 	if (in_len <= 0)
2391 		return (u8 *)p2p_ie_ptr;
2392 
2393 	cnt = 0;
2394 
2395 	while (cnt + 1 + 4 < in_len) {
2396 		eid = in_ie[cnt];
2397 
2398 		if (cnt + 1 + 4 >= MAX_IE_SZ) {
2399 			rtw_warn_on(1);
2400 			return NULL;
2401 		}
2402 
2403 		if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], p2p_oui, 4) == _TRUE) {
2404 			p2p_ie_ptr = in_ie + cnt;
2405 
2406 			if (p2p_ie)
2407 				_rtw_memcpy(p2p_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
2408 
2409 			if (p2p_ielen)
2410 				*p2p_ielen = in_ie[cnt + 1] + 2;
2411 
2412 			break;
2413 		} else
2414 			cnt += in_ie[cnt + 1] + 2;
2415 
2416 	}
2417 
2418 	return (u8 *)p2p_ie_ptr;
2419 }
2420 
2421 /**
2422  * rtw_get_p2p_attr - Search a specific P2P attribute from a given P2P IE
2423  * @p2p_ie: Address of P2P IE to search
2424  * @p2p_ielen: Length limit from p2p_ie
2425  * @target_attr_id: The attribute ID of P2P attribute to search
2426  * @buf_attr: If not NULL and the P2P attribute is found, P2P attribute will be copied to the buf starting from buf_attr
2427  * @len_attr: If not NULL and the P2P attribute is found, will set to the length of the entire P2P attribute
2428  *
2429  * Returns: the address of the specific WPS attribute found, or NULL
2430  */
rtw_get_p2p_attr(u8 * p2p_ie,uint p2p_ielen,u8 target_attr_id,u8 * buf_attr,u32 * len_attr)2431 u8 *rtw_get_p2p_attr(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_attr, u32 *len_attr)
2432 {
2433 	u8 *attr_ptr = NULL;
2434 	u8 *target_attr_ptr = NULL;
2435 	u8 p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
2436 
2437 	if (len_attr)
2438 		*len_attr = 0;
2439 
2440 	if (!p2p_ie
2441 	    || p2p_ielen <= 6
2442 	    || (p2p_ie[0] != WLAN_EID_VENDOR_SPECIFIC)
2443 	    || (_rtw_memcmp(p2p_ie + 2, p2p_oui, 4) != _TRUE))
2444 		return attr_ptr;
2445 
2446 	/* 6 = 1(Element ID) + 1(Length) + 3 (OUI) + 1(OUI Type) */
2447 	attr_ptr = p2p_ie + 6; /* goto first attr */
2448 
2449 	while ((attr_ptr - p2p_ie + 3) <= p2p_ielen) {
2450 		/* 3 = 1(Attribute ID) + 2(Length) */
2451 		u8 attr_id = *attr_ptr;
2452 		u16 attr_data_len = RTW_GET_LE16(attr_ptr + 1);
2453 		u16 attr_len = attr_data_len + 3;
2454 
2455 		if (0)
2456 			RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len);
2457 
2458 		if ((attr_ptr - p2p_ie + attr_len) > p2p_ielen)
2459 			break;
2460 
2461 		if (attr_id == target_attr_id) {
2462 			target_attr_ptr = attr_ptr;
2463 
2464 			if (buf_attr)
2465 				_rtw_memcpy(buf_attr, attr_ptr, attr_len);
2466 
2467 			if (len_attr)
2468 				*len_attr = attr_len;
2469 
2470 			break;
2471 		} else
2472 			attr_ptr += attr_len;
2473 	}
2474 
2475 	return target_attr_ptr;
2476 }
2477 
2478 /**
2479  * rtw_get_p2p_attr_content - Search a specific P2P attribute content from a given P2P IE
2480  * @p2p_ie: Address of P2P IE to search
2481  * @p2p_ielen: Length limit from p2p_ie
2482  * @target_attr_id: The attribute ID of P2P attribute to search
2483  * @buf_content: If not NULL and the P2P attribute is found, P2P attribute content will be copied to the buf starting from buf_content
2484  *               If len_content is NULL, only copy one byte.
2485  * @len_content: If not NULL and the P2P attribute is found, will set to the length of the P2P attribute content
2486  *
2487  * Returns: the address of the specific P2P attribute content found, or NULL
2488  */
rtw_get_p2p_attr_content(u8 * p2p_ie,uint p2p_ielen,u8 target_attr_id,u8 * buf_content,uint * len_content)2489 u8 *rtw_get_p2p_attr_content(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_content, uint *len_content)
2490 {
2491 	u8 *attr_ptr;
2492 	u32 attr_len;
2493 
2494 	attr_ptr = rtw_get_p2p_attr(p2p_ie, p2p_ielen, target_attr_id, NULL, &attr_len);
2495 
2496 	if (attr_ptr && attr_len) {
2497 		if (len_content) {
2498 			if ((buf_content && (*len_content > (attr_len - 3))) || !buf_content)
2499 				*len_content = attr_len - 3;
2500 		}
2501 
2502 		if (len_content && buf_content) {
2503 			_rtw_memcpy(buf_content, attr_ptr + 3, *len_content);
2504 		} else if (buf_content) {
2505 			_rtw_memcpy(buf_content, attr_ptr + 3, 1);
2506 		}
2507 
2508 		return attr_ptr + 3;
2509 	}
2510 
2511 	if (len_content)
2512 		*len_content = 0;
2513 
2514 	return NULL;
2515 }
2516 
rtw_set_p2p_attr_content(u8 * pbuf,u8 attr_id,u16 attr_len,u8 * pdata_attr)2517 u32 rtw_set_p2p_attr_content(u8 *pbuf, u8 attr_id, u16 attr_len, u8 *pdata_attr)
2518 {
2519 	u32 a_len;
2520 
2521 	*pbuf = attr_id;
2522 
2523 	/* *(u16*)(pbuf + 1) = cpu_to_le16(attr_len); */
2524 	RTW_PUT_LE16(pbuf + 1, attr_len);
2525 
2526 	if (pdata_attr)
2527 		_rtw_memcpy(pbuf + 3, pdata_attr, attr_len);
2528 
2529 	a_len = attr_len + 3;
2530 
2531 	return a_len;
2532 }
2533 
rtw_del_p2p_ie(u8 * ies,uint ies_len_ori,const char * msg)2534 uint rtw_del_p2p_ie(u8 *ies, uint ies_len_ori, const char *msg)
2535 {
2536 #define DBG_DEL_P2P_IE 0
2537 
2538 	u8 *target_ie;
2539 	u32 target_ie_len;
2540 	uint ies_len = ies_len_ori;
2541 	int index = 0;
2542 
2543 	while (1) {
2544 		target_ie = rtw_get_p2p_ie(ies, ies_len, NULL, &target_ie_len);
2545 		if (target_ie && target_ie_len) {
2546 			u8 *next_ie = target_ie + target_ie_len;
2547 			uint remain_len = ies_len - (next_ie - ies);
2548 
2549 			if (DBG_DEL_P2P_IE && msg) {
2550 				RTW_INFO("%s %d before\n", __func__, index);
2551 				dump_ies(RTW_DBGDUMP, ies, ies_len);
2552 
2553 				RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
2554 				RTW_INFO("target_ie:%p, target_ie_len:%u\n", target_ie, target_ie_len);
2555 				RTW_INFO("next_ie:%p, remain_len:%u\n", next_ie, remain_len);
2556 			}
2557 
2558 			_rtw_memmove(target_ie, next_ie, remain_len);
2559 			_rtw_memset(target_ie + remain_len, 0, target_ie_len);
2560 			ies_len -= target_ie_len;
2561 
2562 			if (DBG_DEL_P2P_IE && msg) {
2563 				RTW_INFO("%s %d after\n", __func__, index);
2564 				dump_ies(RTW_DBGDUMP, ies, ies_len);
2565 			}
2566 
2567 			index++;
2568 		} else
2569 			break;
2570 	}
2571 
2572 	return ies_len;
2573 }
2574 
rtw_del_p2p_attr(u8 * ie,uint ielen_ori,u8 attr_id)2575 uint rtw_del_p2p_attr(u8 *ie, uint ielen_ori, u8 attr_id)
2576 {
2577 #define DBG_DEL_P2P_ATTR 0
2578 
2579 	u8 *target_attr;
2580 	u32 target_attr_len;
2581 	uint ielen = ielen_ori;
2582 	int index = 0;
2583 
2584 	while (1) {
2585 		target_attr = rtw_get_p2p_attr(ie, ielen, attr_id, NULL, &target_attr_len);
2586 		if (target_attr && target_attr_len) {
2587 			u8 *next_attr = target_attr + target_attr_len;
2588 			uint remain_len = ielen - (next_attr - ie);
2589 
2590 			if (DBG_DEL_P2P_ATTR) {
2591 				RTW_INFO("%s %d before\n", __func__, index);
2592 				dump_ies(RTW_DBGDUMP, ie, ielen);
2593 
2594 				RTW_INFO("ie:%p, ielen:%u\n", ie, ielen);
2595 				RTW_INFO("target_attr:%p, target_attr_len:%u\n", target_attr, target_attr_len);
2596 				RTW_INFO("next_attr:%p, remain_len:%u\n", next_attr, remain_len);
2597 			}
2598 
2599 			_rtw_memmove(target_attr, next_attr, remain_len);
2600 			_rtw_memset(target_attr + remain_len, 0, target_attr_len);
2601 			*(ie + 1) -= target_attr_len;
2602 			ielen -= target_attr_len;
2603 
2604 			if (DBG_DEL_P2P_ATTR) {
2605 				RTW_INFO("%s %d after\n", __func__, index);
2606 				dump_ies(RTW_DBGDUMP, ie, ielen);
2607 			}
2608 
2609 			index++;
2610 		} else
2611 			break;
2612 	}
2613 
2614 	return ielen;
2615 }
2616 
rtw_bss_ex_get_p2p_ie(WLAN_BSSID_EX * bss_ex,u8 * p2p_ie,uint * p2p_ielen)2617 inline u8 *rtw_bss_ex_get_p2p_ie(WLAN_BSSID_EX *bss_ex, u8 *p2p_ie, uint *p2p_ielen)
2618 {
2619 	return rtw_get_p2p_ie(BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex), p2p_ie, p2p_ielen);
2620 }
2621 
rtw_bss_ex_del_p2p_ie(WLAN_BSSID_EX * bss_ex)2622 void rtw_bss_ex_del_p2p_ie(WLAN_BSSID_EX *bss_ex)
2623 {
2624 #define DBG_BSS_EX_DEL_P2P_IE 0
2625 
2626 	u8 *ies = BSS_EX_TLV_IES(bss_ex);
2627 	uint ies_len_ori = BSS_EX_TLV_IES_LEN(bss_ex);
2628 	uint ies_len;
2629 
2630 	ies_len = rtw_del_p2p_ie(ies, ies_len_ori, DBG_BSS_EX_DEL_P2P_IE ? __func__ : NULL);
2631 	bss_ex->IELength -= ies_len_ori - ies_len;
2632 }
2633 
rtw_bss_ex_del_p2p_attr(WLAN_BSSID_EX * bss_ex,u8 attr_id)2634 void rtw_bss_ex_del_p2p_attr(WLAN_BSSID_EX *bss_ex, u8 attr_id)
2635 {
2636 #define DBG_BSS_EX_DEL_P2P_ATTR 0
2637 
2638 	u8 *ies = BSS_EX_TLV_IES(bss_ex);
2639 	uint ies_len = BSS_EX_TLV_IES_LEN(bss_ex);
2640 
2641 	u8 *ie;
2642 	uint ie_len, ie_len_ori;
2643 
2644 	int index = 0;
2645 
2646 	while (1) {
2647 		ie = rtw_get_p2p_ie(ies, ies_len, NULL, &ie_len_ori);
2648 		if (ie) {
2649 			u8 *next_ie_ori = ie + ie_len_ori;
2650 			uint remain_len = bss_ex->IELength - (next_ie_ori - bss_ex->IEs);
2651 			u8 has_target_attr = 0;
2652 
2653 			if (DBG_BSS_EX_DEL_P2P_ATTR) {
2654 				if (rtw_get_p2p_attr(ie, ie_len_ori, attr_id, NULL, NULL)) {
2655 					RTW_INFO("%s %d before\n", __func__, index);
2656 					dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
2657 
2658 					RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
2659 					RTW_INFO("ie:%p, ie_len_ori:%u\n", ie, ie_len_ori);
2660 					RTW_INFO("next_ie_ori:%p, remain_len:%u\n", next_ie_ori, remain_len);
2661 					has_target_attr = 1;
2662 				}
2663 			}
2664 
2665 			ie_len = rtw_del_p2p_attr(ie, ie_len_ori, attr_id);
2666 			if (ie_len != ie_len_ori) {
2667 				u8 *next_ie = ie + ie_len;
2668 
2669 				_rtw_memmove(next_ie, next_ie_ori, remain_len);
2670 				_rtw_memset(next_ie + remain_len, 0, ie_len_ori - ie_len);
2671 				bss_ex->IELength -= ie_len_ori - ie_len;
2672 
2673 				ies = next_ie;
2674 			} else
2675 				ies = next_ie_ori;
2676 
2677 			if (DBG_BSS_EX_DEL_P2P_ATTR) {
2678 				if (has_target_attr) {
2679 					RTW_INFO("%s %d after\n", __func__, index);
2680 					dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
2681 				}
2682 			}
2683 
2684 			ies_len = remain_len;
2685 
2686 			index++;
2687 		} else
2688 			break;
2689 	}
2690 }
2691 #endif	/*	CONFIG_P2P	*/
2692 
2693 /**
2694  * rtw_get_wfd_ie - Search WFD IE from a series of IEs
2695  * @in_ie: Address of IEs to search
2696  * @in_len: Length limit from in_ie
2697  * @wfd_ie: If not NULL and WFD IE is found, WFD IE will be copied to the buf starting from wfd_ie
2698  * @wfd_ielen: If not NULL and WFD IE is found, will set to the length of the entire WFD IE
2699  *
2700  * Returns: The address of the P2P IE found, or NULL
2701  */
rtw_get_wfd_ie(const u8 * in_ie,int in_len,u8 * wfd_ie,uint * wfd_ielen)2702 u8 *rtw_get_wfd_ie(const u8 *in_ie, int in_len, u8 *wfd_ie, uint *wfd_ielen)
2703 {
2704 	uint cnt;
2705 	const u8 *wfd_ie_ptr = NULL;
2706 	u8 eid, wfd_oui[4] = {0x50, 0x6F, 0x9A, 0x0A};
2707 
2708 	if (wfd_ielen)
2709 		*wfd_ielen = 0;
2710 
2711 	if (!in_ie || in_len < 0) {
2712 		rtw_warn_on(1);
2713 		return (u8 *)wfd_ie_ptr;
2714 	}
2715 
2716 	if (in_len <= 0)
2717 		return (u8 *)wfd_ie_ptr;
2718 
2719 	cnt = 0;
2720 
2721 	while (cnt + 1 + 4 < in_len) {
2722 		eid = in_ie[cnt];
2723 
2724 		if (cnt + 1 + 4 >= MAX_IE_SZ) {
2725 			rtw_warn_on(1);
2726 			return NULL;
2727 		}
2728 
2729 		if (eid == WLAN_EID_VENDOR_SPECIFIC && _rtw_memcmp(&in_ie[cnt + 2], wfd_oui, 4) == _TRUE) {
2730 			wfd_ie_ptr = in_ie + cnt;
2731 
2732 			if (wfd_ie)
2733 				_rtw_memcpy(wfd_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
2734 
2735 			if (wfd_ielen)
2736 				*wfd_ielen = in_ie[cnt + 1] + 2;
2737 
2738 			break;
2739 		} else
2740 			cnt += in_ie[cnt + 1] + 2;
2741 
2742 	}
2743 
2744 	return (u8 *)wfd_ie_ptr;
2745 }
2746 
rtw_del_wfd_ie(u8 * ies,uint ies_len_ori,const char * msg)2747 uint rtw_del_wfd_ie(u8 *ies, uint ies_len_ori, const char *msg)
2748 {
2749 #define DBG_DEL_WFD_IE 0
2750 
2751 	u8 *target_ie;
2752 	u32 target_ie_len;
2753 	uint ies_len = ies_len_ori;
2754 	int index = 0;
2755 
2756 	while (1) {
2757 		target_ie = rtw_get_wfd_ie(ies, ies_len, NULL, &target_ie_len);
2758 		if (target_ie && target_ie_len) {
2759 			u8 *next_ie = target_ie + target_ie_len;
2760 			uint remain_len = ies_len - (next_ie - ies);
2761 
2762 			if (DBG_DEL_WFD_IE && msg) {
2763 				RTW_INFO("%s %d before\n", __func__, index);
2764 				dump_ies(RTW_DBGDUMP, ies, ies_len);
2765 
2766 				RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
2767 				RTW_INFO("target_ie:%p, target_ie_len:%u\n", target_ie, target_ie_len);
2768 				RTW_INFO("next_ie:%p, remain_len:%u\n", next_ie, remain_len);
2769 			}
2770 
2771 			_rtw_memmove(target_ie, next_ie, remain_len);
2772 			_rtw_memset(target_ie + remain_len, 0, target_ie_len);
2773 			ies_len -= target_ie_len;
2774 
2775 			if (DBG_DEL_WFD_IE && msg) {
2776 				RTW_INFO("%s %d after\n", __func__, index);
2777 				dump_ies(RTW_DBGDUMP, ies, ies_len);
2778 			}
2779 
2780 			index++;
2781 		} else
2782 			break;
2783 	}
2784 
2785 	return ies_len;
2786 }
2787 
rtw_bss_ex_del_wfd_ie(WLAN_BSSID_EX * bss_ex)2788 void rtw_bss_ex_del_wfd_ie(WLAN_BSSID_EX *bss_ex)
2789 {
2790 #define DBG_BSS_EX_DEL_WFD_IE 0
2791 	u8 *ies = BSS_EX_TLV_IES(bss_ex);
2792 	uint ies_len_ori = BSS_EX_TLV_IES_LEN(bss_ex);
2793 	uint ies_len;
2794 
2795 	ies_len = rtw_del_wfd_ie(ies, ies_len_ori, DBG_BSS_EX_DEL_WFD_IE ? __func__ : NULL);
2796 	bss_ex->IELength -= ies_len_ori - ies_len;
2797 }
2798 
2799 #ifdef CONFIG_WFD
dump_wfd_ie(void * sel,const u8 * ie,u32 ie_len)2800 void dump_wfd_ie(void *sel, const u8 *ie, u32 ie_len)
2801 {
2802 	const u8 *pos = ie;
2803 	u8 id;
2804 	u16 len;
2805 
2806 	const u8 *wfd_ie;
2807 	uint wfd_ielen;
2808 
2809 	wfd_ie = rtw_get_wfd_ie(ie, ie_len, NULL, &wfd_ielen);
2810 	if (wfd_ie != ie || wfd_ielen == 0)
2811 		return;
2812 
2813 	pos += 6;
2814 	while (pos - ie + 3 <= ie_len) {
2815 		id = *pos;
2816 		len = RTW_GET_BE16(pos + 1);
2817 
2818 		RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u%s\n", __func__, id, len
2819 			, ((pos - ie + 3 + len) <= ie_len) ? "" : "(exceed ie_len)");
2820 
2821 		pos += (3 + len);
2822 	}
2823 }
2824 
2825 /**
2826  * rtw_get_wfd_attr - Search a specific WFD attribute from a given WFD IE
2827  * @wfd_ie: Address of WFD IE to search
2828  * @wfd_ielen: Length limit from wfd_ie
2829  * @target_attr_id: The attribute ID of WFD attribute to search
2830  * @buf_attr: If not NULL and the WFD attribute is found, WFD attribute will be copied to the buf starting from buf_attr
2831  * @len_attr: If not NULL and the WFD attribute is found, will set to the length of the entire WFD attribute
2832  *
2833  * Returns: the address of the specific WPS attribute found, or NULL
2834  */
rtw_get_wfd_attr(u8 * wfd_ie,uint wfd_ielen,u8 target_attr_id,u8 * buf_attr,u32 * len_attr)2835 u8 *rtw_get_wfd_attr(u8 *wfd_ie, uint wfd_ielen, u8 target_attr_id, u8 *buf_attr, u32 *len_attr)
2836 {
2837 	u8 *attr_ptr = NULL;
2838 	u8 *target_attr_ptr = NULL;
2839 	u8 wfd_oui[4] = {0x50, 0x6F, 0x9A, 0x0A};
2840 
2841 	if (len_attr)
2842 		*len_attr = 0;
2843 
2844 	if (!wfd_ie
2845 	    || wfd_ielen <= 6
2846 	    || (wfd_ie[0] != WLAN_EID_VENDOR_SPECIFIC)
2847 	    || (_rtw_memcmp(wfd_ie + 2, wfd_oui, 4) != _TRUE))
2848 		return attr_ptr;
2849 
2850 	/* 6 = 1(Element ID) + 1(Length) + 3 (OUI) + 1(OUI Type) */
2851 	attr_ptr = wfd_ie + 6; /* goto first attr */
2852 
2853 	while ((attr_ptr - wfd_ie + 3) <= wfd_ielen) {
2854 		/* 3 = 1(Attribute ID) + 2(Length) */
2855 		u8 attr_id = *attr_ptr;
2856 		u16 attr_data_len = RTW_GET_BE16(attr_ptr + 1);
2857 		u16 attr_len = attr_data_len + 3;
2858 
2859 		if (0)
2860 			RTW_INFO("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len);
2861 
2862 		if ((attr_ptr - wfd_ie + attr_len) > wfd_ielen)
2863 			break;
2864 
2865 		if (attr_id == target_attr_id) {
2866 			target_attr_ptr = attr_ptr;
2867 
2868 			if (buf_attr)
2869 				_rtw_memcpy(buf_attr, attr_ptr, attr_len);
2870 
2871 			if (len_attr)
2872 				*len_attr = attr_len;
2873 
2874 			break;
2875 		} else
2876 			attr_ptr += attr_len;
2877 	}
2878 
2879 	return target_attr_ptr;
2880 }
2881 
2882 /**
2883  * rtw_get_wfd_attr_content - Search a specific WFD attribute content from a given WFD IE
2884  * @wfd_ie: Address of WFD IE to search
2885  * @wfd_ielen: Length limit from wfd_ie
2886  * @target_attr_id: The attribute ID of WFD attribute to search
2887  * @buf_content: If not NULL and the WFD attribute is found, WFD attribute content will be copied to the buf starting from buf_content
2888  * @len_content: If not NULL and the WFD attribute is found, will set to the length of the WFD attribute content
2889  *
2890  * Returns: the address of the specific WFD attribute content found, or NULL
2891  */
rtw_get_wfd_attr_content(u8 * wfd_ie,uint wfd_ielen,u8 target_attr_id,u8 * buf_content,uint * len_content)2892 u8 *rtw_get_wfd_attr_content(u8 *wfd_ie, uint wfd_ielen, u8 target_attr_id, u8 *buf_content, uint *len_content)
2893 {
2894 	u8 *attr_ptr;
2895 	u32 attr_len;
2896 
2897 	if (len_content)
2898 		*len_content = 0;
2899 
2900 	attr_ptr = rtw_get_wfd_attr(wfd_ie, wfd_ielen, target_attr_id, NULL, &attr_len);
2901 
2902 	if (attr_ptr && attr_len) {
2903 		if (buf_content)
2904 			_rtw_memcpy(buf_content, attr_ptr + 3, attr_len - 3);
2905 
2906 		if (len_content)
2907 			*len_content = attr_len - 3;
2908 
2909 		return attr_ptr + 3;
2910 	}
2911 
2912 	return NULL;
2913 }
2914 
rtw_del_wfd_attr(u8 * ie,uint ielen_ori,u8 attr_id)2915 uint rtw_del_wfd_attr(u8 *ie, uint ielen_ori, u8 attr_id)
2916 {
2917 #define DBG_DEL_WFD_ATTR 0
2918 
2919 	u8 *target_attr;
2920 	u32 target_attr_len;
2921 	uint ielen = ielen_ori;
2922 	int index = 0;
2923 
2924 	while (1) {
2925 		target_attr = rtw_get_wfd_attr(ie, ielen, attr_id, NULL, &target_attr_len);
2926 		if (target_attr && target_attr_len) {
2927 			u8 *next_attr = target_attr + target_attr_len;
2928 			uint remain_len = ielen - (next_attr - ie);
2929 
2930 			if (DBG_DEL_WFD_ATTR) {
2931 				RTW_INFO("%s %d before\n", __func__, index);
2932 				dump_ies(RTW_DBGDUMP, ie, ielen);
2933 
2934 				RTW_INFO("ie:%p, ielen:%u\n", ie, ielen);
2935 				RTW_INFO("target_attr:%p, target_attr_len:%u\n", target_attr, target_attr_len);
2936 				RTW_INFO("next_attr:%p, remain_len:%u\n", next_attr, remain_len);
2937 			}
2938 
2939 			_rtw_memmove(target_attr, next_attr, remain_len);
2940 			_rtw_memset(target_attr + remain_len, 0, target_attr_len);
2941 			*(ie + 1) -= target_attr_len;
2942 			ielen -= target_attr_len;
2943 
2944 			if (DBG_DEL_WFD_ATTR) {
2945 				RTW_INFO("%s %d after\n", __func__, index);
2946 				dump_ies(RTW_DBGDUMP, ie, ielen);
2947 			}
2948 
2949 			index++;
2950 		} else
2951 			break;
2952 	}
2953 
2954 	return ielen;
2955 }
2956 
rtw_bss_ex_get_wfd_ie(WLAN_BSSID_EX * bss_ex,u8 * wfd_ie,uint * wfd_ielen)2957 inline u8 *rtw_bss_ex_get_wfd_ie(WLAN_BSSID_EX *bss_ex, u8 *wfd_ie, uint *wfd_ielen)
2958 {
2959 	return rtw_get_wfd_ie(BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex), wfd_ie, wfd_ielen);
2960 }
2961 
rtw_bss_ex_del_wfd_attr(WLAN_BSSID_EX * bss_ex,u8 attr_id)2962 void rtw_bss_ex_del_wfd_attr(WLAN_BSSID_EX *bss_ex, u8 attr_id)
2963 {
2964 #define DBG_BSS_EX_DEL_WFD_ATTR 0
2965 
2966 	u8 *ies = BSS_EX_TLV_IES(bss_ex);
2967 	uint ies_len = BSS_EX_TLV_IES_LEN(bss_ex);
2968 
2969 	u8 *ie;
2970 	uint ie_len, ie_len_ori;
2971 
2972 	int index = 0;
2973 
2974 	while (1) {
2975 		ie = rtw_get_wfd_ie(ies, ies_len, NULL, &ie_len_ori);
2976 		if (ie) {
2977 			u8 *next_ie_ori = ie + ie_len_ori;
2978 			uint remain_len = bss_ex->IELength - (next_ie_ori - bss_ex->IEs);
2979 			u8 has_target_attr = 0;
2980 
2981 			if (DBG_BSS_EX_DEL_WFD_ATTR) {
2982 				if (rtw_get_wfd_attr(ie, ie_len_ori, attr_id, NULL, NULL)) {
2983 					RTW_INFO("%s %d before\n", __func__, index);
2984 					dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
2985 
2986 					RTW_INFO("ies:%p, ies_len:%u\n", ies, ies_len);
2987 					RTW_INFO("ie:%p, ie_len_ori:%u\n", ie, ie_len_ori);
2988 					RTW_INFO("next_ie_ori:%p, remain_len:%u\n", next_ie_ori, remain_len);
2989 					has_target_attr = 1;
2990 				}
2991 			}
2992 
2993 			ie_len = rtw_del_wfd_attr(ie, ie_len_ori, attr_id);
2994 			if (ie_len != ie_len_ori) {
2995 				u8 *next_ie = ie + ie_len;
2996 
2997 				_rtw_memmove(next_ie, next_ie_ori, remain_len);
2998 				_rtw_memset(next_ie + remain_len, 0, ie_len_ori - ie_len);
2999 				bss_ex->IELength -= ie_len_ori - ie_len;
3000 
3001 				ies = next_ie;
3002 			} else
3003 				ies = next_ie_ori;
3004 
3005 			if (DBG_BSS_EX_DEL_WFD_ATTR) {
3006 				if (has_target_attr) {
3007 					RTW_INFO("%s %d after\n", __func__, index);
3008 					dump_ies(RTW_DBGDUMP, BSS_EX_TLV_IES(bss_ex), BSS_EX_TLV_IES_LEN(bss_ex));
3009 				}
3010 			}
3011 
3012 			ies_len = remain_len;
3013 
3014 			index++;
3015 		} else
3016 			break;
3017 	}
3018 }
3019 #endif /*	CONFIG_WFD	*/
3020 
3021 #ifdef CONFIG_RTW_MULTI_AP
dump_multi_ap_ie(void * sel,const u8 * ie,u32 ie_len)3022 void dump_multi_ap_ie(void *sel, const u8 *ie, u32 ie_len)
3023 {
3024 	const u8 *pos = ie;
3025 	u8 id;
3026 	u8 len;
3027 
3028 	const u8 *multi_ap_ie;
3029 	uint multi_ap_ielen;
3030 
3031 	multi_ap_ie = rtw_get_ie_ex(ie, ie_len, WLAN_EID_VENDOR_SPECIFIC, MULTI_AP_OUI, 4, NULL, &multi_ap_ielen);
3032 	if (multi_ap_ie != ie || multi_ap_ielen == 0)
3033 		return;
3034 
3035 	pos += 6;
3036 	while (pos - ie + 2 <= ie_len) {
3037 		id = *pos;
3038 		len = *(pos + 1);
3039 
3040 		RTW_PRINT_SEL(sel, "%s ID:%u, LEN:%u%s\n", __func__, id, len
3041 			, ((pos - ie + 2 + len) <= ie_len) ? "" : "(exceed ie_len)");
3042 		RTW_DUMP_SEL(sel, pos + 2, len);
3043 
3044 		pos += (2 + len);
3045 	}
3046 }
3047 
3048 /**
3049  * rtw_get_multi_ap_ext - Search Multi-AP IE from a series of IEs and return extension subelement value
3050  * @ies: Address of IEs to search
3051  * @ies_len: Length limit from in_ie
3052  *
3053  * Returns: The address of the target IE found, or NULL
3054  */
rtw_get_multi_ap_ie_ext(const u8 * ies,int ies_len)3055 u8 rtw_get_multi_ap_ie_ext(const u8 *ies, int ies_len)
3056 {
3057 	u8 *ie;
3058 	uint ielen;
3059 	u8 val = 0;
3060 
3061 	ie = rtw_get_ie_ex(ies, ies_len, WLAN_EID_VENDOR_SPECIFIC, MULTI_AP_OUI, 4, NULL, &ielen);
3062 	if (ielen < 9)
3063 		goto exit;
3064 
3065 	if (ie[6] != MULTI_AP_SUB_ELEM_TYPE)
3066 		goto exit;
3067 
3068 	val = ie[8];
3069 
3070 exit:
3071 	return val;
3072 }
3073 
rtw_set_multi_ap_ie_ext(u8 * pbuf,uint * frlen,u8 val)3074 u8 *rtw_set_multi_ap_ie_ext(u8 *pbuf, uint *frlen, u8 val)
3075 {
3076 	u8 cont_len = 7;
3077 
3078 	*pbuf++ = WLAN_EID_VENDOR_SPECIFIC;
3079 	*pbuf++ = cont_len;
3080 	_rtw_memcpy(pbuf, MULTI_AP_OUI, 4);
3081 	pbuf += 4;
3082 	*pbuf++ = MULTI_AP_SUB_ELEM_TYPE;
3083 	*pbuf++ = 1; /* len */
3084 	*pbuf++ = val;
3085 
3086 	if (frlen)
3087 		*frlen = *frlen + (cont_len + 2);
3088 
3089 	return pbuf;
3090 }
3091 #endif /* CONFIG_RTW_MULTI_AP */
3092 
3093 /* Baron adds to avoid FreeBSD warning */
ieee80211_is_empty_essid(const char * essid,int essid_len)3094 int ieee80211_is_empty_essid(const char *essid, int essid_len)
3095 {
3096 	/* Single white space is for Linksys APs */
3097 	if (essid_len == 1 && essid[0] == ' ')
3098 		return 1;
3099 
3100 	/* Otherwise, if the entire essid is 0, we assume it is hidden */
3101 	while (essid_len) {
3102 		essid_len--;
3103 		if (essid[essid_len] != '\0')
3104 			return 0;
3105 	}
3106 
3107 	return 1;
3108 }
3109 
ieee80211_get_hdrlen(u16 fc)3110 int ieee80211_get_hdrlen(u16 fc)
3111 {
3112 	int hdrlen = 24;
3113 
3114 	switch (WLAN_FC_GET_TYPE(fc)) {
3115 	case RTW_IEEE80211_FTYPE_DATA:
3116 		if (fc & RTW_IEEE80211_STYPE_QOS_DATA)
3117 			hdrlen += 2;
3118 		if ((fc & RTW_IEEE80211_FCTL_FROMDS) && (fc & RTW_IEEE80211_FCTL_TODS))
3119 			hdrlen += 6; /* Addr4 */
3120 		break;
3121 	case RTW_IEEE80211_FTYPE_CTL:
3122 		switch (WLAN_FC_GET_STYPE(fc)) {
3123 		case RTW_IEEE80211_STYPE_CTS:
3124 		case RTW_IEEE80211_STYPE_ACK:
3125 			hdrlen = 10;
3126 			break;
3127 		default:
3128 			hdrlen = 16;
3129 			break;
3130 		}
3131 		break;
3132 	}
3133 
3134 	return hdrlen;
3135 }
3136 
rtw_ht_mcsset_to_nss(u8 * supp_mcs_set)3137 u8	rtw_ht_mcsset_to_nss(u8 *supp_mcs_set)
3138 {
3139 	u8 nss = 1;
3140 
3141 	if (supp_mcs_set[3])
3142 		nss = 4;
3143 	else if (supp_mcs_set[2])
3144 		nss = 3;
3145 	else if (supp_mcs_set[1])
3146 		nss = 2;
3147 	else if (supp_mcs_set[0])
3148 		nss = 1;
3149 	else
3150 		RTW_INFO("%s,%d, warning! supp_mcs_set is zero\n", __func__, __LINE__);
3151 	/* RTW_INFO("%s HT: %dSS\n", __FUNCTION__, nss); */
3152 	return nss;
3153 }
3154 
rtw_ht_mcs_set_to_bitmap(u8 * mcs_set,u8 nss)3155 u32	rtw_ht_mcs_set_to_bitmap(u8 *mcs_set, u8 nss)
3156 {
3157 	u8 i;
3158 	u32 bitmap = 0;
3159 
3160 	for (i = 0; i < nss; i++)
3161 		bitmap |= mcs_set[i] << (i * 8);
3162 
3163 	RTW_INFO("ht_mcs_set=%02x %02x %02x %02x, nss=%u, bitmap=%08x\n"
3164 		, mcs_set[0], mcs_set[1], mcs_set[2], mcs_set[3], nss, bitmap);
3165 
3166 	return bitmap;
3167 }
3168 
3169 /* show MCS rate, unit: 100Kbps */
rtw_ht_mcs_rate(u8 bw_40MHz,u8 short_GI,unsigned char * MCS_rate)3170 u16 rtw_ht_mcs_rate(u8 bw_40MHz, u8 short_GI, unsigned char *MCS_rate)
3171 {
3172 	u16 max_rate = 0;
3173 
3174 	if (MCS_rate[3]) {
3175 		if (MCS_rate[3] & BIT(7))
3176 			max_rate = (bw_40MHz) ? ((short_GI) ? 6000 : 5400) : ((short_GI) ? 2889 : 2600);
3177 		else if (MCS_rate[3] & BIT(6))
3178 			max_rate = (bw_40MHz) ? ((short_GI) ? 5400 : 4860) : ((short_GI) ? 2600 : 2340);
3179 		else if (MCS_rate[3] & BIT(5))
3180 			max_rate = (bw_40MHz) ? ((short_GI) ? 4800 : 4320) : ((short_GI) ? 2311 : 2080);
3181 		else if (MCS_rate[3] & BIT(4))
3182 			max_rate = (bw_40MHz) ? ((short_GI) ? 3600 : 3240) : ((short_GI) ? 1733 : 1560);
3183 		else if (MCS_rate[3] & BIT(3))
3184 			max_rate = (bw_40MHz) ? ((short_GI) ? 2400 : 2160) : ((short_GI) ? 1156 : 1040);
3185 		else if (MCS_rate[3] & BIT(2))
3186 			max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
3187 		else if (MCS_rate[3] & BIT(1))
3188 			max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
3189 		else if (MCS_rate[3] & BIT(0))
3190 			max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
3191 	} else if (MCS_rate[2]) {
3192 		if (MCS_rate[2] & BIT(7))
3193 			max_rate = (bw_40MHz) ? ((short_GI) ? 4500 : 4050) : ((short_GI) ? 2167 : 1950);
3194 		else if (MCS_rate[2] & BIT(6))
3195 			max_rate = (bw_40MHz) ? ((short_GI) ? 4050 : 3645) : ((short_GI) ? 1950 : 1750);
3196 		else if (MCS_rate[2] & BIT(5))
3197 			max_rate = (bw_40MHz) ? ((short_GI) ? 3600 : 3240) : ((short_GI) ? 1733 : 1560);
3198 		else if (MCS_rate[2] & BIT(4))
3199 			max_rate = (bw_40MHz) ? ((short_GI) ? 2700 : 2430) : ((short_GI) ? 1300 : 1170);
3200 		else if (MCS_rate[2] & BIT(3))
3201 			max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
3202 		else if (MCS_rate[2] & BIT(2))
3203 			max_rate = (bw_40MHz) ? ((short_GI) ? 1350 : 1215) : ((short_GI) ? 650 : 585);
3204 		else if (MCS_rate[2] & BIT(1))
3205 			max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
3206 		else if (MCS_rate[2] & BIT(0))
3207 			max_rate = (bw_40MHz) ? ((short_GI) ? 450 : 405) : ((short_GI) ? 217 : 195);
3208 	} else if (MCS_rate[1]) {
3209 		if (MCS_rate[1] & BIT(7))
3210 			max_rate = (bw_40MHz) ? ((short_GI) ? 3000 : 2700) : ((short_GI) ? 1444 : 1300);
3211 		else if (MCS_rate[1] & BIT(6))
3212 			max_rate = (bw_40MHz) ? ((short_GI) ? 2700 : 2430) : ((short_GI) ? 1300 : 1170);
3213 		else if (MCS_rate[1] & BIT(5))
3214 			max_rate = (bw_40MHz) ? ((short_GI) ? 2400 : 2160) : ((short_GI) ? 1156 : 1040);
3215 		else if (MCS_rate[1] & BIT(4))
3216 			max_rate = (bw_40MHz) ? ((short_GI) ? 1800 : 1620) : ((short_GI) ? 867 : 780);
3217 		else if (MCS_rate[1] & BIT(3))
3218 			max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
3219 		else if (MCS_rate[1] & BIT(2))
3220 			max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
3221 		else if (MCS_rate[1] & BIT(1))
3222 			max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
3223 		else if (MCS_rate[1] & BIT(0))
3224 			max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130);
3225 	} else {
3226 		if (MCS_rate[0] & BIT(7))
3227 			max_rate = (bw_40MHz) ? ((short_GI) ? 1500 : 1350) : ((short_GI) ? 722 : 650);
3228 		else if (MCS_rate[0] & BIT(6))
3229 			max_rate = (bw_40MHz) ? ((short_GI) ? 1350 : 1215) : ((short_GI) ? 650 : 585);
3230 		else if (MCS_rate[0] & BIT(5))
3231 			max_rate = (bw_40MHz) ? ((short_GI) ? 1200 : 1080) : ((short_GI) ? 578 : 520);
3232 		else if (MCS_rate[0] & BIT(4))
3233 			max_rate = (bw_40MHz) ? ((short_GI) ? 900 : 810) : ((short_GI) ? 433 : 390);
3234 		else if (MCS_rate[0] & BIT(3))
3235 			max_rate = (bw_40MHz) ? ((short_GI) ? 600 : 540) : ((short_GI) ? 289 : 260);
3236 		else if (MCS_rate[0] & BIT(2))
3237 			max_rate = (bw_40MHz) ? ((short_GI) ? 450 : 405) : ((short_GI) ? 217 : 195);
3238 		else if (MCS_rate[0] & BIT(1))
3239 			max_rate = (bw_40MHz) ? ((short_GI) ? 300 : 270) : ((short_GI) ? 144 : 130);
3240 		else if (MCS_rate[0] & BIT(0))
3241 			max_rate = (bw_40MHz) ? ((short_GI) ? 150 : 135) : ((short_GI) ? 72 : 65);
3242 	}
3243 
3244 	return max_rate;
3245 }
3246 
rtw_ht_cap_get_rx_nss(u8 * ht_cap)3247 u8 rtw_ht_cap_get_rx_nss(u8 *ht_cap)
3248 {
3249 	u8 *ht_mcs_set = HT_CAP_ELE_SUP_MCS_SET(ht_cap);
3250 
3251 	return rtw_ht_mcsset_to_nss(ht_mcs_set);
3252 }
3253 
rtw_ht_cap_get_tx_nss(u8 * ht_cap)3254 u8 rtw_ht_cap_get_tx_nss(u8 *ht_cap)
3255 {
3256 	u8 *ht_mcs_set = HT_CAP_ELE_SUP_MCS_SET(ht_cap);
3257 
3258 	if (GET_HT_CAP_ELE_TX_MCS_DEF(ht_cap) && GET_HT_CAP_ELE_TRX_MCS_NEQ(ht_cap))
3259 		return GET_HT_CAP_ELE_TX_MAX_SS(ht_cap) + 1;
3260 
3261 	return rtw_ht_cap_get_rx_nss(ht_cap);
3262 }
3263 
rtw_action_frame_parse(const u8 * frame,u32 frame_len,u8 * category,u8 * action)3264 int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action)
3265 {
3266 	const u8 *frame_body = frame + sizeof(struct rtw_ieee80211_hdr_3addr);
3267 	u16 fc;
3268 	u8 c;
3269 	u8 a = ACT_PUBLIC_MAX;
3270 
3271 	fc = le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)frame)->frame_ctl);
3272 
3273 	if ((fc & (RTW_IEEE80211_FCTL_FTYPE | RTW_IEEE80211_FCTL_STYPE))
3274 	    != (RTW_IEEE80211_FTYPE_MGMT | RTW_IEEE80211_STYPE_ACTION)
3275 	   )
3276 		return _FALSE;
3277 
3278 	c = frame_body[0];
3279 
3280 	switch (c) {
3281 	case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */
3282 		break;
3283 	default:
3284 		a = frame_body[1];
3285 	}
3286 
3287 	if (category)
3288 		*category = c;
3289 	if (action)
3290 		*action = a;
3291 
3292 	return _TRUE;
3293 }
3294 
3295 static const char *_action_public_str[] = {
3296 	[ACT_PUBLIC_BSSCOEXIST]				= "ACT_PUB_BSSCOEXIST",
3297 	[ACT_PUBLIC_DSE_ENABLE]				= "ACT_PUB_DSE_ENABLE",
3298 	[ACT_PUBLIC_DSE_DEENABLE]			= "ACT_PUB_DSE_DEENABLE",
3299 	[ACT_PUBLIC_DSE_REG_LOCATION]		= "ACT_PUB_DSE_REG_LOCATION",
3300 	[ACT_PUBLIC_EXT_CHL_SWITCH]			= "ACT_PUB_EXT_CHL_SWITCH",
3301 	[ACT_PUBLIC_DSE_MSR_REQ]			= "ACT_PUB_DSE_MSR_REQ",
3302 	[ACT_PUBLIC_DSE_MSR_RPRT]			= "ACT_PUB_DSE_MSR_RPRT",
3303 	[ACT_PUBLIC_MP]						= "ACT_PUB_MP",
3304 	[ACT_PUBLIC_DSE_PWR_CONSTRAINT]		= "ACT_PUB_DSE_PWR_CONSTRAINT",
3305 	[ACT_PUBLIC_VENDOR]					= "ACT_PUB_VENDOR",
3306 	[ACT_PUBLIC_GAS_INITIAL_REQ]		= "ACT_PUB_GAS_INITIAL_REQ",
3307 	[ACT_PUBLIC_GAS_INITIAL_RSP]		= "ACT_PUB_GAS_INITIAL_RSP",
3308 	[ACT_PUBLIC_GAS_COMEBACK_REQ]		= "ACT_PUB_GAS_COMEBACK_REQ",
3309 	[ACT_PUBLIC_GAS_COMEBACK_RSP]		= "ACT_PUB_GAS_COMEBACK_RSP",
3310 	[ACT_PUBLIC_TDLS_DISCOVERY_RSP]		= "ACT_PUB_TDLS_DISCOVERY_RSP",
3311 	[ACT_PUBLIC_LOCATION_TRACK]			= "ACT_PUB_LOCATION_TRACK",
3312 	[ACT_PUBLIC_QAB_REQ]				= "ACT_PUB_QAB_REQ",
3313 	[ACT_PUBLIC_QAB_RSP]				= "ACT_PUB_QAB_RSP",
3314 	[ACT_PUBLIC_QMF_POLICY]				= "ACT_PUB_QMF_POLICY",
3315 	[ACT_PUBLIC_QMF_POLICY_CHANGE]		= "ACT_PUB_QMF_POLICY_CHANGE",
3316 	[ACT_PUBLIC_QLOAD_REQ]				= "ACT_PUB_QLOAD_REQ",
3317 	[ACT_PUBLIC_QLOAD_REPORT]			= "ACT_PUB_QLOAD_REPORT",
3318 	[ACT_PUBLIC_HCCA_TXOP_ADV]			= "ACT_PUB_HCCA_TXOP_ADV",
3319 	[ACT_PUBLIC_HCCA_TXOP_RSP]			= "ACT_PUB_HCCA_TXOP_RSP",
3320 	[ACT_PUBLIC_PUBLIC_KEY]				= "ACT_PUB_PUBLIC_KEY",
3321 	[ACT_PUBLIC_CH_AVAILABILITY_QUERY]	= "ACT_PUB_CH_AVAILABILITY_QUERY",
3322 	[ACT_PUBLIC_CH_SCHEDULE_MGMT]		= "ACT_PUB_CH_SCHEDULE_MGMT",
3323 	[ACT_PUBLIC_CONTACT_VERI_SIGNAL]	= "ACT_PUB_CONTACT_VERI_SIGNAL",
3324 	[ACT_PUBLIC_GDD_ENABLE_REQ]			= "ACT_PUB_GDD_ENABLE_REQ",
3325 	[ACT_PUBLIC_GDD_ENABLE_RSP]			= "ACT_PUB_GDD_ENABLE_RSP",
3326 	[ACT_PUBLIC_NETWORK_CH_CONTROL]		= "ACT_PUB_NETWORK_CH_CONTROL",
3327 	[ACT_PUBLIC_WHITE_SPACE_MAP_ANN]	= "ACT_PUB_WHITE_SPACE_MAP_ANN",
3328 	[ACT_PUBLIC_FTM_REQ]				= "ACT_PUB_FTM_REQ",
3329 	[ACT_PUBLIC_FTM]					= "ACT_PUB_FTM",
3330 	[ACT_PUBLIC_MAX]					= "ACT_PUB_RSVD",
3331 };
3332 
action_public_str(u8 action)3333 const char *action_public_str(u8 action)
3334 {
3335 	action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action;
3336 	return _action_public_str[action];
3337 }
3338 
3339 /*tmp for sta mode, root cause have to wait supplicant's update.*/
rtw_set_spp_amsdu_mode(u8 mode,u8 * rsn_ie,int rsn_ie_len)3340 void rtw_set_spp_amsdu_mode(u8 mode, u8 *rsn_ie, int rsn_ie_len)
3341 {
3342 	struct rsne_info info;
3343 	int i, ret = _SUCCESS;
3344 	u8 spp_req_cap = 0;
3345 
3346 	if (mode == RTW_AMSDU_MODE_NON_SPP ) {
3347 		spp_req_cap = (u8)(~SPP_CAP | ~SPP_REQ);
3348 	} else if (mode == RTW_AMSDU_MODE_SPP) {
3349 		spp_req_cap = (u8)(SPP_CAP | SPP_REQ);
3350 	} else if (mode == RTW_AMSDU_MODE_ALL_DROP) {
3351 		spp_req_cap = (u8)(~SPP_CAP | SPP_REQ);
3352 	} else {
3353 		RTW_INFO("%s unexpected mode = %d, please check the config\n", __func__, mode);
3354 		return;
3355 	}
3356 
3357 	ret = rtw_rsne_info_parse(rsn_ie, rsn_ie_len, &info);
3358 	if (ret != _SUCCESS)
3359 		return;
3360 
3361 	SET_RSN_CAP_SPP(info.cap, spp_req_cap);
3362 	RTW_INFO("%s set spp opt = %d\n", __func__, GET_RSN_CAP_SPP_OPT(info.cap));
3363 }
3364 
rtw_check_amsdu_disable(u8 mode,u8 spp_opt)3365 u8 rtw_check_amsdu_disable(u8 mode, u8 spp_opt)
3366 {
3367 	u8 ret = _FALSE;
3368 	RTW_INFO("%s spp_opt=%u \n", __func__, spp_opt);
3369 
3370 	/* pp amsdu: peer's required has to be 0, or disable */
3371 	if ((mode == RTW_AMSDU_MODE_NON_SPP) && (spp_opt & SPP_REQ))
3372 		ret = _TRUE;
3373 	/* spp amsdu: peer's cap has to be 1, or disable */
3374 	else if ((mode == RTW_AMSDU_MODE_SPP) && ~(spp_opt & SPP_CAP))
3375 		ret = _TRUE;
3376 	/* mode = all drop */
3377 	else if (mode == RTW_AMSDU_MODE_ALL_DROP)
3378 		ret = _TRUE;
3379 	else
3380 		ret = _FALSE;
3381 	return ret;
3382 }
3383 
3384