parse.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2002-2005, Instant802 Networks, Inc.
  4. * Copyright 2005-2006, Devicescape Software, Inc.
  5. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  6. * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2013-2014 Intel Mobile Communications GmbH
  8. * Copyright (C) 2015-2017 Intel Deutschland GmbH
  9. * Copyright (C) 2018-2024 Intel Corporation
  10. *
  11. * element parsing for mac80211
  12. */
  13. #include <net/mac80211.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/export.h>
  16. #include <linux/types.h>
  17. #include <linux/slab.h>
  18. #include <linux/skbuff.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/crc32.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cfg80211.h>
  25. #include <net/rtnetlink.h>
  26. #include <kunit/visibility.h>
  27. #include "ieee80211_i.h"
  28. #include "driver-ops.h"
  29. #include "rate.h"
  30. #include "mesh.h"
  31. #include "wme.h"
  32. #include "led.h"
  33. #include "wep.h"
  34. struct ieee80211_elems_parse {
  35. /* must be first for kfree to work */
  36. struct ieee802_11_elems elems;
  37. /* The basic Multi-Link element in the original elements */
  38. const struct element *ml_basic_elem;
  39. /* The reconfiguration Multi-Link element in the original elements */
  40. const struct element *ml_reconf_elem;
  41. /*
  42. * scratch buffer that can be used for various element parsing related
  43. * tasks, e.g., element de-fragmentation etc.
  44. */
  45. size_t scratch_len;
  46. u8 *scratch_pos;
  47. u8 scratch[] __counted_by(scratch_len);
  48. };
  49. static void
  50. ieee80211_parse_extension_element(u32 *crc,
  51. const struct element *elem,
  52. struct ieee80211_elems_parse *elems_parse,
  53. struct ieee80211_elems_parse_params *params)
  54. {
  55. struct ieee802_11_elems *elems = &elems_parse->elems;
  56. const void *data = elem->data + 1;
  57. bool calc_crc = false;
  58. u8 len;
  59. if (!elem->datalen)
  60. return;
  61. len = elem->datalen - 1;
  62. switch (elem->data[0]) {
  63. case WLAN_EID_EXT_HE_MU_EDCA:
  64. if (params->mode < IEEE80211_CONN_MODE_HE)
  65. break;
  66. calc_crc = true;
  67. if (len >= sizeof(*elems->mu_edca_param_set))
  68. elems->mu_edca_param_set = data;
  69. break;
  70. case WLAN_EID_EXT_HE_CAPABILITY:
  71. if (params->mode < IEEE80211_CONN_MODE_HE)
  72. break;
  73. if (ieee80211_he_capa_size_ok(data, len)) {
  74. elems->he_cap = data;
  75. elems->he_cap_len = len;
  76. }
  77. break;
  78. case WLAN_EID_EXT_HE_OPERATION:
  79. if (params->mode < IEEE80211_CONN_MODE_HE)
  80. break;
  81. calc_crc = true;
  82. if (len >= sizeof(*elems->he_operation) &&
  83. len >= ieee80211_he_oper_size(data) - 1)
  84. elems->he_operation = data;
  85. break;
  86. case WLAN_EID_EXT_UORA:
  87. if (params->mode < IEEE80211_CONN_MODE_HE)
  88. break;
  89. if (len >= 1)
  90. elems->uora_element = data;
  91. break;
  92. case WLAN_EID_EXT_MAX_CHANNEL_SWITCH_TIME:
  93. if (len == 3)
  94. elems->max_channel_switch_time = data;
  95. break;
  96. case WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION:
  97. if (len >= sizeof(*elems->mbssid_config_ie))
  98. elems->mbssid_config_ie = data;
  99. break;
  100. case WLAN_EID_EXT_HE_SPR:
  101. if (params->mode < IEEE80211_CONN_MODE_HE)
  102. break;
  103. if (len >= sizeof(*elems->he_spr) &&
  104. len >= ieee80211_he_spr_size(data) - 1)
  105. elems->he_spr = data;
  106. break;
  107. case WLAN_EID_EXT_HE_6GHZ_CAPA:
  108. if (params->mode < IEEE80211_CONN_MODE_HE)
  109. break;
  110. if (len >= sizeof(*elems->he_6ghz_capa))
  111. elems->he_6ghz_capa = data;
  112. break;
  113. case WLAN_EID_EXT_EHT_CAPABILITY:
  114. if (params->mode < IEEE80211_CONN_MODE_EHT)
  115. break;
  116. if (ieee80211_eht_capa_size_ok(elems->he_cap,
  117. data, len,
  118. params->from_ap)) {
  119. elems->eht_cap = data;
  120. elems->eht_cap_len = len;
  121. }
  122. break;
  123. case WLAN_EID_EXT_EHT_OPERATION:
  124. if (params->mode < IEEE80211_CONN_MODE_EHT)
  125. break;
  126. if (ieee80211_eht_oper_size_ok(data, len))
  127. elems->eht_operation = data;
  128. calc_crc = true;
  129. break;
  130. case WLAN_EID_EXT_EHT_MULTI_LINK:
  131. if (params->mode < IEEE80211_CONN_MODE_EHT)
  132. break;
  133. calc_crc = true;
  134. if (ieee80211_mle_size_ok(data, len)) {
  135. const struct ieee80211_multi_link_elem *mle =
  136. (void *)data;
  137. switch (le16_get_bits(mle->control,
  138. IEEE80211_ML_CONTROL_TYPE)) {
  139. case IEEE80211_ML_CONTROL_TYPE_BASIC:
  140. if (elems_parse->ml_basic_elem) {
  141. elems->parse_error |=
  142. IEEE80211_PARSE_ERR_DUP_NEST_ML_BASIC;
  143. break;
  144. }
  145. elems_parse->ml_basic_elem = elem;
  146. break;
  147. case IEEE80211_ML_CONTROL_TYPE_RECONF:
  148. elems_parse->ml_reconf_elem = elem;
  149. break;
  150. default:
  151. break;
  152. }
  153. }
  154. break;
  155. case WLAN_EID_EXT_BANDWIDTH_INDICATION:
  156. if (params->mode < IEEE80211_CONN_MODE_EHT)
  157. break;
  158. if (ieee80211_bandwidth_indication_size_ok(data, len))
  159. elems->bandwidth_indication = data;
  160. calc_crc = true;
  161. break;
  162. case WLAN_EID_EXT_TID_TO_LINK_MAPPING:
  163. if (params->mode < IEEE80211_CONN_MODE_EHT)
  164. break;
  165. calc_crc = true;
  166. if (ieee80211_tid_to_link_map_size_ok(data, len) &&
  167. elems->ttlm_num < ARRAY_SIZE(elems->ttlm)) {
  168. elems->ttlm[elems->ttlm_num] = (void *)data;
  169. elems->ttlm_num++;
  170. }
  171. break;
  172. }
  173. if (crc && calc_crc)
  174. *crc = crc32_be(*crc, (void *)elem, elem->datalen + 2);
  175. }
  176. static void ieee80211_parse_tpe(struct ieee80211_parsed_tpe *tpe,
  177. const u8 *data, u8 len)
  178. {
  179. const struct ieee80211_tx_pwr_env *env = (const void *)data;
  180. u8 count, interpret, category;
  181. u8 *out, N, *cnt_out = NULL, *N_out = NULL;
  182. if (!ieee80211_valid_tpe_element(data, len))
  183. return;
  184. count = u8_get_bits(env->info, IEEE80211_TX_PWR_ENV_INFO_COUNT);
  185. interpret = u8_get_bits(env->info, IEEE80211_TX_PWR_ENV_INFO_INTERPRET);
  186. category = u8_get_bits(env->info, IEEE80211_TX_PWR_ENV_INFO_CATEGORY);
  187. switch (interpret) {
  188. case IEEE80211_TPE_LOCAL_EIRP:
  189. out = tpe->max_local[category].power;
  190. cnt_out = &tpe->max_local[category].count;
  191. tpe->max_local[category].valid = true;
  192. break;
  193. case IEEE80211_TPE_REG_CLIENT_EIRP:
  194. out = tpe->max_reg_client[category].power;
  195. cnt_out = &tpe->max_reg_client[category].count;
  196. tpe->max_reg_client[category].valid = true;
  197. break;
  198. case IEEE80211_TPE_LOCAL_EIRP_PSD:
  199. out = tpe->psd_local[category].power;
  200. cnt_out = &tpe->psd_local[category].count;
  201. N_out = &tpe->psd_local[category].n;
  202. tpe->psd_local[category].valid = true;
  203. break;
  204. case IEEE80211_TPE_REG_CLIENT_EIRP_PSD:
  205. out = tpe->psd_reg_client[category].power;
  206. cnt_out = &tpe->psd_reg_client[category].count;
  207. N_out = &tpe->psd_reg_client[category].n;
  208. tpe->psd_reg_client[category].valid = true;
  209. break;
  210. }
  211. switch (interpret) {
  212. case IEEE80211_TPE_LOCAL_EIRP:
  213. case IEEE80211_TPE_REG_CLIENT_EIRP:
  214. /* count was validated <= 3, plus 320 MHz */
  215. BUILD_BUG_ON(IEEE80211_TPE_EIRP_ENTRIES_320MHZ < 5);
  216. memcpy(out, env->variable, count + 1);
  217. *cnt_out = count + 1;
  218. /* separately take 320 MHz if present */
  219. if (count == 3 && len > sizeof(*env) + count + 1) {
  220. out[4] = env->variable[4];
  221. *cnt_out = 5;
  222. }
  223. break;
  224. case IEEE80211_TPE_LOCAL_EIRP_PSD:
  225. case IEEE80211_TPE_REG_CLIENT_EIRP_PSD:
  226. if (!count) {
  227. memset(out, env->variable[0],
  228. IEEE80211_TPE_PSD_ENTRIES_320MHZ);
  229. *cnt_out = IEEE80211_TPE_PSD_ENTRIES_320MHZ;
  230. break;
  231. }
  232. N = 1 << (count - 1);
  233. memcpy(out, env->variable, N);
  234. *cnt_out = N;
  235. *N_out = N;
  236. if (len > sizeof(*env) + N) {
  237. int K = u8_get_bits(env->variable[N],
  238. IEEE80211_TX_PWR_ENV_EXT_COUNT);
  239. K = min(K, IEEE80211_TPE_PSD_ENTRIES_320MHZ - N);
  240. memcpy(out + N, env->variable + N + 1, K);
  241. (*cnt_out) += K;
  242. }
  243. break;
  244. }
  245. }
  246. static u32
  247. _ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params,
  248. struct ieee80211_elems_parse *elems_parse,
  249. const struct element *check_inherit)
  250. {
  251. struct ieee802_11_elems *elems = &elems_parse->elems;
  252. const struct element *elem;
  253. bool calc_crc = params->filter != 0;
  254. DECLARE_BITMAP(seen_elems, 256);
  255. u32 crc = params->crc;
  256. bitmap_zero(seen_elems, 256);
  257. for_each_element(elem, params->start, params->len) {
  258. const struct element *subelem;
  259. u8 elem_parse_failed;
  260. u8 id = elem->id;
  261. u8 elen = elem->datalen;
  262. const u8 *pos = elem->data;
  263. if (check_inherit &&
  264. !cfg80211_is_element_inherited(elem,
  265. check_inherit))
  266. continue;
  267. switch (id) {
  268. case WLAN_EID_SSID:
  269. case WLAN_EID_SUPP_RATES:
  270. case WLAN_EID_FH_PARAMS:
  271. case WLAN_EID_DS_PARAMS:
  272. case WLAN_EID_CF_PARAMS:
  273. case WLAN_EID_TIM:
  274. case WLAN_EID_IBSS_PARAMS:
  275. case WLAN_EID_CHALLENGE:
  276. case WLAN_EID_RSN:
  277. case WLAN_EID_ERP_INFO:
  278. case WLAN_EID_EXT_SUPP_RATES:
  279. case WLAN_EID_HT_CAPABILITY:
  280. case WLAN_EID_HT_OPERATION:
  281. case WLAN_EID_VHT_CAPABILITY:
  282. case WLAN_EID_VHT_OPERATION:
  283. case WLAN_EID_MESH_ID:
  284. case WLAN_EID_MESH_CONFIG:
  285. case WLAN_EID_PEER_MGMT:
  286. case WLAN_EID_PREQ:
  287. case WLAN_EID_PREP:
  288. case WLAN_EID_PERR:
  289. case WLAN_EID_RANN:
  290. case WLAN_EID_CHANNEL_SWITCH:
  291. case WLAN_EID_EXT_CHANSWITCH_ANN:
  292. case WLAN_EID_COUNTRY:
  293. case WLAN_EID_PWR_CONSTRAINT:
  294. case WLAN_EID_TIMEOUT_INTERVAL:
  295. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  296. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  297. case WLAN_EID_CHAN_SWITCH_PARAM:
  298. case WLAN_EID_EXT_CAPABILITY:
  299. case WLAN_EID_CHAN_SWITCH_TIMING:
  300. case WLAN_EID_LINK_ID:
  301. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  302. case WLAN_EID_RSNX:
  303. case WLAN_EID_S1G_BCN_COMPAT:
  304. case WLAN_EID_S1G_CAPABILITIES:
  305. case WLAN_EID_S1G_OPERATION:
  306. case WLAN_EID_AID_RESPONSE:
  307. case WLAN_EID_S1G_SHORT_BCN_INTERVAL:
  308. /*
  309. * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
  310. * that if the content gets bigger it might be needed more than once
  311. */
  312. if (test_bit(id, seen_elems)) {
  313. elems->parse_error |=
  314. IEEE80211_PARSE_ERR_DUP_ELEM;
  315. continue;
  316. }
  317. break;
  318. }
  319. if (calc_crc && id < 64 && (params->filter & (1ULL << id)))
  320. crc = crc32_be(crc, pos - 2, elen + 2);
  321. elem_parse_failed = 0;
  322. switch (id) {
  323. case WLAN_EID_LINK_ID:
  324. if (elen + 2 < sizeof(struct ieee80211_tdls_lnkie)) {
  325. elem_parse_failed =
  326. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  327. break;
  328. }
  329. elems->lnk_id = (void *)(pos - 2);
  330. break;
  331. case WLAN_EID_CHAN_SWITCH_TIMING:
  332. if (elen < sizeof(struct ieee80211_ch_switch_timing)) {
  333. elem_parse_failed =
  334. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  335. break;
  336. }
  337. elems->ch_sw_timing = (void *)pos;
  338. break;
  339. case WLAN_EID_EXT_CAPABILITY:
  340. elems->ext_capab = pos;
  341. elems->ext_capab_len = elen;
  342. break;
  343. case WLAN_EID_SSID:
  344. elems->ssid = pos;
  345. elems->ssid_len = elen;
  346. break;
  347. case WLAN_EID_SUPP_RATES:
  348. elems->supp_rates = pos;
  349. elems->supp_rates_len = elen;
  350. break;
  351. case WLAN_EID_DS_PARAMS:
  352. if (elen >= 1)
  353. elems->ds_params = pos;
  354. else
  355. elem_parse_failed =
  356. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  357. break;
  358. case WLAN_EID_TIM:
  359. if (elen >= sizeof(struct ieee80211_tim_ie)) {
  360. elems->tim = (void *)pos;
  361. elems->tim_len = elen;
  362. } else
  363. elem_parse_failed =
  364. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  365. break;
  366. case WLAN_EID_VENDOR_SPECIFIC:
  367. if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
  368. pos[2] == 0xf2) {
  369. /* Microsoft OUI (00:50:F2) */
  370. if (calc_crc)
  371. crc = crc32_be(crc, pos - 2, elen + 2);
  372. if (elen >= 5 && pos[3] == 2) {
  373. /* OUI Type 2 - WMM IE */
  374. if (pos[4] == 0) {
  375. elems->wmm_info = pos;
  376. elems->wmm_info_len = elen;
  377. } else if (pos[4] == 1) {
  378. elems->wmm_param = pos;
  379. elems->wmm_param_len = elen;
  380. }
  381. }
  382. }
  383. break;
  384. case WLAN_EID_RSN:
  385. elems->rsn = pos;
  386. elems->rsn_len = elen;
  387. break;
  388. case WLAN_EID_ERP_INFO:
  389. if (elen >= 1)
  390. elems->erp_info = pos;
  391. else
  392. elem_parse_failed =
  393. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  394. break;
  395. case WLAN_EID_EXT_SUPP_RATES:
  396. elems->ext_supp_rates = pos;
  397. elems->ext_supp_rates_len = elen;
  398. break;
  399. case WLAN_EID_HT_CAPABILITY:
  400. if (params->mode < IEEE80211_CONN_MODE_HT)
  401. break;
  402. if (elen >= sizeof(struct ieee80211_ht_cap))
  403. elems->ht_cap_elem = (void *)pos;
  404. else
  405. elem_parse_failed =
  406. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  407. break;
  408. case WLAN_EID_HT_OPERATION:
  409. if (params->mode < IEEE80211_CONN_MODE_HT)
  410. break;
  411. if (elen >= sizeof(struct ieee80211_ht_operation))
  412. elems->ht_operation = (void *)pos;
  413. else
  414. elem_parse_failed =
  415. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  416. break;
  417. case WLAN_EID_VHT_CAPABILITY:
  418. if (params->mode < IEEE80211_CONN_MODE_VHT)
  419. break;
  420. if (elen >= sizeof(struct ieee80211_vht_cap))
  421. elems->vht_cap_elem = (void *)pos;
  422. else
  423. elem_parse_failed =
  424. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  425. break;
  426. case WLAN_EID_VHT_OPERATION:
  427. if (params->mode < IEEE80211_CONN_MODE_VHT)
  428. break;
  429. if (elen >= sizeof(struct ieee80211_vht_operation)) {
  430. elems->vht_operation = (void *)pos;
  431. if (calc_crc)
  432. crc = crc32_be(crc, pos - 2, elen + 2);
  433. break;
  434. }
  435. elem_parse_failed =
  436. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  437. break;
  438. case WLAN_EID_OPMODE_NOTIF:
  439. if (params->mode < IEEE80211_CONN_MODE_VHT)
  440. break;
  441. if (elen > 0) {
  442. elems->opmode_notif = pos;
  443. if (calc_crc)
  444. crc = crc32_be(crc, pos - 2, elen + 2);
  445. break;
  446. }
  447. elem_parse_failed =
  448. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  449. break;
  450. case WLAN_EID_MESH_ID:
  451. elems->mesh_id = pos;
  452. elems->mesh_id_len = elen;
  453. break;
  454. case WLAN_EID_MESH_CONFIG:
  455. if (elen >= sizeof(struct ieee80211_meshconf_ie))
  456. elems->mesh_config = (void *)pos;
  457. else
  458. elem_parse_failed =
  459. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  460. break;
  461. case WLAN_EID_PEER_MGMT:
  462. elems->peering = pos;
  463. elems->peering_len = elen;
  464. break;
  465. case WLAN_EID_MESH_AWAKE_WINDOW:
  466. if (elen >= 2)
  467. elems->awake_window = (void *)pos;
  468. break;
  469. case WLAN_EID_PREQ:
  470. elems->preq = pos;
  471. elems->preq_len = elen;
  472. break;
  473. case WLAN_EID_PREP:
  474. elems->prep = pos;
  475. elems->prep_len = elen;
  476. break;
  477. case WLAN_EID_PERR:
  478. elems->perr = pos;
  479. elems->perr_len = elen;
  480. break;
  481. case WLAN_EID_RANN:
  482. if (elen >= sizeof(struct ieee80211_rann_ie))
  483. elems->rann = (void *)pos;
  484. else
  485. elem_parse_failed =
  486. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  487. break;
  488. case WLAN_EID_CHANNEL_SWITCH:
  489. if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
  490. elem_parse_failed =
  491. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  492. break;
  493. }
  494. elems->ch_switch_ie = (void *)pos;
  495. break;
  496. case WLAN_EID_EXT_CHANSWITCH_ANN:
  497. if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
  498. elem_parse_failed =
  499. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  500. break;
  501. }
  502. elems->ext_chansw_ie = (void *)pos;
  503. break;
  504. case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
  505. if (params->mode < IEEE80211_CONN_MODE_HT)
  506. break;
  507. if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
  508. elem_parse_failed =
  509. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  510. break;
  511. }
  512. elems->sec_chan_offs = (void *)pos;
  513. break;
  514. case WLAN_EID_CHAN_SWITCH_PARAM:
  515. if (elen <
  516. sizeof(*elems->mesh_chansw_params_ie)) {
  517. elem_parse_failed =
  518. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  519. break;
  520. }
  521. elems->mesh_chansw_params_ie = (void *)pos;
  522. break;
  523. case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
  524. if (params->mode < IEEE80211_CONN_MODE_VHT)
  525. break;
  526. if (!params->action) {
  527. elem_parse_failed =
  528. IEEE80211_PARSE_ERR_UNEXPECTED_ELEM;
  529. break;
  530. }
  531. if (elen < sizeof(*elems->wide_bw_chansw_ie)) {
  532. elem_parse_failed =
  533. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  534. break;
  535. }
  536. elems->wide_bw_chansw_ie = (void *)pos;
  537. break;
  538. case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
  539. if (params->mode < IEEE80211_CONN_MODE_VHT)
  540. break;
  541. if (params->action) {
  542. elem_parse_failed =
  543. IEEE80211_PARSE_ERR_UNEXPECTED_ELEM;
  544. break;
  545. }
  546. /*
  547. * This is a bit tricky, but as we only care about
  548. * a few elements, parse them out manually.
  549. */
  550. subelem = cfg80211_find_elem(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
  551. pos, elen);
  552. if (subelem) {
  553. if (subelem->datalen >= sizeof(*elems->wide_bw_chansw_ie))
  554. elems->wide_bw_chansw_ie =
  555. (void *)subelem->data;
  556. else
  557. elem_parse_failed =
  558. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  559. }
  560. if (params->mode < IEEE80211_CONN_MODE_EHT)
  561. break;
  562. subelem = cfg80211_find_ext_elem(WLAN_EID_EXT_BANDWIDTH_INDICATION,
  563. pos, elen);
  564. if (subelem) {
  565. const void *edata = subelem->data + 1;
  566. u8 edatalen = subelem->datalen - 1;
  567. if (ieee80211_bandwidth_indication_size_ok(edata,
  568. edatalen))
  569. elems->bandwidth_indication = edata;
  570. else
  571. elem_parse_failed =
  572. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  573. }
  574. subelem = cfg80211_find_ext_elem(WLAN_EID_TX_POWER_ENVELOPE,
  575. pos, elen);
  576. if (subelem)
  577. ieee80211_parse_tpe(&elems->csa_tpe,
  578. subelem->data + 1,
  579. subelem->datalen - 1);
  580. break;
  581. case WLAN_EID_COUNTRY:
  582. elems->country_elem = pos;
  583. elems->country_elem_len = elen;
  584. break;
  585. case WLAN_EID_PWR_CONSTRAINT:
  586. if (elen != 1) {
  587. elem_parse_failed =
  588. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  589. break;
  590. }
  591. elems->pwr_constr_elem = pos;
  592. break;
  593. case WLAN_EID_CISCO_VENDOR_SPECIFIC:
  594. /* Lots of different options exist, but we only care
  595. * about the Dynamic Transmit Power Control element.
  596. * First check for the Cisco OUI, then for the DTPC
  597. * tag (0x00).
  598. */
  599. if (elen < 4) {
  600. elem_parse_failed =
  601. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  602. break;
  603. }
  604. if (pos[0] != 0x00 || pos[1] != 0x40 ||
  605. pos[2] != 0x96 || pos[3] != 0x00)
  606. break;
  607. if (elen != 6) {
  608. elem_parse_failed =
  609. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  610. break;
  611. }
  612. if (calc_crc)
  613. crc = crc32_be(crc, pos - 2, elen + 2);
  614. elems->cisco_dtpc_elem = pos;
  615. break;
  616. case WLAN_EID_ADDBA_EXT:
  617. if (elen < sizeof(struct ieee80211_addba_ext_ie)) {
  618. elem_parse_failed =
  619. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  620. break;
  621. }
  622. elems->addba_ext_ie = (void *)pos;
  623. break;
  624. case WLAN_EID_TIMEOUT_INTERVAL:
  625. if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
  626. elems->timeout_int = (void *)pos;
  627. else
  628. elem_parse_failed =
  629. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  630. break;
  631. case WLAN_EID_BSS_MAX_IDLE_PERIOD:
  632. if (elen >= sizeof(*elems->max_idle_period_ie))
  633. elems->max_idle_period_ie = (void *)pos;
  634. break;
  635. case WLAN_EID_RSNX:
  636. elems->rsnx = pos;
  637. elems->rsnx_len = elen;
  638. break;
  639. case WLAN_EID_TX_POWER_ENVELOPE:
  640. if (params->mode < IEEE80211_CONN_MODE_HE)
  641. break;
  642. ieee80211_parse_tpe(&elems->tpe, pos, elen);
  643. break;
  644. case WLAN_EID_EXTENSION:
  645. ieee80211_parse_extension_element(calc_crc ?
  646. &crc : NULL,
  647. elem, elems_parse,
  648. params);
  649. break;
  650. case WLAN_EID_S1G_CAPABILITIES:
  651. if (params->mode != IEEE80211_CONN_MODE_S1G)
  652. break;
  653. if (elen >= sizeof(*elems->s1g_capab))
  654. elems->s1g_capab = (void *)pos;
  655. else
  656. elem_parse_failed =
  657. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  658. break;
  659. case WLAN_EID_S1G_OPERATION:
  660. if (params->mode != IEEE80211_CONN_MODE_S1G)
  661. break;
  662. if (elen == sizeof(*elems->s1g_oper))
  663. elems->s1g_oper = (void *)pos;
  664. else
  665. elem_parse_failed =
  666. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  667. break;
  668. case WLAN_EID_S1G_BCN_COMPAT:
  669. if (params->mode != IEEE80211_CONN_MODE_S1G)
  670. break;
  671. if (elen == sizeof(*elems->s1g_bcn_compat))
  672. elems->s1g_bcn_compat = (void *)pos;
  673. else
  674. elem_parse_failed =
  675. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  676. break;
  677. case WLAN_EID_AID_RESPONSE:
  678. if (params->mode != IEEE80211_CONN_MODE_S1G)
  679. break;
  680. if (elen == sizeof(struct ieee80211_aid_response_ie))
  681. elems->aid_resp = (void *)pos;
  682. else
  683. elem_parse_failed =
  684. IEEE80211_PARSE_ERR_BAD_ELEM_SIZE;
  685. break;
  686. default:
  687. break;
  688. }
  689. if (elem_parse_failed)
  690. elems->parse_error |= elem_parse_failed;
  691. else
  692. __set_bit(id, seen_elems);
  693. }
  694. if (!for_each_element_completed(elem, params->start, params->len))
  695. elems->parse_error |= IEEE80211_PARSE_ERR_INVALID_END;
  696. return crc;
  697. }
  698. static size_t ieee802_11_find_bssid_profile(const u8 *start, size_t len,
  699. struct ieee802_11_elems *elems,
  700. struct cfg80211_bss *bss,
  701. u8 *nontransmitted_profile)
  702. {
  703. const struct element *elem, *sub;
  704. size_t profile_len = 0;
  705. bool found = false;
  706. if (!bss || !bss->transmitted_bss)
  707. return profile_len;
  708. for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, start, len) {
  709. if (elem->datalen < 2)
  710. continue;
  711. if (elem->data[0] < 1 || elem->data[0] > 8)
  712. continue;
  713. for_each_element(sub, elem->data + 1, elem->datalen - 1) {
  714. u8 new_bssid[ETH_ALEN];
  715. const u8 *index;
  716. if (sub->id != 0 || sub->datalen < 4) {
  717. /* not a valid BSS profile */
  718. continue;
  719. }
  720. if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
  721. sub->data[1] != 2) {
  722. /* The first element of the
  723. * Nontransmitted BSSID Profile is not
  724. * the Nontransmitted BSSID Capability
  725. * element.
  726. */
  727. continue;
  728. }
  729. memset(nontransmitted_profile, 0, len);
  730. profile_len = cfg80211_merge_profile(start, len,
  731. elem,
  732. sub,
  733. nontransmitted_profile,
  734. len);
  735. /* found a Nontransmitted BSSID Profile */
  736. index = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX,
  737. nontransmitted_profile,
  738. profile_len);
  739. if (!index || index[1] < 1 || index[2] == 0) {
  740. /* Invalid MBSSID Index element */
  741. continue;
  742. }
  743. cfg80211_gen_new_bssid(bss->transmitted_bss->bssid,
  744. elem->data[0],
  745. index[2],
  746. new_bssid);
  747. if (ether_addr_equal(new_bssid, bss->bssid)) {
  748. found = true;
  749. elems->bssid_index_len = index[1];
  750. elems->bssid_index = (void *)&index[2];
  751. break;
  752. }
  753. }
  754. }
  755. return found ? profile_len : 0;
  756. }
  757. static void
  758. ieee80211_mle_get_sta_prof(struct ieee80211_elems_parse *elems_parse,
  759. u8 link_id)
  760. {
  761. struct ieee802_11_elems *elems = &elems_parse->elems;
  762. const struct ieee80211_multi_link_elem *ml = elems->ml_basic;
  763. ssize_t ml_len = elems->ml_basic_len;
  764. const struct element *sub;
  765. for_each_mle_subelement(sub, (u8 *)ml, ml_len) {
  766. struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data;
  767. ssize_t sta_prof_len;
  768. u16 control;
  769. if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE)
  770. continue;
  771. if (!ieee80211_mle_basic_sta_prof_size_ok(sub->data,
  772. sub->datalen))
  773. return;
  774. control = le16_to_cpu(prof->control);
  775. if (link_id != u16_get_bits(control,
  776. IEEE80211_MLE_STA_CONTROL_LINK_ID))
  777. continue;
  778. if (!(control & IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE))
  779. return;
  780. /* the sub element can be fragmented */
  781. sta_prof_len =
  782. cfg80211_defragment_element(sub,
  783. (u8 *)ml, ml_len,
  784. elems_parse->scratch_pos,
  785. elems_parse->scratch +
  786. elems_parse->scratch_len -
  787. elems_parse->scratch_pos,
  788. IEEE80211_MLE_SUBELEM_FRAGMENT);
  789. if (sta_prof_len < 0)
  790. return;
  791. elems->prof = (void *)elems_parse->scratch_pos;
  792. elems->sta_prof_len = sta_prof_len;
  793. elems_parse->scratch_pos += sta_prof_len;
  794. return;
  795. }
  796. }
  797. static void ieee80211_mle_parse_link(struct ieee80211_elems_parse *elems_parse,
  798. struct ieee80211_elems_parse_params *params)
  799. {
  800. struct ieee802_11_elems *elems = &elems_parse->elems;
  801. struct ieee80211_mle_per_sta_profile *prof;
  802. struct ieee80211_elems_parse_params sub = {
  803. .mode = params->mode,
  804. .action = params->action,
  805. .from_ap = params->from_ap,
  806. .link_id = -1,
  807. };
  808. ssize_t ml_len = elems->ml_basic_len;
  809. const struct element *non_inherit = NULL;
  810. const u8 *end;
  811. ml_len = cfg80211_defragment_element(elems_parse->ml_basic_elem,
  812. elems->ie_start,
  813. elems->total_len,
  814. elems_parse->scratch_pos,
  815. elems_parse->scratch +
  816. elems_parse->scratch_len -
  817. elems_parse->scratch_pos,
  818. WLAN_EID_FRAGMENT);
  819. if (ml_len < 0)
  820. return;
  821. elems->ml_basic = (const void *)elems_parse->scratch_pos;
  822. elems->ml_basic_len = ml_len;
  823. elems_parse->scratch_pos += ml_len;
  824. if (params->link_id == -1)
  825. return;
  826. ieee80211_mle_get_sta_prof(elems_parse, params->link_id);
  827. prof = elems->prof;
  828. if (!prof)
  829. return;
  830. /* check if we have the 4 bytes for the fixed part in assoc response */
  831. if (elems->sta_prof_len < sizeof(*prof) + prof->sta_info_len - 1 + 4) {
  832. elems->prof = NULL;
  833. elems->sta_prof_len = 0;
  834. return;
  835. }
  836. /*
  837. * Skip the capability information and the status code that are expected
  838. * as part of the station profile in association response frames. Note
  839. * the -1 is because the 'sta_info_len' is accounted to as part of the
  840. * per-STA profile, but not part of the 'u8 variable[]' portion.
  841. */
  842. sub.start = prof->variable + prof->sta_info_len - 1 + 4;
  843. end = (const u8 *)prof + elems->sta_prof_len;
  844. sub.len = end - sub.start;
  845. non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
  846. sub.start, sub.len);
  847. _ieee802_11_parse_elems_full(&sub, elems_parse, non_inherit);
  848. }
  849. static void
  850. ieee80211_mle_defrag_reconf(struct ieee80211_elems_parse *elems_parse)
  851. {
  852. struct ieee802_11_elems *elems = &elems_parse->elems;
  853. ssize_t ml_len;
  854. ml_len = cfg80211_defragment_element(elems_parse->ml_reconf_elem,
  855. elems->ie_start,
  856. elems->total_len,
  857. elems_parse->scratch_pos,
  858. elems_parse->scratch +
  859. elems_parse->scratch_len -
  860. elems_parse->scratch_pos,
  861. WLAN_EID_FRAGMENT);
  862. if (ml_len < 0)
  863. return;
  864. elems->ml_reconf = (void *)elems_parse->scratch_pos;
  865. elems->ml_reconf_len = ml_len;
  866. elems_parse->scratch_pos += ml_len;
  867. }
  868. struct ieee802_11_elems *
  869. ieee802_11_parse_elems_full(struct ieee80211_elems_parse_params *params)
  870. {
  871. struct ieee80211_elems_parse *elems_parse;
  872. struct ieee802_11_elems *elems;
  873. const struct element *non_inherit = NULL;
  874. u8 *nontransmitted_profile;
  875. int nontransmitted_profile_len = 0;
  876. size_t scratch_len = 3 * params->len;
  877. BUILD_BUG_ON(offsetof(typeof(*elems_parse), elems) != 0);
  878. elems_parse = kzalloc(struct_size(elems_parse, scratch, scratch_len),
  879. GFP_ATOMIC);
  880. if (!elems_parse)
  881. return NULL;
  882. elems_parse->scratch_len = scratch_len;
  883. elems_parse->scratch_pos = elems_parse->scratch;
  884. elems = &elems_parse->elems;
  885. elems->ie_start = params->start;
  886. elems->total_len = params->len;
  887. /* set all TPE entries to unlimited (but invalid) */
  888. ieee80211_clear_tpe(&elems->tpe);
  889. ieee80211_clear_tpe(&elems->csa_tpe);
  890. nontransmitted_profile = elems_parse->scratch_pos;
  891. nontransmitted_profile_len =
  892. ieee802_11_find_bssid_profile(params->start, params->len,
  893. elems, params->bss,
  894. nontransmitted_profile);
  895. elems_parse->scratch_pos += nontransmitted_profile_len;
  896. non_inherit = cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
  897. nontransmitted_profile,
  898. nontransmitted_profile_len);
  899. elems->crc = _ieee802_11_parse_elems_full(params, elems_parse,
  900. non_inherit);
  901. /* Override with nontransmitted profile, if found */
  902. if (nontransmitted_profile_len) {
  903. struct ieee80211_elems_parse_params sub = {
  904. .mode = params->mode,
  905. .start = nontransmitted_profile,
  906. .len = nontransmitted_profile_len,
  907. .action = params->action,
  908. .link_id = params->link_id,
  909. };
  910. _ieee802_11_parse_elems_full(&sub, elems_parse, NULL);
  911. }
  912. ieee80211_mle_parse_link(elems_parse, params);
  913. ieee80211_mle_defrag_reconf(elems_parse);
  914. if (elems->tim && !elems->parse_error) {
  915. const struct ieee80211_tim_ie *tim_ie = elems->tim;
  916. elems->dtim_period = tim_ie->dtim_period;
  917. elems->dtim_count = tim_ie->dtim_count;
  918. }
  919. /* Override DTIM period and count if needed */
  920. if (elems->bssid_index &&
  921. elems->bssid_index_len >=
  922. offsetofend(struct ieee80211_bssid_index, dtim_period))
  923. elems->dtim_period = elems->bssid_index->dtim_period;
  924. if (elems->bssid_index &&
  925. elems->bssid_index_len >=
  926. offsetofend(struct ieee80211_bssid_index, dtim_count))
  927. elems->dtim_count = elems->bssid_index->dtim_count;
  928. return elems;
  929. }
  930. EXPORT_SYMBOL_IF_KUNIT(ieee802_11_parse_elems_full);
  931. int ieee80211_parse_bitrates(enum nl80211_chan_width width,
  932. const struct ieee80211_supported_band *sband,
  933. const u8 *srates, int srates_len, u32 *rates)
  934. {
  935. u32 rate_flags = ieee80211_chanwidth_rate_flags(width);
  936. struct ieee80211_rate *br;
  937. int brate, rate, i, j, count = 0;
  938. *rates = 0;
  939. for (i = 0; i < srates_len; i++) {
  940. rate = srates[i] & 0x7f;
  941. for (j = 0; j < sband->n_bitrates; j++) {
  942. br = &sband->bitrates[j];
  943. if ((rate_flags & br->flags) != rate_flags)
  944. continue;
  945. brate = DIV_ROUND_UP(br->bitrate, 5);
  946. if (brate == rate) {
  947. *rates |= BIT(j);
  948. count++;
  949. break;
  950. }
  951. }
  952. }
  953. return count;
  954. }