ieee802_11_common.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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. if (elen < sizeof(struct ieee80211_ht_capabilities))
  252. break;
  253. elems->ht_capabilities = pos;
  254. break;
  255. case WLAN_EID_HT_OPERATION:
  256. if (elen < sizeof(struct ieee80211_ht_operation))
  257. break;
  258. elems->ht_operation = pos;
  259. break;
  260. case WLAN_EID_MESH_CONFIG:
  261. elems->mesh_config = pos;
  262. elems->mesh_config_len = elen;
  263. break;
  264. case WLAN_EID_MESH_ID:
  265. elems->mesh_id = pos;
  266. elems->mesh_id_len = elen;
  267. break;
  268. case WLAN_EID_PEER_MGMT:
  269. elems->peer_mgmt = pos;
  270. elems->peer_mgmt_len = elen;
  271. break;
  272. case WLAN_EID_VHT_CAP:
  273. if (elen < sizeof(struct ieee80211_vht_capabilities))
  274. break;
  275. elems->vht_capabilities = pos;
  276. break;
  277. case WLAN_EID_VHT_OPERATION:
  278. if (elen < sizeof(struct ieee80211_vht_operation))
  279. break;
  280. elems->vht_operation = pos;
  281. break;
  282. case WLAN_EID_VHT_OPERATING_MODE_NOTIFICATION:
  283. if (elen != 1)
  284. break;
  285. elems->vht_opmode_notif = pos;
  286. break;
  287. case WLAN_EID_LINK_ID:
  288. if (elen < 18)
  289. break;
  290. elems->link_id = pos;
  291. break;
  292. case WLAN_EID_INTERWORKING:
  293. elems->interworking = pos;
  294. elems->interworking_len = elen;
  295. break;
  296. case WLAN_EID_QOS_MAP_SET:
  297. if (elen < 16)
  298. break;
  299. elems->qos_map_set = pos;
  300. elems->qos_map_set_len = elen;
  301. break;
  302. case WLAN_EID_EXT_CAPAB:
  303. elems->ext_capab = pos;
  304. elems->ext_capab_len = elen;
  305. break;
  306. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  307. if (elen < 3)
  308. break;
  309. elems->bss_max_idle_period = pos;
  310. break;
  311. case WLAN_EID_SSID_LIST:
  312. elems->ssid_list = pos;
  313. elems->ssid_list_len = elen;
  314. break;
  315. case WLAN_EID_AMPE:
  316. elems->ampe = pos;
  317. elems->ampe_len = elen;
  318. break;
  319. case WLAN_EID_MIC:
  320. elems->mic = pos;
  321. elems->mic_len = elen;
  322. /* after mic everything is encrypted, so stop. */
  323. left = elen;
  324. break;
  325. default:
  326. unknown++;
  327. if (!show_errors)
  328. break;
  329. wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
  330. "ignored unknown element (id=%d elen=%d)",
  331. id, elen);
  332. break;
  333. }
  334. left -= elen;
  335. pos += elen;
  336. }
  337. if (left)
  338. return ParseFailed;
  339. return unknown ? ParseUnknown : ParseOK;
  340. }
  341. int ieee802_11_ie_count(const u8 *ies, size_t ies_len)
  342. {
  343. int count = 0;
  344. const u8 *pos, *end;
  345. if (ies == NULL)
  346. return 0;
  347. pos = ies;
  348. end = ies + ies_len;
  349. while (pos + 2 <= end) {
  350. if (pos + 2 + pos[1] > end)
  351. break;
  352. count++;
  353. pos += 2 + pos[1];
  354. }
  355. return count;
  356. }
  357. struct wpabuf * ieee802_11_vendor_ie_concat(const u8 *ies, size_t ies_len,
  358. u32 oui_type)
  359. {
  360. struct wpabuf *buf;
  361. const u8 *end, *pos, *ie;
  362. pos = ies;
  363. end = ies + ies_len;
  364. ie = NULL;
  365. while (pos + 1 < end) {
  366. if (pos + 2 + pos[1] > end)
  367. return NULL;
  368. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  369. WPA_GET_BE32(&pos[2]) == oui_type) {
  370. ie = pos;
  371. break;
  372. }
  373. pos += 2 + pos[1];
  374. }
  375. if (ie == NULL)
  376. return NULL; /* No specified vendor IE found */
  377. buf = wpabuf_alloc(ies_len);
  378. if (buf == NULL)
  379. return NULL;
  380. /*
  381. * There may be multiple vendor IEs in the message, so need to
  382. * concatenate their data fields.
  383. */
  384. while (pos + 1 < end) {
  385. if (pos + 2 + pos[1] > end)
  386. break;
  387. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  388. WPA_GET_BE32(&pos[2]) == oui_type)
  389. wpabuf_put_data(buf, pos + 6, pos[1] - 4);
  390. pos += 2 + pos[1];
  391. }
  392. return buf;
  393. }
  394. const u8 * get_hdr_bssid(const struct ieee80211_hdr *hdr, size_t len)
  395. {
  396. u16 fc, type, stype;
  397. /*
  398. * PS-Poll frames are 16 bytes. All other frames are
  399. * 24 bytes or longer.
  400. */
  401. if (len < 16)
  402. return NULL;
  403. fc = le_to_host16(hdr->frame_control);
  404. type = WLAN_FC_GET_TYPE(fc);
  405. stype = WLAN_FC_GET_STYPE(fc);
  406. switch (type) {
  407. case WLAN_FC_TYPE_DATA:
  408. if (len < 24)
  409. return NULL;
  410. switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
  411. case WLAN_FC_FROMDS | WLAN_FC_TODS:
  412. case WLAN_FC_TODS:
  413. return hdr->addr1;
  414. case WLAN_FC_FROMDS:
  415. return hdr->addr2;
  416. default:
  417. return NULL;
  418. }
  419. case WLAN_FC_TYPE_CTRL:
  420. if (stype != WLAN_FC_STYPE_PSPOLL)
  421. return NULL;
  422. return hdr->addr1;
  423. case WLAN_FC_TYPE_MGMT:
  424. return hdr->addr3;
  425. default:
  426. return NULL;
  427. }
  428. }
  429. int hostapd_config_wmm_ac(struct hostapd_wmm_ac_params wmm_ac_params[],
  430. const char *name, const char *val)
  431. {
  432. int num, v;
  433. const char *pos;
  434. struct hostapd_wmm_ac_params *ac;
  435. /* skip 'wme_ac_' or 'wmm_ac_' prefix */
  436. pos = name + 7;
  437. if (os_strncmp(pos, "be_", 3) == 0) {
  438. num = 0;
  439. pos += 3;
  440. } else if (os_strncmp(pos, "bk_", 3) == 0) {
  441. num = 1;
  442. pos += 3;
  443. } else if (os_strncmp(pos, "vi_", 3) == 0) {
  444. num = 2;
  445. pos += 3;
  446. } else if (os_strncmp(pos, "vo_", 3) == 0) {
  447. num = 3;
  448. pos += 3;
  449. } else {
  450. wpa_printf(MSG_ERROR, "Unknown WMM name '%s'", pos);
  451. return -1;
  452. }
  453. ac = &wmm_ac_params[num];
  454. if (os_strcmp(pos, "aifs") == 0) {
  455. v = atoi(val);
  456. if (v < 1 || v > 255) {
  457. wpa_printf(MSG_ERROR, "Invalid AIFS value %d", v);
  458. return -1;
  459. }
  460. ac->aifs = v;
  461. } else if (os_strcmp(pos, "cwmin") == 0) {
  462. v = atoi(val);
  463. if (v < 0 || v > 12) {
  464. wpa_printf(MSG_ERROR, "Invalid cwMin value %d", v);
  465. return -1;
  466. }
  467. ac->cwmin = v;
  468. } else if (os_strcmp(pos, "cwmax") == 0) {
  469. v = atoi(val);
  470. if (v < 0 || v > 12) {
  471. wpa_printf(MSG_ERROR, "Invalid cwMax value %d", v);
  472. return -1;
  473. }
  474. ac->cwmax = v;
  475. } else if (os_strcmp(pos, "txop_limit") == 0) {
  476. v = atoi(val);
  477. if (v < 0 || v > 0xffff) {
  478. wpa_printf(MSG_ERROR, "Invalid txop value %d", v);
  479. return -1;
  480. }
  481. ac->txop_limit = v;
  482. } else if (os_strcmp(pos, "acm") == 0) {
  483. v = atoi(val);
  484. if (v < 0 || v > 1) {
  485. wpa_printf(MSG_ERROR, "Invalid acm value %d", v);
  486. return -1;
  487. }
  488. ac->admission_control_mandatory = v;
  489. } else {
  490. wpa_printf(MSG_ERROR, "Unknown wmm_ac_ field '%s'", pos);
  491. return -1;
  492. }
  493. return 0;
  494. }
  495. enum hostapd_hw_mode ieee80211_freq_to_chan(int freq, u8 *channel)
  496. {
  497. enum hostapd_hw_mode mode = NUM_HOSTAPD_MODES;
  498. if (freq >= 2412 && freq <= 2472) {
  499. mode = HOSTAPD_MODE_IEEE80211G;
  500. *channel = (freq - 2407) / 5;
  501. } else if (freq == 2484) {
  502. mode = HOSTAPD_MODE_IEEE80211B;
  503. *channel = 14;
  504. } else if (freq >= 4900 && freq < 5000) {
  505. mode = HOSTAPD_MODE_IEEE80211A;
  506. *channel = (freq - 4000) / 5;
  507. } else if (freq >= 5000 && freq < 5900) {
  508. mode = HOSTAPD_MODE_IEEE80211A;
  509. *channel = (freq - 5000) / 5;
  510. } else if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
  511. mode = HOSTAPD_MODE_IEEE80211AD;
  512. *channel = (freq - 56160) / 2160;
  513. }
  514. return mode;
  515. }
  516. static const char *const us_op_class_cc[] = {
  517. "US", "CA", NULL
  518. };
  519. static const char *const eu_op_class_cc[] = {
  520. "AL", "AM", "AT", "AZ", "BA", "BE", "BG", "BY", "CH", "CY", "CZ", "DE",
  521. "DK", "EE", "EL", "ES", "FI", "FR", "GE", "HR", "HU", "IE", "IS", "IT",
  522. "LI", "LT", "LU", "LV", "MD", "ME", "MK", "MT", "NL", "NO", "PL", "PT",
  523. "RO", "RS", "RU", "SE", "SI", "SK", "TR", "UA", "UK", NULL
  524. };
  525. static const char *const jp_op_class_cc[] = {
  526. "JP", NULL
  527. };
  528. static const char *const cn_op_class_cc[] = {
  529. "CN", NULL
  530. };
  531. static int country_match(const char *const cc[], const char *const country)
  532. {
  533. int i;
  534. if (country == NULL)
  535. return 0;
  536. for (i = 0; cc[i]; i++) {
  537. if (cc[i][0] == country[0] && cc[i][1] == country[1])
  538. return 1;
  539. }
  540. return 0;
  541. }
  542. static int ieee80211_chan_to_freq_us(u8 op_class, u8 chan)
  543. {
  544. switch (op_class) {
  545. case 12: /* channels 1..11 */
  546. case 32: /* channels 1..7; 40 MHz */
  547. case 33: /* channels 5..11; 40 MHz */
  548. if (chan < 1 || chan > 11)
  549. return -1;
  550. return 2407 + 5 * chan;
  551. case 1: /* channels 36,40,44,48 */
  552. case 2: /* channels 52,56,60,64; dfs */
  553. case 22: /* channels 36,44; 40 MHz */
  554. case 23: /* channels 52,60; 40 MHz */
  555. case 27: /* channels 40,48; 40 MHz */
  556. case 28: /* channels 56,64; 40 MHz */
  557. if (chan < 36 || chan > 64)
  558. return -1;
  559. return 5000 + 5 * chan;
  560. case 4: /* channels 100-144 */
  561. case 24: /* channels 100-140; 40 MHz */
  562. if (chan < 100 || chan > 144)
  563. return -1;
  564. return 5000 + 5 * chan;
  565. case 3: /* channels 149,153,157,161 */
  566. case 25: /* channels 149,157; 40 MHz */
  567. case 26: /* channels 149,157; 40 MHz */
  568. case 30: /* channels 153,161; 40 MHz */
  569. case 31: /* channels 153,161; 40 MHz */
  570. if (chan < 149 || chan > 161)
  571. return -1;
  572. return 5000 + 5 * chan;
  573. case 5: /* channels 149,153,157,161,165 */
  574. if (chan < 149 || chan > 165)
  575. return -1;
  576. return 5000 + 5 * chan;
  577. case 34: /* 60 GHz band, channels 1..3 */
  578. if (chan < 1 || chan > 3)
  579. return -1;
  580. return 56160 + 2160 * chan;
  581. }
  582. return -1;
  583. }
  584. static int ieee80211_chan_to_freq_eu(u8 op_class, u8 chan)
  585. {
  586. switch (op_class) {
  587. case 4: /* channels 1..13 */
  588. case 11: /* channels 1..9; 40 MHz */
  589. case 12: /* channels 5..13; 40 MHz */
  590. if (chan < 1 || chan > 13)
  591. return -1;
  592. return 2407 + 5 * chan;
  593. case 1: /* channels 36,40,44,48 */
  594. case 2: /* channels 52,56,60,64; dfs */
  595. case 5: /* channels 36,44; 40 MHz */
  596. case 6: /* channels 52,60; 40 MHz */
  597. case 8: /* channels 40,48; 40 MHz */
  598. case 9: /* channels 56,64; 40 MHz */
  599. if (chan < 36 || chan > 64)
  600. return -1;
  601. return 5000 + 5 * chan;
  602. case 3: /* channels 100-140 */
  603. case 7: /* channels 100-132; 40 MHz */
  604. case 10: /* channels 104-136; 40 MHz */
  605. case 16: /* channels 100-140 */
  606. if (chan < 100 || chan > 140)
  607. return -1;
  608. return 5000 + 5 * chan;
  609. case 17: /* channels 149,153,157,161,165,169 */
  610. if (chan < 149 || chan > 169)
  611. return -1;
  612. return 5000 + 5 * chan;
  613. case 18: /* 60 GHz band, channels 1..4 */
  614. if (chan < 1 || chan > 4)
  615. return -1;
  616. return 56160 + 2160 * chan;
  617. }
  618. return -1;
  619. }
  620. static int ieee80211_chan_to_freq_jp(u8 op_class, u8 chan)
  621. {
  622. switch (op_class) {
  623. case 30: /* channels 1..13 */
  624. case 56: /* channels 1..9; 40 MHz */
  625. case 57: /* channels 5..13; 40 MHz */
  626. if (chan < 1 || chan > 13)
  627. return -1;
  628. return 2407 + 5 * chan;
  629. case 31: /* channel 14 */
  630. if (chan != 14)
  631. return -1;
  632. return 2414 + 5 * chan;
  633. case 1: /* channels 34,38,42,46(old) or 36,40,44,48 */
  634. case 32: /* channels 52,56,60,64 */
  635. case 33: /* channels 52,56,60,64 */
  636. case 36: /* channels 36,44; 40 MHz */
  637. case 37: /* channels 52,60; 40 MHz */
  638. case 38: /* channels 52,60; 40 MHz */
  639. case 41: /* channels 40,48; 40 MHz */
  640. case 42: /* channels 56,64; 40 MHz */
  641. case 43: /* channels 56,64; 40 MHz */
  642. if (chan < 34 || chan > 64)
  643. return -1;
  644. return 5000 + 5 * chan;
  645. case 34: /* channels 100-140 */
  646. case 35: /* channels 100-140 */
  647. case 39: /* channels 100-132; 40 MHz */
  648. case 40: /* channels 100-132; 40 MHz */
  649. case 44: /* channels 104-136; 40 MHz */
  650. case 45: /* channels 104-136; 40 MHz */
  651. case 58: /* channels 100-140 */
  652. if (chan < 100 || chan > 140)
  653. return -1;
  654. return 5000 + 5 * chan;
  655. case 59: /* 60 GHz band, channels 1..4 */
  656. if (chan < 1 || chan > 3)
  657. return -1;
  658. return 56160 + 2160 * chan;
  659. }
  660. return -1;
  661. }
  662. static int ieee80211_chan_to_freq_cn(u8 op_class, u8 chan)
  663. {
  664. switch (op_class) {
  665. case 7: /* channels 1..13 */
  666. case 8: /* channels 1..9; 40 MHz */
  667. case 9: /* channels 5..13; 40 MHz */
  668. if (chan < 1 || chan > 13)
  669. return -1;
  670. return 2407 + 5 * chan;
  671. case 1: /* channels 36,40,44,48 */
  672. case 2: /* channels 52,56,60,64; dfs */
  673. case 4: /* channels 36,44; 40 MHz */
  674. case 5: /* channels 52,60; 40 MHz */
  675. if (chan < 36 || chan > 64)
  676. return -1;
  677. return 5000 + 5 * chan;
  678. case 3: /* channels 149,153,157,161,165 */
  679. case 6: /* channels 149,157; 40 MHz */
  680. if (chan < 149 || chan > 165)
  681. return -1;
  682. return 5000 + 5 * chan;
  683. }
  684. return -1;
  685. }
  686. static int ieee80211_chan_to_freq_global(u8 op_class, u8 chan)
  687. {
  688. /* Table E-4 in IEEE Std 802.11-2012 - Global operating classes */
  689. switch (op_class) {
  690. case 81:
  691. /* channels 1..13 */
  692. if (chan < 1 || chan > 13)
  693. return -1;
  694. return 2407 + 5 * chan;
  695. case 82:
  696. /* channel 14 */
  697. if (chan != 14)
  698. return -1;
  699. return 2414 + 5 * chan;
  700. case 83: /* channels 1..9; 40 MHz */
  701. case 84: /* channels 5..13; 40 MHz */
  702. if (chan < 1 || chan > 13)
  703. return -1;
  704. return 2407 + 5 * chan;
  705. case 115: /* channels 36,40,44,48; indoor only */
  706. case 116: /* channels 36,44; 40 MHz; indoor only */
  707. case 117: /* channels 40,48; 40 MHz; indoor only */
  708. case 118: /* channels 52,56,60,64; dfs */
  709. case 119: /* channels 52,60; 40 MHz; dfs */
  710. case 120: /* channels 56,64; 40 MHz; dfs */
  711. if (chan < 36 || chan > 64)
  712. return -1;
  713. return 5000 + 5 * chan;
  714. case 121: /* channels 100-140 */
  715. case 122: /* channels 100-142; 40 MHz */
  716. case 123: /* channels 104-136; 40 MHz */
  717. if (chan < 100 || chan > 140)
  718. return -1;
  719. return 5000 + 5 * chan;
  720. case 124: /* channels 149,153,157,161 */
  721. case 126: /* channels 149,157; 40 MHz */
  722. case 127: /* channels 153,161; 40 MHz */
  723. if (chan < 149 || chan > 161)
  724. return -1;
  725. return 5000 + 5 * chan;
  726. case 125: /* channels 149,153,157,161,165,169 */
  727. if (chan < 149 || chan > 169)
  728. return -1;
  729. return 5000 + 5 * chan;
  730. case 128: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  731. case 130: /* center freqs 42, 58, 106, 122, 138, 155; 80 MHz */
  732. if (chan < 36 || chan > 161)
  733. return -1;
  734. return 5000 + 5 * chan;
  735. case 129: /* center freqs 50, 114; 160 MHz */
  736. if (chan < 50 || chan > 114)
  737. return -1;
  738. return 5000 + 5 * chan;
  739. case 180: /* 60 GHz band, channels 1..4 */
  740. if (chan < 1 || chan > 4)
  741. return -1;
  742. return 56160 + 2160 * chan;
  743. }
  744. return -1;
  745. }
  746. /**
  747. * ieee80211_chan_to_freq - Convert channel info to frequency
  748. * @country: Country code, if known; otherwise, global operating class is used
  749. * @op_class: Operating class
  750. * @chan: Channel number
  751. * Returns: Frequency in MHz or -1 if the specified channel is unknown
  752. */
  753. int ieee80211_chan_to_freq(const char *country, u8 op_class, u8 chan)
  754. {
  755. int freq;
  756. if (country_match(us_op_class_cc, country)) {
  757. freq = ieee80211_chan_to_freq_us(op_class, chan);
  758. if (freq > 0)
  759. return freq;
  760. }
  761. if (country_match(eu_op_class_cc, country)) {
  762. freq = ieee80211_chan_to_freq_eu(op_class, chan);
  763. if (freq > 0)
  764. return freq;
  765. }
  766. if (country_match(jp_op_class_cc, country)) {
  767. freq = ieee80211_chan_to_freq_jp(op_class, chan);
  768. if (freq > 0)
  769. return freq;
  770. }
  771. if (country_match(cn_op_class_cc, country)) {
  772. freq = ieee80211_chan_to_freq_cn(op_class, chan);
  773. if (freq > 0)
  774. return freq;
  775. }
  776. return ieee80211_chan_to_freq_global(op_class, chan);
  777. }
  778. int ieee80211_is_dfs(int freq)
  779. {
  780. /* TODO: this could be more accurate to better cover all domains */
  781. return (freq >= 5260 && freq <= 5320) || (freq >= 5500 && freq <= 5700);
  782. }
  783. static int is_11b(u8 rate)
  784. {
  785. return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
  786. }
  787. int supp_rates_11b_only(struct ieee802_11_elems *elems)
  788. {
  789. int num_11b = 0, num_others = 0;
  790. int i;
  791. if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
  792. return 0;
  793. for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
  794. if (is_11b(elems->supp_rates[i]))
  795. num_11b++;
  796. else
  797. num_others++;
  798. }
  799. for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
  800. i++) {
  801. if (is_11b(elems->ext_supp_rates[i]))
  802. num_11b++;
  803. else
  804. num_others++;
  805. }
  806. return num_11b > 0 && num_others == 0;
  807. }
  808. const char * fc2str(u16 fc)
  809. {
  810. u16 stype = WLAN_FC_GET_STYPE(fc);
  811. #define C2S(x) case x: return #x;
  812. switch (WLAN_FC_GET_TYPE(fc)) {
  813. case WLAN_FC_TYPE_MGMT:
  814. switch (stype) {
  815. C2S(WLAN_FC_STYPE_ASSOC_REQ)
  816. C2S(WLAN_FC_STYPE_ASSOC_RESP)
  817. C2S(WLAN_FC_STYPE_REASSOC_REQ)
  818. C2S(WLAN_FC_STYPE_REASSOC_RESP)
  819. C2S(WLAN_FC_STYPE_PROBE_REQ)
  820. C2S(WLAN_FC_STYPE_PROBE_RESP)
  821. C2S(WLAN_FC_STYPE_BEACON)
  822. C2S(WLAN_FC_STYPE_ATIM)
  823. C2S(WLAN_FC_STYPE_DISASSOC)
  824. C2S(WLAN_FC_STYPE_AUTH)
  825. C2S(WLAN_FC_STYPE_DEAUTH)
  826. C2S(WLAN_FC_STYPE_ACTION)
  827. }
  828. break;
  829. case WLAN_FC_TYPE_CTRL:
  830. switch (stype) {
  831. C2S(WLAN_FC_STYPE_PSPOLL)
  832. C2S(WLAN_FC_STYPE_RTS)
  833. C2S(WLAN_FC_STYPE_CTS)
  834. C2S(WLAN_FC_STYPE_ACK)
  835. C2S(WLAN_FC_STYPE_CFEND)
  836. C2S(WLAN_FC_STYPE_CFENDACK)
  837. }
  838. break;
  839. case WLAN_FC_TYPE_DATA:
  840. switch (stype) {
  841. C2S(WLAN_FC_STYPE_DATA)
  842. C2S(WLAN_FC_STYPE_DATA_CFACK)
  843. C2S(WLAN_FC_STYPE_DATA_CFPOLL)
  844. C2S(WLAN_FC_STYPE_DATA_CFACKPOLL)
  845. C2S(WLAN_FC_STYPE_NULLFUNC)
  846. C2S(WLAN_FC_STYPE_CFACK)
  847. C2S(WLAN_FC_STYPE_CFPOLL)
  848. C2S(WLAN_FC_STYPE_CFACKPOLL)
  849. C2S(WLAN_FC_STYPE_QOS_DATA)
  850. C2S(WLAN_FC_STYPE_QOS_DATA_CFACK)
  851. C2S(WLAN_FC_STYPE_QOS_DATA_CFPOLL)
  852. C2S(WLAN_FC_STYPE_QOS_DATA_CFACKPOLL)
  853. C2S(WLAN_FC_STYPE_QOS_NULL)
  854. C2S(WLAN_FC_STYPE_QOS_CFPOLL)
  855. C2S(WLAN_FC_STYPE_QOS_CFACKPOLL)
  856. }
  857. break;
  858. }
  859. return "WLAN_FC_TYPE_UNKNOWN";
  860. #undef C2S
  861. }