blob: 038f249966d6ec8a5f49a40d50e7ce6ded602b78 [file] [log] [blame]
Johannes Bergf444de02010-05-05 15:25:02 +02001/*
2 * mac80211 - channel management
3 */
4
Johannes Berg0aaffa92010-05-05 15:28:27 +02005#include <linux/nl80211.h>
Johannes Berg3448c002012-09-11 17:57:42 +02006#include <linux/export.h>
Johannes Berg4d76d212012-12-11 20:38:41 +01007#include <linux/rtnetlink.h>
Paul Stewart3117bbdb2012-03-13 07:46:18 -07008#include <net/cfg80211.h>
Johannes Bergf444de02010-05-05 15:25:02 +02009#include "ieee80211_i.h"
Michal Kazior35f2fce2012-06-26 14:37:20 +020010#include "driver-ops.h"
Johannes Bergf444de02010-05-05 15:25:02 +020011
Johannes Berg4bf88532012-11-09 11:39:59 +010012static void ieee80211_change_chandef(struct ieee80211_local *local,
13 struct ieee80211_chanctx *ctx,
14 const struct cfg80211_chan_def *chandef)
Michal Kazior23a85b452012-06-26 14:37:21 +020015{
Johannes Berg4bf88532012-11-09 11:39:59 +010016 if (cfg80211_chandef_identical(&ctx->conf.def, chandef))
Michal Kaziore89a96f2012-06-26 14:37:22 +020017 return;
18
Johannes Berg4bf88532012-11-09 11:39:59 +010019 WARN_ON(!cfg80211_chandef_compatible(&ctx->conf.def, chandef));
20
21 ctx->conf.def = *chandef;
22 drv_change_chanctx(local, ctx, IEEE80211_CHANCTX_CHANGE_WIDTH);
Johannes Berg55de9082012-07-26 17:24:39 +020023
24 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010025 local->_oper_channel_type = cfg80211_get_chandef_type(chandef);
Johannes Berg55de9082012-07-26 17:24:39 +020026 ieee80211_hw_config(local, 0);
27 }
Johannes Berg0aaffa92010-05-05 15:28:27 +020028}
Michal Kaziord01a1e62012-06-26 14:37:16 +020029
30static struct ieee80211_chanctx *
31ieee80211_find_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010032 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020033 enum ieee80211_chanctx_mode mode)
34{
35 struct ieee80211_chanctx *ctx;
36
37 lockdep_assert_held(&local->chanctx_mtx);
38
39 if (mode == IEEE80211_CHANCTX_EXCLUSIVE)
40 return NULL;
Michal Kaziord01a1e62012-06-26 14:37:16 +020041
42 list_for_each_entry(ctx, &local->chanctx_list, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +010043 const struct cfg80211_chan_def *compat;
Michal Kaziore89a96f2012-06-26 14:37:22 +020044
Michal Kaziord01a1e62012-06-26 14:37:16 +020045 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE)
46 continue;
Johannes Berg4bf88532012-11-09 11:39:59 +010047
48 compat = cfg80211_chandef_compatible(&ctx->conf.def, chandef);
49 if (!compat)
Michal Kaziord01a1e62012-06-26 14:37:16 +020050 continue;
51
Johannes Berg4bf88532012-11-09 11:39:59 +010052 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +020053
Michal Kaziord01a1e62012-06-26 14:37:16 +020054 return ctx;
55 }
56
57 return NULL;
58}
59
60static struct ieee80211_chanctx *
61ieee80211_new_chanctx(struct ieee80211_local *local,
Johannes Berg4bf88532012-11-09 11:39:59 +010062 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +020063 enum ieee80211_chanctx_mode mode)
64{
65 struct ieee80211_chanctx *ctx;
Michal Kazior35f2fce2012-06-26 14:37:20 +020066 int err;
Michal Kaziord01a1e62012-06-26 14:37:16 +020067
68 lockdep_assert_held(&local->chanctx_mtx);
69
70 ctx = kzalloc(sizeof(*ctx) + local->hw.chanctx_data_size, GFP_KERNEL);
71 if (!ctx)
72 return ERR_PTR(-ENOMEM);
73
Johannes Berg4bf88532012-11-09 11:39:59 +010074 ctx->conf.def = *chandef;
Johannes Berg04ecd252012-09-11 14:34:12 +020075 ctx->conf.rx_chains_static = 1;
76 ctx->conf.rx_chains_dynamic = 1;
Michal Kaziord01a1e62012-06-26 14:37:16 +020077 ctx->mode = mode;
78
Johannes Berg55de9082012-07-26 17:24:39 +020079 if (!local->use_chanctx) {
Johannes Berg4bf88532012-11-09 11:39:59 +010080 local->_oper_channel_type =
81 cfg80211_get_chandef_type(chandef);
82 local->_oper_channel = chandef->chan;
Johannes Berg55de9082012-07-26 17:24:39 +020083 ieee80211_hw_config(local, 0);
84 } else {
85 err = drv_add_chanctx(local, ctx);
86 if (err) {
87 kfree(ctx);
88 return ERR_PTR(err);
89 }
Michal Kazior35f2fce2012-06-26 14:37:20 +020090 }
91
Johannes Berg3448c002012-09-11 17:57:42 +020092 list_add_rcu(&ctx->list, &local->chanctx_list);
Michal Kaziord01a1e62012-06-26 14:37:16 +020093
Johannes Bergfd0f9792013-02-07 00:14:51 +010094 mutex_lock(&local->mtx);
95 ieee80211_recalc_idle(local);
96 mutex_unlock(&local->mtx);
97
Michal Kaziord01a1e62012-06-26 14:37:16 +020098 return ctx;
99}
100
101static void ieee80211_free_chanctx(struct ieee80211_local *local,
102 struct ieee80211_chanctx *ctx)
103{
104 lockdep_assert_held(&local->chanctx_mtx);
105
106 WARN_ON_ONCE(ctx->refcount != 0);
107
Johannes Berg55de9082012-07-26 17:24:39 +0200108 if (!local->use_chanctx) {
109 local->_oper_channel_type = NL80211_CHAN_NO_HT;
110 ieee80211_hw_config(local, 0);
111 } else {
112 drv_remove_chanctx(local, ctx);
113 }
Michal Kazior35f2fce2012-06-26 14:37:20 +0200114
Johannes Berg3448c002012-09-11 17:57:42 +0200115 list_del_rcu(&ctx->list);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200116 kfree_rcu(ctx, rcu_head);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100117
118 mutex_lock(&local->mtx);
119 ieee80211_recalc_idle(local);
120 mutex_unlock(&local->mtx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200121}
122
123static int ieee80211_assign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
124 struct ieee80211_chanctx *ctx)
125{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200126 struct ieee80211_local *local = sdata->local;
127 int ret;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200128
129 lockdep_assert_held(&local->chanctx_mtx);
130
Michal Kazior35f2fce2012-06-26 14:37:20 +0200131 ret = drv_assign_vif_chanctx(local, sdata, ctx);
132 if (ret)
133 return ret;
134
Michal Kaziord01a1e62012-06-26 14:37:16 +0200135 rcu_assign_pointer(sdata->vif.chanctx_conf, &ctx->conf);
136 ctx->refcount++;
137
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200138 ieee80211_recalc_txpower(sdata);
Johannes Bergfd0f9792013-02-07 00:14:51 +0100139 sdata->vif.bss_conf.idle = false;
140 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
Johannes Berg1ea6f9c2012-10-24 10:59:25 +0200141
Michal Kaziord01a1e62012-06-26 14:37:16 +0200142 return 0;
143}
144
Johannes Berg4bf88532012-11-09 11:39:59 +0100145static void ieee80211_recalc_chanctx_chantype(struct ieee80211_local *local,
146 struct ieee80211_chanctx *ctx)
Michal Kaziore89a96f2012-06-26 14:37:22 +0200147{
148 struct ieee80211_chanctx_conf *conf = &ctx->conf;
149 struct ieee80211_sub_if_data *sdata;
Johannes Berg4bf88532012-11-09 11:39:59 +0100150 const struct cfg80211_chan_def *compat = NULL;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200151
152 lockdep_assert_held(&local->chanctx_mtx);
153
154 rcu_read_lock();
155 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg4bf88532012-11-09 11:39:59 +0100156
Michal Kaziore89a96f2012-06-26 14:37:22 +0200157 if (!ieee80211_sdata_running(sdata))
158 continue;
159 if (rcu_access_pointer(sdata->vif.chanctx_conf) != conf)
160 continue;
161
Johannes Berg4bf88532012-11-09 11:39:59 +0100162 if (!compat)
163 compat = &sdata->vif.bss_conf.chandef;
164
165 compat = cfg80211_chandef_compatible(
166 &sdata->vif.bss_conf.chandef, compat);
167 if (!compat)
168 break;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200169 }
170 rcu_read_unlock();
171
Johannes Berg4bf88532012-11-09 11:39:59 +0100172 if (WARN_ON_ONCE(!compat))
173 return;
Michal Kaziore89a96f2012-06-26 14:37:22 +0200174
Johannes Berg4bf88532012-11-09 11:39:59 +0100175 ieee80211_change_chandef(local, ctx, compat);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200176}
177
Michal Kaziord01a1e62012-06-26 14:37:16 +0200178static void ieee80211_unassign_vif_chanctx(struct ieee80211_sub_if_data *sdata,
179 struct ieee80211_chanctx *ctx)
180{
Michal Kazior35f2fce2012-06-26 14:37:20 +0200181 struct ieee80211_local *local = sdata->local;
Michal Kaziord01a1e62012-06-26 14:37:16 +0200182
183 lockdep_assert_held(&local->chanctx_mtx);
184
185 ctx->refcount--;
186 rcu_assign_pointer(sdata->vif.chanctx_conf, NULL);
Michal Kazior35f2fce2012-06-26 14:37:20 +0200187
Johannes Bergfd0f9792013-02-07 00:14:51 +0100188 sdata->vif.bss_conf.idle = true;
189 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_IDLE);
190
Michal Kazior35f2fce2012-06-26 14:37:20 +0200191 drv_unassign_vif_chanctx(local, sdata, ctx);
Michal Kaziore89a96f2012-06-26 14:37:22 +0200192
Johannes Berg04ecd252012-09-11 14:34:12 +0200193 if (ctx->refcount > 0) {
Michal Kaziore89a96f2012-06-26 14:37:22 +0200194 ieee80211_recalc_chanctx_chantype(sdata->local, ctx);
Johannes Berg04ecd252012-09-11 14:34:12 +0200195 ieee80211_recalc_smps_chanctx(local, ctx);
196 }
Michal Kaziord01a1e62012-06-26 14:37:16 +0200197}
198
199static void __ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
200{
201 struct ieee80211_local *local = sdata->local;
202 struct ieee80211_chanctx_conf *conf;
203 struct ieee80211_chanctx *ctx;
204
205 lockdep_assert_held(&local->chanctx_mtx);
206
207 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
208 lockdep_is_held(&local->chanctx_mtx));
209 if (!conf)
210 return;
211
212 ctx = container_of(conf, struct ieee80211_chanctx, conf);
213
214 ieee80211_unassign_vif_chanctx(sdata, ctx);
215 if (ctx->refcount == 0)
216 ieee80211_free_chanctx(local, ctx);
217}
218
Johannes Berg04ecd252012-09-11 14:34:12 +0200219void ieee80211_recalc_smps_chanctx(struct ieee80211_local *local,
220 struct ieee80211_chanctx *chanctx)
221{
222 struct ieee80211_sub_if_data *sdata;
223 u8 rx_chains_static, rx_chains_dynamic;
224
225 lockdep_assert_held(&local->chanctx_mtx);
226
227 rx_chains_static = 1;
228 rx_chains_dynamic = 1;
229
230 rcu_read_lock();
231 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
232 u8 needed_static, needed_dynamic;
233
234 if (!ieee80211_sdata_running(sdata))
235 continue;
236
237 if (rcu_access_pointer(sdata->vif.chanctx_conf) !=
238 &chanctx->conf)
239 continue;
240
241 switch (sdata->vif.type) {
242 case NL80211_IFTYPE_P2P_DEVICE:
243 continue;
244 case NL80211_IFTYPE_STATION:
245 if (!sdata->u.mgd.associated)
246 continue;
247 break;
248 case NL80211_IFTYPE_AP_VLAN:
249 continue;
250 case NL80211_IFTYPE_AP:
251 case NL80211_IFTYPE_ADHOC:
252 case NL80211_IFTYPE_WDS:
253 case NL80211_IFTYPE_MESH_POINT:
254 break;
255 default:
256 WARN_ON_ONCE(1);
257 }
258
259 switch (sdata->smps_mode) {
260 default:
261 WARN_ONCE(1, "Invalid SMPS mode %d\n",
262 sdata->smps_mode);
263 /* fall through */
264 case IEEE80211_SMPS_OFF:
265 needed_static = sdata->needed_rx_chains;
266 needed_dynamic = sdata->needed_rx_chains;
267 break;
268 case IEEE80211_SMPS_DYNAMIC:
269 needed_static = 1;
270 needed_dynamic = sdata->needed_rx_chains;
271 break;
272 case IEEE80211_SMPS_STATIC:
273 needed_static = 1;
274 needed_dynamic = 1;
275 break;
276 }
277
278 rx_chains_static = max(rx_chains_static, needed_static);
279 rx_chains_dynamic = max(rx_chains_dynamic, needed_dynamic);
280 }
281 rcu_read_unlock();
282
283 if (!local->use_chanctx) {
284 if (rx_chains_static > 1)
285 local->smps_mode = IEEE80211_SMPS_OFF;
286 else if (rx_chains_dynamic > 1)
287 local->smps_mode = IEEE80211_SMPS_DYNAMIC;
288 else
289 local->smps_mode = IEEE80211_SMPS_STATIC;
290 ieee80211_hw_config(local, 0);
291 }
292
293 if (rx_chains_static == chanctx->conf.rx_chains_static &&
294 rx_chains_dynamic == chanctx->conf.rx_chains_dynamic)
295 return;
296
297 chanctx->conf.rx_chains_static = rx_chains_static;
298 chanctx->conf.rx_chains_dynamic = rx_chains_dynamic;
299 drv_change_chanctx(local, chanctx, IEEE80211_CHANCTX_CHANGE_RX_CHAINS);
300}
301
Michal Kaziord01a1e62012-06-26 14:37:16 +0200302int ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
Johannes Berg4bf88532012-11-09 11:39:59 +0100303 const struct cfg80211_chan_def *chandef,
Michal Kaziord01a1e62012-06-26 14:37:16 +0200304 enum ieee80211_chanctx_mode mode)
305{
306 struct ieee80211_local *local = sdata->local;
307 struct ieee80211_chanctx *ctx;
308 int ret;
309
Johannes Berg55de9082012-07-26 17:24:39 +0200310 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
311
Michal Kaziord01a1e62012-06-26 14:37:16 +0200312 mutex_lock(&local->chanctx_mtx);
313 __ieee80211_vif_release_channel(sdata);
314
Johannes Berg4bf88532012-11-09 11:39:59 +0100315 ctx = ieee80211_find_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200316 if (!ctx)
Johannes Berg4bf88532012-11-09 11:39:59 +0100317 ctx = ieee80211_new_chanctx(local, chandef, mode);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200318 if (IS_ERR(ctx)) {
319 ret = PTR_ERR(ctx);
320 goto out;
321 }
322
Johannes Berg4bf88532012-11-09 11:39:59 +0100323 sdata->vif.bss_conf.chandef = *chandef;
Johannes Berg55de9082012-07-26 17:24:39 +0200324
Michal Kaziord01a1e62012-06-26 14:37:16 +0200325 ret = ieee80211_assign_vif_chanctx(sdata, ctx);
326 if (ret) {
327 /* if assign fails refcount stays the same */
328 if (ctx->refcount == 0)
329 ieee80211_free_chanctx(local, ctx);
330 goto out;
331 }
332
Johannes Berg04ecd252012-09-11 14:34:12 +0200333 ieee80211_recalc_smps_chanctx(local, ctx);
Michal Kaziord01a1e62012-06-26 14:37:16 +0200334 out:
335 mutex_unlock(&local->chanctx_mtx);
336 return ret;
337}
338
339void ieee80211_vif_release_channel(struct ieee80211_sub_if_data *sdata)
340{
Johannes Berg55de9082012-07-26 17:24:39 +0200341 WARN_ON(sdata->dev && netif_carrier_ok(sdata->dev));
342
Michal Kaziord01a1e62012-06-26 14:37:16 +0200343 mutex_lock(&sdata->local->chanctx_mtx);
344 __ieee80211_vif_release_channel(sdata);
345 mutex_unlock(&sdata->local->chanctx_mtx);
346}
Johannes Berg3448c002012-09-11 17:57:42 +0200347
Johannes Berg4d76d212012-12-11 20:38:41 +0100348void ieee80211_vif_vlan_copy_chanctx(struct ieee80211_sub_if_data *sdata)
349{
350 struct ieee80211_local *local = sdata->local;
351 struct ieee80211_sub_if_data *ap;
352 struct ieee80211_chanctx_conf *conf;
353
354 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN || !sdata->bss))
355 return;
356
357 ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
358
359 mutex_lock(&local->chanctx_mtx);
360
361 conf = rcu_dereference_protected(ap->vif.chanctx_conf,
362 lockdep_is_held(&local->chanctx_mtx));
363 rcu_assign_pointer(sdata->vif.chanctx_conf, conf);
364 mutex_unlock(&local->chanctx_mtx);
365}
366
Johannes Berg1f4ac5a2013-02-08 12:07:44 +0100367void ieee80211_vif_copy_chanctx_to_vlans(struct ieee80211_sub_if_data *sdata,
368 bool clear)
369{
370 struct ieee80211_local *local = sdata->local;
371 struct ieee80211_sub_if_data *vlan;
372 struct ieee80211_chanctx_conf *conf;
373
374 ASSERT_RTNL();
375
376 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
377 return;
378
379 mutex_lock(&local->chanctx_mtx);
380
381 /*
382 * Check that conf exists, even when clearing this function
383 * must be called with the AP's channel context still there
384 * as it would otherwise cause VLANs to have an invalid
385 * channel context pointer for a while, possibly pointing
386 * to a channel context that has already been freed.
387 */
388 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
389 lockdep_is_held(&local->chanctx_mtx));
390 WARN_ON(!conf);
391
392 if (clear)
393 conf = NULL;
394
395 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
396 rcu_assign_pointer(vlan->vif.chanctx_conf, conf);
397
398 mutex_unlock(&local->chanctx_mtx);
399}
400
Johannes Berg3448c002012-09-11 17:57:42 +0200401void ieee80211_iter_chan_contexts_atomic(
402 struct ieee80211_hw *hw,
403 void (*iter)(struct ieee80211_hw *hw,
404 struct ieee80211_chanctx_conf *chanctx_conf,
405 void *data),
406 void *iter_data)
407{
408 struct ieee80211_local *local = hw_to_local(hw);
409 struct ieee80211_chanctx *ctx;
410
411 rcu_read_lock();
412 list_for_each_entry_rcu(ctx, &local->chanctx_list, list)
Johannes Berg8a61af62012-12-13 17:42:30 +0100413 if (ctx->driver_present)
414 iter(hw, &ctx->conf, iter_data);
Johannes Berg3448c002012-09-11 17:57:42 +0200415 rcu_read_unlock();
416}
417EXPORT_SYMBOL_GPL(ieee80211_iter_chan_contexts_atomic);