chan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 void ieee80211_change_chanctx(struct ieee80211_local *local,
  11. struct ieee80211_chanctx *ctx,
  12. const struct cfg80211_chan_def *chandef)
  13. {
  14. if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
  15. return;
  16. WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
  17. ctx->conf.def = *chandef;
  18. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
  19. if (!local->use_chanctx) {
  20. local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
  21. ieee80211_hw_config(local, 0);
  22. }
  23. }
  24. static struct ieee80211_chanctx *
  25. ieee80211_find_chanctx(struct ieee80211_local *local,
  26. const struct cfg80211_chan_def *chandef,
  27. enum ieee80211_chanctx_mode mode)
  28. {
  29. struct ieee80211_chanctx *ctx;
  30. lockdep_assert_held(&local->chanctx_mtx);
  31. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  32. return NULL;
  33. list_for_each_entry(ctx, &local->chanctx_list, list) {
  34. const struct cfg80211_chan_def *compat;
  35. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  36. continue;
  37. compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
  38. if (!compat)
  39. continue;
  40. ieee80211_change_chanctx(local, ctx, compat);
  41. return ctx;
  42. }
  43. return NULL;
  44. }
  45. static struct ieee80211_chanctx *
  46. ieee80211_new_chanctx(struct ieee80211_local *local,
  47. const struct cfg80211_chan_def *chandef,
  48. enum ieee80211_chanctx_mode mode)
  49. {
  50. struct ieee80211_chanctx *ctx;
  51. u32 changed;
  52. int err;
  53. lockdep_assert_held(&local->chanctx_mtx);
  54. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  55. if (!ctx)
  56. return ERR_PTR(-ENOMEM);
  57. ctx->conf.def = *chandef;
  58. ctx->conf.rx_chains_static = 1;
  59. ctx->conf.rx_chains_dynamic = 1;
  60. ctx->mode = mode;
  61. /* acquire mutex to prevent idle from changing */
  62. mutex_lock(&local->mtx);
  63. /* turn idle off *before* setting channel -- some drivers need that */
  64. changed = ieee80211_idle_off(local);
  65. if (changed)
  66. ieee80211_hw_config(local, changed);
  67. if (!local->use_chanctx) {
  68. local->_oper_channel_type =
  69. cfg80211_get_chandef_type(chandef);
  70. local->_oper_channel = chandef->chan;
  71. ieee80211_hw_config(local, 0);
  72. } else {
  73. err = drv_add_chanctx(local, ctx);
  74. if (err) {
  75. kfree(ctx);
  76. ctx = ERR_PTR(err);
  77. ieee80211_recalc_idle(local);
  78. goto out;
  79. }
  80. }
  81. /* and keep the mutex held until the new chanctx is on the list */
  82. list_add_rcu(&ctx->list, &local->chanctx_list);
  83. out:
  84. mutex_unlock(&local->mtx);
  85. return ctx;
  86. }
  87. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  88. struct ieee80211_chanctx *ctx)
  89. {
  90. lockdep_assert_held(&local->chanctx_mtx);
  91. WARN_ON_ONCE(ctx->refcount != 0);
  92. if (!local->use_chanctx) {
  93. local->_oper_channel_type = NL80211_CHAN_NO_HT;
  94. ieee80211_hw_config(local, 0);
  95. } else {
  96. drv_remove_chanctx(local, ctx);
  97. }
  98. list_del_rcu(&ctx->list);
  99. kfree_rcu(ctx, rcu_head);
  100. mutex_lock(&local->mtx);
  101. ieee80211_recalc_idle(local);
  102. mutex_unlock(&local->mtx);
  103. }
  104. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  105. struct ieee80211_chanctx *ctx)
  106. {
  107. struct ieee80211_local *local = sdata->local;
  108. int ret;
  109. lockdep_assert_held(&local->chanctx_mtx);
  110. ret = drv_assign_vif_chanctx(local, sdata, ctx);
  111. if (ret)
  112. return ret;
  113. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  114. ctx->refcount++;
  115. ieee80211_recalc_txpower(sdata);
  116. sdata->vif.bss_conf.idle = false;
  117. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  118. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  119. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  120. return 0;
  121. }
  122. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  123. struct ieee80211_chanctx *ctx)
  124. {
  125. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  126. struct ieee80211_sub_if_data *sdata;
  127. const struct cfg80211_chan_def *compat = NULL;
  128. lockdep_assert_held(&local->chanctx_mtx);
  129. rcu_read_lock();
  130. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  131. if (!ieee80211_sdata_running(sdata))
  132. continue;
  133. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  134. continue;
  135. if (!compat)
  136. compat = &sdata->vif.bss_conf.chandef;
  137. compat = cfg80211_chandef_compatible(
  138. &sdata->vif.bss_conf.chandef, compat);
  139. if (!compat)
  140. break;
  141. }
  142. rcu_read_unlock();
  143. if (WARN_ON_ONCE(!compat))
  144. return;
  145. ieee80211_change_chanctx(local, ctx, compat);
  146. }
  147. static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  148. struct ieee80211_chanctx *ctx)
  149. {
  150. struct ieee80211_local *local = sdata->local;
  151. lockdep_assert_held(&local->chanctx_mtx);
  152. ctx->refcount--;
  153. rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
  154. sdata->vif.bss_conf.idle = true;
  155. if (sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
  156. sdata->vif.type != NL80211_IFTYPE_MONITOR)
  157. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
  158. drv_unassign_vif_chanctx(local, sdata, ctx);
  159. if (ctx->refcount > 0) {
  160. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  161. ieee80211_recalc_smps_chanctx(local, ctx);
  162. ieee80211_recalc_radar_chanctx(local, ctx);
  163. }
  164. }
  165. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  166. {
  167. struct ieee80211_local *local = sdata->local;
  168. struct ieee80211_chanctx_conf *conf;
  169. struct ieee80211_chanctx *ctx;
  170. lockdep_assert_held(&local->chanctx_mtx);
  171. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  172. lockdep_is_held(&local->chanctx_mtx));
  173. if (!conf)
  174. return;
  175. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  176. ieee80211_unassign_vif_chanctx(sdata, ctx);
  177. if (ctx->refcount == 0)
  178. ieee80211_free_chanctx(local, ctx);
  179. }
  180. void ieee80211_recalc_radar_chanctx(struct ieee80211_local *local,
  181. struct ieee80211_chanctx *chanctx)
  182. {
  183. struct ieee80211_sub_if_data *sdata;
  184. bool radar_enabled = false;
  185. lockdep_assert_held(&local->chanctx_mtx);
  186. rcu_read_lock();
  187. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  188. if (sdata->radar_required) {
  189. radar_enabled = true;
  190. break;
  191. }
  192. }
  193. rcu_read_unlock();
  194. if (radar_enabled == chanctx->conf.radar_enabled)
  195. return;
  196. chanctx->conf.radar_enabled = radar_enabled;
  197. local->radar_detect_enabled = chanctx->conf.radar_enabled;
  198. if (!local->use_chanctx) {
  199. local->hw.conf.radar_enabled = chanctx->conf.radar_enabled;
  200. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  201. }
  202. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RADAR);
  203. }
  204. void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
  205. struct ieee80211_chanctx *chanctx)
  206. {
  207. struct ieee80211_sub_if_data *sdata;
  208. u8 rx_chains_static, rx_chains_dynamic;
  209. lockdep_assert_held(&local->chanctx_mtx);
  210. rx_chains_static = 1;
  211. rx_chains_dynamic = 1;
  212. rcu_read_lock();
  213. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  214. u8 needed_static, needed_dynamic;
  215. if (!ieee80211_sdata_running(sdata))
  216. continue;
  217. if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
  218. &chanctx->conf)
  219. continue;
  220. switch (sdata->vif.type) {
  221. case NL80211_IFTYPE_P2P_DEVICE:
  222. continue;
  223. case NL80211_IFTYPE_STATION:
  224. if (!sdata->u.mgd.associated)
  225. continue;
  226. break;
  227. case NL80211_IFTYPE_AP_VLAN:
  228. continue;
  229. case NL80211_IFTYPE_AP:
  230. case NL80211_IFTYPE_ADHOC:
  231. case NL80211_IFTYPE_WDS:
  232. case NL80211_IFTYPE_MESH_POINT:
  233. break;
  234. default:
  235. WARN_ON_ONCE(1);
  236. }
  237. switch (sdata->smps_mode) {
  238. default:
  239. WARN_ONCE(1, "Invalid SMPS mode %d\n",
  240. sdata->smps_mode);
  241. /* fall through */
  242. case IEEE80211_SMPS_OFF:
  243. needed_static = sdata->needed_rx_chains;
  244. needed_dynamic = sdata->needed_rx_chains;
  245. break;
  246. case IEEE80211_SMPS_DYNAMIC:
  247. needed_static = 1;
  248. needed_dynamic = sdata->needed_rx_chains;
  249. break;
  250. case IEEE80211_SMPS_STATIC:
  251. needed_static = 1;
  252. needed_dynamic = 1;
  253. break;
  254. }
  255. rx_chains_static = max(rx_chains_static, needed_static);
  256. rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
  257. }
  258. rcu_read_unlock();
  259. if (!local->use_chanctx) {
  260. if (rx_chains_static > 1)
  261. local->smps_mode = IEEE80211_SMPS_OFF;
  262. else if (rx_chains_dynamic > 1)
  263. local->smps_mode = IEEE80211_SMPS_DYNAMIC;
  264. else
  265. local->smps_mode = IEEE80211_SMPS_STATIC;
  266. ieee80211_hw_config(local, 0);
  267. }
  268. if (rx_chains_static == chanctx->conf.rx_chains_static &&
  269. rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
  270. return;
  271. chanctx->conf.rx_chains_static = rx_chains_static;
  272. chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
  273. drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
  274. }
  275. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  276. const struct cfg80211_chan_def *chandef,
  277. enum ieee80211_chanctx_mode mode)
  278. {
  279. struct ieee80211_local *local = sdata->local;
  280. struct ieee80211_chanctx *ctx;
  281. int ret;
  282. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  283. mutex_lock(&local->chanctx_mtx);
  284. __ieee80211_vif_release_channel(sdata);
  285. ctx = ieee80211_find_chanctx(local, chandef, mode);
  286. if (!ctx)
  287. ctx = ieee80211_new_chanctx(local, chandef, mode);
  288. if (IS_ERR(ctx)) {
  289. ret = PTR_ERR(ctx);
  290. goto out;
  291. }
  292. sdata->vif.bss_conf.chandef = *chandef;
  293. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  294. if (ret) {
  295. /* if assign fails refcount stays the same */
  296. if (ctx->refcount == 0)
  297. ieee80211_free_chanctx(local, ctx);
  298. goto out;
  299. }
  300. ieee80211_recalc_smps_chanctx(local, ctx);
  301. ieee80211_recalc_radar_chanctx(local, ctx);
  302. out:
  303. mutex_unlock(&local->chanctx_mtx);
  304. return ret;
  305. }
  306. int ieee80211_vif_change_bandwidth(struct ieee80211_sub_if_data *sdata,
  307. const struct cfg80211_chan_def *chandef,
  308. u32 *changed)
  309. {
  310. struct ieee80211_local *local = sdata->local;
  311. struct ieee80211_chanctx_conf *conf;
  312. struct ieee80211_chanctx *ctx;
  313. int ret;
  314. if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef,
  315. IEEE80211_CHAN_DISABLED))
  316. return -EINVAL;
  317. mutex_lock(&local->chanctx_mtx);
  318. if (cfg80211_chandef_identical(chandef, &sdata->vif.bss_conf.chandef)) {
  319. ret = 0;
  320. goto out;
  321. }
  322. if (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
  323. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT) {
  324. ret = -EINVAL;
  325. goto out;
  326. }
  327. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  328. lockdep_is_held(&local->chanctx_mtx));
  329. if (!conf) {
  330. ret = -EINVAL;
  331. goto out;
  332. }
  333. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  334. if (!cfg80211_chandef_compatible(&conf->def, chandef)) {
  335. ret = -EINVAL;
  336. goto out;
  337. }
  338. sdata->vif.bss_conf.chandef = *chandef;
  339. ieee80211_recalc_chanctx_chantype(local, ctx);
  340. *changed |= BSS_CHANGED_BANDWIDTH;
  341. ret = 0;
  342. out:
  343. mutex_unlock(&local->chanctx_mtx);
  344. return ret;
  345. }
  346. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  347. {
  348. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  349. mutex_lock(&sdata->local->chanctx_mtx);
  350. __ieee80211_vif_release_channel(sdata);
  351. mutex_unlock(&sdata->local->chanctx_mtx);
  352. }
  353. void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
  354. {
  355. struct ieee80211_local *local = sdata->local;
  356. struct ieee80211_sub_if_data *ap;
  357. struct ieee80211_chanctx_conf *conf;
  358. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
  359. return;
  360. ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
  361. mutex_lock(&local->chanctx_mtx);
  362. conf = rcu_dereference_protected(ap->vif.chanctx_conf,
  363. lockdep_is_held(&local->chanctx_mtx));
  364. rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
  365. mutex_unlock(&local->chanctx_mtx);
  366. }
  367. void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
  368. bool clear)
  369. {
  370. struct ieee80211_local *local = sdata->local;
  371. struct ieee80211_sub_if_data *vlan;
  372. struct ieee80211_chanctx_conf *conf;
  373. ASSERT_RTNL();
  374. if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
  375. return;
  376. mutex_lock(&local->chanctx_mtx);
  377. /*
  378. * Check that conf exists, even when clearing this function
  379. * must be called with the AP's channel context still there
  380. * as it would otherwise cause VLANs to have an invalid
  381. * channel context pointer for a while, possibly pointing
  382. * to a channel context that has already been freed.
  383. */
  384. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  385. lockdep_is_held(&local->chanctx_mtx));
  386. WARN_ON(!conf);
  387. if (clear)
  388. conf = NULL;
  389. list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
  390. rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
  391. mutex_unlock(&local->chanctx_mtx);
  392. }
  393. void ieee80211_iter_chan_contexts_atomic(
  394. struct ieee80211_hw *hw,
  395. void (*iter)(struct ieee80211_hw *hw,
  396. struct ieee80211_chanctx_conf *chanctx_conf,
  397. void *data),
  398. void *iter_data)
  399. {
  400. struct ieee80211_local *local = hw_to_local(hw);
  401. struct ieee80211_chanctx *ctx;
  402. rcu_read_lock();
  403. list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
  404. if (ctx->driver_present)
  405. iter(hw, &ctx->conf, iter_data);
  406. rcu_read_unlock();
  407. }
  408. EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);