ieee802_11_common.c 31 KB

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