xref: /linux/net/mac80211/offchannel.c (revision 93df8a1ed6231727c5db94a80b1a6bd5ee67cec3)
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  */
15 #include <linux/export.h>
16 #include <net/mac80211.h>
17 #include "ieee80211_i.h"
18 #include "driver-ops.h"
19 
20 /*
21  * Tell our hardware to disable PS.
22  * Optionally inform AP that we will go to sleep so that it will buffer
23  * the frames while we are doing off-channel work.  This is optional
24  * because we *may* be doing work on-operating channel, and want our
25  * hardware unconditionally awake, but still let the AP send us normal frames.
26  */
27 static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
28 {
29 	struct ieee80211_local *local = sdata->local;
30 	struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
31 
32 	local->offchannel_ps_enabled = false;
33 
34 	/* FIXME: what to do when local->pspolling is true? */
35 
36 	del_timer_sync(&local->dynamic_ps_timer);
37 	del_timer_sync(&ifmgd->bcn_mon_timer);
38 	del_timer_sync(&ifmgd->conn_mon_timer);
39 
40 	cancel_work_sync(&local->dynamic_ps_enable_work);
41 
42 	if (local->hw.conf.flags & IEEE80211_CONF_PS) {
43 		local->offchannel_ps_enabled = true;
44 		local->hw.conf.flags &= ~IEEE80211_CONF_PS;
45 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
46 	}
47 
48 	if (!local->offchannel_ps_enabled ||
49 	    !ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
50 		/*
51 		 * If power save was enabled, no need to send a nullfunc
52 		 * frame because AP knows that we are sleeping. But if the
53 		 * hardware is creating the nullfunc frame for power save
54 		 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
55 		 * enabled) and power save was enabled, the firmware just
56 		 * sent a null frame with power save disabled. So we need
57 		 * to send a new nullfunc frame to inform the AP that we
58 		 * are again sleeping.
59 		 */
60 		ieee80211_send_nullfunc(local, sdata, 1);
61 }
62 
63 /* inform AP that we are awake again, unless power save is enabled */
64 static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
65 {
66 	struct ieee80211_local *local = sdata->local;
67 
68 	if (!local->ps_sdata)
69 		ieee80211_send_nullfunc(local, sdata, 0);
70 	else if (local->offchannel_ps_enabled) {
71 		/*
72 		 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
73 		 * will send a nullfunc frame with the powersave bit set
74 		 * even though the AP already knows that we are sleeping.
75 		 * This could be avoided by sending a null frame with power
76 		 * save bit disabled before enabling the power save, but
77 		 * this doesn't gain anything.
78 		 *
79 		 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
80 		 * to send a nullfunc frame because AP already knows that
81 		 * we are sleeping, let's just enable power save mode in
82 		 * hardware.
83 		 */
84 		/* TODO:  Only set hardware if CONF_PS changed?
85 		 * TODO:  Should we set offchannel_ps_enabled to false?
86 		 */
87 		local->hw.conf.flags |= IEEE80211_CONF_PS;
88 		ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
89 	} else if (local->hw.conf.dynamic_ps_timeout > 0) {
90 		/*
91 		 * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
92 		 * had been running before leaving the operating channel,
93 		 * restart the timer now and send a nullfunc frame to inform
94 		 * the AP that we are awake.
95 		 */
96 		ieee80211_send_nullfunc(local, sdata, 0);
97 		mod_timer(&local->dynamic_ps_timer, jiffies +
98 			  msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
99 	}
100 
101 	ieee80211_sta_reset_beacon_monitor(sdata);
102 	ieee80211_sta_reset_conn_monitor(sdata);
103 }
104 
105 void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local)
106 {
107 	struct ieee80211_sub_if_data *sdata;
108 
109 	if (WARN_ON(local->use_chanctx))
110 		return;
111 
112 	/*
113 	 * notify the AP about us leaving the channel and stop all
114 	 * STA interfaces.
115 	 */
116 
117 	/*
118 	 * Stop queues and transmit all frames queued by the driver
119 	 * before sending nullfunc to enable powersave at the AP.
120 	 */
121 	ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
122 					IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
123 					false);
124 	ieee80211_flush_queues(local, NULL, false);
125 
126 	mutex_lock(&local->iflist_mtx);
127 	list_for_each_entry(sdata, &local->interfaces, list) {
128 		if (!ieee80211_sdata_running(sdata))
129 			continue;
130 
131 		if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
132 			continue;
133 
134 		if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
135 			set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
136 
137 		/* Check to see if we should disable beaconing. */
138 		if (sdata->vif.bss_conf.enable_beacon) {
139 			set_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
140 				&sdata->state);
141 			sdata->vif.bss_conf.enable_beacon = false;
142 			ieee80211_bss_info_change_notify(
143 				sdata, BSS_CHANGED_BEACON_ENABLED);
144 		}
145 
146 		if (sdata->vif.type == NL80211_IFTYPE_STATION &&
147 		    sdata->u.mgd.associated)
148 			ieee80211_offchannel_ps_enable(sdata);
149 	}
150 	mutex_unlock(&local->iflist_mtx);
151 }
152 
153 void ieee80211_offchannel_return(struct ieee80211_local *local)
154 {
155 	struct ieee80211_sub_if_data *sdata;
156 
157 	if (WARN_ON(local->use_chanctx))
158 		return;
159 
160 	mutex_lock(&local->iflist_mtx);
161 	list_for_each_entry(sdata, &local->interfaces, list) {
162 		if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
163 			continue;
164 
165 		if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
166 			clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
167 
168 		if (!ieee80211_sdata_running(sdata))
169 			continue;
170 
171 		/* Tell AP we're back */
172 		if (sdata->vif.type == NL80211_IFTYPE_STATION &&
173 		    sdata->u.mgd.associated)
174 			ieee80211_offchannel_ps_disable(sdata);
175 
176 		if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
177 				       &sdata->state)) {
178 			sdata->vif.bss_conf.enable_beacon = true;
179 			ieee80211_bss_info_change_notify(
180 				sdata, BSS_CHANGED_BEACON_ENABLED);
181 		}
182 	}
183 	mutex_unlock(&local->iflist_mtx);
184 
185 	ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
186 					IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
187 					false);
188 }
189 
190 void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc)
191 {
192 	if (roc->notified)
193 		return;
194 
195 	if (roc->mgmt_tx_cookie) {
196 		if (!WARN_ON(!roc->frame)) {
197 			ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
198 						  roc->chan->band);
199 			roc->frame = NULL;
200 		}
201 	} else {
202 		cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
203 					  roc->chan, roc->req_duration,
204 					  GFP_KERNEL);
205 	}
206 
207 	roc->notified = true;
208 }
209 
210 static void ieee80211_hw_roc_start(struct work_struct *work)
211 {
212 	struct ieee80211_local *local =
213 		container_of(work, struct ieee80211_local, hw_roc_start);
214 	struct ieee80211_roc_work *roc, *dep, *tmp;
215 
216 	mutex_lock(&local->mtx);
217 
218 	if (list_empty(&local->roc_list))
219 		goto out_unlock;
220 
221 	roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
222 			       list);
223 
224 	if (!roc->started)
225 		goto out_unlock;
226 
227 	roc->hw_begun = true;
228 	roc->hw_start_time = local->hw_roc_start_time;
229 
230 	ieee80211_handle_roc_started(roc);
231 	list_for_each_entry_safe(dep, tmp, &roc->dependents, list) {
232 		ieee80211_handle_roc_started(dep);
233 
234 		if (dep->duration > roc->duration) {
235 			u32 dur = dep->duration;
236 			dep->duration = dur - roc->duration;
237 			roc->duration = dur;
238 			list_move(&dep->list, &roc->list);
239 		}
240 	}
241  out_unlock:
242 	mutex_unlock(&local->mtx);
243 }
244 
245 void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
246 {
247 	struct ieee80211_local *local = hw_to_local(hw);
248 
249 	local->hw_roc_start_time = jiffies;
250 
251 	trace_api_ready_on_channel(local);
252 
253 	ieee80211_queue_work(hw, &local->hw_roc_start);
254 }
255 EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
256 
257 void ieee80211_start_next_roc(struct ieee80211_local *local)
258 {
259 	struct ieee80211_roc_work *roc;
260 
261 	lockdep_assert_held(&local->mtx);
262 
263 	if (list_empty(&local->roc_list)) {
264 		ieee80211_run_deferred_scan(local);
265 		return;
266 	}
267 
268 	roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
269 			       list);
270 
271 	if (WARN_ON_ONCE(roc->started))
272 		return;
273 
274 	if (local->ops->remain_on_channel) {
275 		int ret, duration = roc->duration;
276 
277 		/* XXX: duplicated, see ieee80211_start_roc_work() */
278 		if (!duration)
279 			duration = 10;
280 
281 		ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
282 					    duration, roc->type);
283 
284 		roc->started = true;
285 
286 		if (ret) {
287 			wiphy_warn(local->hw.wiphy,
288 				   "failed to start next HW ROC (%d)\n", ret);
289 			/*
290 			 * queue the work struct again to avoid recursion
291 			 * when multiple failures occur
292 			 */
293 			ieee80211_remain_on_channel_expired(&local->hw);
294 		}
295 	} else {
296 		/* delay it a bit */
297 		ieee80211_queue_delayed_work(&local->hw, &roc->work,
298 					     round_jiffies_relative(HZ/2));
299 	}
300 }
301 
302 void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc, bool free)
303 {
304 	struct ieee80211_roc_work *dep, *tmp;
305 
306 	if (WARN_ON(roc->to_be_freed))
307 		return;
308 
309 	/* was never transmitted */
310 	if (roc->frame) {
311 		cfg80211_mgmt_tx_status(&roc->sdata->wdev,
312 					(unsigned long)roc->frame,
313 					roc->frame->data, roc->frame->len,
314 					false, GFP_KERNEL);
315 		kfree_skb(roc->frame);
316 	}
317 
318 	if (!roc->mgmt_tx_cookie)
319 		cfg80211_remain_on_channel_expired(&roc->sdata->wdev,
320 						   roc->cookie, roc->chan,
321 						   GFP_KERNEL);
322 
323 	list_for_each_entry_safe(dep, tmp, &roc->dependents, list)
324 		ieee80211_roc_notify_destroy(dep, true);
325 
326 	if (free)
327 		kfree(roc);
328 	else
329 		roc->to_be_freed = true;
330 }
331 
332 void ieee80211_sw_roc_work(struct work_struct *work)
333 {
334 	struct ieee80211_roc_work *roc =
335 		container_of(work, struct ieee80211_roc_work, work.work);
336 	struct ieee80211_sub_if_data *sdata = roc->sdata;
337 	struct ieee80211_local *local = sdata->local;
338 	bool started, on_channel;
339 
340 	mutex_lock(&local->mtx);
341 
342 	if (roc->to_be_freed)
343 		goto out_unlock;
344 
345 	if (roc->abort)
346 		goto finish;
347 
348 	if (WARN_ON(list_empty(&local->roc_list)))
349 		goto out_unlock;
350 
351 	if (WARN_ON(roc != list_first_entry(&local->roc_list,
352 					    struct ieee80211_roc_work,
353 					    list)))
354 		goto out_unlock;
355 
356 	if (!roc->started) {
357 		struct ieee80211_roc_work *dep;
358 
359 		WARN_ON(local->use_chanctx);
360 
361 		/* If actually operating on the desired channel (with at least
362 		 * 20 MHz channel width) don't stop all the operations but still
363 		 * treat it as though the ROC operation started properly, so
364 		 * other ROC operations won't interfere with this one.
365 		 */
366 		roc->on_channel = roc->chan == local->_oper_chandef.chan &&
367 				  local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
368 				  local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
369 
370 		/* start this ROC */
371 		ieee80211_recalc_idle(local);
372 
373 		if (!roc->on_channel) {
374 			ieee80211_offchannel_stop_vifs(local);
375 
376 			local->tmp_channel = roc->chan;
377 			ieee80211_hw_config(local, 0);
378 		}
379 
380 		/* tell userspace or send frame */
381 		ieee80211_handle_roc_started(roc);
382 		list_for_each_entry(dep, &roc->dependents, list)
383 			ieee80211_handle_roc_started(dep);
384 
385 		/* if it was pure TX, just finish right away */
386 		if (!roc->duration)
387 			goto finish;
388 
389 		roc->started = true;
390 		ieee80211_queue_delayed_work(&local->hw, &roc->work,
391 					     msecs_to_jiffies(roc->duration));
392 	} else {
393 		/* finish this ROC */
394  finish:
395 		list_del(&roc->list);
396 		started = roc->started;
397 		on_channel = roc->on_channel;
398 		ieee80211_roc_notify_destroy(roc, !roc->abort);
399 
400 		if (started && !on_channel) {
401 			ieee80211_flush_queues(local, NULL, false);
402 
403 			local->tmp_channel = NULL;
404 			ieee80211_hw_config(local, 0);
405 
406 			ieee80211_offchannel_return(local);
407 		}
408 
409 		ieee80211_recalc_idle(local);
410 
411 		if (started)
412 			ieee80211_start_next_roc(local);
413 		else if (list_empty(&local->roc_list))
414 			ieee80211_run_deferred_scan(local);
415 	}
416 
417  out_unlock:
418 	mutex_unlock(&local->mtx);
419 }
420 
421 static void ieee80211_hw_roc_done(struct work_struct *work)
422 {
423 	struct ieee80211_local *local =
424 		container_of(work, struct ieee80211_local, hw_roc_done);
425 	struct ieee80211_roc_work *roc;
426 
427 	mutex_lock(&local->mtx);
428 
429 	if (list_empty(&local->roc_list))
430 		goto out_unlock;
431 
432 	roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
433 			       list);
434 
435 	if (!roc->started)
436 		goto out_unlock;
437 
438 	list_del(&roc->list);
439 
440 	ieee80211_roc_notify_destroy(roc, true);
441 
442 	/* if there's another roc, start it now */
443 	ieee80211_start_next_roc(local);
444 
445  out_unlock:
446 	mutex_unlock(&local->mtx);
447 }
448 
449 void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
450 {
451 	struct ieee80211_local *local = hw_to_local(hw);
452 
453 	trace_api_remain_on_channel_expired(local);
454 
455 	ieee80211_queue_work(hw, &local->hw_roc_done);
456 }
457 EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
458 
459 void ieee80211_roc_setup(struct ieee80211_local *local)
460 {
461 	INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
462 	INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
463 	INIT_LIST_HEAD(&local->roc_list);
464 }
465 
466 void ieee80211_roc_purge(struct ieee80211_local *local,
467 			 struct ieee80211_sub_if_data *sdata)
468 {
469 	struct ieee80211_roc_work *roc, *tmp;
470 	LIST_HEAD(tmp_list);
471 
472 	mutex_lock(&local->mtx);
473 	list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
474 		if (sdata && roc->sdata != sdata)
475 			continue;
476 
477 		if (roc->started && local->ops->remain_on_channel) {
478 			/* can race, so ignore return value */
479 			drv_cancel_remain_on_channel(local);
480 		}
481 
482 		list_move_tail(&roc->list, &tmp_list);
483 		roc->abort = true;
484 	}
485 	mutex_unlock(&local->mtx);
486 
487 	list_for_each_entry_safe(roc, tmp, &tmp_list, list) {
488 		if (local->ops->remain_on_channel) {
489 			list_del(&roc->list);
490 			ieee80211_roc_notify_destroy(roc, true);
491 		} else {
492 			ieee80211_queue_delayed_work(&local->hw, &roc->work, 0);
493 
494 			/* work will clean up etc */
495 			flush_delayed_work(&roc->work);
496 			WARN_ON(!roc->to_be_freed);
497 			kfree(roc);
498 		}
499 	}
500 
501 	WARN_ON_ONCE(!list_empty(&tmp_list));
502 }
503