chan.c 11 KB

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