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