chan.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * mac80211 - channel management
  3. */
  4. #include <linux/nl80211.h>
  5. #include <linux/export.h>
  6. #include <linux/rtnetlink.h>
  7. #include <net/cfg80211.h>
  8. #include "ieee80211_i.h"
  9. #include "driver-ops.h"
  10. static enum nl80211_chan_width ieee80211_get_sta_bw(struct ieee80211_sta *sta)
  11. {
  12. switch (sta->bandwidth) {
  13. case IEEE80211_STA_RX_BW_20:
  14. if (sta->ht_cap.ht_supported)
  15. return NL80211_CHAN_WIDTH_20;
  16. else
  17. return NL80211_CHAN_WIDTH_20_NOHT;
  18. case IEEE80211_STA_RX_BW_40:
  19. return NL80211_CHAN_WIDTH_40;
  20. case IEEE80211_STA_RX_BW_80:
  21. return NL80211_CHAN_WIDTH_80;
  22. case IEEE80211_STA_RX_BW_160:
  23. /*
  24. * This applied for both 160 and 80+80. since we use
  25. * the returned value to consider degradation of
  26. * ctx->conf.min_def, we have to make sure to take
  27. * the bigger one (NL80211_CHAN_WIDTH_160).
  28. * Otherwise we might try degrading even when not
  29. * needed, as the max required sta_bw returned (80+80)
  30. * might be smaller than the configured bw (160).
  31. */
  32. return NL80211_CHAN_WIDTH_160;
  33. default:
  34. WARN_ON(1);
  35. return NL80211_CHAN_WIDTH_20;
  36. }
  37. }
  38. static enum nl80211_chan_width
  39. ieee80211_get_max_required_bw(struct ieee80211_sub_if_data *sdata)
  40. {
  41. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  42. struct sta_info *sta;
  43. rcu_read_lock();
  44. list_for_each_entry_rcu(sta, &sdata->local->sta_list, list) {
  45. if (sdata != sta->sdata &&
  46. !(sta->sdata->bss && sta->sdata->bss == sdata->bss))
  47. continue;
  48. if (!sta->uploaded)
  49. continue;
  50. max_bw = max(max_bw, ieee80211_get_sta_bw(&sta->sta));
  51. }
  52. rcu_read_unlock();
  53. return max_bw;
  54. }
  55. static enum nl80211_chan_width
  56. ieee80211_get_chanctx_max_required_bw(struct ieee80211_local *local,
  57. struct ieee80211_chanctx_conf *conf)
  58. {
  59. struct ieee80211_sub_if_data *sdata;
  60. enum nl80211_chan_width max_bw = NL80211_CHAN_WIDTH_20_NOHT;
  61. rcu_read_lock();
  62. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  63. struct ieee80211_vif *vif = &sdata->vif;
  64. enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20_NOHT;
  65. if (!ieee80211_sdata_running(sdata))
  66. continue;
  67. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  68. continue;
  69. switch (vif->type) {
  70. case NL80211_IFTYPE_AP:
  71. case NL80211_IFTYPE_AP_VLAN:
  72. width = ieee80211_get_max_required_bw(sdata);
  73. break;
  74. case NL80211_IFTYPE_P2P_DEVICE:
  75. continue;
  76. case NL80211_IFTYPE_STATION:
  77. case NL80211_IFTYPE_ADHOC:
  78. case NL80211_IFTYPE_WDS:
  79. case NL80211_IFTYPE_MESH_POINT:
  80. width = vif->bss_conf.chandef.width;
  81. break;
  82. case NL80211_IFTYPE_UNSPECIFIED:
  83. case NUM_NL80211_IFTYPES:
  84. case NL80211_IFTYPE_MONITOR:
  85. case NL80211_IFTYPE_P2P_CLIENT:
  86. case NL80211_IFTYPE_P2P_GO:
  87. WARN_ON_ONCE(1);
  88. }
  89. max_bw = max(max_bw, width);
  90. }
  91. rcu_read_unlock();
  92. return max_bw;
  93. }
  94. /*
  95. * recalc the min required chan width of the channel context, which is
  96. * the max of min required widths of all the interfaces bound to this
  97. * channel context.
  98. */
  99. void ieee80211_recalc_chanctx_min_def(struct ieee80211_local *local,
  100. struct ieee80211_chanctx *ctx)
  101. {
  102. enum nl80211_chan_width max_bw;
  103. struct cfg80211_chan_def min_def;
  104. lockdep_assert_held(&local->chanctx_mtx);
  105. /* don't optimize 5MHz, 10MHz, and radar_enabled confs */
  106. if (ctx->conf.def.width == NL80211_CHAN_WIDTH_5 ||
  107. ctx->conf.def.width == NL80211_CHAN_WIDTH_10 ||
  108. ctx->conf.radar_enabled) {
  109. ctx->conf.min_def = ctx->conf.def;
  110. return;
  111. }
  112. max_bw = ieee80211_get_chanctx_max_required_bw(local, &ctx->conf);
  113. /* downgrade chandef up to max_bw */
  114. min_def = ctx->conf.def;
  115. while (min_def.width > max_bw)
  116. ieee80211_chandef_downgrade(&min_def);
  117. if (cfg80211_chandef_identical(&ctx->conf.min_def, &min_def))
  118. return;
  119. ctx->conf.min_def = min_def;
  120. if (!ctx->driver_present)
  121. return;
  122. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_MIN_WIDTH);
  123. }
  124. static void ieee80211_change_chanctx(struct ieee80211_local *local,
  125. struct ieee80211_chanctx *ctx,
  126. const struct cfg80211_chan_def *chandef)
  127. {
  128. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  129. return;
  130. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  131. ctx->conf.def = *chandef;
  132. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  133. ieee80211_recalc_chanctx_min_def(local, ctx);
  134. if (!local->use_chanctx) {
  135. local->_oper_chandef = *chandef;
  136. ieee80211_hw_config(local, 0);
  137. }
  138. }
  139. static struct ieee80211_chanctx *
  140. ieee80211_find_chanctx(struct ieee80211_local *local,
  141. const struct cfg80211_chan_def *chandef,
  142. enum ieee80211_chanctx_mode mode)
  143. {
  144. struct ieee80211_chanctx *ctx;
  145. lockdep_assert_held(&local->chanctx_mtx);
  146. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  147. return NULL;
  148. list_for_each_entry(ctx, &local->chanctx_list, list) {
  149. const struct cfg80211_chan_def *compat;
  150. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  151. continue;
  152. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  153. if (!compat)
  154. continue;
  155. ieee80211_change_chanctx(local, ctx, compat);
  156. return ctx;
  157. }
  158. return NULL;
  159. }
  160. static bool ieee80211_is_radar_required(struct ieee80211_local *local)
  161. {
  162. struct ieee80211_sub_if_data *sdata;
  163. lockdep_assert_held(&local->mtx);
  164. rcu_read_lock();
  165. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  166. if (sdata->radar_required) {
  167. rcu_read_unlock();
  168. return true;
  169. }
  170. }
  171. rcu_read_unlock();
  172. return false;
  173. }
  174. static struct ieee80211_chanctx *
  175. ieee80211_new_chanctx(struct ieee80211_local *local,
  176. const struct cfg80211_chan_def *chandef,
  177. enum ieee80211_chanctx_mode mode)
  178. {
  179. struct ieee80211_chanctx *ctx;
  180. u32 changed;
  181. int err;
  182. lockdep_assert_held(&local->chanctx_mtx);
  183. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  184. if (!ctx)
  185. return ERR_PTR(-ENOMEM);
  186. ctx->conf.def = *chandef;
  187. ctx->conf.rx_chains_static = 1;
  188. ctx->conf.rx_chains_dynamic = 1;
  189. ctx->mode = mode;
  190. ctx->conf.radar_enabled = ieee80211_is_radar_required(local);
  191. ieee80211_recalc_chanctx_min_def(local, ctx);
  192. if (!local->use_chanctx)
  193. local->hw.conf.radar_enabled = ctx->conf.radar_enabled;
  194. /* we hold the mutex to prevent idle from changing */
  195. lockdep_assert_held(&local->mtx);
  196. /* turn idle off *before* setting channel -- some drivers need that */
  197. changed = ieee80211_idle_off(local);
  198. if (changed)
  199. ieee80211_hw_config(local, changed);
  200. if (!local->use_chanctx) {
  201. local->_oper_chandef = *chandef;
  202. ieee80211_hw_config(local, 0);
  203. } else {
  204. err = drv_add_chanctx(local, ctx);
  205. if (err) {
  206. kfree(ctx);
  207. ieee80211_recalc_idle(local);
  208. return ERR_PTR(err);
  209. }
  210. }
  211. /* and keep the mutex held until the new chanctx is on the list */
  212. list_add_rcu(&ctx->list, &local->chanctx_list);
  213. return ctx;
  214. }
  215. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  216. struct ieee80211_chanctx *ctx)
  217. {
  218. bool check_single_channel = false;
  219. lockdep_assert_held(&local->chanctx_mtx);
  220. WARN_ON_ONCE(ctx->refcount != 0);
  221. if (!local->use_chanctx) {
  222. struct cfg80211_chan_def *chandef = &local->_oper_chandef;
  223. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  224. chandef->center_freq1 = chandef->chan->center_freq;
  225. chandef->center_freq2 = 0;
  226. /* NOTE: Disabling radar is only valid here for
  227. * single channel context. To be sure, check it ...
  228. */
  229. if (local->hw.conf.radar_enabled)
  230. check_single_channel = true;
  231. local->hw.conf.radar_enabled = false;
  232. ieee80211_hw_config(local, 0);
  233. } else {
  234. drv_remove_chanctx(local, ctx);
  235. }
  236. list_del_rcu(&ctx->list);
  237. kfree_rcu(ctx, rcu_head);
  238. /* throw a warning if this wasn't the only channel context. */
  239. WARN_ON(check_single_channel && !list_empty(&local->chanctx_list));
  240. ieee80211_recalc_idle(local);
  241. }
  242. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  243. struct ieee80211_chanctx *ctx)
  244. {
  245. struct ieee80211_local *local = sdata->local;
  246. int ret;
  247. lockdep_assert_held(&local->chanctx_mtx);
  248. ret = drv_assign_vif_chanctx(local, sdata, ctx);
  249. if (ret)
  250. return ret;
  251. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  252. ctx->refcount++;
  253. ieee80211_recalc_txpower(sdata);
  254. ieee80211_recalc_chanctx_min_def(local, ctx);
  255. sdata->vif.bss_conf.idle = false;
  256. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  257. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  258. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  259. return 0;
  260. }
  261. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  262. struct ieee80211_chanctx *ctx)
  263. {
  264. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  265. struct ieee80211_sub_if_data *sdata;
  266. const struct cfg80211_chan_def *compat = NULL;
  267. lockdep_assert_held(&local->chanctx_mtx);
  268. rcu_read_lock();
  269. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  270. if (!ieee80211_sdata_running(sdata))
  271. continue;
  272. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  273. continue;
  274. if (!compat)
  275. compat = &sdata->vif.bss_conf.chandef;
  276. compat = cfg80211_chandef_compatible(
  277. &sdata->vif.bss_conf.chandef, compat);
  278. if (!compat)
  279. break;
  280. }
  281. rcu_read_unlock();
  282. if (WARN_ON_ONCE(!compat))
  283. return;
  284. ieee80211_change_chanctx(local, ctx, compat);
  285. }
  286. static void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  287. struct ieee80211_chanctx *chanctx)
  288. {
  289. bool radar_enabled;
  290. lockdep_assert_held(&local->chanctx_mtx);
  291. /* for setting local->radar_detect_enabled */
  292. lockdep_assert_held(&local->mtx);
  293. radar_enabled = ieee80211_is_radar_required(local);
  294. if (radar_enabled == chanctx->conf.radar_enabled)
  295. return;
  296. chanctx->conf.radar_enabled = radar_enabled;
  297. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  298. if (!local->use_chanctx) {
  299. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  300. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  301. }
  302. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  303. }
  304. static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  305. struct ieee80211_chanctx *ctx)
  306. {
  307. struct ieee80211_local *local = sdata->local;
  308. lockdep_assert_held(&local->chanctx_mtx);
  309. ctx->refcount--;
  310. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  311. sdata->vif.bss_conf.idle = true;
  312. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  313. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  314. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  315. drv_unassign_vif_chanctx(local, sdata, ctx);
  316. if (ctx->refcount > 0) {
  317. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  318. ieee80211_recalc_smps_chanctx(local, ctx);
  319. ieee80211_recalc_radar_chanctx(local, ctx);
  320. ieee80211_recalc_chanctx_min_def(local, ctx);
  321. }
  322. }
  323. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  324. {
  325. struct ieee80211_local *local = sdata->local;
  326. struct ieee80211_chanctx_conf *conf;
  327. struct ieee80211_chanctx *ctx;
  328. lockdep_assert_held(&local->chanctx_mtx);
  329. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  330. lockdep_is_held(&local->chanctx_mtx));
  331. if (!conf)
  332. return;
  333. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  334. ieee80211_unassign_vif_chanctx(sdata, ctx);
  335. if (ctx->refcount == 0)
  336. ieee80211_free_chanctx(local, ctx);
  337. }
  338. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  339. struct ieee80211_chanctx *chanctx)
  340. {
  341. struct ieee80211_sub_if_data *sdata;
  342. u8 rx_chains_static, rx_chains_dynamic;
  343. lockdep_assert_held(&local->chanctx_mtx);
  344. rx_chains_static = 1;
  345. rx_chains_dynamic = 1;
  346. rcu_read_lock();
  347. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  348. u8 needed_static, needed_dynamic;
  349. if (!ieee80211_sdata_running(sdata))
  350. continue;
  351. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  352. &chanctx->conf)
  353. continue;
  354. switch (sdata->vif.type) {
  355. case NL80211_IFTYPE_P2P_DEVICE:
  356. continue;
  357. case NL80211_IFTYPE_STATION:
  358. if (!sdata->u.mgd.associated)
  359. continue;
  360. break;
  361. case NL80211_IFTYPE_AP_VLAN:
  362. continue;
  363. case NL80211_IFTYPE_AP:
  364. case NL80211_IFTYPE_ADHOC:
  365. case NL80211_IFTYPE_WDS:
  366. case NL80211_IFTYPE_MESH_POINT:
  367. break;
  368. default:
  369. WARN_ON_ONCE(1);
  370. }
  371. switch (sdata->smps_mode) {
  372. default:
  373. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  374. sdata->smps_mode);
  375. /* fall through */
  376. case IEEE80211_SMPS_OFF:
  377. needed_static = sdata->needed_rx_chains;
  378. needed_dynamic = sdata->needed_rx_chains;
  379. break;
  380. case IEEE80211_SMPS_DYNAMIC:
  381. needed_static = 1;
  382. needed_dynamic = sdata->needed_rx_chains;
  383. break;
  384. case IEEE80211_SMPS_STATIC:
  385. needed_static = 1;
  386. needed_dynamic = 1;
  387. break;
  388. }
  389. rx_chains_static = max(rx_chains_static, needed_static);
  390. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  391. }
  392. rcu_read_unlock();
  393. if (!local->use_chanctx) {
  394. if (rx_chains_static > 1)
  395. local->smps_mode = IEEE80211_SMPS_OFF;
  396. else if (rx_chains_dynamic > 1)
  397. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  398. else
  399. local->smps_mode = IEEE80211_SMPS_STATIC;
  400. ieee80211_hw_config(local, 0);
  401. }
  402. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  403. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  404. return;
  405. chanctx->conf.rx_chains_static = rx_chains_static;
  406. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  407. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  408. }
  409. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  410. const struct cfg80211_chan_def *chandef,
  411. enum ieee80211_chanctx_mode mode)
  412. {
  413. struct ieee80211_local *local = sdata->local;
  414. struct ieee80211_chanctx *ctx;
  415. int ret;
  416. lockdep_assert_held(&local->mtx);
  417. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  418. mutex_lock(&local->chanctx_mtx);
  419. __ieee80211_vif_release_channel(sdata);
  420. ctx = ieee80211_find_chanctx(local, chandef, mode);
  421. if (!ctx)
  422. ctx = ieee80211_new_chanctx(local, chandef, mode);
  423. if (IS_ERR(ctx)) {
  424. ret = PTR_ERR(ctx);
  425. goto out;
  426. }
  427. sdata->vif.bss_conf.chandef = *chandef;
  428. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  429. if (ret) {
  430. /* if assign fails refcount stays the same */
  431. if (ctx->refcount == 0)
  432. ieee80211_free_chanctx(local, ctx);
  433. goto out;
  434. }
  435. ieee80211_recalc_smps_chanctx(local, ctx);
  436. ieee80211_recalc_radar_chanctx(local, ctx);
  437. out:
  438. mutex_unlock(&local->chanctx_mtx);
  439. return ret;
  440. }
  441. int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
  442. u32 *changed)
  443. {
  444. struct ieee80211_local *local = sdata->local;
  445. struct ieee80211_chanctx_conf *conf;
  446. struct ieee80211_chanctx *ctx;
  447. const struct cfg80211_chan_def *chandef = &sdata->csa_chandef;
  448. int ret;
  449. u32 chanctx_changed = 0;
  450. lockdep_assert_held(&local->mtx);
  451. /* should never be called if not performing a channel switch. */
  452. if (WARN_ON(!sdata->vif.csa_active))
  453. return -EINVAL;
  454. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  455. IEEE80211_CHAN_DISABLED))
  456. return -EINVAL;
  457. mutex_lock(&local->chanctx_mtx);
  458. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  459. lockdep_is_held(&local->chanctx_mtx));
  460. if (!conf) {
  461. ret = -EINVAL;
  462. goto out;
  463. }
  464. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  465. if (ctx->refcount != 1) {
  466. ret = -EINVAL;
  467. goto out;
  468. }
  469. if (sdata->vif.bss_conf.chandef.width != chandef->width) {
  470. chanctx_changed = IEEE80211_CHANCTX_CHANGE_WIDTH;
  471. *changed |= BSS_CHANGED_BANDWIDTH;
  472. }
  473. sdata->vif.bss_conf.chandef = *chandef;
  474. ctx->conf.def = *chandef;
  475. chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
  476. drv_change_chanctx(local, ctx, chanctx_changed);
  477. ieee80211_recalc_chanctx_chantype(local, ctx);
  478. ieee80211_recalc_smps_chanctx(local, ctx);
  479. ieee80211_recalc_radar_chanctx(local, ctx);
  480. ieee80211_recalc_chanctx_min_def(local, ctx);
  481. ret = 0;
  482. out:
  483. mutex_unlock(&local->chanctx_mtx);
  484. return ret;
  485. }
  486. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  487. const struct cfg80211_chan_def *chandef,
  488. u32 *changed)
  489. {
  490. struct ieee80211_local *local = sdata->local;
  491. struct ieee80211_chanctx_conf *conf;
  492. struct ieee80211_chanctx *ctx;
  493. int ret;
  494. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  495. IEEE80211_CHAN_DISABLED))
  496. return -EINVAL;
  497. mutex_lock(&local->chanctx_mtx);
  498. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  499. ret = 0;
  500. goto out;
  501. }
  502. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  503. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  504. ret = -EINVAL;
  505. goto out;
  506. }
  507. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  508. lockdep_is_held(&local->chanctx_mtx));
  509. if (!conf) {
  510. ret = -EINVAL;
  511. goto out;
  512. }
  513. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  514. if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
  515. ret = -EINVAL;
  516. goto out;
  517. }
  518. sdata->vif.bss_conf.chandef = *chandef;
  519. ieee80211_recalc_chanctx_chantype(local, ctx);
  520. *changed |= BSS_CHANGED_BANDWIDTH;
  521. ret = 0;
  522. out:
  523. mutex_unlock(&local->chanctx_mtx);
  524. return ret;
  525. }
  526. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  527. {
  528. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  529. lockdep_assert_held(&sdata->local->mtx);
  530. mutex_lock(&sdata->local->chanctx_mtx);
  531. __ieee80211_vif_release_channel(sdata);
  532. mutex_unlock(&sdata->local->chanctx_mtx);
  533. }
  534. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  535. {
  536. struct ieee80211_local *local = sdata->local;
  537. struct ieee80211_sub_if_data *ap;
  538. struct ieee80211_chanctx_conf *conf;
  539. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  540. return;
  541. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  542. mutex_lock(&local->chanctx_mtx);
  543. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  544. lockdep_is_held(&local->chanctx_mtx));
  545. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  546. mutex_unlock(&local->chanctx_mtx);
  547. }
  548. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  549. bool clear)
  550. {
  551. struct ieee80211_local *local = sdata->local;
  552. struct ieee80211_sub_if_data *vlan;
  553. struct ieee80211_chanctx_conf *conf;
  554. ASSERT_RTNL();
  555. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  556. return;
  557. mutex_lock(&local->chanctx_mtx);
  558. /*
  559. * Check that conf exists, even when clearing this function
  560. * must be called with the AP's channel context still there
  561. * as it would otherwise cause VLANs to have an invalid
  562. * channel context pointer for a while, possibly pointing
  563. * to a channel context that has already been freed.
  564. */
  565. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  566. lockdep_is_held(&local->chanctx_mtx));
  567. WARN_ON(!conf);
  568. if (clear)
  569. conf = NULL;
  570. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  571. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  572. mutex_unlock(&local->chanctx_mtx);
  573. }
  574. void ieee80211_iter_chan_contexts_atomic(
  575. struct ieee80211_hw *hw,
  576. void (*iter)(struct ieee80211_hw *hw,
  577. struct ieee80211_chanctx_conf *chanctx_conf,
  578. void *data),
  579. void *iter_data)
  580. {
  581. struct ieee80211_local *local = hw_to_local(hw);
  582. struct ieee80211_chanctx *ctx;
  583. rcu_read_lock();
  584. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  585. if (ctx->driver_present)
  586. iter(hw, &ctx->conf, iter_data);
  587. rcu_read_unlock();
  588. }
  589. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);