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