chan.c 12 KB

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