ieee802_11_common.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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. static int ieee802_11_parse_extension(const u8 *pos, size_t elen,
  170. struct ieee802_11_elems *elems,
  171. int show_errors)
  172. {
  173. u8 ext_id;
  174. if (elen < 1) {
  175. if (show_errors) {
  176. wpa_printf(MSG_MSGDUMP,
  177. "short information element (Ext)");
  178. }
  179. return -1;
  180. }
  181. ext_id = *pos++;
  182. elen--;
  183. switch (ext_id) {
  184. case WLAN_EID_EXT_ASSOC_DELAY_INFO:
  185. if (elen != 1)
  186. break;
  187. elems->assoc_delay_info = pos;
  188. break;
  189. case WLAN_EID_EXT_FILS_REQ_PARAMS:
  190. if (elen < 3)
  191. break;
  192. elems->fils_req_params = pos;
  193. elems->fils_req_params_len = elen;
  194. break;
  195. case WLAN_EID_EXT_FILS_KEY_CONFIRM:
  196. elems->fils_key_confirm = pos;
  197. elems->fils_key_confirm_len = elen;
  198. break;
  199. case WLAN_EID_EXT_FILS_SESSION:
  200. if (elen != FILS_SESSION_LEN)
  201. break;
  202. elems->fils_session = pos;
  203. break;
  204. case WLAN_EID_EXT_FILS_HLP_CONTAINER:
  205. if (elen < 2 * ETH_ALEN)
  206. break;
  207. elems->fils_hlp = pos;
  208. elems->fils_hlp_len = elen;
  209. break;
  210. case WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN:
  211. if (elen < 1)
  212. break;
  213. elems->fils_ip_addr_assign = pos;
  214. elems->fils_ip_addr_assign_len = elen;
  215. break;
  216. case WLAN_EID_EXT_KEY_DELIVERY:
  217. if (elen < WPA_KEY_RSC_LEN)
  218. break;
  219. elems->key_delivery = pos;
  220. elems->key_delivery_len = elen;
  221. break;
  222. case WLAN_EID_EXT_FILS_WRAPPED_DATA:
  223. elems->fils_wrapped_data = pos;
  224. elems->fils_wrapped_data_len = elen;
  225. break;
  226. case WLAN_EID_EXT_FILS_PUBLIC_KEY:
  227. if (elen < 1)
  228. break;
  229. elems->fils_pk = pos;
  230. elems->fils_pk_len = elen;
  231. break;
  232. case WLAN_EID_EXT_FILS_NONCE:
  233. if (elen != FILS_NONCE_LEN)
  234. break;
  235. elems->fils_nonce = pos;
  236. break;
  237. case WLAN_EID_EXT_OWE_DH_PARAM:
  238. if (elen < 2)
  239. break;
  240. elems->owe_dh = pos;
  241. elems->owe_dh_len = elen;
  242. break;
  243. default:
  244. if (show_errors) {
  245. wpa_printf(MSG_MSGDUMP,
  246. "IEEE 802.11 element parsing ignored unknown element extension (ext_id=%u elen=%u)",
  247. ext_id, (unsigned int) elen);
  248. }
  249. return -1;
  250. }
  251. return 0;
  252. }
  253. /**
  254. * ieee802_11_parse_elems - Parse information elements in management frames
  255. * @start: Pointer to the start of IEs
  256. * @len: Length of IE buffer in octets
  257. * @elems: Data structure for parsed elements
  258. * @show_errors: Whether to show parsing errors in debug log
  259. * Returns: Parsing result
  260. */
  261. ParseRes ieee802_11_parse_elems(const u8 *start, size_t len,
  262. struct ieee802_11_elems *elems,
  263. int show_errors)
  264. {
  265. size_t left = len;
  266. const u8 *pos = start;
  267. int unknown = 0;
  268. os_memset(elems, 0, sizeof(*elems));
  269. while (left >= 2) {
  270. u8 id, elen;
  271. id = *pos++;
  272. elen = *pos++;
  273. left -= 2;
  274. if (elen > left) {
  275. if (show_errors) {
  276. wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
  277. "parse failed (id=%d elen=%d "
  278. "left=%lu)",
  279. id, elen, (unsigned long) left);
  280. wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
  281. }
  282. return ParseFailed;
  283. }
  284. switch (id) {
  285. case WLAN_EID_SSID:
  286. if (elen > SSID_MAX_LEN) {
  287. wpa_printf(MSG_DEBUG,
  288. "Ignored too long SSID element (elen=%u)",
  289. elen);
  290. break;
  291. }
  292. elems->ssid = pos;
  293. elems->ssid_len = elen;
  294. break;
  295. case WLAN_EID_SUPP_RATES:
  296. elems->supp_rates = pos;
  297. elems->supp_rates_len = elen;
  298. break;
  299. case WLAN_EID_DS_PARAMS:
  300. if (elen < 1)
  301. break;
  302. elems->ds_params = pos;
  303. break;
  304. case WLAN_EID_CF_PARAMS:
  305. case WLAN_EID_TIM:
  306. break;
  307. case WLAN_EID_CHALLENGE:
  308. elems->challenge = pos;
  309. elems->challenge_len = elen;
  310. break;
  311. case WLAN_EID_ERP_INFO:
  312. if (elen < 1)
  313. break;
  314. elems->erp_info = pos;
  315. break;
  316. case WLAN_EID_EXT_SUPP_RATES:
  317. elems->ext_supp_rates = pos;
  318. elems->ext_supp_rates_len = elen;
  319. break;
  320. case WLAN_EID_VENDOR_SPECIFIC:
  321. if (ieee802_11_parse_vendor_specific(pos, elen,
  322. elems,
  323. show_errors))
  324. unknown++;
  325. break;
  326. case WLAN_EID_RSN:
  327. elems->rsn_ie = pos;
  328. elems->rsn_ie_len = elen;
  329. break;
  330. case WLAN_EID_PWR_CAPABILITY:
  331. break;
  332. case WLAN_EID_SUPPORTED_CHANNELS:
  333. elems->supp_channels = pos;
  334. elems->supp_channels_len = elen;
  335. break;
  336. case WLAN_EID_MOBILITY_DOMAIN:
  337. if (elen < sizeof(struct rsn_mdie))
  338. break;
  339. elems->mdie = pos;
  340. elems->mdie_len = elen;
  341. break;
  342. case WLAN_EID_FAST_BSS_TRANSITION:
  343. if (elen < sizeof(struct rsn_ftie))
  344. break;
  345. elems->ftie = pos;
  346. elems->ftie_len = elen;
  347. break;
  348. case WLAN_EID_TIMEOUT_INTERVAL:
  349. if (elen != 5)
  350. break;
  351. elems->timeout_int = pos;
  352. break;
  353. case WLAN_EID_HT_CAP:
  354. if (elen < sizeof(struct ieee80211_ht_capabilities))
  355. break;
  356. elems->ht_capabilities = pos;
  357. break;
  358. case WLAN_EID_HT_OPERATION:
  359. if (elen < sizeof(struct ieee80211_ht_operation))
  360. break;
  361. elems->ht_operation = pos;
  362. break;
  363. case WLAN_EID_MESH_CONFIG:
  364. elems->mesh_config = pos;
  365. elems->mesh_config_len = elen;
  366. break;
  367. case WLAN_EID_MESH_ID:
  368. elems->mesh_id = pos;
  369. elems->mesh_id_len = elen;
  370. break;
  371. case WLAN_EID_PEER_MGMT:
  372. elems->peer_mgmt = pos;
  373. elems->peer_mgmt_len = elen;
  374. break;
  375. case WLAN_EID_VHT_CAP:
  376. if (elen < sizeof(struct ieee80211_vht_capabilities))
  377. break;
  378. elems->vht_capabilities = pos;
  379. break;
  380. case WLAN_EID_VHT_OPERATION:
  381. if (elen < sizeof(struct ieee80211_vht_operation))
  382. break;
  383. elems->vht_operation = pos;
  384. break;
  385. case WLAN_EID_VHT_OPERATING_MODE_NOTIFICATION:
  386. if (elen != 1)
  387. break;
  388. elems->vht_opmode_notif = pos;
  389. break;
  390. case WLAN_EID_LINK_ID:
  391. if (elen < 18)
  392. break;
  393. elems->link_id = pos;
  394. break;
  395. case WLAN_EID_INTERWORKING:
  396. elems->interworking = pos;
  397. elems->interworking_len = elen;
  398. break;
  399. case WLAN_EID_QOS_MAP_SET:
  400. if (elen < 16)
  401. break;
  402. elems->qos_map_set = pos;
  403. elems->qos_map_set_len = elen;
  404. break;
  405. case WLAN_EID_EXT_CAPAB:
  406. elems->ext_capab = pos;
  407. elems->ext_capab_len = elen;
  408. break;
  409. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  410. if (elen < 3)
  411. break;
  412. elems->bss_max_idle_period = pos;
  413. break;
  414. case WLAN_EID_SSID_LIST:
  415. elems->ssid_list = pos;
  416. elems->ssid_list_len = elen;
  417. break;
  418. case WLAN_EID_AMPE:
  419. elems->ampe = pos;
  420. elems->ampe_len = elen;
  421. break;
  422. case WLAN_EID_MIC:
  423. elems->mic = pos;
  424. elems->mic_len = elen;
  425. /* after mic everything is encrypted, so stop. */
  426. left = elen;
  427. break;
  428. case WLAN_EID_MULTI_BAND:
  429. if (elems->mb_ies.nof_ies >= MAX_NOF_MB_IES_SUPPORTED) {
  430. wpa_printf(MSG_MSGDUMP,
  431. "IEEE 802.11 element parse ignored MB IE (id=%d elen=%d)",
  432. id, elen);
  433. break;
  434. }
  435. elems->mb_ies.ies[elems->mb_ies.nof_ies].ie = pos;
  436. elems->mb_ies.ies[elems->mb_ies.nof_ies].ie_len = elen;
  437. elems->mb_ies.nof_ies++;
  438. break;
  439. case WLAN_EID_SUPPORTED_OPERATING_CLASSES:
  440. elems->supp_op_classes = pos;
  441. elems->supp_op_classes_len = elen;
  442. break;
  443. case WLAN_EID_RRM_ENABLED_CAPABILITIES:
  444. elems->rrm_enabled = pos;
  445. elems->rrm_enabled_len = elen;
  446. break;
  447. case WLAN_EID_CAG_NUMBER:
  448. elems->cag_number = pos;
  449. elems->cag_number_len = elen;
  450. break;
  451. case WLAN_EID_AP_CSN:
  452. if (elen < 1)
  453. break;
  454. elems->ap_csn = pos;
  455. break;
  456. case WLAN_EID_FILS_INDICATION:
  457. if (elen < 2)
  458. break;
  459. elems->fils_indic = pos;
  460. elems->fils_indic_len = elen;
  461. break;
  462. case WLAN_EID_DILS:
  463. if (elen < 2)
  464. break;
  465. elems->dils = pos;
  466. elems->dils_len = elen;
  467. break;
  468. case WLAN_EID_FRAGMENT:
  469. /* TODO */
  470. break;
  471. case WLAN_EID_EXTENSION:
  472. if (ieee802_11_parse_extension(pos, elen, elems,
  473. show_errors))
  474. unknown++;
  475. break;
  476. default:
  477. unknown++;
  478. if (!show_errors)
  479. break;
  480. wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
  481. "ignored unknown element (id=%d elen=%d)",
  482. id, elen);
  483. break;
  484. }
  485. left -= elen;
  486. pos += elen;
  487. }
  488. if (left)
  489. return ParseFailed;
  490. return unknown ? ParseUnknown : ParseOK;
  491. }
  492. int ieee802_11_ie_count(const u8 *ies, size_t ies_len)
  493. {
  494. int count = 0;
  495. const u8 *pos, *end;
  496. if (ies == NULL)
  497. return 0;
  498. pos = ies;
  499. end = ies + ies_len;
  500. while (end - pos >= 2) {
  501. if (2 + pos[1] > end - pos)
  502. break;
  503. count++;
  504. pos += 2 + pos[1];
  505. }
  506. return count;
  507. }
  508. struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
  509. u32 oui_type)
  510. {
  511. struct wpabuf *buf;
  512. const u8 *end, *pos, *ie;
  513. pos = ies;
  514. end = ies + ies_len;
  515. ie = NULL;
  516. while (end - pos > 1) {
  517. if (2 + pos[1] > end - pos)
  518. return NULL;
  519. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  520. WPA_GET_BE32(&pos[2]) == oui_type) {
  521. ie = pos;
  522. break;
  523. }
  524. pos += 2 + pos[1];
  525. }
  526. if (ie == NULL)
  527. return NULL; /* No specified vendor IE found */
  528. buf = wpabuf_alloc(ies_len);
  529. if (buf == NULL)
  530. return NULL;
  531. /*
  532. * There may be multiple vendor IEs in the message, so need to
  533. * concatenate their data fields.
  534. */
  535. while (end - pos > 1) {
  536. if (2 + pos[1] > end - pos)
  537. break;
  538. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  539. WPA_GET_BE32(&pos[2]) == oui_type)
  540. wpabuf_put_data(buf, pos + 6, pos[1] - 4);
  541. pos += 2 + pos[1];
  542. }
  543. return buf;
  544. }
  545. const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  546. {
  547. u16 fc, type, stype;
  548. /*
  549. * PS-Poll frames are 16 bytes. All other frames are
  550. * 24 bytes or longer.
  551. */
  552. if (len < 16)
  553. return NULL;
  554. fc = le_to_host16(hdr->frame_control);
  555. type = WLAN_FC_GET_TYPE(fc);
  556. stype = WLAN_FC_GET_STYPE(fc);
  557. switch (type) {
  558. case WLAN_FC_TYPE_DATA:
  559. if (len < 24)
  560. return NULL;
  561. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  562. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  563. case WLAN_FC_TODS:
  564. return hdr->addr1;
  565. case WLAN_FC_FROMDS:
  566. return hdr->addr2;
  567. default:
  568. return NULL;
  569. }
  570. case WLAN_FC_TYPE_CTRL:
  571. if (stype != WLAN_FC_STYPE_PSPOLL)
  572. return NULL;
  573. return hdr->addr1;
  574. case WLAN_FC_TYPE_MGMT:
  575. return hdr->addr3;
  576. default:
  577. return NULL;
  578. }
  579. }
  580. int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
  581. const char *name, const char *val)
  582. {
  583. int num, v;
  584. const char *pos;
  585. struct hostapd_wmm_ac_params *ac;
  586. /* skip 'wme_ac_' or 'wmm_ac_' prefix */
  587. pos = name + 7;
  588. if (os_strncmp(pos, "be_", 3) == 0) {
  589. num = 0;
  590. pos += 3;
  591. } else if (os_strncmp(pos, "bk_", 3) == 0) {
  592. num = 1;
  593. pos += 3;
  594. } else if (os_strncmp(pos, "vi_", 3) == 0) {
  595. num = 2;
  596. pos += 3;
  597. } else if (os_strncmp(pos, "vo_", 3) == 0) {
  598. num = 3;
  599. pos += 3;
  600. } else {
  601. wpa_printf(MSG_ERROR, "Unknown WMM name '%s'", pos);
  602. return -1;
  603. }
  604. ac = &wmm_ac_params[num];
  605. if (os_strcmp(pos, "aifs") == 0) {
  606. v = atoi(val);
  607. if (v < 1 || v > 255) {
  608. wpa_printf(MSG_ERROR, "Invalid AIFS value %d", v);
  609. return -1;
  610. }
  611. ac->aifs = v;
  612. } else if (os_strcmp(pos, "cwmin") == 0) {
  613. v = atoi(val);
  614. if (v < 0 || v > 15) {
  615. wpa_printf(MSG_ERROR, "Invalid cwMin value %d", v);
  616. return -1;
  617. }
  618. ac->cwmin = v;
  619. } else if (os_strcmp(pos, "cwmax") == 0) {
  620. v = atoi(val);
  621. if (v < 0 || v > 15) {
  622. wpa_printf(MSG_ERROR, "Invalid cwMax value %d", v);
  623. return -1;
  624. }
  625. ac->cwmax = v;
  626. } else if (os_strcmp(pos, "txop_limit") == 0) {
  627. v = atoi(val);
  628. if (v < 0 || v > 0xffff) {
  629. wpa_printf(MSG_ERROR, "Invalid txop value %d", v);
  630. return -1;
  631. }
  632. ac->txop_limit = v;
  633. } else if (os_strcmp(pos, "acm") == 0) {
  634. v = atoi(val);
  635. if (v < 0 || v > 1) {
  636. wpa_printf(MSG_ERROR, "Invalid acm value %d", v);
  637. return -1;
  638. }
  639. ac->admission_control_mandatory = v;
  640. } else {
  641. wpa_printf(MSG_ERROR, "Unknown wmm_ac_ field '%s'", pos);
  642. return -1;
  643. }
  644. return 0;
  645. }
  646. enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel)
  647. {
  648. u8 op_class;
  649. return ieee80211_freq_to_channel_ext(freq, 0, VHT_CHANWIDTH_USE_HT,
  650. &op_class, channel);
  651. }
  652. /**
  653. * ieee80211_freq_to_channel_ext - Convert frequency into channel info
  654. * for HT40 and VHT. DFS channels are not covered.
  655. * @freq: Frequency (MHz) to convert
  656. * @sec_channel: 0 = non-HT40, 1 = sec. channel above, -1 = sec. channel below
  657. * @vht: VHT channel width (VHT_CHANWIDTH_*)
  658. * @op_class: Buffer for returning operating class
  659. * @channel: Buffer for returning channel number
  660. * Returns: hw_mode on success, NUM_HOSTAPD_MODES on failure
  661. */
  662. enum hostapd_hw_mode ieee80211_freq_to_channel_ext(unsigned int freq,
  663. int sec_channel, int vht,
  664. u8 *op_class, u8 *channel)
  665. {
  666. u8 vht_opclass;
  667. /* TODO: more operating classes */
  668. if (sec_channel > 1 || sec_channel < -1)
  669. return NUM_HOSTAPD_MODES;
  670. if (freq >= 2412 && freq <= 2472) {
  671. if ((freq - 2407) % 5)
  672. return NUM_HOSTAPD_MODES;
  673. if (vht)
  674. return NUM_HOSTAPD_MODES;
  675. /* 2.407 GHz, channels 1..13 */
  676. if (sec_channel == 1)
  677. *op_class = 83;
  678. else if (sec_channel == -1)
  679. *op_class = 84;
  680. else
  681. *op_class = 81;
  682. *channel = (freq - 2407) / 5;
  683. return HOSTAPD_MODE_IEEE80211G;
  684. }
  685. if (freq == 2484) {
  686. if (sec_channel || vht)
  687. return NUM_HOSTAPD_MODES;
  688. *op_class = 82; /* channel 14 */
  689. *channel = 14;
  690. return HOSTAPD_MODE_IEEE80211B;
  691. }
  692. if (freq >= 4900 && freq < 5000) {
  693. if ((freq - 4000) % 5)
  694. return NUM_HOSTAPD_MODES;
  695. *channel = (freq - 4000) / 5;
  696. *op_class = 0; /* TODO */
  697. return HOSTAPD_MODE_IEEE80211A;
  698. }
  699. switch (vht) {
  700. case VHT_CHANWIDTH_80MHZ:
  701. vht_opclass = 128;
  702. break;
  703. case VHT_CHANWIDTH_160MHZ:
  704. vht_opclass = 129;
  705. break;
  706. case VHT_CHANWIDTH_80P80MHZ:
  707. vht_opclass = 130;
  708. break;
  709. default:
  710. vht_opclass = 0;
  711. break;
  712. }
  713. /* 5 GHz, channels 36..48 */
  714. if (freq >= 5180 && freq <= 5240) {
  715. if ((freq - 5000) % 5)
  716. return NUM_HOSTAPD_MODES;
  717. if (vht_opclass)
  718. *op_class = vht_opclass;
  719. else if (sec_channel == 1)
  720. *op_class = 116;
  721. else if (sec_channel == -1)
  722. *op_class = 117;
  723. else
  724. *op_class = 115;
  725. *channel = (freq - 5000) / 5;
  726. return HOSTAPD_MODE_IEEE80211A;
  727. }
  728. /* 5 GHz, channels 52..64 */
  729. if (freq >= 5260 && freq <= 5320) {
  730. if ((freq - 5000) % 5)
  731. return NUM_HOSTAPD_MODES;
  732. if (vht_opclass)
  733. *op_class = vht_opclass;
  734. else if (sec_channel == 1)
  735. *op_class = 119;
  736. else if (sec_channel == -1)
  737. *op_class = 120;
  738. else
  739. *op_class = 118;
  740. *channel = (freq - 5000) / 5;
  741. return HOSTAPD_MODE_IEEE80211A;
  742. }
  743. /* 5 GHz, channels 149..169 */
  744. if (freq >= 5745 && freq <= 5845) {
  745. if ((freq - 5000) % 5)
  746. return NUM_HOSTAPD_MODES;
  747. if (vht_opclass)
  748. *op_class = vht_opclass;
  749. else if (sec_channel == 1)
  750. *op_class = 126;
  751. else if (sec_channel == -1)
  752. *op_class = 127;
  753. else if (freq <= 5805)
  754. *op_class = 124;
  755. else
  756. *op_class = 125;
  757. *channel = (freq - 5000) / 5;
  758. return HOSTAPD_MODE_IEEE80211A;
  759. }
  760. /* 5 GHz, channels 100..140 */
  761. if (freq >= 5000 && freq <= 5700) {
  762. if ((freq - 5000) % 5)
  763. return NUM_HOSTAPD_MODES;
  764. if (vht_opclass)
  765. *op_class = vht_opclass;
  766. else if (sec_channel == 1)
  767. *op_class = 122;
  768. else if (sec_channel == -1)
  769. *op_class = 123;
  770. else
  771. *op_class = 121;
  772. *channel = (freq - 5000) / 5;
  773. return HOSTAPD_MODE_IEEE80211A;
  774. }
  775. if (freq >= 5000 && freq < 5900) {
  776. if ((freq - 5000) % 5)
  777. return NUM_HOSTAPD_MODES;
  778. *channel = (freq - 5000) / 5;
  779. *op_class = 0; /* TODO */
  780. return HOSTAPD_MODE_IEEE80211A;
  781. }
  782. /* 56.16 GHz, channel 1..4 */
  783. if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  784. if (sec_channel || vht)
  785. return NUM_HOSTAPD_MODES;
  786. *channel = (freq - 56160) / 2160;
  787. *op_class = 180;
  788. return HOSTAPD_MODE_IEEE80211AD;
  789. }
  790. return NUM_HOSTAPD_MODES;
  791. }
  792. static const char *const us_op_class_cc[] = {
  793. "US", "CA", NULL
  794. };
  795. static const char *const eu_op_class_cc[] = {
  796. "AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
  797. "DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
  798. "LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
  799. "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", NULL
  800. };
  801. static const char *const jp_op_class_cc[] = {
  802. "JP", NULL
  803. };
  804. static const char *const cn_op_class_cc[] = {
  805. "CN", NULL
  806. };
  807. static int country_match(const char *const cc[], const char *const country)
  808. {
  809. int i;
  810. if (country == NULL)
  811. return 0;
  812. for (i = 0; cc[i]; i++) {
  813. if (cc[i][0] == country[0] && cc[i][1] == country[1])
  814. return 1;
  815. }
  816. return 0;
  817. }
  818. static int ieee80211_chan_to_freq_us(u8 op_class, u8 chan)
  819. {
  820. switch (op_class) {
  821. case 12: /* channels 1..11 */
  822. case 32: /* channels 1..7; 40 MHz */
  823. case 33: /* channels 5..11; 40 MHz */
  824. if (chan < 1 || chan > 11)
  825. return -1;
  826. return 2407 + 5 * chan;
  827. case 1: /* channels 36,40,44,48 */
  828. case 2: /* channels 52,56,60,64; dfs */
  829. case 22: /* channels 36,44; 40 MHz */
  830. case 23: /* channels 52,60; 40 MHz */
  831. case 27: /* channels 40,48; 40 MHz */
  832. case 28: /* channels 56,64; 40 MHz */
  833. if (chan < 36 || chan > 64)
  834. return -1;
  835. return 5000 + 5 * chan;
  836. case 4: /* channels 100-144 */
  837. case 24: /* channels 100-140; 40 MHz */
  838. if (chan < 100 || chan > 144)
  839. return -1;
  840. return 5000 + 5 * chan;
  841. case 3: /* channels 149,153,157,161 */
  842. case 25: /* channels 149,157; 40 MHz */
  843. case 26: /* channels 149,157; 40 MHz */
  844. case 30: /* channels 153,161; 40 MHz */
  845. case 31: /* channels 153,161; 40 MHz */
  846. if (chan < 149 || chan > 161)
  847. return -1;
  848. return 5000 + 5 * chan;
  849. case 5: /* channels 149,153,157,161,165 */
  850. if (chan < 149 || chan > 165)
  851. return -1;
  852. return 5000 + 5 * chan;
  853. case 34: /* 60 GHz band, channels 1..3 */
  854. if (chan < 1 || chan > 3)
  855. return -1;
  856. return 56160 + 2160 * chan;
  857. }
  858. return -1;
  859. }
  860. static int ieee80211_chan_to_freq_eu(u8 op_class, u8 chan)
  861. {
  862. switch (op_class) {
  863. case 4: /* channels 1..13 */
  864. case 11: /* channels 1..9; 40 MHz */
  865. case 12: /* channels 5..13; 40 MHz */
  866. if (chan < 1 || chan > 13)
  867. return -1;
  868. return 2407 + 5 * chan;
  869. case 1: /* channels 36,40,44,48 */
  870. case 2: /* channels 52,56,60,64; dfs */
  871. case 5: /* channels 36,44; 40 MHz */
  872. case 6: /* channels 52,60; 40 MHz */
  873. case 8: /* channels 40,48; 40 MHz */
  874. case 9: /* channels 56,64; 40 MHz */
  875. if (chan < 36 || chan > 64)
  876. return -1;
  877. return 5000 + 5 * chan;
  878. case 3: /* channels 100-140 */
  879. case 7: /* channels 100-132; 40 MHz */
  880. case 10: /* channels 104-136; 40 MHz */
  881. case 16: /* channels 100-140 */
  882. if (chan < 100 || chan > 140)
  883. return -1;
  884. return 5000 + 5 * chan;
  885. case 17: /* channels 149,153,157,161,165,169 */
  886. if (chan < 149 || chan > 169)
  887. return -1;
  888. return 5000 + 5 * chan;
  889. case 18: /* 60 GHz band, channels 1..4 */
  890. if (chan < 1 || chan > 4)
  891. return -1;
  892. return 56160 + 2160 * chan;
  893. }
  894. return -1;
  895. }
  896. static int ieee80211_chan_to_freq_jp(u8 op_class, u8 chan)
  897. {
  898. switch (op_class) {
  899. case 30: /* channels 1..13 */
  900. case 56: /* channels 1..9; 40 MHz */
  901. case 57: /* channels 5..13; 40 MHz */
  902. if (chan < 1 || chan > 13)
  903. return -1;
  904. return 2407 + 5 * chan;
  905. case 31: /* channel 14 */
  906. if (chan != 14)
  907. return -1;
  908. return 2414 + 5 * chan;
  909. case 1: /* channels 34,38,42,46(old) or 36,40,44,48 */
  910. case 32: /* channels 52,56,60,64 */
  911. case 33: /* channels 52,56,60,64 */
  912. case 36: /* channels 36,44; 40 MHz */
  913. case 37: /* channels 52,60; 40 MHz */
  914. case 38: /* channels 52,60; 40 MHz */
  915. case 41: /* channels 40,48; 40 MHz */
  916. case 42: /* channels 56,64; 40 MHz */
  917. case 43: /* channels 56,64; 40 MHz */
  918. if (chan < 34 || chan > 64)
  919. return -1;
  920. return 5000 + 5 * chan;
  921. case 34: /* channels 100-140 */
  922. case 35: /* channels 100-140 */
  923. case 39: /* channels 100-132; 40 MHz */
  924. case 40: /* channels 100-132; 40 MHz */
  925. case 44: /* channels 104-136; 40 MHz */
  926. case 45: /* channels 104-136; 40 MHz */
  927. case 58: /* channels 100-140 */
  928. if (chan < 100 || chan > 140)
  929. return -1;
  930. return 5000 + 5 * chan;
  931. case 59: /* 60 GHz band, channels 1..4 */
  932. if (chan < 1 || chan > 3)
  933. return -1;
  934. return 56160 + 2160 * chan;
  935. }
  936. return -1;
  937. }
  938. static int ieee80211_chan_to_freq_cn(u8 op_class, u8 chan)
  939. {
  940. switch (op_class) {
  941. case 7: /* channels 1..13 */
  942. case 8: /* channels 1..9; 40 MHz */
  943. case 9: /* channels 5..13; 40 MHz */
  944. if (chan < 1 || chan > 13)
  945. return -1;
  946. return 2407 + 5 * chan;
  947. case 1: /* channels 36,40,44,48 */
  948. case 2: /* channels 52,56,60,64; dfs */
  949. case 4: /* channels 36,44; 40 MHz */
  950. case 5: /* channels 52,60; 40 MHz */
  951. if (chan < 36 || chan > 64)
  952. return -1;
  953. return 5000 + 5 * chan;
  954. case 3: /* channels 149,153,157,161,165 */
  955. case 6: /* channels 149,157; 40 MHz */
  956. if (chan < 149 || chan > 165)
  957. return -1;
  958. return 5000 + 5 * chan;
  959. }
  960. return -1;
  961. }
  962. static int ieee80211_chan_to_freq_global(u8 op_class, u8 chan)
  963. {
  964. /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */
  965. switch (op_class) {
  966. case 81:
  967. /* channels 1..13 */
  968. if (chan < 1 || chan > 13)
  969. return -1;
  970. return 2407 + 5 * chan;
  971. case 82:
  972. /* channel 14 */
  973. if (chan != 14)
  974. return -1;
  975. return 2414 + 5 * chan;
  976. case 83: /* channels 1..9; 40 MHz */
  977. case 84: /* channels 5..13; 40 MHz */
  978. if (chan < 1 || chan > 13)
  979. return -1;
  980. return 2407 + 5 * chan;
  981. case 115: /* channels 36,40,44,48; indoor only */
  982. case 116: /* channels 36,44; 40 MHz; indoor only */
  983. case 117: /* channels 40,48; 40 MHz; indoor only */
  984. case 118: /* channels 52,56,60,64; dfs */
  985. case 119: /* channels 52,60; 40 MHz; dfs */
  986. case 120: /* channels 56,64; 40 MHz; dfs */
  987. if (chan < 36 || chan > 64)
  988. return -1;
  989. return 5000 + 5 * chan;
  990. case 121: /* channels 100-140 */
  991. case 122: /* channels 100-142; 40 MHz */
  992. case 123: /* channels 104-136; 40 MHz */
  993. if (chan < 100 || chan > 140)
  994. return -1;
  995. return 5000 + 5 * chan;
  996. case 124: /* channels 149,153,157,161 */
  997. case 126: /* channels 149,157; 40 MHz */
  998. case 127: /* channels 153,161; 40 MHz */
  999. if (chan < 149 || chan > 161)
  1000. return -1;
  1001. return 5000 + 5 * chan;
  1002. case 125: /* channels 149,153,157,161,165,169 */
  1003. if (chan < 149 || chan > 169)
  1004. return -1;
  1005. return 5000 + 5 * chan;
  1006. case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  1007. case 130: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  1008. if (chan < 36 || chan > 161)
  1009. return -1;
  1010. return 5000 + 5 * chan;
  1011. case 129: /* center freqs 50, 114; 160 MHz */
  1012. if (chan < 36 || chan > 128)
  1013. return -1;
  1014. return 5000 + 5 * chan;
  1015. case 180: /* 60 GHz band, channels 1..4 */
  1016. if (chan < 1 || chan > 4)
  1017. return -1;
  1018. return 56160 + 2160 * chan;
  1019. }
  1020. return -1;
  1021. }
  1022. /**
  1023. * ieee80211_chan_to_freq - Convert channel info to frequency
  1024. * @country: Country code, if known; otherwise, global operating class is used
  1025. * @op_class: Operating class
  1026. * @chan: Channel number
  1027. * Returns: Frequency in MHz or -1 if the specified channel is unknown
  1028. */
  1029. int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan)
  1030. {
  1031. int freq;
  1032. if (country_match(us_op_class_cc, country)) {
  1033. freq = ieee80211_chan_to_freq_us(op_class, chan);
  1034. if (freq > 0)
  1035. return freq;
  1036. }
  1037. if (country_match(eu_op_class_cc, country)) {
  1038. freq = ieee80211_chan_to_freq_eu(op_class, chan);
  1039. if (freq > 0)
  1040. return freq;
  1041. }
  1042. if (country_match(jp_op_class_cc, country)) {
  1043. freq = ieee80211_chan_to_freq_jp(op_class, chan);
  1044. if (freq > 0)
  1045. return freq;
  1046. }
  1047. if (country_match(cn_op_class_cc, country)) {
  1048. freq = ieee80211_chan_to_freq_cn(op_class, chan);
  1049. if (freq > 0)
  1050. return freq;
  1051. }
  1052. return ieee80211_chan_to_freq_global(op_class, chan);
  1053. }
  1054. int ieee80211_is_dfs(int freq)
  1055. {
  1056. /* TODO: this could be more accurate to better cover all domains */
  1057. return (freq >= 5260 && freq <= 5320) || (freq >= 5500 && freq <= 5700);
  1058. }
  1059. static int is_11b(u8 rate)
  1060. {
  1061. return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
  1062. }
  1063. int supp_rates_11b_only(struct ieee802_11_elems *elems)
  1064. {
  1065. int num_11b = 0, num_others = 0;
  1066. int i;
  1067. if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
  1068. return 0;
  1069. for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
  1070. if (is_11b(elems->supp_rates[i]))
  1071. num_11b++;
  1072. else
  1073. num_others++;
  1074. }
  1075. for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
  1076. i++) {
  1077. if (is_11b(elems->ext_supp_rates[i]))
  1078. num_11b++;
  1079. else
  1080. num_others++;
  1081. }
  1082. return num_11b > 0 && num_others == 0;
  1083. }
  1084. const char * fc2str(u16 fc)
  1085. {
  1086. u16 stype = WLAN_FC_GET_STYPE(fc);
  1087. #define C2S(x) case x: return #x;
  1088. switch (WLAN_FC_GET_TYPE(fc)) {
  1089. case WLAN_FC_TYPE_MGMT:
  1090. switch (stype) {
  1091. C2S(WLAN_FC_STYPE_ASSOC_REQ)
  1092. C2S(WLAN_FC_STYPE_ASSOC_RESP)
  1093. C2S(WLAN_FC_STYPE_REASSOC_REQ)
  1094. C2S(WLAN_FC_STYPE_REASSOC_RESP)
  1095. C2S(WLAN_FC_STYPE_PROBE_REQ)
  1096. C2S(WLAN_FC_STYPE_PROBE_RESP)
  1097. C2S(WLAN_FC_STYPE_BEACON)
  1098. C2S(WLAN_FC_STYPE_ATIM)
  1099. C2S(WLAN_FC_STYPE_DISASSOC)
  1100. C2S(WLAN_FC_STYPE_AUTH)
  1101. C2S(WLAN_FC_STYPE_DEAUTH)
  1102. C2S(WLAN_FC_STYPE_ACTION)
  1103. }
  1104. break;
  1105. case WLAN_FC_TYPE_CTRL:
  1106. switch (stype) {
  1107. C2S(WLAN_FC_STYPE_PSPOLL)
  1108. C2S(WLAN_FC_STYPE_RTS)
  1109. C2S(WLAN_FC_STYPE_CTS)
  1110. C2S(WLAN_FC_STYPE_ACK)
  1111. C2S(WLAN_FC_STYPE_CFEND)
  1112. C2S(WLAN_FC_STYPE_CFENDACK)
  1113. }
  1114. break;
  1115. case WLAN_FC_TYPE_DATA:
  1116. switch (stype) {
  1117. C2S(WLAN_FC_STYPE_DATA)
  1118. C2S(WLAN_FC_STYPE_DATA_CFACK)
  1119. C2S(WLAN_FC_STYPE_DATA_CFPOLL)
  1120. C2S(WLAN_FC_STYPE_DATA_CFACKPOLL)
  1121. C2S(WLAN_FC_STYPE_NULLFUNC)
  1122. C2S(WLAN_FC_STYPE_CFACK)
  1123. C2S(WLAN_FC_STYPE_CFPOLL)
  1124. C2S(WLAN_FC_STYPE_CFACKPOLL)
  1125. C2S(WLAN_FC_STYPE_QOS_DATA)
  1126. C2S(WLAN_FC_STYPE_QOS_DATA_CFACK)
  1127. C2S(WLAN_FC_STYPE_QOS_DATA_CFPOLL)
  1128. C2S(WLAN_FC_STYPE_QOS_DATA_CFACKPOLL)
  1129. C2S(WLAN_FC_STYPE_QOS_NULL)
  1130. C2S(WLAN_FC_STYPE_QOS_CFPOLL)
  1131. C2S(WLAN_FC_STYPE_QOS_CFACKPOLL)
  1132. }
  1133. break;
  1134. }
  1135. return "WLAN_FC_TYPE_UNKNOWN";
  1136. #undef C2S
  1137. }
  1138. int mb_ies_info_by_ies(struct mb_ies_info *info, const u8 *ies_buf,
  1139. size_t ies_len)
  1140. {
  1141. os_memset(info, 0, sizeof(*info));
  1142. while (ies_buf && ies_len >= 2 &&
  1143. info->nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
  1144. size_t len = 2 + ies_buf[1];
  1145. if (len > ies_len) {
  1146. wpa_hexdump(MSG_DEBUG, "Truncated IEs",
  1147. ies_buf, ies_len);
  1148. return -1;
  1149. }
  1150. if (ies_buf[0] == WLAN_EID_MULTI_BAND) {
  1151. wpa_printf(MSG_DEBUG, "MB IE of %zu bytes found", len);
  1152. info->ies[info->nof_ies].ie = ies_buf + 2;
  1153. info->ies[info->nof_ies].ie_len = ies_buf[1];
  1154. info->nof_ies++;
  1155. }
  1156. ies_len -= len;
  1157. ies_buf += len;
  1158. }
  1159. return 0;
  1160. }
  1161. struct wpabuf * mb_ies_by_info(struct mb_ies_info *info)
  1162. {
  1163. struct wpabuf *mb_ies = NULL;
  1164. WPA_ASSERT(info != NULL);
  1165. if (info->nof_ies) {
  1166. u8 i;
  1167. size_t mb_ies_size = 0;
  1168. for (i = 0; i < info->nof_ies; i++)
  1169. mb_ies_size += 2 + info->ies[i].ie_len;
  1170. mb_ies = wpabuf_alloc(mb_ies_size);
  1171. if (mb_ies) {
  1172. for (i = 0; i < info->nof_ies; i++) {
  1173. wpabuf_put_u8(mb_ies, WLAN_EID_MULTI_BAND);
  1174. wpabuf_put_u8(mb_ies, info->ies[i].ie_len);
  1175. wpabuf_put_data(mb_ies,
  1176. info->ies[i].ie,
  1177. info->ies[i].ie_len);
  1178. }
  1179. }
  1180. }
  1181. return mb_ies;
  1182. }
  1183. const struct oper_class_map global_op_class[] = {
  1184. { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20, P2P_SUPP },
  1185. { HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20, NO_P2P_SUPP },
  1186. /* Do not enable HT40 on 2.4 GHz for P2P use for now */
  1187. { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS, NO_P2P_SUPP },
  1188. { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS, NO_P2P_SUPP },
  1189. { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20, P2P_SUPP },
  1190. { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS, P2P_SUPP },
  1191. { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS, P2P_SUPP },
  1192. { HOSTAPD_MODE_IEEE80211A, 118, 52, 64, 4, BW20, NO_P2P_SUPP },
  1193. { HOSTAPD_MODE_IEEE80211A, 119, 52, 60, 8, BW40PLUS, NO_P2P_SUPP },
  1194. { HOSTAPD_MODE_IEEE80211A, 120, 56, 64, 8, BW40MINUS, NO_P2P_SUPP },
  1195. { HOSTAPD_MODE_IEEE80211A, 121, 100, 140, 4, BW20, NO_P2P_SUPP },
  1196. { HOSTAPD_MODE_IEEE80211A, 122, 100, 132, 8, BW40PLUS, NO_P2P_SUPP },
  1197. { HOSTAPD_MODE_IEEE80211A, 123, 104, 136, 8, BW40MINUS, NO_P2P_SUPP },
  1198. { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20, P2P_SUPP },
  1199. { HOSTAPD_MODE_IEEE80211A, 125, 149, 169, 4, BW20, P2P_SUPP },
  1200. { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS, P2P_SUPP },
  1201. { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS, P2P_SUPP },
  1202. /*
  1203. * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
  1204. * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
  1205. * 80 MHz, but currently use the following definition for simplicity
  1206. * (these center frequencies are not actual channels, which makes
  1207. * wpas_p2p_allow_channel() fail). wpas_p2p_verify_80mhz() should take
  1208. * care of removing invalid channels.
  1209. */
  1210. { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80, P2P_SUPP },
  1211. { HOSTAPD_MODE_IEEE80211A, 129, 50, 114, 16, BW160, P2P_SUPP },
  1212. { HOSTAPD_MODE_IEEE80211A, 130, 36, 161, 4, BW80P80, P2P_SUPP },
  1213. { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160, P2P_SUPP },
  1214. { -1, 0, 0, 0, 0, BW20, NO_P2P_SUPP }
  1215. };
  1216. static enum phy_type ieee80211_phy_type_by_freq(int freq)
  1217. {
  1218. enum hostapd_hw_mode hw_mode;
  1219. u8 channel;
  1220. hw_mode = ieee80211_freq_to_chan(freq, &channel);
  1221. switch (hw_mode) {
  1222. case HOSTAPD_MODE_IEEE80211A:
  1223. return PHY_TYPE_OFDM;
  1224. case HOSTAPD_MODE_IEEE80211B:
  1225. return PHY_TYPE_HRDSSS;
  1226. case HOSTAPD_MODE_IEEE80211G:
  1227. return PHY_TYPE_ERP;
  1228. case HOSTAPD_MODE_IEEE80211AD:
  1229. return PHY_TYPE_DMG;
  1230. default:
  1231. return PHY_TYPE_UNSPECIFIED;
  1232. };
  1233. }
  1234. /* ieee80211_get_phy_type - Derive the phy type by freq and bandwidth */
  1235. enum phy_type ieee80211_get_phy_type(int freq, int ht, int vht)
  1236. {
  1237. if (vht)
  1238. return PHY_TYPE_VHT;
  1239. if (ht)
  1240. return PHY_TYPE_HT;
  1241. return ieee80211_phy_type_by_freq(freq);
  1242. }
  1243. size_t global_op_class_size = ARRAY_SIZE(global_op_class);
  1244. /**
  1245. * get_ie - Fetch a specified information element from IEs buffer
  1246. * @ies: Information elements buffer
  1247. * @len: Information elements buffer length
  1248. * @eid: Information element identifier (WLAN_EID_*)
  1249. * Returns: Pointer to the information element (id field) or %NULL if not found
  1250. *
  1251. * This function returns the first matching information element in the IEs
  1252. * buffer or %NULL in case the element is not found.
  1253. */
  1254. const u8 * get_ie(const u8 *ies, size_t len, u8 eid)
  1255. {
  1256. const u8 *end;
  1257. if (!ies)
  1258. return NULL;
  1259. end = ies + len;
  1260. while (end - ies > 1) {
  1261. if (2 + ies[1] > end - ies)
  1262. break;
  1263. if (ies[0] == eid)
  1264. return ies;
  1265. ies += 2 + ies[1];
  1266. }
  1267. return NULL;
  1268. }
  1269. size_t mbo_add_ie(u8 *buf, size_t len, const u8 *attr, size_t attr_len)
  1270. {
  1271. /*
  1272. * MBO IE requires 6 bytes without the attributes: EID (1), length (1),
  1273. * OUI (3), OUI type (1).
  1274. */
  1275. if (len < 6 + attr_len) {
  1276. wpa_printf(MSG_DEBUG,
  1277. "MBO: Not enough room in buffer for MBO IE: buf len = %zu, attr_len = %zu",
  1278. len, attr_len);
  1279. return 0;
  1280. }
  1281. *buf++ = WLAN_EID_VENDOR_SPECIFIC;
  1282. *buf++ = attr_len + 4;
  1283. WPA_PUT_BE24(buf, OUI_WFA);
  1284. buf += 3;
  1285. *buf++ = MBO_OUI_TYPE;
  1286. os_memcpy(buf, attr, attr_len);
  1287. return 6 + attr_len;
  1288. }
  1289. static const struct country_op_class us_op_class[] = {
  1290. { 1, 115 },
  1291. { 2, 118 },
  1292. { 3, 124 },
  1293. { 4, 121 },
  1294. { 5, 125 },
  1295. { 12, 81 },
  1296. { 22, 116 },
  1297. { 23, 119 },
  1298. { 24, 122 },
  1299. { 25, 126 },
  1300. { 26, 126 },
  1301. { 27, 117 },
  1302. { 28, 120 },
  1303. { 29, 123 },
  1304. { 30, 127 },
  1305. { 31, 127 },
  1306. { 32, 83 },
  1307. { 33, 84 },
  1308. { 34, 180 },
  1309. };
  1310. static const struct country_op_class eu_op_class[] = {
  1311. { 1, 115 },
  1312. { 2, 118 },
  1313. { 3, 121 },
  1314. { 4, 81 },
  1315. { 5, 116 },
  1316. { 6, 119 },
  1317. { 7, 122 },
  1318. { 8, 117 },
  1319. { 9, 120 },
  1320. { 10, 123 },
  1321. { 11, 83 },
  1322. { 12, 84 },
  1323. { 17, 125 },
  1324. { 18, 180 },
  1325. };
  1326. static const struct country_op_class jp_op_class[] = {
  1327. { 1, 115 },
  1328. { 30, 81 },
  1329. { 31, 82 },
  1330. { 32, 118 },
  1331. { 33, 118 },
  1332. { 34, 121 },
  1333. { 35, 121 },
  1334. { 36, 116 },
  1335. { 37, 119 },
  1336. { 38, 119 },
  1337. { 39, 122 },
  1338. { 40, 122 },
  1339. { 41, 117 },
  1340. { 42, 120 },
  1341. { 43, 120 },
  1342. { 44, 123 },
  1343. { 45, 123 },
  1344. { 56, 83 },
  1345. { 57, 84 },
  1346. { 58, 121 },
  1347. { 59, 180 },
  1348. };
  1349. static const struct country_op_class cn_op_class[] = {
  1350. { 1, 115 },
  1351. { 2, 118 },
  1352. { 3, 125 },
  1353. { 4, 116 },
  1354. { 5, 119 },
  1355. { 6, 126 },
  1356. { 7, 81 },
  1357. { 8, 83 },
  1358. { 9, 84 },
  1359. };
  1360. static u8
  1361. global_op_class_from_country_array(u8 op_class, size_t array_size,
  1362. const struct country_op_class *country_array)
  1363. {
  1364. size_t i;
  1365. for (i = 0; i < array_size; i++) {
  1366. if (country_array[i].country_op_class == op_class)
  1367. return country_array[i].global_op_class;
  1368. }
  1369. return 0;
  1370. }
  1371. u8 country_to_global_op_class(const char *country, u8 op_class)
  1372. {
  1373. const struct country_op_class *country_array;
  1374. size_t size;
  1375. u8 g_op_class;
  1376. if (country_match(us_op_class_cc, country)) {
  1377. country_array = us_op_class;
  1378. size = ARRAY_SIZE(us_op_class);
  1379. } else if (country_match(eu_op_class_cc, country)) {
  1380. country_array = eu_op_class;
  1381. size = ARRAY_SIZE(eu_op_class);
  1382. } else if (country_match(jp_op_class_cc, country)) {
  1383. country_array = jp_op_class;
  1384. size = ARRAY_SIZE(jp_op_class);
  1385. } else if (country_match(cn_op_class_cc, country)) {
  1386. country_array = cn_op_class;
  1387. size = ARRAY_SIZE(cn_op_class);
  1388. } else {
  1389. /*
  1390. * Countries that do not match any of the above countries use
  1391. * global operating classes
  1392. */
  1393. return op_class;
  1394. }
  1395. g_op_class = global_op_class_from_country_array(op_class, size,
  1396. country_array);
  1397. /*
  1398. * If the given operating class did not match any of the country's
  1399. * operating classes, assume that global operating class is used.
  1400. */
  1401. return g_op_class ? g_op_class : op_class;
  1402. }
  1403. const struct oper_class_map * get_oper_class(const char *country, u8 op_class)
  1404. {
  1405. const struct oper_class_map *op;
  1406. if (country)
  1407. op_class = country_to_global_op_class(country, op_class);
  1408. op = &global_op_class[0];
  1409. while (op->op_class && op->op_class != op_class)
  1410. op++;
  1411. if (!op->op_class)
  1412. return NULL;
  1413. return op;
  1414. }
  1415. int ieee802_11_parse_candidate_list(const char *pos, u8 *nei_rep,
  1416. size_t nei_rep_len)
  1417. {
  1418. u8 *nei_pos = nei_rep;
  1419. const char *end;
  1420. /*
  1421. * BSS Transition Candidate List Entries - Neighbor Report elements
  1422. * neighbor=<BSSID>,<BSSID Information>,<Operating Class>,
  1423. * <Channel Number>,<PHY Type>[,<hexdump of Optional Subelements>]
  1424. */
  1425. while (pos) {
  1426. u8 *nei_start;
  1427. long int val;
  1428. char *endptr, *tmp;
  1429. pos = os_strstr(pos, " neighbor=");
  1430. if (!pos)
  1431. break;
  1432. if (nei_pos + 15 > nei_rep + nei_rep_len) {
  1433. wpa_printf(MSG_DEBUG,
  1434. "Not enough room for additional neighbor");
  1435. return -1;
  1436. }
  1437. pos += 10;
  1438. nei_start = nei_pos;
  1439. *nei_pos++ = WLAN_EID_NEIGHBOR_REPORT;
  1440. nei_pos++; /* length to be filled in */
  1441. if (hwaddr_aton(pos, nei_pos)) {
  1442. wpa_printf(MSG_DEBUG, "Invalid BSSID");
  1443. return -1;
  1444. }
  1445. nei_pos += ETH_ALEN;
  1446. pos += 17;
  1447. if (*pos != ',') {
  1448. wpa_printf(MSG_DEBUG, "Missing BSSID Information");
  1449. return -1;
  1450. }
  1451. pos++;
  1452. val = strtol(pos, &endptr, 0);
  1453. WPA_PUT_LE32(nei_pos, val);
  1454. nei_pos += 4;
  1455. if (*endptr != ',') {
  1456. wpa_printf(MSG_DEBUG, "Missing Operating Class");
  1457. return -1;
  1458. }
  1459. pos = endptr + 1;
  1460. *nei_pos++ = atoi(pos); /* Operating Class */
  1461. pos = os_strchr(pos, ',');
  1462. if (pos == NULL) {
  1463. wpa_printf(MSG_DEBUG, "Missing Channel Number");
  1464. return -1;
  1465. }
  1466. pos++;
  1467. *nei_pos++ = atoi(pos); /* Channel Number */
  1468. pos = os_strchr(pos, ',');
  1469. if (pos == NULL) {
  1470. wpa_printf(MSG_DEBUG, "Missing PHY Type");
  1471. return -1;
  1472. }
  1473. pos++;
  1474. *nei_pos++ = atoi(pos); /* PHY Type */
  1475. end = os_strchr(pos, ' ');
  1476. tmp = os_strchr(pos, ',');
  1477. if (tmp && (!end || tmp < end)) {
  1478. /* Optional Subelements (hexdump) */
  1479. size_t len;
  1480. pos = tmp + 1;
  1481. end = os_strchr(pos, ' ');
  1482. if (end)
  1483. len = end - pos;
  1484. else
  1485. len = os_strlen(pos);
  1486. if (nei_pos + len / 2 > nei_rep + nei_rep_len) {
  1487. wpa_printf(MSG_DEBUG,
  1488. "Not enough room for neighbor subelements");
  1489. return -1;
  1490. }
  1491. if (len & 0x01 ||
  1492. hexstr2bin(pos, nei_pos, len / 2) < 0) {
  1493. wpa_printf(MSG_DEBUG,
  1494. "Invalid neighbor subelement info");
  1495. return -1;
  1496. }
  1497. nei_pos += len / 2;
  1498. pos = end;
  1499. }
  1500. nei_start[1] = nei_pos - nei_start - 2;
  1501. }
  1502. return nei_pos - nei_rep;
  1503. }