chan.c 9.8 KB

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