mac80211: fix and improve mesh RANN processing
[deliverable/linux.git] / net / mac80211 / offchannel.c
CommitLineData
b203ffc3
JM
1/*
2 * Off-channel operation helpers
3 *
4 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
5 * Copyright 2004, Instant802 Networks, Inc.
6 * Copyright 2005, Devicescape Software, Inc.
7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
9 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 */
bc3b2d7f 15#include <linux/export.h>
b203ffc3
JM
16#include <net/mac80211.h>
17#include "ieee80211_i.h"
21f83589 18#include "driver-trace.h"
2eb278e0 19#include "driver-ops.h"
b203ffc3
JM
20
21/*
b23b025f
BG
22 * Tell our hardware to disable PS.
23 * Optionally inform AP that we will go to sleep so that it will buffer
24 * the frames while we are doing off-channel work. This is optional
25 * because we *may* be doing work on-operating channel, and want our
26 * hardware unconditionally awake, but still let the AP send us normal frames.
b203ffc3 27 */
b23b025f
BG
28static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata,
29 bool tell_ap)
b203ffc3
JM
30{
31 struct ieee80211_local *local = sdata->local;
4730d597 32 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
b203ffc3
JM
33
34 local->offchannel_ps_enabled = false;
35
36 /* FIXME: what to do when local->pspolling is true? */
37
38 del_timer_sync(&local->dynamic_ps_timer);
3bc3c0d7 39 del_timer_sync(&ifmgd->bcn_mon_timer);
4730d597
LR
40 del_timer_sync(&ifmgd->conn_mon_timer);
41
b203ffc3
JM
42 cancel_work_sync(&local->dynamic_ps_enable_work);
43
44 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
45 local->offchannel_ps_enabled = true;
46 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
47 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
48 }
49
b23b025f
BG
50 if (tell_ap && (!local->offchannel_ps_enabled ||
51 !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)))
b203ffc3
JM
52 /*
53 * If power save was enabled, no need to send a nullfunc
54 * frame because AP knows that we are sleeping. But if the
55 * hardware is creating the nullfunc frame for power save
56 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
57 * enabled) and power save was enabled, the firmware just
58 * sent a null frame with power save disabled. So we need
59 * to send a new nullfunc frame to inform the AP that we
60 * are again sleeping.
61 */
62 ieee80211_send_nullfunc(local, sdata, 1);
63}
64
65/* inform AP that we are awake again, unless power save is enabled */
66static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
67{
68 struct ieee80211_local *local = sdata->local;
69
70 if (!local->ps_sdata)
71 ieee80211_send_nullfunc(local, sdata, 0);
72 else if (local->offchannel_ps_enabled) {
73 /*
74 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
75 * will send a nullfunc frame with the powersave bit set
76 * even though the AP already knows that we are sleeping.
77 * This could be avoided by sending a null frame with power
78 * save bit disabled before enabling the power save, but
79 * this doesn't gain anything.
80 *
81 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
82 * to send a nullfunc frame because AP already knows that
83 * we are sleeping, let's just enable power save mode in
84 * hardware.
85 */
b23b025f
BG
86 /* TODO: Only set hardware if CONF_PS changed?
87 * TODO: Should we set offchannel_ps_enabled to false?
88 */
b203ffc3
JM
89 local->hw.conf.flags |= IEEE80211_CONF_PS;
90 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
91 } else if (local->hw.conf.dynamic_ps_timeout > 0) {
92 /*
93 * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
94 * had been running before leaving the operating channel,
95 * restart the timer now and send a nullfunc frame to inform
96 * the AP that we are awake.
97 */
98 ieee80211_send_nullfunc(local, sdata, 0);
99 mod_timer(&local->dynamic_ps_timer, jiffies +
100 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
101 }
4730d597 102
3bc3c0d7 103 ieee80211_sta_reset_beacon_monitor(sdata);
4730d597 104 ieee80211_sta_reset_conn_monitor(sdata);
b203ffc3
JM
105}
106
b23b025f
BG
107void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local,
108 bool offchannel_ps_enable)
b203ffc3
JM
109{
110 struct ieee80211_sub_if_data *sdata;
111
b23b025f
BG
112 /*
113 * notify the AP about us leaving the channel and stop all
114 * STA interfaces.
115 */
b203ffc3
JM
116 mutex_lock(&local->iflist_mtx);
117 list_for_each_entry(sdata, &local->interfaces, list) {
118 if (!ieee80211_sdata_running(sdata))
119 continue;
120
b23b025f
BG
121 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
122 set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
123
124 /* Check to see if we should disable beaconing. */
b203ffc3
JM
125 if (sdata->vif.type == NL80211_IFTYPE_AP ||
126 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
127 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
128 ieee80211_bss_info_change_notify(
129 sdata, BSS_CHANGED_BEACON_ENABLED);
130
b23b025f 131 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
cfa6cb20 132 netif_tx_stop_all_queues(sdata->dev);
b23b025f
BG
133 if (offchannel_ps_enable &&
134 (sdata->vif.type == NL80211_IFTYPE_STATION) &&
135 sdata->u.mgd.associated)
136 ieee80211_offchannel_ps_enable(sdata, true);
5b714c6a 137 }
b203ffc3
JM
138 }
139 mutex_unlock(&local->iflist_mtx);
140}
141
b203ffc3 142void ieee80211_offchannel_return(struct ieee80211_local *local,
b23b025f 143 bool offchannel_ps_disable)
b203ffc3
JM
144{
145 struct ieee80211_sub_if_data *sdata;
146
147 mutex_lock(&local->iflist_mtx);
148 list_for_each_entry(sdata, &local->interfaces, list) {
f6e8cb72
EP
149 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
150 clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
151
b203ffc3
JM
152 if (!ieee80211_sdata_running(sdata))
153 continue;
154
155 /* Tell AP we're back */
b23b025f
BG
156 if (offchannel_ps_disable &&
157 sdata->vif.type == NL80211_IFTYPE_STATION) {
b203ffc3
JM
158 if (sdata->u.mgd.associated)
159 ieee80211_offchannel_ps_disable(sdata);
b203ffc3
JM
160 }
161
5b714c6a 162 if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
5b714c6a
JB
163 /*
164 * This may wake up queues even though the driver
165 * currently has them stopped. This is not very
166 * likely, since the driver won't have gotten any
167 * (or hardly any) new packets while we weren't
168 * on the right channel, and even if it happens
169 * it will at most lead to queueing up one more
170 * packet per queue in mac80211 rather than on
171 * the interface qdisc.
172 */
93895757 173 netif_tx_wake_all_queues(sdata->dev);
5b714c6a 174 }
93895757 175
e76aadc5
JB
176 if (sdata->vif.type == NL80211_IFTYPE_AP ||
177 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
178 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
b203ffc3
JM
179 ieee80211_bss_info_change_notify(
180 sdata, BSS_CHANGED_BEACON_ENABLED);
181 }
182 mutex_unlock(&local->iflist_mtx);
183}
21f83589 184
2eb278e0
JB
185void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc)
186{
187 if (roc->notified)
188 return;
189
190 if (roc->mgmt_tx_cookie) {
191 if (!WARN_ON(!roc->frame)) {
192 ieee80211_tx_skb(roc->sdata, roc->frame);
193 roc->frame = NULL;
194 }
195 } else {
196 cfg80211_ready_on_channel(roc->sdata->dev, (unsigned long)roc,
197 roc->chan, roc->chan_type,
198 roc->req_duration, GFP_KERNEL);
199 }
200
201 roc->notified = true;
202}
203
21f83589
JB
204static void ieee80211_hw_roc_start(struct work_struct *work)
205{
206 struct ieee80211_local *local =
207 container_of(work, struct ieee80211_local, hw_roc_start);
2eb278e0 208 struct ieee80211_roc_work *roc, *dep, *tmp;
21f83589
JB
209
210 mutex_lock(&local->mtx);
211
2eb278e0
JB
212 if (list_empty(&local->roc_list))
213 goto out_unlock;
21f83589 214
2eb278e0
JB
215 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
216 list);
217
218 if (!roc->started)
219 goto out_unlock;
220
221 roc->hw_begun = true;
222 roc->hw_start_time = local->hw_roc_start_time;
90fc4b3a 223
2eb278e0
JB
224 ieee80211_handle_roc_started(roc);
225 list_for_each_entry_safe(dep, tmp, &roc->dependents, list) {
226 ieee80211_handle_roc_started(dep);
227
228 if (dep->duration > roc->duration) {
229 u32 dur = dep->duration;
230 dep->duration = dur - roc->duration;
231 roc->duration = dur;
232 list_del(&dep->list);
233 list_add(&dep->list, &roc->list);
234 }
235 }
236 out_unlock:
21f83589
JB
237 mutex_unlock(&local->mtx);
238}
239
240void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
241{
242 struct ieee80211_local *local = hw_to_local(hw);
243
2eb278e0
JB
244 local->hw_roc_start_time = jiffies;
245
21f83589
JB
246 trace_api_ready_on_channel(local);
247
248 ieee80211_queue_work(hw, &local->hw_roc_start);
249}
250EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
251
2eb278e0 252void ieee80211_start_next_roc(struct ieee80211_local *local)
21f83589 253{
2eb278e0 254 struct ieee80211_roc_work *roc;
21f83589 255
2eb278e0 256 lockdep_assert_held(&local->mtx);
21f83589 257
2eb278e0
JB
258 if (list_empty(&local->roc_list)) {
259 ieee80211_run_deferred_scan(local);
21f83589
JB
260 return;
261 }
262
2eb278e0
JB
263 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
264 list);
71ecfa18 265
2eb278e0
JB
266 if (local->ops->remain_on_channel) {
267 int ret, duration = roc->duration;
71ecfa18 268
2eb278e0
JB
269 /* XXX: duplicated, see ieee80211_start_roc_work() */
270 if (!duration)
271 duration = 10;
71ecfa18 272
2eb278e0
JB
273 ret = drv_remain_on_channel(local, roc->chan,
274 roc->chan_type,
275 duration);
276
277 roc->started = true;
278
279 if (ret) {
280 wiphy_warn(local->hw.wiphy,
281 "failed to start next HW ROC (%d)\n", ret);
282 /*
283 * queue the work struct again to avoid recursion
284 * when multiple failures occur
285 */
286 ieee80211_remain_on_channel_expired(&local->hw);
287 }
288 } else {
289 /* delay it a bit */
290 ieee80211_queue_delayed_work(&local->hw, &roc->work,
291 round_jiffies_relative(HZ/2));
292 }
293}
294
295void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
296{
297 struct ieee80211_roc_work *dep, *tmp;
298
299 /* was never transmitted */
300 if (roc->frame) {
301 cfg80211_mgmt_tx_status(roc->sdata->dev,
302 (unsigned long)roc->frame,
303 roc->frame->data, roc->frame->len,
304 false, GFP_KERNEL);
305 kfree_skb(roc->frame);
71ecfa18
JB
306 }
307
2eb278e0
JB
308 if (!roc->mgmt_tx_cookie)
309 cfg80211_remain_on_channel_expired(roc->sdata->dev,
310 (unsigned long)roc,
311 roc->chan, roc->chan_type,
90fc4b3a 312 GFP_KERNEL);
21f83589 313
2eb278e0
JB
314 list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
315 ieee80211_roc_notify_destroy(dep);
316
317 kfree(roc);
318}
319
320void ieee80211_sw_roc_work(struct work_struct *work)
321{
322 struct ieee80211_roc_work *roc =
323 container_of(work, struct ieee80211_roc_work, work.work);
324 struct ieee80211_sub_if_data *sdata = roc->sdata;
325 struct ieee80211_local *local = sdata->local;
326
327 mutex_lock(&local->mtx);
328
329 if (roc->abort)
330 goto finish;
331
332 if (WARN_ON(list_empty(&local->roc_list)))
333 goto out_unlock;
334
335 if (WARN_ON(roc != list_first_entry(&local->roc_list,
336 struct ieee80211_roc_work,
337 list)))
338 goto out_unlock;
339
340 if (!roc->started) {
341 struct ieee80211_roc_work *dep;
342
343 /* start this ROC */
21f83589 344
2eb278e0
JB
345 /* switch channel etc */
346 ieee80211_recalc_idle(local);
347
348 local->tmp_channel = roc->chan;
349 local->tmp_channel_type = roc->chan_type;
350 ieee80211_hw_config(local, 0);
351
352 /* tell userspace or send frame */
353 ieee80211_handle_roc_started(roc);
354 list_for_each_entry(dep, &roc->dependents, list)
355 ieee80211_handle_roc_started(dep);
356
357 /* if it was pure TX, just finish right away */
358 if (!roc->duration)
359 goto finish;
360
361 roc->started = true;
362 ieee80211_queue_delayed_work(&local->hw, &roc->work,
363 msecs_to_jiffies(roc->duration));
364 } else {
365 /* finish this ROC */
366 finish:
367 list_del(&roc->list);
368 ieee80211_roc_notify_destroy(roc);
369
370 if (roc->started) {
371 drv_flush(local, false);
372
373 local->tmp_channel = NULL;
374 ieee80211_hw_config(local, 0);
375
376 ieee80211_offchannel_return(local, true);
377 }
378
379 ieee80211_recalc_idle(local);
380
381 ieee80211_start_next_roc(local);
2eb278e0
JB
382 }
383
384 out_unlock:
385 mutex_unlock(&local->mtx);
386}
387
388static void ieee80211_hw_roc_done(struct work_struct *work)
389{
390 struct ieee80211_local *local =
391 container_of(work, struct ieee80211_local, hw_roc_done);
392 struct ieee80211_roc_work *roc;
393
394 mutex_lock(&local->mtx);
395
396 if (list_empty(&local->roc_list))
397 goto out_unlock;
398
399 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
400 list);
401
402 if (!roc->started)
403 goto out_unlock;
404
405 list_del(&roc->list);
406
407 ieee80211_roc_notify_destroy(roc);
408
409 /* if there's another roc, start it now */
410 ieee80211_start_next_roc(local);
411
2eb278e0 412 out_unlock:
21f83589
JB
413 mutex_unlock(&local->mtx);
414}
415
416void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
417{
418 struct ieee80211_local *local = hw_to_local(hw);
419
420 trace_api_remain_on_channel_expired(local);
421
422 ieee80211_queue_work(hw, &local->hw_roc_done);
423}
424EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
425
2eb278e0 426void ieee80211_roc_setup(struct ieee80211_local *local)
21f83589
JB
427{
428 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
429 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
2eb278e0
JB
430 INIT_LIST_HEAD(&local->roc_list);
431}
432
433void ieee80211_roc_purge(struct ieee80211_sub_if_data *sdata)
434{
435 struct ieee80211_local *local = sdata->local;
436 struct ieee80211_roc_work *roc, *tmp;
437 LIST_HEAD(tmp_list);
438
439 mutex_lock(&local->mtx);
440 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
441 if (roc->sdata != sdata)
442 continue;
443
444 if (roc->started && local->ops->remain_on_channel) {
445 /* can race, so ignore return value */
446 drv_cancel_remain_on_channel(local);
447 }
448
449 list_move_tail(&roc->list, &tmp_list);
450 roc->abort = true;
451 }
452
453 ieee80211_start_next_roc(local);
2eb278e0
JB
454 mutex_unlock(&local->mtx);
455
456 list_for_each_entry_safe(roc, tmp, &tmp_list, list) {
457 if (local->ops->remain_on_channel) {
458 list_del(&roc->list);
459 ieee80211_roc_notify_destroy(roc);
460 } else {
461 ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
462
463 /* work will clean up etc */
464 flush_delayed_work(&roc->work);
465 }
466 }
467
468 WARN_ON_ONCE(!list_empty(&tmp_list));
21f83589 469}
This page took 0.356116 seconds and 5 git commands to generate.