spectmgmt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * spectrum management
  4. *
  5. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  6. * Copyright 2002-2005, Instant802 Networks, Inc.
  7. * Copyright 2005-2006, Devicescape Software, Inc.
  8. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  9. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  10. * Copyright 2007-2008, Intel Corporation
  11. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  12. * Copyright (C) 2018, 2020, 2022-2024 Intel Corporation
  13. */
  14. #include <linux/ieee80211.h>
  15. #include <net/cfg80211.h>
  16. #include <net/mac80211.h>
  17. #include "ieee80211_i.h"
  18. #include "sta_info.h"
  19. #include "wme.h"
  20. static bool
  21. wbcs_elem_to_chandef(const struct ieee80211_wide_bw_chansw_ie *wbcs_elem,
  22. struct cfg80211_chan_def *chandef)
  23. {
  24. u8 ccfs0 = wbcs_elem->new_center_freq_seg0;
  25. u8 ccfs1 = wbcs_elem->new_center_freq_seg1;
  26. u32 cf0 = ieee80211_channel_to_frequency(ccfs0, chandef->chan->band);
  27. u32 cf1 = ieee80211_channel_to_frequency(ccfs1, chandef->chan->band);
  28. switch (wbcs_elem->new_channel_width) {
  29. case IEEE80211_VHT_CHANWIDTH_160MHZ:
  30. /* deprecated encoding */
  31. chandef->width = NL80211_CHAN_WIDTH_160;
  32. chandef->center_freq1 = cf0;
  33. break;
  34. case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
  35. /* deprecated encoding */
  36. chandef->width = NL80211_CHAN_WIDTH_80P80;
  37. chandef->center_freq1 = cf0;
  38. chandef->center_freq2 = cf1;
  39. break;
  40. case IEEE80211_VHT_CHANWIDTH_80MHZ:
  41. chandef->width = NL80211_CHAN_WIDTH_80;
  42. chandef->center_freq1 = cf0;
  43. if (ccfs1) {
  44. u8 diff = abs(ccfs0 - ccfs1);
  45. if (diff == 8) {
  46. chandef->width = NL80211_CHAN_WIDTH_160;
  47. chandef->center_freq1 = cf1;
  48. } else if (diff > 8) {
  49. chandef->width = NL80211_CHAN_WIDTH_80P80;
  50. chandef->center_freq2 = cf1;
  51. }
  52. }
  53. break;
  54. case IEEE80211_VHT_CHANWIDTH_USE_HT:
  55. default:
  56. /* If the WBCS Element is present, new channel bandwidth is
  57. * at least 40 MHz.
  58. */
  59. chandef->width = NL80211_CHAN_WIDTH_40;
  60. chandef->center_freq1 = cf0;
  61. break;
  62. }
  63. return cfg80211_chandef_valid(chandef);
  64. }
  65. static void
  66. validate_chandef_by_ht_vht_oper(struct ieee80211_sub_if_data *sdata,
  67. struct ieee80211_conn_settings *conn,
  68. u32 vht_cap_info,
  69. struct cfg80211_chan_def *chandef)
  70. {
  71. u32 control_freq, center_freq1, center_freq2;
  72. enum nl80211_chan_width chan_width;
  73. struct ieee80211_ht_operation ht_oper;
  74. struct ieee80211_vht_operation vht_oper;
  75. if (conn->mode < IEEE80211_CONN_MODE_HT ||
  76. conn->bw_limit < IEEE80211_CONN_BW_LIMIT_40) {
  77. chandef->chan = NULL;
  78. return;
  79. }
  80. control_freq = chandef->chan->center_freq;
  81. center_freq1 = chandef->center_freq1;
  82. center_freq2 = chandef->center_freq2;
  83. chan_width = chandef->width;
  84. ht_oper.primary_chan = ieee80211_frequency_to_channel(control_freq);
  85. if (control_freq != center_freq1)
  86. ht_oper.ht_param = control_freq > center_freq1 ?
  87. IEEE80211_HT_PARAM_CHA_SEC_BELOW :
  88. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  89. else
  90. ht_oper.ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  91. ieee80211_chandef_ht_oper(&ht_oper, chandef);
  92. if (conn->mode < IEEE80211_CONN_MODE_VHT)
  93. return;
  94. vht_oper.center_freq_seg0_idx =
  95. ieee80211_frequency_to_channel(center_freq1);
  96. vht_oper.center_freq_seg1_idx = center_freq2 ?
  97. ieee80211_frequency_to_channel(center_freq2) : 0;
  98. switch (chan_width) {
  99. case NL80211_CHAN_WIDTH_320:
  100. WARN_ON(1);
  101. break;
  102. case NL80211_CHAN_WIDTH_160:
  103. vht_oper.chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  104. vht_oper.center_freq_seg1_idx = vht_oper.center_freq_seg0_idx;
  105. vht_oper.center_freq_seg0_idx +=
  106. control_freq < center_freq1 ? -8 : 8;
  107. break;
  108. case NL80211_CHAN_WIDTH_80P80:
  109. vht_oper.chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  110. break;
  111. case NL80211_CHAN_WIDTH_80:
  112. vht_oper.chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
  113. break;
  114. default:
  115. vht_oper.chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
  116. break;
  117. }
  118. ht_oper.operation_mode =
  119. le16_encode_bits(vht_oper.center_freq_seg1_idx,
  120. IEEE80211_HT_OP_MODE_CCFS2_MASK);
  121. if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info,
  122. &vht_oper, &ht_oper, chandef))
  123. chandef->chan = NULL;
  124. }
  125. static void
  126. validate_chandef_by_6ghz_he_eht_oper(struct ieee80211_sub_if_data *sdata,
  127. struct ieee80211_conn_settings *conn,
  128. struct cfg80211_chan_def *chandef)
  129. {
  130. struct ieee80211_local *local = sdata->local;
  131. u32 control_freq, center_freq1, center_freq2;
  132. enum nl80211_chan_width chan_width;
  133. struct {
  134. struct ieee80211_he_operation _oper;
  135. struct ieee80211_he_6ghz_oper _6ghz_oper;
  136. } __packed he;
  137. struct {
  138. struct ieee80211_eht_operation _oper;
  139. struct ieee80211_eht_operation_info _oper_info;
  140. } __packed eht;
  141. const struct ieee80211_eht_operation *eht_oper;
  142. if (conn->mode < IEEE80211_CONN_MODE_HE) {
  143. chandef->chan = NULL;
  144. return;
  145. }
  146. control_freq = chandef->chan->center_freq;
  147. center_freq1 = chandef->center_freq1;
  148. center_freq2 = chandef->center_freq2;
  149. chan_width = chandef->width;
  150. he._oper.he_oper_params =
  151. le32_encode_bits(1, IEEE80211_HE_OPERATION_6GHZ_OP_INFO);
  152. he._6ghz_oper.primary =
  153. ieee80211_frequency_to_channel(control_freq);
  154. he._6ghz_oper.ccfs0 = ieee80211_frequency_to_channel(center_freq1);
  155. he._6ghz_oper.ccfs1 = center_freq2 ?
  156. ieee80211_frequency_to_channel(center_freq2) : 0;
  157. switch (chan_width) {
  158. case NL80211_CHAN_WIDTH_320:
  159. he._6ghz_oper.ccfs1 = he._6ghz_oper.ccfs0;
  160. he._6ghz_oper.ccfs0 += control_freq < center_freq1 ? -16 : 16;
  161. he._6ghz_oper.control = IEEE80211_EHT_OPER_CHAN_WIDTH_320MHZ;
  162. break;
  163. case NL80211_CHAN_WIDTH_160:
  164. he._6ghz_oper.ccfs1 = he._6ghz_oper.ccfs0;
  165. he._6ghz_oper.ccfs0 += control_freq < center_freq1 ? -8 : 8;
  166. fallthrough;
  167. case NL80211_CHAN_WIDTH_80P80:
  168. he._6ghz_oper.control =
  169. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_160MHZ;
  170. break;
  171. case NL80211_CHAN_WIDTH_80:
  172. he._6ghz_oper.control =
  173. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_80MHZ;
  174. break;
  175. case NL80211_CHAN_WIDTH_40:
  176. he._6ghz_oper.control =
  177. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_40MHZ;
  178. break;
  179. default:
  180. he._6ghz_oper.control =
  181. IEEE80211_HE_6GHZ_OPER_CTRL_CHANWIDTH_20MHZ;
  182. break;
  183. }
  184. if (conn->mode < IEEE80211_CONN_MODE_EHT) {
  185. eht_oper = NULL;
  186. } else {
  187. eht._oper.params = IEEE80211_EHT_OPER_INFO_PRESENT;
  188. eht._oper_info.control = he._6ghz_oper.control;
  189. eht._oper_info.ccfs0 = he._6ghz_oper.ccfs0;
  190. eht._oper_info.ccfs1 = he._6ghz_oper.ccfs1;
  191. eht_oper = &eht._oper;
  192. }
  193. if (!ieee80211_chandef_he_6ghz_oper(local, &he._oper,
  194. eht_oper, chandef))
  195. chandef->chan = NULL;
  196. }
  197. int ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data *sdata,
  198. struct ieee802_11_elems *elems,
  199. enum nl80211_band current_band,
  200. u32 vht_cap_info,
  201. struct ieee80211_conn_settings *conn,
  202. u8 *bssid, bool unprot_action,
  203. struct ieee80211_csa_ie *csa_ie)
  204. {
  205. enum nl80211_band new_band = current_band;
  206. int new_freq;
  207. u8 new_chan_no = 0, new_op_class = 0;
  208. struct ieee80211_channel *new_chan;
  209. struct cfg80211_chan_def new_chandef = {};
  210. const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
  211. const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
  212. const struct ieee80211_bandwidth_indication *bwi;
  213. const struct ieee80211_ext_chansw_ie *ext_chansw_elem;
  214. int secondary_channel_offset = -1;
  215. memset(csa_ie, 0, sizeof(*csa_ie));
  216. sec_chan_offs = elems->sec_chan_offs;
  217. wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
  218. bwi = elems->bandwidth_indication;
  219. ext_chansw_elem = elems->ext_chansw_ie;
  220. if (conn->mode < IEEE80211_CONN_MODE_HT ||
  221. conn->bw_limit < IEEE80211_CONN_BW_LIMIT_40) {
  222. sec_chan_offs = NULL;
  223. wide_bw_chansw_ie = NULL;
  224. }
  225. if (conn->mode < IEEE80211_CONN_MODE_VHT)
  226. wide_bw_chansw_ie = NULL;
  227. if (ext_chansw_elem) {
  228. new_op_class = ext_chansw_elem->new_operating_class;
  229. if (!ieee80211_operating_class_to_band(new_op_class, &new_band)) {
  230. new_op_class = 0;
  231. if (!unprot_action)
  232. sdata_info(sdata,
  233. "cannot understand ECSA IE operating class, %d, ignoring\n",
  234. ext_chansw_elem->new_operating_class);
  235. } else {
  236. new_chan_no = ext_chansw_elem->new_ch_num;
  237. csa_ie->count = ext_chansw_elem->count;
  238. csa_ie->mode = ext_chansw_elem->mode;
  239. }
  240. }
  241. if (!new_op_class && elems->ch_switch_ie) {
  242. new_chan_no = elems->ch_switch_ie->new_ch_num;
  243. csa_ie->count = elems->ch_switch_ie->count;
  244. csa_ie->mode = elems->ch_switch_ie->mode;
  245. }
  246. /* nothing here we understand */
  247. if (!new_chan_no)
  248. return 1;
  249. /* Mesh Channel Switch Parameters Element */
  250. if (elems->mesh_chansw_params_ie) {
  251. csa_ie->ttl = elems->mesh_chansw_params_ie->mesh_ttl;
  252. csa_ie->mode = elems->mesh_chansw_params_ie->mesh_flags;
  253. csa_ie->pre_value = le16_to_cpu(
  254. elems->mesh_chansw_params_ie->mesh_pre_value);
  255. if (elems->mesh_chansw_params_ie->mesh_flags &
  256. WLAN_EID_CHAN_SWITCH_PARAM_REASON)
  257. csa_ie->reason_code = le16_to_cpu(
  258. elems->mesh_chansw_params_ie->mesh_reason);
  259. }
  260. new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
  261. new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
  262. if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
  263. if (!unprot_action)
  264. sdata_info(sdata,
  265. "BSS %pM switches to unsupported channel (%d MHz), disconnecting\n",
  266. bssid, new_freq);
  267. return -EINVAL;
  268. }
  269. if (sec_chan_offs) {
  270. secondary_channel_offset = sec_chan_offs->sec_chan_offs;
  271. } else if (conn->mode >= IEEE80211_CONN_MODE_HT) {
  272. /* If the secondary channel offset IE is not present,
  273. * we can't know what's the post-CSA offset, so the
  274. * best we can do is use 20MHz.
  275. */
  276. secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  277. }
  278. switch (secondary_channel_offset) {
  279. default:
  280. /* secondary_channel_offset was present but is invalid */
  281. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  282. cfg80211_chandef_create(&csa_ie->chanreq.oper, new_chan,
  283. NL80211_CHAN_HT20);
  284. break;
  285. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  286. cfg80211_chandef_create(&csa_ie->chanreq.oper, new_chan,
  287. NL80211_CHAN_HT40PLUS);
  288. break;
  289. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  290. cfg80211_chandef_create(&csa_ie->chanreq.oper, new_chan,
  291. NL80211_CHAN_HT40MINUS);
  292. break;
  293. case -1:
  294. cfg80211_chandef_create(&csa_ie->chanreq.oper, new_chan,
  295. NL80211_CHAN_NO_HT);
  296. /* keep width for 5/10 MHz channels */
  297. switch (sdata->vif.bss_conf.chanreq.oper.width) {
  298. case NL80211_CHAN_WIDTH_5:
  299. case NL80211_CHAN_WIDTH_10:
  300. csa_ie->chanreq.oper.width =
  301. sdata->vif.bss_conf.chanreq.oper.width;
  302. break;
  303. default:
  304. break;
  305. }
  306. break;
  307. }
  308. /* capture the AP configuration */
  309. csa_ie->chanreq.ap = csa_ie->chanreq.oper;
  310. /* parse one of the Elements to build a new chandef */
  311. memset(&new_chandef, 0, sizeof(new_chandef));
  312. new_chandef.chan = new_chan;
  313. if (bwi) {
  314. /* start with the CSA one */
  315. new_chandef = csa_ie->chanreq.oper;
  316. /* and update the width accordingly */
  317. ieee80211_chandef_eht_oper(&bwi->info, &new_chandef);
  318. if (bwi->params & IEEE80211_BW_IND_DIS_SUBCH_PRESENT)
  319. new_chandef.punctured =
  320. get_unaligned_le16(bwi->info.optional);
  321. } else if (!wide_bw_chansw_ie || !wbcs_elem_to_chandef(wide_bw_chansw_ie,
  322. &new_chandef)) {
  323. if (!ieee80211_operating_class_to_chandef(new_op_class, new_chan,
  324. &new_chandef))
  325. new_chandef = csa_ie->chanreq.oper;
  326. }
  327. /* check if the new chandef fits the capabilities */
  328. if (new_band == NL80211_BAND_6GHZ)
  329. validate_chandef_by_6ghz_he_eht_oper(sdata, conn, &new_chandef);
  330. else
  331. validate_chandef_by_ht_vht_oper(sdata, conn, vht_cap_info,
  332. &new_chandef);
  333. /* if data is there validate the bandwidth & use it */
  334. if (new_chandef.chan) {
  335. /* capture the AP chandef before (potential) downgrading */
  336. csa_ie->chanreq.ap = new_chandef;
  337. if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_320 &&
  338. new_chandef.width == NL80211_CHAN_WIDTH_320)
  339. ieee80211_chandef_downgrade(&new_chandef, NULL);
  340. if (conn->bw_limit < IEEE80211_CONN_BW_LIMIT_160 &&
  341. (new_chandef.width == NL80211_CHAN_WIDTH_80P80 ||
  342. new_chandef.width == NL80211_CHAN_WIDTH_160))
  343. ieee80211_chandef_downgrade(&new_chandef, NULL);
  344. if (!cfg80211_chandef_compatible(&new_chandef,
  345. &csa_ie->chanreq.oper)) {
  346. sdata_info(sdata,
  347. "BSS %pM: CSA has inconsistent channel data, disconnecting\n",
  348. bssid);
  349. return -EINVAL;
  350. }
  351. csa_ie->chanreq.oper = new_chandef;
  352. }
  353. if (elems->max_channel_switch_time)
  354. csa_ie->max_switch_time =
  355. (elems->max_channel_switch_time[0] << 0) |
  356. (elems->max_channel_switch_time[1] << 8) |
  357. (elems->max_channel_switch_time[2] << 16);
  358. return 0;
  359. }
  360. static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
  361. struct ieee80211_msrment_ie *request_ie,
  362. const u8 *da, const u8 *bssid,
  363. u8 dialog_token)
  364. {
  365. struct ieee80211_local *local = sdata->local;
  366. struct sk_buff *skb;
  367. struct ieee80211_mgmt *msr_report;
  368. skb = dev_alloc_skb(sizeof(*msr_report) + local->hw.extra_tx_headroom +
  369. sizeof(struct ieee80211_msrment_ie));
  370. if (!skb)
  371. return;
  372. skb_reserve(skb, local->hw.extra_tx_headroom);
  373. msr_report = skb_put_zero(skb, 24);
  374. memcpy(msr_report->da, da, ETH_ALEN);
  375. memcpy(msr_report->sa, sdata->vif.addr, ETH_ALEN);
  376. memcpy(msr_report->bssid, bssid, ETH_ALEN);
  377. msr_report->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
  378. IEEE80211_STYPE_ACTION);
  379. skb_put(skb, 1 + sizeof(msr_report->u.action.u.measurement));
  380. msr_report->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
  381. msr_report->u.action.u.measurement.action_code =
  382. WLAN_ACTION_SPCT_MSR_RPRT;
  383. msr_report->u.action.u.measurement.dialog_token = dialog_token;
  384. msr_report->u.action.u.measurement.element_id = WLAN_EID_MEASURE_REPORT;
  385. msr_report->u.action.u.measurement.length =
  386. sizeof(struct ieee80211_msrment_ie);
  387. memset(&msr_report->u.action.u.measurement.msr_elem, 0,
  388. sizeof(struct ieee80211_msrment_ie));
  389. msr_report->u.action.u.measurement.msr_elem.token = request_ie->token;
  390. msr_report->u.action.u.measurement.msr_elem.mode |=
  391. IEEE80211_SPCT_MSR_RPRT_MODE_REFUSED;
  392. msr_report->u.action.u.measurement.msr_elem.type = request_ie->type;
  393. ieee80211_tx_skb(sdata, skb);
  394. }
  395. void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata,
  396. struct ieee80211_mgmt *mgmt,
  397. size_t len)
  398. {
  399. /*
  400. * Ignoring measurement request is spec violation.
  401. * Mandatory measurements must be reported optional
  402. * measurements might be refused or reported incapable
  403. * For now just refuse
  404. * TODO: Answer basic measurement as unmeasured
  405. */
  406. ieee80211_send_refuse_measurement_request(sdata,
  407. &mgmt->u.action.u.measurement.msr_elem,
  408. mgmt->sa, mgmt->bssid,
  409. mgmt->u.action.u.measurement.dialog_token);
  410. }