chan.c 13 KB

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