chan.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * mac80211 - channel management
  3. */
  4. #include <linux/nl80211.h>
  5. #include <net/cfg80211.h>
  6. #include "ieee80211_i.h"
  7. #include "driver-ops.h"
  8. static bool
  9. ieee80211_channel_types_are_compatible(enum nl80211_channel_type chantype1,
  10. enum nl80211_channel_type chantype2,
  11. enum nl80211_channel_type *compat)
  12. {
  13. /*
  14. * start out with chantype1 being the result,
  15. * overwriting later if needed
  16. */
  17. if (compat)
  18. *compat = chantype1;
  19. switch (chantype1) {
  20. case NL80211_CHAN_NO_HT:
  21. if (compat)
  22. *compat = chantype2;
  23. break;
  24. case NL80211_CHAN_HT20:
  25. /*
  26. * allow any change that doesn't go to no-HT
  27. * (if it already is no-HT no change is needed)
  28. */
  29. if (chantype2 == NL80211_CHAN_NO_HT)
  30. break;
  31. if (compat)
  32. *compat = chantype2;
  33. break;
  34. case NL80211_CHAN_HT40PLUS:
  35. case NL80211_CHAN_HT40MINUS:
  36. /* allow smaller bandwidth and same */
  37. if (chantype2 == NL80211_CHAN_NO_HT)
  38. break;
  39. if (chantype2 == NL80211_CHAN_HT20)
  40. break;
  41. if (chantype2 == chantype1)
  42. break;
  43. return false;
  44. }
  45. return true;
  46. }
  47. static void ieee80211_change_chantype(struct ieee80211_local *local,
  48. struct ieee80211_chanctx *ctx,
  49. enum nl80211_channel_type chantype)
  50. {
  51. if (chantype == ctx->conf.channel_type)
  52. return;
  53. ctx->conf.channel_type = chantype;
  54. drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_CHANNEL_TYPE);
  55. if (!local->use_chanctx) {
  56. local->_oper_channel_type = chantype;
  57. ieee80211_hw_config(local, 0);
  58. }
  59. }
  60. static struct ieee80211_chanctx *
  61. ieee80211_find_chanctx(struct ieee80211_local *local,
  62. struct ieee80211_channel *channel,
  63. enum nl80211_channel_type channel_type,
  64. enum ieee80211_chanctx_mode mode)
  65. {
  66. struct ieee80211_chanctx *ctx;
  67. enum nl80211_channel_type compat_type;
  68. lockdep_assert_held(&local->chanctx_mtx);
  69. if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
  70. return NULL;
  71. if (WARN_ON(!channel))
  72. return NULL;
  73. list_for_each_entry(ctx, &local->chanctx_list, list) {
  74. compat_type = ctx->conf.channel_type;
  75. if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
  76. continue;
  77. if (ctx->conf.channel != channel)
  78. continue;
  79. if (!ieee80211_channel_types_are_compatible(ctx->conf.channel_type,
  80. channel_type,
  81. &compat_type))
  82. continue;
  83. ieee80211_change_chantype(local, ctx, compat_type);
  84. return ctx;
  85. }
  86. return NULL;
  87. }
  88. static struct ieee80211_chanctx *
  89. ieee80211_new_chanctx(struct ieee80211_local *local,
  90. struct ieee80211_channel *channel,
  91. enum nl80211_channel_type channel_type,
  92. enum ieee80211_chanctx_mode mode)
  93. {
  94. struct ieee80211_chanctx *ctx;
  95. int err;
  96. lockdep_assert_held(&local->chanctx_mtx);
  97. ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
  98. if (!ctx)
  99. return ERR_PTR(-ENOMEM);
  100. ctx->conf.channel = channel;
  101. ctx->conf.channel_type = channel_type;
  102. ctx->mode = mode;
  103. if (!local->use_chanctx) {
  104. local->_oper_channel_type = channel_type;
  105. local->_oper_channel = channel;
  106. ieee80211_hw_config(local, 0);
  107. } else {
  108. err = drv_add_chanctx(local, ctx);
  109. if (err) {
  110. kfree(ctx);
  111. return ERR_PTR(err);
  112. }
  113. }
  114. list_add(&ctx->list, &local->chanctx_list);
  115. return ctx;
  116. }
  117. static void ieee80211_free_chanctx(struct ieee80211_local *local,
  118. struct ieee80211_chanctx *ctx)
  119. {
  120. lockdep_assert_held(&local->chanctx_mtx);
  121. WARN_ON_ONCE(ctx->refcount != 0);
  122. if (!local->use_chanctx) {
  123. local->_oper_channel_type = NL80211_CHAN_NO_HT;
  124. ieee80211_hw_config(local, 0);
  125. } else {
  126. drv_remove_chanctx(local, ctx);
  127. }
  128. list_del(&ctx->list);
  129. kfree_rcu(ctx, rcu_head);
  130. }
  131. static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
  132. struct ieee80211_chanctx *ctx)
  133. {
  134. struct ieee80211_local *local = sdata->local;
  135. int ret;
  136. lockdep_assert_held(&local->chanctx_mtx);
  137. ret = drv_assign_vif_chanctx(local, sdata, ctx);
  138. if (ret)
  139. return ret;
  140. rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
  141. ctx->refcount++;
  142. return 0;
  143. }
  144. static enum nl80211_channel_type
  145. ieee80211_calc_chantype(struct ieee80211_local *local,
  146. struct ieee80211_chanctx *ctx)
  147. {
  148. struct ieee80211_chanctx_conf *conf = &ctx->conf;
  149. struct ieee80211_sub_if_data *sdata;
  150. enum nl80211_channel_type result = NL80211_CHAN_NO_HT;
  151. lockdep_assert_held(&local->chanctx_mtx);
  152. rcu_read_lock();
  153. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  154. if (!ieee80211_sdata_running(sdata))
  155. continue;
  156. if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
  157. continue;
  158. WARN_ON_ONCE(!ieee80211_channel_types_are_compatible(
  159. sdata->vif.bss_conf.channel_type,
  160. result, &result));
  161. }
  162. rcu_read_unlock();
  163. return result;
  164. }
  165. static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
  166. struct ieee80211_chanctx *ctx)
  167. {
  168. enum nl80211_channel_type chantype;
  169. lockdep_assert_held(&local->chanctx_mtx);
  170. chantype = ieee80211_calc_chantype(local, ctx);
  171. ieee80211_change_chantype(local, ctx, chantype);
  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. drv_unassign_vif_chanctx(local, sdata, ctx);
  181. if (ctx->refcount > 0)
  182. ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
  183. }
  184. static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  185. {
  186. struct ieee80211_local *local = sdata->local;
  187. struct ieee80211_chanctx_conf *conf;
  188. struct ieee80211_chanctx *ctx;
  189. lockdep_assert_held(&local->chanctx_mtx);
  190. conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
  191. lockdep_is_held(&local->chanctx_mtx));
  192. if (!conf)
  193. return;
  194. ctx = container_of(conf, struct ieee80211_chanctx, conf);
  195. ieee80211_unassign_vif_chanctx(sdata, ctx);
  196. if (ctx->refcount == 0)
  197. ieee80211_free_chanctx(local, ctx);
  198. }
  199. int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
  200. struct ieee80211_channel *channel,
  201. enum nl80211_channel_type channel_type,
  202. enum ieee80211_chanctx_mode mode)
  203. {
  204. struct ieee80211_local *local = sdata->local;
  205. struct ieee80211_chanctx *ctx;
  206. int ret;
  207. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  208. mutex_lock(&local->chanctx_mtx);
  209. __ieee80211_vif_release_channel(sdata);
  210. ctx = ieee80211_find_chanctx(local, channel, channel_type, mode);
  211. if (!ctx)
  212. ctx = ieee80211_new_chanctx(local, channel, channel_type, mode);
  213. if (IS_ERR(ctx)) {
  214. ret = PTR_ERR(ctx);
  215. goto out;
  216. }
  217. sdata->vif.bss_conf.channel_type = channel_type;
  218. ret = ieee80211_assign_vif_chanctx(sdata, ctx);
  219. if (ret) {
  220. /* if assign fails refcount stays the same */
  221. if (ctx->refcount == 0)
  222. ieee80211_free_chanctx(local, ctx);
  223. goto out;
  224. }
  225. out:
  226. mutex_unlock(&local->chanctx_mtx);
  227. return ret;
  228. }
  229. void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
  230. {
  231. WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
  232. mutex_lock(&sdata->local->chanctx_mtx);
  233. __ieee80211_vif_release_channel(sdata);
  234. mutex_unlock(&sdata->local->chanctx_mtx);
  235. }