Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * mac80211 - channel management |
| 3 | */ |
| 4 | |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 5 | #include <linux/nl80211.h> |
Paul Stewart | 3117bbdb | 2012-03-13 07:46:18 -0700 | [diff] [blame] | 6 | #include <net/cfg80211.h> |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 7 | #include "ieee80211_i.h" |
| 8 | |
Johannes Berg | 368a07d | 2010-05-28 14:26:23 +0200 | [diff] [blame] | 9 | static enum ieee80211_chan_mode |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 10 | __ieee80211_get_channel_mode(struct ieee80211_local *local, |
| 11 | struct ieee80211_sub_if_data *ignore) |
| 12 | { |
| 13 | struct ieee80211_sub_if_data *sdata; |
| 14 | |
Johannes Berg | 46a5eba | 2010-09-15 13:28:15 +0200 | [diff] [blame] | 15 | lockdep_assert_held(&local->iflist_mtx); |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 16 | |
| 17 | list_for_each_entry(sdata, &local->interfaces, list) { |
| 18 | if (sdata == ignore) |
| 19 | continue; |
| 20 | |
| 21 | if (!ieee80211_sdata_running(sdata)) |
| 22 | continue; |
| 23 | |
Johannes Berg | e9980e6 | 2012-01-09 13:57:36 +0100 | [diff] [blame] | 24 | switch (sdata->vif.type) { |
| 25 | case NL80211_IFTYPE_MONITOR: |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 26 | continue; |
Johannes Berg | e9980e6 | 2012-01-09 13:57:36 +0100 | [diff] [blame] | 27 | case NL80211_IFTYPE_STATION: |
| 28 | if (!sdata->u.mgd.associated) |
| 29 | continue; |
| 30 | break; |
| 31 | case NL80211_IFTYPE_ADHOC: |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 32 | if (!sdata->u.ibss.ssid_len) |
| 33 | continue; |
| 34 | if (!sdata->u.ibss.fixed_channel) |
| 35 | return CHAN_MODE_HOPPING; |
Johannes Berg | e9980e6 | 2012-01-09 13:57:36 +0100 | [diff] [blame] | 36 | break; |
| 37 | case NL80211_IFTYPE_AP_VLAN: |
| 38 | /* will also have _AP interface */ |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 39 | continue; |
Johannes Berg | e9980e6 | 2012-01-09 13:57:36 +0100 | [diff] [blame] | 40 | case NL80211_IFTYPE_AP: |
| 41 | if (!sdata->u.ap.beacon) |
| 42 | continue; |
| 43 | break; |
Thomas Pedersen | be0f423 | 2012-05-13 22:24:08 -0700 | [diff] [blame] | 44 | case NL80211_IFTYPE_MESH_POINT: |
| 45 | if (!sdata->wdev.mesh_id_len) |
| 46 | continue; |
| 47 | break; |
Johannes Berg | e9980e6 | 2012-01-09 13:57:36 +0100 | [diff] [blame] | 48 | default: |
| 49 | break; |
| 50 | } |
Johannes Berg | f444de0 | 2010-05-05 15:25:02 +0200 | [diff] [blame] | 51 | |
| 52 | return CHAN_MODE_FIXED; |
| 53 | } |
| 54 | |
| 55 | return CHAN_MODE_UNDEFINED; |
| 56 | } |
| 57 | |
| 58 | enum ieee80211_chan_mode |
| 59 | ieee80211_get_channel_mode(struct ieee80211_local *local, |
| 60 | struct ieee80211_sub_if_data *ignore) |
| 61 | { |
| 62 | enum ieee80211_chan_mode mode; |
| 63 | |
| 64 | mutex_lock(&local->iflist_mtx); |
| 65 | mode = __ieee80211_get_channel_mode(local, ignore); |
| 66 | mutex_unlock(&local->iflist_mtx); |
| 67 | |
| 68 | return mode; |
| 69 | } |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 70 | |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 71 | static enum nl80211_channel_type |
| 72 | ieee80211_get_superchan(struct ieee80211_local *local, |
| 73 | struct ieee80211_sub_if_data *sdata) |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 74 | { |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 75 | enum nl80211_channel_type superchan = NL80211_CHAN_NO_HT; |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 76 | struct ieee80211_sub_if_data *tmp; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 77 | |
| 78 | mutex_lock(&local->iflist_mtx); |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 79 | list_for_each_entry(tmp, &local->interfaces, list) { |
| 80 | if (tmp == sdata) |
| 81 | continue; |
| 82 | |
| 83 | if (!ieee80211_sdata_running(tmp)) |
| 84 | continue; |
| 85 | |
| 86 | switch (tmp->vif.bss_conf.channel_type) { |
| 87 | case NL80211_CHAN_NO_HT: |
| 88 | case NL80211_CHAN_HT20: |
Felix Fietkau | 9db372f | 2011-03-11 21:45:51 +0100 | [diff] [blame] | 89 | if (superchan > tmp->vif.bss_conf.channel_type) |
| 90 | break; |
| 91 | |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 92 | superchan = tmp->vif.bss_conf.channel_type; |
| 93 | break; |
| 94 | case NL80211_CHAN_HT40PLUS: |
| 95 | WARN_ON(superchan == NL80211_CHAN_HT40MINUS); |
| 96 | superchan = NL80211_CHAN_HT40PLUS; |
| 97 | break; |
| 98 | case NL80211_CHAN_HT40MINUS: |
| 99 | WARN_ON(superchan == NL80211_CHAN_HT40PLUS); |
| 100 | superchan = NL80211_CHAN_HT40MINUS; |
| 101 | break; |
| 102 | } |
| 103 | } |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 104 | mutex_unlock(&local->iflist_mtx); |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 105 | |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 106 | return superchan; |
| 107 | } |
| 108 | |
| 109 | static bool |
| 110 | ieee80211_channel_types_are_compatible(enum nl80211_channel_type chantype1, |
| 111 | enum nl80211_channel_type chantype2, |
| 112 | enum nl80211_channel_type *compat) |
| 113 | { |
| 114 | /* |
| 115 | * start out with chantype1 being the result, |
| 116 | * overwriting later if needed |
| 117 | */ |
| 118 | if (compat) |
| 119 | *compat = chantype1; |
| 120 | |
| 121 | switch (chantype1) { |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 122 | case NL80211_CHAN_NO_HT: |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 123 | if (compat) |
| 124 | *compat = chantype2; |
| 125 | break; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 126 | case NL80211_CHAN_HT20: |
| 127 | /* |
| 128 | * allow any change that doesn't go to no-HT |
| 129 | * (if it already is no-HT no change is needed) |
| 130 | */ |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 131 | if (chantype2 == NL80211_CHAN_NO_HT) |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 132 | break; |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 133 | if (compat) |
| 134 | *compat = chantype2; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 135 | break; |
| 136 | case NL80211_CHAN_HT40PLUS: |
| 137 | case NL80211_CHAN_HT40MINUS: |
| 138 | /* allow smaller bandwidth and same */ |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 139 | if (chantype2 == NL80211_CHAN_NO_HT) |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 140 | break; |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 141 | if (chantype2 == NL80211_CHAN_HT20) |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 142 | break; |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 143 | if (chantype2 == chantype1) |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 144 | break; |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 145 | return false; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bool ieee80211_set_channel_type(struct ieee80211_local *local, |
| 152 | struct ieee80211_sub_if_data *sdata, |
| 153 | enum nl80211_channel_type chantype) |
| 154 | { |
| 155 | enum nl80211_channel_type superchan; |
| 156 | enum nl80211_channel_type compatchan; |
| 157 | |
| 158 | superchan = ieee80211_get_superchan(local, sdata); |
| 159 | if (!ieee80211_channel_types_are_compatible(superchan, chantype, |
| 160 | &compatchan)) |
| 161 | return false; |
| 162 | |
| 163 | local->_oper_channel_type = compatchan; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 164 | |
| 165 | if (sdata) |
| 166 | sdata->vif.bss_conf.channel_type = chantype; |
| 167 | |
Michal Kazior | 23a85b45 | 2012-06-26 14:37:21 +0200 | [diff] [blame] | 168 | return true; |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 169 | |
Johannes Berg | 0aaffa9 | 2010-05-05 15:28:27 +0200 | [diff] [blame] | 170 | } |
Michal Kazior | d01a1e6 | 2012-06-26 14:37:16 +0200 | [diff] [blame^] | 171 | |
| 172 | static struct ieee80211_chanctx * |
| 173 | ieee80211_find_chanctx(struct ieee80211_local *local, |
| 174 | struct ieee80211_channel *channel, |
| 175 | enum nl80211_channel_type channel_type, |
| 176 | enum ieee80211_chanctx_mode mode) |
| 177 | { |
| 178 | struct ieee80211_chanctx *ctx; |
| 179 | |
| 180 | lockdep_assert_held(&local->chanctx_mtx); |
| 181 | |
| 182 | if (mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 183 | return NULL; |
| 184 | if (WARN_ON(!channel)) |
| 185 | return NULL; |
| 186 | |
| 187 | list_for_each_entry(ctx, &local->chanctx_list, list) { |
| 188 | if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) |
| 189 | continue; |
| 190 | if (ctx->conf.channel != channel) |
| 191 | continue; |
| 192 | if (ctx->conf.channel_type != channel_type) |
| 193 | continue; |
| 194 | |
| 195 | return ctx; |
| 196 | } |
| 197 | |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | static struct ieee80211_chanctx * |
| 202 | ieee80211_new_chanctx(struct ieee80211_local *local, |
| 203 | struct ieee80211_channel *channel, |
| 204 | enum nl80211_channel_type channel_type, |
| 205 | enum ieee80211_chanctx_mode mode) |
| 206 | { |
| 207 | struct ieee80211_chanctx *ctx; |
| 208 | |
| 209 | lockdep_assert_held(&local->chanctx_mtx); |
| 210 | |
| 211 | ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL); |
| 212 | if (!ctx) |
| 213 | return ERR_PTR(-ENOMEM); |
| 214 | |
| 215 | ctx->conf.channel = channel; |
| 216 | ctx->conf.channel_type = channel_type; |
| 217 | ctx->mode = mode; |
| 218 | |
| 219 | list_add(&ctx->list, &local->chanctx_list); |
| 220 | |
| 221 | return ctx; |
| 222 | } |
| 223 | |
| 224 | static void ieee80211_free_chanctx(struct ieee80211_local *local, |
| 225 | struct ieee80211_chanctx *ctx) |
| 226 | { |
| 227 | lockdep_assert_held(&local->chanctx_mtx); |
| 228 | |
| 229 | WARN_ON_ONCE(ctx->refcount != 0); |
| 230 | |
| 231 | list_del(&ctx->list); |
| 232 | kfree_rcu(ctx, rcu_head); |
| 233 | } |
| 234 | |
| 235 | static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 236 | struct ieee80211_chanctx *ctx) |
| 237 | { |
| 238 | struct ieee80211_local *local __maybe_unused = sdata->local; |
| 239 | |
| 240 | lockdep_assert_held(&local->chanctx_mtx); |
| 241 | |
| 242 | rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf); |
| 243 | ctx->refcount++; |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata, |
| 249 | struct ieee80211_chanctx *ctx) |
| 250 | { |
| 251 | struct ieee80211_local *local __maybe_unused = sdata->local; |
| 252 | |
| 253 | lockdep_assert_held(&local->chanctx_mtx); |
| 254 | |
| 255 | ctx->refcount--; |
| 256 | rcu_assign_pointer(sdata->vif.chanctx_conf, NULL); |
| 257 | } |
| 258 | |
| 259 | static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 260 | { |
| 261 | struct ieee80211_local *local = sdata->local; |
| 262 | struct ieee80211_chanctx_conf *conf; |
| 263 | struct ieee80211_chanctx *ctx; |
| 264 | |
| 265 | lockdep_assert_held(&local->chanctx_mtx); |
| 266 | |
| 267 | conf = rcu_dereference_protected(sdata->vif.chanctx_conf, |
| 268 | lockdep_is_held(&local->chanctx_mtx)); |
| 269 | if (!conf) |
| 270 | return; |
| 271 | |
| 272 | ctx = container_of(conf, struct ieee80211_chanctx, conf); |
| 273 | |
| 274 | ieee80211_unassign_vif_chanctx(sdata, ctx); |
| 275 | if (ctx->refcount == 0) |
| 276 | ieee80211_free_chanctx(local, ctx); |
| 277 | } |
| 278 | |
| 279 | int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, |
| 280 | struct ieee80211_channel *channel, |
| 281 | enum nl80211_channel_type channel_type, |
| 282 | enum ieee80211_chanctx_mode mode) |
| 283 | { |
| 284 | struct ieee80211_local *local = sdata->local; |
| 285 | struct ieee80211_chanctx *ctx; |
| 286 | int ret; |
| 287 | |
| 288 | mutex_lock(&local->chanctx_mtx); |
| 289 | __ieee80211_vif_release_channel(sdata); |
| 290 | |
| 291 | ctx = ieee80211_find_chanctx(local, channel, channel_type, mode); |
| 292 | if (!ctx) |
| 293 | ctx = ieee80211_new_chanctx(local, channel, channel_type, mode); |
| 294 | if (IS_ERR(ctx)) { |
| 295 | ret = PTR_ERR(ctx); |
| 296 | goto out; |
| 297 | } |
| 298 | |
| 299 | ret = ieee80211_assign_vif_chanctx(sdata, ctx); |
| 300 | if (ret) { |
| 301 | /* if assign fails refcount stays the same */ |
| 302 | if (ctx->refcount == 0) |
| 303 | ieee80211_free_chanctx(local, ctx); |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | out: |
| 308 | mutex_unlock(&local->chanctx_mtx); |
| 309 | return ret; |
| 310 | } |
| 311 | |
| 312 | void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata) |
| 313 | { |
| 314 | mutex_lock(&sdata->local->chanctx_mtx); |
| 315 | __ieee80211_vif_release_channel(sdata); |
| 316 | mutex_unlock(&sdata->local->chanctx_mtx); |
| 317 | } |