ieee802_11_common.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  1. /*
  2. * IEEE 802.11 Common routines
  3. * Copyright (c) 2002-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "defs.h"
  11. #include "wpa_common.h"
  12. #include "qca-vendor.h"
  13. #include "ieee802_11_defs.h"
  14. #include "ieee802_11_common.h"
  15. static int ieee802_11_parse_vendor_specific(const u8 *pos, size_t elen,
  16. struct ieee802_11_elems *elems,
  17. int show_errors)
  18. {
  19. unsigned int oui;
  20. /* first 3 bytes in vendor specific information element are the IEEE
  21. * OUI of the vendor. The following byte is used a vendor specific
  22. * sub-type. */
  23. if (elen < 4) {
  24. if (show_errors) {
  25. wpa_printf(MSG_MSGDUMP, "short vendor specific "
  26. "information element ignored (len=%lu)",
  27. (unsigned long) elen);
  28. }
  29. return -1;
  30. }
  31. oui = WPA_GET_BE24(pos);
  32. switch (oui) {
  33. case OUI_MICROSOFT:
  34. /* Microsoft/Wi-Fi information elements are further typed and
  35. * subtyped */
  36. switch (pos[3]) {
  37. case 1:
  38. /* Microsoft OUI (00:50:F2) with OUI Type 1:
  39. * real WPA information element */
  40. elems->wpa_ie = pos;
  41. elems->wpa_ie_len = elen;
  42. break;
  43. case WMM_OUI_TYPE:
  44. /* WMM information element */
  45. if (elen < 5) {
  46. wpa_printf(MSG_MSGDUMP, "short WMM "
  47. "information element ignored "
  48. "(len=%lu)",
  49. (unsigned long) elen);
  50. return -1;
  51. }
  52. switch (pos[4]) {
  53. case WMM_OUI_SUBTYPE_INFORMATION_ELEMENT:
  54. case WMM_OUI_SUBTYPE_PARAMETER_ELEMENT:
  55. /*
  56. * Share same pointer since only one of these
  57. * is used and they start with same data.
  58. * Length field can be used to distinguish the
  59. * IEs.
  60. */
  61. elems->wmm = pos;
  62. elems->wmm_len = elen;
  63. break;
  64. case WMM_OUI_SUBTYPE_TSPEC_ELEMENT:
  65. elems->wmm_tspec = pos;
  66. elems->wmm_tspec_len = elen;
  67. break;
  68. default:
  69. wpa_printf(MSG_EXCESSIVE, "unknown WMM "
  70. "information element ignored "
  71. "(subtype=%d len=%lu)",
  72. pos[4], (unsigned long) elen);
  73. return -1;
  74. }
  75. break;
  76. case 4:
  77. /* Wi-Fi Protected Setup (WPS) IE */
  78. elems->wps_ie = pos;
  79. elems->wps_ie_len = elen;
  80. break;
  81. default:
  82. wpa_printf(MSG_EXCESSIVE, "Unknown Microsoft "
  83. "information element ignored "
  84. "(type=%d len=%lu)",
  85. pos[3], (unsigned long) elen);
  86. return -1;
  87. }
  88. break;
  89. case OUI_WFA:
  90. switch (pos[3]) {
  91. case P2P_OUI_TYPE:
  92. /* Wi-Fi Alliance - P2P IE */
  93. elems->p2p = pos;
  94. elems->p2p_len = elen;
  95. break;
  96. case WFD_OUI_TYPE:
  97. /* Wi-Fi Alliance - WFD IE */
  98. elems->wfd = pos;
  99. elems->wfd_len = elen;
  100. break;
  101. case HS20_INDICATION_OUI_TYPE:
  102. /* Hotspot 2.0 */
  103. elems->hs20 = pos;
  104. elems->hs20_len = elen;
  105. break;
  106. case HS20_OSEN_OUI_TYPE:
  107. /* Hotspot 2.0 OSEN */
  108. elems->osen = pos;
  109. elems->osen_len = elen;
  110. break;
  111. default:
  112. wpa_printf(MSG_MSGDUMP, "Unknown WFA "
  113. "information element ignored "
  114. "(type=%d len=%lu)",
  115. pos[3], (unsigned long) elen);
  116. return -1;
  117. }
  118. break;
  119. case OUI_BROADCOM:
  120. switch (pos[3]) {
  121. case VENDOR_HT_CAPAB_OUI_TYPE:
  122. elems->vendor_ht_cap = pos;
  123. elems->vendor_ht_cap_len = elen;
  124. break;
  125. case VENDOR_VHT_TYPE:
  126. if (elen > 4 &&
  127. (pos[4] == VENDOR_VHT_SUBTYPE ||
  128. pos[4] == VENDOR_VHT_SUBTYPE2)) {
  129. elems->vendor_vht = pos;
  130. elems->vendor_vht_len = elen;
  131. } else
  132. return -1;
  133. break;
  134. default:
  135. wpa_printf(MSG_EXCESSIVE, "Unknown Broadcom "
  136. "information element ignored "
  137. "(type=%d len=%lu)",
  138. pos[3], (unsigned long) elen);
  139. return -1;
  140. }
  141. break;
  142. case OUI_QCA:
  143. switch (pos[3]) {
  144. case QCA_VENDOR_ELEM_P2P_PREF_CHAN_LIST:
  145. elems->pref_freq_list = pos;
  146. elems->pref_freq_list_len = elen;
  147. break;
  148. default:
  149. wpa_printf(MSG_EXCESSIVE,
  150. "Unknown QCA information element ignored (type=%d len=%lu)",
  151. pos[3], (unsigned long) elen);
  152. return -1;
  153. }
  154. break;
  155. default:
  156. wpa_printf(MSG_EXCESSIVE, "unknown vendor specific "
  157. "information element ignored (vendor OUI "
  158. "%02x:%02x:%02x len=%lu)",
  159. pos[0], pos[1], pos[2], (unsigned long) elen);
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * ieee802_11_parse_elems - Parse information elements in management frames
  166. * @start: Pointer to the start of IEs
  167. * @len: Length of IE buffer in octets
  168. * @elems: Data structure for parsed elements
  169. * @show_errors: Whether to show parsing errors in debug log
  170. * Returns: Parsing result
  171. */
  172. ParseRes ieee802_11_parse_elems(const u8 *start, size_t len,
  173. struct ieee802_11_elems *elems,
  174. int show_errors)
  175. {
  176. size_t left = len;
  177. const u8 *pos = start;
  178. int unknown = 0;
  179. os_memset(elems, 0, sizeof(*elems));
  180. while (left >= 2) {
  181. u8 id, elen;
  182. id = *pos++;
  183. elen = *pos++;
  184. left -= 2;
  185. if (elen > left) {
  186. if (show_errors) {
  187. wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
  188. "parse failed (id=%d elen=%d "
  189. "left=%lu)",
  190. id, elen, (unsigned long) left);
  191. wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
  192. }
  193. return ParseFailed;
  194. }
  195. switch (id) {
  196. case WLAN_EID_SSID:
  197. if (elen > SSID_MAX_LEN) {
  198. wpa_printf(MSG_DEBUG,
  199. "Ignored too long SSID element (elen=%u)",
  200. elen);
  201. break;
  202. }
  203. elems->ssid = pos;
  204. elems->ssid_len = elen;
  205. break;
  206. case WLAN_EID_SUPP_RATES:
  207. elems->supp_rates = pos;
  208. elems->supp_rates_len = elen;
  209. break;
  210. case WLAN_EID_DS_PARAMS:
  211. if (elen < 1)
  212. break;
  213. elems->ds_params = pos;
  214. break;
  215. case WLAN_EID_CF_PARAMS:
  216. case WLAN_EID_TIM:
  217. break;
  218. case WLAN_EID_CHALLENGE:
  219. elems->challenge = pos;
  220. elems->challenge_len = elen;
  221. break;
  222. case WLAN_EID_ERP_INFO:
  223. if (elen < 1)
  224. break;
  225. elems->erp_info = pos;
  226. break;
  227. case WLAN_EID_EXT_SUPP_RATES:
  228. elems->ext_supp_rates = pos;
  229. elems->ext_supp_rates_len = elen;
  230. break;
  231. case WLAN_EID_VENDOR_SPECIFIC:
  232. if (ieee802_11_parse_vendor_specific(pos, elen,
  233. elems,
  234. show_errors))
  235. unknown++;
  236. break;
  237. case WLAN_EID_RSN:
  238. elems->rsn_ie = pos;
  239. elems->rsn_ie_len = elen;
  240. break;
  241. case WLAN_EID_PWR_CAPABILITY:
  242. break;
  243. case WLAN_EID_SUPPORTED_CHANNELS:
  244. elems->supp_channels = pos;
  245. elems->supp_channels_len = elen;
  246. break;
  247. case WLAN_EID_MOBILITY_DOMAIN:
  248. if (elen < sizeof(struct rsn_mdie))
  249. break;
  250. elems->mdie = pos;
  251. elems->mdie_len = elen;
  252. break;
  253. case WLAN_EID_FAST_BSS_TRANSITION:
  254. if (elen < sizeof(struct rsn_ftie))
  255. break;
  256. elems->ftie = pos;
  257. elems->ftie_len = elen;
  258. break;
  259. case WLAN_EID_TIMEOUT_INTERVAL:
  260. if (elen != 5)
  261. break;
  262. elems->timeout_int = pos;
  263. break;
  264. case WLAN_EID_HT_CAP:
  265. if (elen < sizeof(struct ieee80211_ht_capabilities))
  266. break;
  267. elems->ht_capabilities = pos;
  268. break;
  269. case WLAN_EID_HT_OPERATION:
  270. if (elen < sizeof(struct ieee80211_ht_operation))
  271. break;
  272. elems->ht_operation = pos;
  273. break;
  274. case WLAN_EID_MESH_CONFIG:
  275. elems->mesh_config = pos;
  276. elems->mesh_config_len = elen;
  277. break;
  278. case WLAN_EID_MESH_ID:
  279. elems->mesh_id = pos;
  280. elems->mesh_id_len = elen;
  281. break;
  282. case WLAN_EID_PEER_MGMT:
  283. elems->peer_mgmt = pos;
  284. elems->peer_mgmt_len = elen;
  285. break;
  286. case WLAN_EID_VHT_CAP:
  287. if (elen < sizeof(struct ieee80211_vht_capabilities))
  288. break;
  289. elems->vht_capabilities = pos;
  290. break;
  291. case WLAN_EID_VHT_OPERATION:
  292. if (elen < sizeof(struct ieee80211_vht_operation))
  293. break;
  294. elems->vht_operation = pos;
  295. break;
  296. case WLAN_EID_VHT_OPERATING_MODE_NOTIFICATION:
  297. if (elen != 1)
  298. break;
  299. elems->vht_opmode_notif = pos;
  300. break;
  301. case WLAN_EID_LINK_ID:
  302. if (elen < 18)
  303. break;
  304. elems->link_id = pos;
  305. break;
  306. case WLAN_EID_INTERWORKING:
  307. elems->interworking = pos;
  308. elems->interworking_len = elen;
  309. break;
  310. case WLAN_EID_QOS_MAP_SET:
  311. if (elen < 16)
  312. break;
  313. elems->qos_map_set = pos;
  314. elems->qos_map_set_len = elen;
  315. break;
  316. case WLAN_EID_EXT_CAPAB:
  317. elems->ext_capab = pos;
  318. elems->ext_capab_len = elen;
  319. break;
  320. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  321. if (elen < 3)
  322. break;
  323. elems->bss_max_idle_period = pos;
  324. break;
  325. case WLAN_EID_SSID_LIST:
  326. elems->ssid_list = pos;
  327. elems->ssid_list_len = elen;
  328. break;
  329. case WLAN_EID_AMPE:
  330. elems->ampe = pos;
  331. elems->ampe_len = elen;
  332. break;
  333. case WLAN_EID_MIC:
  334. elems->mic = pos;
  335. elems->mic_len = elen;
  336. /* after mic everything is encrypted, so stop. */
  337. left = elen;
  338. break;
  339. case WLAN_EID_MULTI_BAND:
  340. if (elems->mb_ies.nof_ies >= MAX_NOF_MB_IES_SUPPORTED) {
  341. wpa_printf(MSG_MSGDUMP,
  342. "IEEE 802.11 element parse ignored MB IE (id=%d elen=%d)",
  343. id, elen);
  344. break;
  345. }
  346. elems->mb_ies.ies[elems->mb_ies.nof_ies].ie = pos;
  347. elems->mb_ies.ies[elems->mb_ies.nof_ies].ie_len = elen;
  348. elems->mb_ies.nof_ies++;
  349. break;
  350. default:
  351. unknown++;
  352. if (!show_errors)
  353. break;
  354. wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
  355. "ignored unknown element (id=%d elen=%d)",
  356. id, elen);
  357. break;
  358. }
  359. left -= elen;
  360. pos += elen;
  361. }
  362. if (left)
  363. return ParseFailed;
  364. return unknown ? ParseUnknown : ParseOK;
  365. }
  366. int ieee802_11_ie_count(const u8 *ies, size_t ies_len)
  367. {
  368. int count = 0;
  369. const u8 *pos, *end;
  370. if (ies == NULL)
  371. return 0;
  372. pos = ies;
  373. end = ies + ies_len;
  374. while (end - pos >= 2) {
  375. if (2 + pos[1] > end - pos)
  376. break;
  377. count++;
  378. pos += 2 + pos[1];
  379. }
  380. return count;
  381. }
  382. struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
  383. u32 oui_type)
  384. {
  385. struct wpabuf *buf;
  386. const u8 *end, *pos, *ie;
  387. pos = ies;
  388. end = ies + ies_len;
  389. ie = NULL;
  390. while (end - pos > 1) {
  391. if (2 + pos[1] > end - pos)
  392. return NULL;
  393. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  394. WPA_GET_BE32(&pos[2]) == oui_type) {
  395. ie = pos;
  396. break;
  397. }
  398. pos += 2 + pos[1];
  399. }
  400. if (ie == NULL)
  401. return NULL; /* No specified vendor IE found */
  402. buf = wpabuf_alloc(ies_len);
  403. if (buf == NULL)
  404. return NULL;
  405. /*
  406. * There may be multiple vendor IEs in the message, so need to
  407. * concatenate their data fields.
  408. */
  409. while (end - pos > 1) {
  410. if (2 + pos[1] > end - pos)
  411. break;
  412. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  413. WPA_GET_BE32(&pos[2]) == oui_type)
  414. wpabuf_put_data(buf, pos + 6, pos[1] - 4);
  415. pos += 2 + pos[1];
  416. }
  417. return buf;
  418. }
  419. const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  420. {
  421. u16 fc, type, stype;
  422. /*
  423. * PS-Poll frames are 16 bytes. All other frames are
  424. * 24 bytes or longer.
  425. */
  426. if (len < 16)
  427. return NULL;
  428. fc = le_to_host16(hdr->frame_control);
  429. type = WLAN_FC_GET_TYPE(fc);
  430. stype = WLAN_FC_GET_STYPE(fc);
  431. switch (type) {
  432. case WLAN_FC_TYPE_DATA:
  433. if (len < 24)
  434. return NULL;
  435. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  436. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  437. case WLAN_FC_TODS:
  438. return hdr->addr1;
  439. case WLAN_FC_FROMDS:
  440. return hdr->addr2;
  441. default:
  442. return NULL;
  443. }
  444. case WLAN_FC_TYPE_CTRL:
  445. if (stype != WLAN_FC_STYPE_PSPOLL)
  446. return NULL;
  447. return hdr->addr1;
  448. case WLAN_FC_TYPE_MGMT:
  449. return hdr->addr3;
  450. default:
  451. return NULL;
  452. }
  453. }
  454. int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
  455. const char *name, const char *val)
  456. {
  457. int num, v;
  458. const char *pos;
  459. struct hostapd_wmm_ac_params *ac;
  460. /* skip 'wme_ac_' or 'wmm_ac_' prefix */
  461. pos = name + 7;
  462. if (os_strncmp(pos, "be_", 3) == 0) {
  463. num = 0;
  464. pos += 3;
  465. } else if (os_strncmp(pos, "bk_", 3) == 0) {
  466. num = 1;
  467. pos += 3;
  468. } else if (os_strncmp(pos, "vi_", 3) == 0) {
  469. num = 2;
  470. pos += 3;
  471. } else if (os_strncmp(pos, "vo_", 3) == 0) {
  472. num = 3;
  473. pos += 3;
  474. } else {
  475. wpa_printf(MSG_ERROR, "Unknown WMM name '%s'", pos);
  476. return -1;
  477. }
  478. ac = &wmm_ac_params[num];
  479. if (os_strcmp(pos, "aifs") == 0) {
  480. v = atoi(val);
  481. if (v < 1 || v > 255) {
  482. wpa_printf(MSG_ERROR, "Invalid AIFS value %d", v);
  483. return -1;
  484. }
  485. ac->aifs = v;
  486. } else if (os_strcmp(pos, "cwmin") == 0) {
  487. v = atoi(val);
  488. if (v < 0 || v > 15) {
  489. wpa_printf(MSG_ERROR, "Invalid cwMin value %d", v);
  490. return -1;
  491. }
  492. ac->cwmin = v;
  493. } else if (os_strcmp(pos, "cwmax") == 0) {
  494. v = atoi(val);
  495. if (v < 0 || v > 15) {
  496. wpa_printf(MSG_ERROR, "Invalid cwMax value %d", v);
  497. return -1;
  498. }
  499. ac->cwmax = v;
  500. } else if (os_strcmp(pos, "txop_limit") == 0) {
  501. v = atoi(val);
  502. if (v < 0 || v > 0xffff) {
  503. wpa_printf(MSG_ERROR, "Invalid txop value %d", v);
  504. return -1;
  505. }
  506. ac->txop_limit = v;
  507. } else if (os_strcmp(pos, "acm") == 0) {
  508. v = atoi(val);
  509. if (v < 0 || v > 1) {
  510. wpa_printf(MSG_ERROR, "Invalid acm value %d", v);
  511. return -1;
  512. }
  513. ac->admission_control_mandatory = v;
  514. } else {
  515. wpa_printf(MSG_ERROR, "Unknown wmm_ac_ field '%s'", pos);
  516. return -1;
  517. }
  518. return 0;
  519. }
  520. enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel)
  521. {
  522. u8 op_class;
  523. return ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  524. &op_class, channel);
  525. }
  526. /**
  527. * ieee80211_freq_to_channel_ext - Convert frequency into channel info
  528. * for HT40 and VHT. DFS channels are not covered.
  529. * @freq: Frequency (MHz) to convert
  530. * @sec_channel: 0 = non-HT40, 1 = sec. channel above, -1 = sec. channel below
  531. * @vht: VHT channel width (VHT_CHANWIDTH_*)
  532. * @op_class: Buffer for returning operating class
  533. * @channel: Buffer for returning channel number
  534. * Returns: hw_mode on success, NUM_HOSTAPD_MODES on failure
  535. */
  536. enum hostapd_hw_mode ieee80211_freq_to_channel_ext(unsigned int freq,
  537. int sec_channel, int vht,
  538. u8 *op_class, u8 *channel)
  539. {
  540. u8 vht_opclass;
  541. /* TODO: more operating classes */
  542. if (sec_channel > 1 || sec_channel < -1)
  543. return NUM_HOSTAPD_MODES;
  544. if (freq >= 2412 && freq <= 2472) {
  545. if ((freq - 2407) % 5)
  546. return NUM_HOSTAPD_MODES;
  547. if (vht)
  548. return NUM_HOSTAPD_MODES;
  549. /* 2.407 GHz, channels 1..13 */
  550. if (sec_channel == 1)
  551. *op_class = 83;
  552. else if (sec_channel == -1)
  553. *op_class = 84;
  554. else
  555. *op_class = 81;
  556. *channel = (freq - 2407) / 5;
  557. return HOSTAPD_MODE_IEEE80211G;
  558. }
  559. if (freq == 2484) {
  560. if (sec_channel || vht)
  561. return NUM_HOSTAPD_MODES;
  562. *op_class = 82; /* channel 14 */
  563. *channel = 14;
  564. return HOSTAPD_MODE_IEEE80211B;
  565. }
  566. if (freq >= 4900 && freq < 5000) {
  567. if ((freq - 4000) % 5)
  568. return NUM_HOSTAPD_MODES;
  569. *channel = (freq - 4000) / 5;
  570. *op_class = 0; /* TODO */
  571. return HOSTAPD_MODE_IEEE80211A;
  572. }
  573. switch (vht) {
  574. case VHT_CHANWIDTH_80MHZ:
  575. vht_opclass = 128;
  576. break;
  577. case VHT_CHANWIDTH_160MHZ:
  578. vht_opclass = 129;
  579. break;
  580. case VHT_CHANWIDTH_80P80MHZ:
  581. vht_opclass = 130;
  582. break;
  583. default:
  584. vht_opclass = 0;
  585. break;
  586. }
  587. /* 5 GHz, channels 36..48 */
  588. if (freq >= 5180 && freq <= 5240) {
  589. if ((freq - 5000) % 5)
  590. return NUM_HOSTAPD_MODES;
  591. if (vht_opclass)
  592. *op_class = vht_opclass;
  593. else if (sec_channel == 1)
  594. *op_class = 116;
  595. else if (sec_channel == -1)
  596. *op_class = 117;
  597. else
  598. *op_class = 115;
  599. *channel = (freq - 5000) / 5;
  600. return HOSTAPD_MODE_IEEE80211A;
  601. }
  602. /* 5 GHz, channels 149..169 */
  603. if (freq >= 5745 && freq <= 5845) {
  604. if ((freq - 5000) % 5)
  605. return NUM_HOSTAPD_MODES;
  606. if (vht_opclass)
  607. *op_class = vht_opclass;
  608. else if (sec_channel == 1)
  609. *op_class = 126;
  610. else if (sec_channel == -1)
  611. *op_class = 127;
  612. else if (freq <= 5805)
  613. *op_class = 124;
  614. else
  615. *op_class = 125;
  616. *channel = (freq - 5000) / 5;
  617. return HOSTAPD_MODE_IEEE80211A;
  618. }
  619. /* 5 GHz, channels 100..140 */
  620. if (freq >= 5000 && freq <= 5700) {
  621. if ((freq - 5000) % 5)
  622. return NUM_HOSTAPD_MODES;
  623. if (vht_opclass)
  624. *op_class = vht_opclass;
  625. else if (sec_channel == 1)
  626. *op_class = 122;
  627. else if (sec_channel == -1)
  628. *op_class = 123;
  629. else
  630. *op_class = 121;
  631. *channel = (freq - 5000) / 5;
  632. return HOSTAPD_MODE_IEEE80211A;
  633. }
  634. if (freq >= 5000 && freq < 5900) {
  635. if ((freq - 5000) % 5)
  636. return NUM_HOSTAPD_MODES;
  637. *channel = (freq - 5000) / 5;
  638. *op_class = 0; /* TODO */
  639. return HOSTAPD_MODE_IEEE80211A;
  640. }
  641. /* 56.16 GHz, channel 1..4 */
  642. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  643. if (sec_channel || vht)
  644. return NUM_HOSTAPD_MODES;
  645. *channel = (freq - 56160) / 2160;
  646. *op_class = 180;
  647. return HOSTAPD_MODE_IEEE80211AD;
  648. }
  649. return NUM_HOSTAPD_MODES;
  650. }
  651. static const char *const us_op_class_cc[] = {
  652. "US", "CA", NULL
  653. };
  654. static const char *const eu_op_class_cc[] = {
  655. "AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
  656. "DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
  657. "LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
  658. "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", NULL
  659. };
  660. static const char *const jp_op_class_cc[] = {
  661. "JP", NULL
  662. };
  663. static const char *const cn_op_class_cc[] = {
  664. "CN", NULL
  665. };
  666. static int country_match(const char *const cc[], const char *const country)
  667. {
  668. int i;
  669. if (country == NULL)
  670. return 0;
  671. for (i = 0; cc[i]; i++) {
  672. if (cc[i][0] == country[0] && cc[i][1] == country[1])
  673. return 1;
  674. }
  675. return 0;
  676. }
  677. static int ieee80211_chan_to_freq_us(u8 op_class, u8 chan)
  678. {
  679. switch (op_class) {
  680. case 12: /* channels 1..11 */
  681. case 32: /* channels 1..7; 40 MHz */
  682. case 33: /* channels 5..11; 40 MHz */
  683. if (chan < 1 || chan > 11)
  684. return -1;
  685. return 2407 + 5 * chan;
  686. case 1: /* channels 36,40,44,48 */
  687. case 2: /* channels 52,56,60,64; dfs */
  688. case 22: /* channels 36,44; 40 MHz */
  689. case 23: /* channels 52,60; 40 MHz */
  690. case 27: /* channels 40,48; 40 MHz */
  691. case 28: /* channels 56,64; 40 MHz */
  692. if (chan < 36 || chan > 64)
  693. return -1;
  694. return 5000 + 5 * chan;
  695. case 4: /* channels 100-144 */
  696. case 24: /* channels 100-140; 40 MHz */
  697. if (chan < 100 || chan > 144)
  698. return -1;
  699. return 5000 + 5 * chan;
  700. case 3: /* channels 149,153,157,161 */
  701. case 25: /* channels 149,157; 40 MHz */
  702. case 26: /* channels 149,157; 40 MHz */
  703. case 30: /* channels 153,161; 40 MHz */
  704. case 31: /* channels 153,161; 40 MHz */
  705. if (chan < 149 || chan > 161)
  706. return -1;
  707. return 5000 + 5 * chan;
  708. case 5: /* channels 149,153,157,161,165 */
  709. if (chan < 149 || chan > 165)
  710. return -1;
  711. return 5000 + 5 * chan;
  712. case 34: /* 60 GHz band, channels 1..3 */
  713. if (chan < 1 || chan > 3)
  714. return -1;
  715. return 56160 + 2160 * chan;
  716. }
  717. return -1;
  718. }
  719. static int ieee80211_chan_to_freq_eu(u8 op_class, u8 chan)
  720. {
  721. switch (op_class) {
  722. case 4: /* channels 1..13 */
  723. case 11: /* channels 1..9; 40 MHz */
  724. case 12: /* channels 5..13; 40 MHz */
  725. if (chan < 1 || chan > 13)
  726. return -1;
  727. return 2407 + 5 * chan;
  728. case 1: /* channels 36,40,44,48 */
  729. case 2: /* channels 52,56,60,64; dfs */
  730. case 5: /* channels 36,44; 40 MHz */
  731. case 6: /* channels 52,60; 40 MHz */
  732. case 8: /* channels 40,48; 40 MHz */
  733. case 9: /* channels 56,64; 40 MHz */
  734. if (chan < 36 || chan > 64)
  735. return -1;
  736. return 5000 + 5 * chan;
  737. case 3: /* channels 100-140 */
  738. case 7: /* channels 100-132; 40 MHz */
  739. case 10: /* channels 104-136; 40 MHz */
  740. case 16: /* channels 100-140 */
  741. if (chan < 100 || chan > 140)
  742. return -1;
  743. return 5000 + 5 * chan;
  744. case 17: /* channels 149,153,157,161,165,169 */
  745. if (chan < 149 || chan > 169)
  746. return -1;
  747. return 5000 + 5 * chan;
  748. case 18: /* 60 GHz band, channels 1..4 */
  749. if (chan < 1 || chan > 4)
  750. return -1;
  751. return 56160 + 2160 * chan;
  752. }
  753. return -1;
  754. }
  755. static int ieee80211_chan_to_freq_jp(u8 op_class, u8 chan)
  756. {
  757. switch (op_class) {
  758. case 30: /* channels 1..13 */
  759. case 56: /* channels 1..9; 40 MHz */
  760. case 57: /* channels 5..13; 40 MHz */
  761. if (chan < 1 || chan > 13)
  762. return -1;
  763. return 2407 + 5 * chan;
  764. case 31: /* channel 14 */
  765. if (chan != 14)
  766. return -1;
  767. return 2414 + 5 * chan;
  768. case 1: /* channels 34,38,42,46(old) or 36,40,44,48 */
  769. case 32: /* channels 52,56,60,64 */
  770. case 33: /* channels 52,56,60,64 */
  771. case 36: /* channels 36,44; 40 MHz */
  772. case 37: /* channels 52,60; 40 MHz */
  773. case 38: /* channels 52,60; 40 MHz */
  774. case 41: /* channels 40,48; 40 MHz */
  775. case 42: /* channels 56,64; 40 MHz */
  776. case 43: /* channels 56,64; 40 MHz */
  777. if (chan < 34 || chan > 64)
  778. return -1;
  779. return 5000 + 5 * chan;
  780. case 34: /* channels 100-140 */
  781. case 35: /* channels 100-140 */
  782. case 39: /* channels 100-132; 40 MHz */
  783. case 40: /* channels 100-132; 40 MHz */
  784. case 44: /* channels 104-136; 40 MHz */
  785. case 45: /* channels 104-136; 40 MHz */
  786. case 58: /* channels 100-140 */
  787. if (chan < 100 || chan > 140)
  788. return -1;
  789. return 5000 + 5 * chan;
  790. case 59: /* 60 GHz band, channels 1..4 */
  791. if (chan < 1 || chan > 3)
  792. return -1;
  793. return 56160 + 2160 * chan;
  794. }
  795. return -1;
  796. }
  797. static int ieee80211_chan_to_freq_cn(u8 op_class, u8 chan)
  798. {
  799. switch (op_class) {
  800. case 7: /* channels 1..13 */
  801. case 8: /* channels 1..9; 40 MHz */
  802. case 9: /* channels 5..13; 40 MHz */
  803. if (chan < 1 || chan > 13)
  804. return -1;
  805. return 2407 + 5 * chan;
  806. case 1: /* channels 36,40,44,48 */
  807. case 2: /* channels 52,56,60,64; dfs */
  808. case 4: /* channels 36,44; 40 MHz */
  809. case 5: /* channels 52,60; 40 MHz */
  810. if (chan < 36 || chan > 64)
  811. return -1;
  812. return 5000 + 5 * chan;
  813. case 3: /* channels 149,153,157,161,165 */
  814. case 6: /* channels 149,157; 40 MHz */
  815. if (chan < 149 || chan > 165)
  816. return -1;
  817. return 5000 + 5 * chan;
  818. }
  819. return -1;
  820. }
  821. static int ieee80211_chan_to_freq_global(u8 op_class, u8 chan)
  822. {
  823. /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */
  824. switch (op_class) {
  825. case 81:
  826. /* channels 1..13 */
  827. if (chan < 1 || chan > 13)
  828. return -1;
  829. return 2407 + 5 * chan;
  830. case 82:
  831. /* channel 14 */
  832. if (chan != 14)
  833. return -1;
  834. return 2414 + 5 * chan;
  835. case 83: /* channels 1..9; 40 MHz */
  836. case 84: /* channels 5..13; 40 MHz */
  837. if (chan < 1 || chan > 13)
  838. return -1;
  839. return 2407 + 5 * chan;
  840. case 115: /* channels 36,40,44,48; indoor only */
  841. case 116: /* channels 36,44; 40 MHz; indoor only */
  842. case 117: /* channels 40,48; 40 MHz; indoor only */
  843. case 118: /* channels 52,56,60,64; dfs */
  844. case 119: /* channels 52,60; 40 MHz; dfs */
  845. case 120: /* channels 56,64; 40 MHz; dfs */
  846. if (chan < 36 || chan > 64)
  847. return -1;
  848. return 5000 + 5 * chan;
  849. case 121: /* channels 100-140 */
  850. case 122: /* channels 100-142; 40 MHz */
  851. case 123: /* channels 104-136; 40 MHz */
  852. if (chan < 100 || chan > 140)
  853. return -1;
  854. return 5000 + 5 * chan;
  855. case 124: /* channels 149,153,157,161 */
  856. case 126: /* channels 149,157; 40 MHz */
  857. case 127: /* channels 153,161; 40 MHz */
  858. if (chan < 149 || chan > 161)
  859. return -1;
  860. return 5000 + 5 * chan;
  861. case 125: /* channels 149,153,157,161,165,169 */
  862. if (chan < 149 || chan > 169)
  863. return -1;
  864. return 5000 + 5 * chan;
  865. case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  866. case 130: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  867. if (chan < 36 || chan > 161)
  868. return -1;
  869. return 5000 + 5 * chan;
  870. case 129: /* center freqs 50, 114; 160 MHz */
  871. if (chan < 50 || chan > 114)
  872. return -1;
  873. return 5000 + 5 * chan;
  874. case 180: /* 60 GHz band, channels 1..4 */
  875. if (chan < 1 || chan > 4)
  876. return -1;
  877. return 56160 + 2160 * chan;
  878. }
  879. return -1;
  880. }
  881. /**
  882. * ieee80211_chan_to_freq - Convert channel info to frequency
  883. * @country: Country code, if known; otherwise, global operating class is used
  884. * @op_class: Operating class
  885. * @chan: Channel number
  886. * Returns: Frequency in MHz or -1 if the specified channel is unknown
  887. */
  888. int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan)
  889. {
  890. int freq;
  891. if (country_match(us_op_class_cc, country)) {
  892. freq = ieee80211_chan_to_freq_us(op_class, chan);
  893. if (freq > 0)
  894. return freq;
  895. }
  896. if (country_match(eu_op_class_cc, country)) {
  897. freq = ieee80211_chan_to_freq_eu(op_class, chan);
  898. if (freq > 0)
  899. return freq;
  900. }
  901. if (country_match(jp_op_class_cc, country)) {
  902. freq = ieee80211_chan_to_freq_jp(op_class, chan);
  903. if (freq > 0)
  904. return freq;
  905. }
  906. if (country_match(cn_op_class_cc, country)) {
  907. freq = ieee80211_chan_to_freq_cn(op_class, chan);
  908. if (freq > 0)
  909. return freq;
  910. }
  911. return ieee80211_chan_to_freq_global(op_class, chan);
  912. }
  913. int ieee80211_is_dfs(int freq)
  914. {
  915. /* TODO: this could be more accurate to better cover all domains */
  916. return (freq >= 5260 && freq <= 5320) || (freq >= 5500 && freq <= 5700);
  917. }
  918. static int is_11b(u8 rate)
  919. {
  920. return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
  921. }
  922. int supp_rates_11b_only(struct ieee802_11_elems *elems)
  923. {
  924. int num_11b = 0, num_others = 0;
  925. int i;
  926. if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
  927. return 0;
  928. for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
  929. if (is_11b(elems->supp_rates[i]))
  930. num_11b++;
  931. else
  932. num_others++;
  933. }
  934. for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
  935. i++) {
  936. if (is_11b(elems->ext_supp_rates[i]))
  937. num_11b++;
  938. else
  939. num_others++;
  940. }
  941. return num_11b > 0 && num_others == 0;
  942. }
  943. const char * fc2str(u16 fc)
  944. {
  945. u16 stype = WLAN_FC_GET_STYPE(fc);
  946. #define C2S(x) case x: return #x;
  947. switch (WLAN_FC_GET_TYPE(fc)) {
  948. case WLAN_FC_TYPE_MGMT:
  949. switch (stype) {
  950. C2S(WLAN_FC_STYPE_ASSOC_REQ)
  951. C2S(WLAN_FC_STYPE_ASSOC_RESP)
  952. C2S(WLAN_FC_STYPE_REASSOC_REQ)
  953. C2S(WLAN_FC_STYPE_REASSOC_RESP)
  954. C2S(WLAN_FC_STYPE_PROBE_REQ)
  955. C2S(WLAN_FC_STYPE_PROBE_RESP)
  956. C2S(WLAN_FC_STYPE_BEACON)
  957. C2S(WLAN_FC_STYPE_ATIM)
  958. C2S(WLAN_FC_STYPE_DISASSOC)
  959. C2S(WLAN_FC_STYPE_AUTH)
  960. C2S(WLAN_FC_STYPE_DEAUTH)
  961. C2S(WLAN_FC_STYPE_ACTION)
  962. }
  963. break;
  964. case WLAN_FC_TYPE_CTRL:
  965. switch (stype) {
  966. C2S(WLAN_FC_STYPE_PSPOLL)
  967. C2S(WLAN_FC_STYPE_RTS)
  968. C2S(WLAN_FC_STYPE_CTS)
  969. C2S(WLAN_FC_STYPE_ACK)
  970. C2S(WLAN_FC_STYPE_CFEND)
  971. C2S(WLAN_FC_STYPE_CFENDACK)
  972. }
  973. break;
  974. case WLAN_FC_TYPE_DATA:
  975. switch (stype) {
  976. C2S(WLAN_FC_STYPE_DATA)
  977. C2S(WLAN_FC_STYPE_DATA_CFACK)
  978. C2S(WLAN_FC_STYPE_DATA_CFPOLL)
  979. C2S(WLAN_FC_STYPE_DATA_CFACKPOLL)
  980. C2S(WLAN_FC_STYPE_NULLFUNC)
  981. C2S(WLAN_FC_STYPE_CFACK)
  982. C2S(WLAN_FC_STYPE_CFPOLL)
  983. C2S(WLAN_FC_STYPE_CFACKPOLL)
  984. C2S(WLAN_FC_STYPE_QOS_DATA)
  985. C2S(WLAN_FC_STYPE_QOS_DATA_CFACK)
  986. C2S(WLAN_FC_STYPE_QOS_DATA_CFPOLL)
  987. C2S(WLAN_FC_STYPE_QOS_DATA_CFACKPOLL)
  988. C2S(WLAN_FC_STYPE_QOS_NULL)
  989. C2S(WLAN_FC_STYPE_QOS_CFPOLL)
  990. C2S(WLAN_FC_STYPE_QOS_CFACKPOLL)
  991. }
  992. break;
  993. }
  994. return "WLAN_FC_TYPE_UNKNOWN";
  995. #undef C2S
  996. }
  997. int mb_ies_info_by_ies(struct mb_ies_info *info, const u8 *ies_buf,
  998. size_t ies_len)
  999. {
  1000. os_memset(info, 0, sizeof(*info));
  1001. while (ies_buf && ies_len >= 2 &&
  1002. info->nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
  1003. size_t len = 2 + ies_buf[1];
  1004. if (len > ies_len) {
  1005. wpa_hexdump(MSG_DEBUG, "Truncated IEs",
  1006. ies_buf, ies_len);
  1007. return -1;
  1008. }
  1009. if (ies_buf[0] == WLAN_EID_MULTI_BAND) {
  1010. wpa_printf(MSG_DEBUG, "MB IE of %zu bytes found", len);
  1011. info->ies[info->nof_ies].ie = ies_buf + 2;
  1012. info->ies[info->nof_ies].ie_len = ies_buf[1];
  1013. info->nof_ies++;
  1014. }
  1015. ies_len -= len;
  1016. ies_buf += len;
  1017. }
  1018. return 0;
  1019. }
  1020. struct wpabuf * mb_ies_by_info(struct mb_ies_info *info)
  1021. {
  1022. struct wpabuf *mb_ies = NULL;
  1023. WPA_ASSERT(info != NULL);
  1024. if (info->nof_ies) {
  1025. u8 i;
  1026. size_t mb_ies_size = 0;
  1027. for (i = 0; i < info->nof_ies; i++)
  1028. mb_ies_size += 2 + info->ies[i].ie_len;
  1029. mb_ies = wpabuf_alloc(mb_ies_size);
  1030. if (mb_ies) {
  1031. for (i = 0; i < info->nof_ies; i++) {
  1032. wpabuf_put_u8(mb_ies, WLAN_EID_MULTI_BAND);
  1033. wpabuf_put_u8(mb_ies, info->ies[i].ie_len);
  1034. wpabuf_put_data(mb_ies,
  1035. info->ies[i].ie,
  1036. info->ies[i].ie_len);
  1037. }
  1038. }
  1039. }
  1040. return mb_ies;
  1041. }