chan.c 12 KB

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