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