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