ieee802_11_common.c 22 KB

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