chan.c 13 KB

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