1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Off-channel operation helpers
4 *
5 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
6 * Copyright 2004, Instant802 Networks, Inc.
7 * Copyright 2005, Devicescape Software, Inc.
8 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
9 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
10 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
11 * Copyright (C) 2019 Intel Corporation
12 */
13 #include <linux/export.h>
14 #include <net/mac80211.h>
15 #include "ieee80211_i.h"
16 #include "driver-ops.h"
17
18 /*
19 * Tell our hardware to disable PS.
20 * Optionally inform AP that we will go to sleep so that it will buffer
21 * the frames while we are doing off-channel work. This is optional
22 * because we *may* be doing work on-operating channel, and want our
23 * hardware unconditionally awake, but still let the AP send us normal frames.
24 */
ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data * sdata)25 static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
26 {
27 struct ieee80211_local *local = sdata->local;
28 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
29
30 local->offchannel_ps_enabled = false;
31
32 /* FIXME: what to do when local->pspolling is true? */
33
34 del_timer_sync(&local->dynamic_ps_timer);
35 del_timer_sync(&ifmgd->bcn_mon_timer);
36 del_timer_sync(&ifmgd->conn_mon_timer);
37
38 cancel_work_sync(&local->dynamic_ps_enable_work);
39
40 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
41 local->offchannel_ps_enabled = true;
42 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
43 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
44 }
45
46 if (!local->offchannel_ps_enabled ||
47 !ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK))
48 /*
49 * If power save was enabled, no need to send a nullfunc
50 * frame because AP knows that we are sleeping. But if the
51 * hardware is creating the nullfunc frame for power save
52 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
53 * enabled) and power save was enabled, the firmware just
54 * sent a null frame with power save disabled. So we need
55 * to send a new nullfunc frame to inform the AP that we
56 * are again sleeping.
57 */
58 ieee80211_send_nullfunc(local, sdata, true);
59 }
60
61 /* inform AP that we are awake again, unless power save is enabled */
ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data * sdata)62 static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
63 {
64 struct ieee80211_local *local = sdata->local;
65
66 if (!local->ps_sdata)
67 ieee80211_send_nullfunc(local, sdata, false);
68 else if (local->offchannel_ps_enabled) {
69 /*
70 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
71 * will send a nullfunc frame with the powersave bit set
72 * even though the AP already knows that we are sleeping.
73 * This could be avoided by sending a null frame with power
74 * save bit disabled before enabling the power save, but
75 * this doesn't gain anything.
76 *
77 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
78 * to send a nullfunc frame because AP already knows that
79 * we are sleeping, let's just enable power save mode in
80 * hardware.
81 */
82 /* TODO: Only set hardware if CONF_PS changed?
83 * TODO: Should we set offchannel_ps_enabled to false?
84 */
85 local->hw.conf.flags |= IEEE80211_CONF_PS;
86 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
87 } else if (local->hw.conf.dynamic_ps_timeout > 0) {
88 /*
89 * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
90 * had been running before leaving the operating channel,
91 * restart the timer now and send a nullfunc frame to inform
92 * the AP that we are awake.
93 */
94 ieee80211_send_nullfunc(local, sdata, false);
95 mod_timer(&local->dynamic_ps_timer, jiffies +
96 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
97 }
98
99 ieee80211_sta_reset_beacon_monitor(sdata);
100 ieee80211_sta_reset_conn_monitor(sdata);
101 }
102
ieee80211_offchannel_stop_vifs(struct ieee80211_local * local)103 void ieee80211_offchannel_stop_vifs(struct ieee80211_local *local)
104 {
105 struct ieee80211_sub_if_data *sdata;
106
107 if (WARN_ON(local->use_chanctx))
108 return;
109
110 /*
111 * notify the AP about us leaving the channel and stop all
112 * STA interfaces.
113 */
114
115 /*
116 * Stop queues and transmit all frames queued by the driver
117 * before sending nullfunc to enable powersave at the AP.
118 */
119 ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
120 IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
121 false);
122 ieee80211_flush_queues(local, NULL, false);
123
124 mutex_lock(&local->iflist_mtx);
125 list_for_each_entry(sdata, &local->interfaces, list) {
126 if (!ieee80211_sdata_running(sdata))
127 continue;
128
129 if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE ||
130 sdata->vif.type == NL80211_IFTYPE_NAN)
131 continue;
132
133 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
134 set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
135
136 /* Check to see if we should disable beaconing. */
137 if (sdata->vif.bss_conf.enable_beacon) {
138 set_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
139 &sdata->state);
140 sdata->vif.bss_conf.enable_beacon = false;
141 ieee80211_bss_info_change_notify(
142 sdata, BSS_CHANGED_BEACON_ENABLED);
143 }
144
145 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
146 sdata->u.mgd.associated)
147 ieee80211_offchannel_ps_enable(sdata);
148 }
149 mutex_unlock(&local->iflist_mtx);
150 }
151
ieee80211_offchannel_return(struct ieee80211_local * local)152 void ieee80211_offchannel_return(struct ieee80211_local *local)
153 {
154 struct ieee80211_sub_if_data *sdata;
155
156 if (WARN_ON(local->use_chanctx))
157 return;
158
159 mutex_lock(&local->iflist_mtx);
160 list_for_each_entry(sdata, &local->interfaces, list) {
161 if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
162 continue;
163
164 if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
165 clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
166
167 if (!ieee80211_sdata_running(sdata))
168 continue;
169
170 /* Tell AP we're back */
171 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
172 sdata->u.mgd.associated)
173 ieee80211_offchannel_ps_disable(sdata);
174
175 if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
176 &sdata->state)) {
177 sdata->vif.bss_conf.enable_beacon = true;
178 ieee80211_bss_info_change_notify(
179 sdata, BSS_CHANGED_BEACON_ENABLED);
180 }
181 }
182 mutex_unlock(&local->iflist_mtx);
183
184 ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
185 IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
186 false);
187 }
188
ieee80211_roc_notify_destroy(struct ieee80211_roc_work * roc)189 static void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
190 {
191 /* was never transmitted */
192 if (roc->frame) {
193 cfg80211_mgmt_tx_status(&roc->sdata->wdev, roc->mgmt_tx_cookie,
194 roc->frame->data, roc->frame->len,
195 false, GFP_KERNEL);
196 mac80211_free_txskb(&roc->sdata->local->hw, roc->frame);
197 }
198
199 if (!roc->mgmt_tx_cookie)
200 cfg80211_remain_on_channel_expired(&roc->sdata->wdev,
201 roc->cookie, roc->chan,
202 GFP_KERNEL);
203 else
204 cfg80211_tx_mgmt_expired(&roc->sdata->wdev,
205 roc->mgmt_tx_cookie,
206 roc->chan, GFP_KERNEL);
207
208 list_del(&roc->list);
209 kfree(roc);
210 }
211
ieee80211_end_finished_rocs(struct ieee80211_local * local,unsigned long now)212 static unsigned long ieee80211_end_finished_rocs(struct ieee80211_local *local,
213 unsigned long now)
214 {
215 struct ieee80211_roc_work *roc, *tmp;
216 long remaining_dur_min = LONG_MAX;
217
218 lockdep_assert_held(&local->mtx);
219
220 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
221 long remaining;
222
223 if (!roc->started)
224 break;
225
226 remaining = roc->start_time +
227 msecs_to_jiffies(roc->duration) -
228 now;
229
230 /* In case of HW ROC, it is possible that the HW finished the
231 * ROC session before the actual requested time. In such a case
232 * end the ROC session (disregarding the remaining time).
233 */
234 if (roc->abort || roc->hw_begun || remaining <= 0)
235 ieee80211_roc_notify_destroy(roc);
236 else
237 remaining_dur_min = min(remaining_dur_min, remaining);
238 }
239
240 return remaining_dur_min;
241 }
242
ieee80211_recalc_sw_work(struct ieee80211_local * local,unsigned long now)243 static bool ieee80211_recalc_sw_work(struct ieee80211_local *local,
244 unsigned long now)
245 {
246 long dur = ieee80211_end_finished_rocs(local, now);
247
248 if (dur == LONG_MAX)
249 return false;
250
251 mod_delayed_work(local->workqueue, &local->roc_work, dur);
252 return true;
253 }
254
ieee80211_handle_roc_started(struct ieee80211_roc_work * roc,unsigned long start_time)255 static void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc,
256 unsigned long start_time)
257 {
258 if (WARN_ON(roc->notified))
259 return;
260
261 roc->start_time = start_time;
262 roc->started = true;
263
264 if (roc->mgmt_tx_cookie) {
265 if (!WARN_ON(!roc->frame)) {
266 ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
267 roc->chan->band, 0);
268 roc->frame = NULL;
269 }
270 } else {
271 cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
272 roc->chan, roc->req_duration,
273 GFP_KERNEL);
274 }
275
276 roc->notified = true;
277 }
278
ieee80211_hw_roc_start(struct work_struct * work)279 static void ieee80211_hw_roc_start(struct work_struct *work)
280 {
281 struct ieee80211_local *local =
282 container_of(work, struct ieee80211_local, hw_roc_start);
283 struct ieee80211_roc_work *roc;
284
285 mutex_lock(&local->mtx);
286
287 list_for_each_entry(roc, &local->roc_list, list) {
288 if (!roc->started)
289 break;
290
291 roc->hw_begun = true;
292 ieee80211_handle_roc_started(roc, local->hw_roc_start_time);
293 }
294
295 mutex_unlock(&local->mtx);
296 }
297
mac80211_ready_on_channel(struct ieee80211_hw * hw)298 void mac80211_ready_on_channel(struct ieee80211_hw *hw)
299 {
300 struct ieee80211_local *local = hw_to_local(hw);
301
302 local->hw_roc_start_time = jiffies;
303
304 trace_api_ready_on_channel(local);
305
306 mac80211_queue_work(hw, &local->hw_roc_start);
307 }
308
_ieee80211_start_next_roc(struct ieee80211_local * local)309 static void _ieee80211_start_next_roc(struct ieee80211_local *local)
310 {
311 struct ieee80211_roc_work *roc, *tmp;
312 enum ieee80211_roc_type type;
313 u32 min_dur, max_dur;
314
315 lockdep_assert_held(&local->mtx);
316
317 if (WARN_ON(list_empty(&local->roc_list)))
318 return;
319
320 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
321 list);
322
323 if (WARN_ON(roc->started))
324 return;
325
326 min_dur = roc->duration;
327 max_dur = roc->duration;
328 type = roc->type;
329
330 list_for_each_entry(tmp, &local->roc_list, list) {
331 if (tmp == roc)
332 continue;
333 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
334 break;
335 max_dur = max(tmp->duration, max_dur);
336 min_dur = min(tmp->duration, min_dur);
337 type = max(tmp->type, type);
338 }
339
340 if (local->ops->remain_on_channel) {
341 int ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
342 max_dur, type);
343
344 if (ret) {
345 wiphy_warn(local->hw.wiphy,
346 "failed to start next HW ROC (%d)\n", ret);
347 /*
348 * queue the work struct again to avoid recursion
349 * when multiple failures occur
350 */
351 list_for_each_entry(tmp, &local->roc_list, list) {
352 if (tmp->sdata != roc->sdata ||
353 tmp->chan != roc->chan)
354 break;
355 tmp->started = true;
356 tmp->abort = true;
357 }
358 mac80211_queue_work(&local->hw, &local->hw_roc_done);
359 return;
360 }
361
362 /* we'll notify about the start once the HW calls back */
363 list_for_each_entry(tmp, &local->roc_list, list) {
364 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
365 break;
366 tmp->started = true;
367 }
368 } else {
369 /* If actually operating on the desired channel (with at least
370 * 20 MHz channel width) don't stop all the operations but still
371 * treat it as though the ROC operation started properly, so
372 * other ROC operations won't interfere with this one.
373 */
374 roc->on_channel = roc->chan == local->_oper_chandef.chan &&
375 local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
376 local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
377
378 /* start this ROC */
379 ieee80211_recalc_idle(local);
380
381 if (!roc->on_channel) {
382 ieee80211_offchannel_stop_vifs(local);
383
384 local->tmp_channel = roc->chan;
385 ieee80211_hw_config(local, 0);
386 }
387
388 mac80211_queue_delayed_work(&local->hw, &local->roc_work,
389 msecs_to_jiffies(min_dur));
390
391 /* tell userspace or send frame(s) */
392 list_for_each_entry(tmp, &local->roc_list, list) {
393 if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
394 break;
395
396 tmp->on_channel = roc->on_channel;
397 ieee80211_handle_roc_started(tmp, jiffies);
398 }
399 }
400 }
401
ieee80211_start_next_roc(struct ieee80211_local * local)402 void ieee80211_start_next_roc(struct ieee80211_local *local)
403 {
404 struct ieee80211_roc_work *roc;
405
406 lockdep_assert_held(&local->mtx);
407
408 if (list_empty(&local->roc_list)) {
409 ieee80211_run_deferred_scan(local);
410 return;
411 }
412
413 /* defer roc if driver is not started (i.e. during reconfig) */
414 if (local->in_reconfig)
415 return;
416
417 roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
418 list);
419
420 if (WARN_ON_ONCE(roc->started))
421 return;
422
423 if (local->ops->remain_on_channel) {
424 _ieee80211_start_next_roc(local);
425 } else {
426 /* delay it a bit */
427 mac80211_queue_delayed_work(&local->hw, &local->roc_work,
428 round_jiffies_relative(HZ/2));
429 }
430 }
431
__ieee80211_roc_work(struct ieee80211_local * local)432 static void __ieee80211_roc_work(struct ieee80211_local *local)
433 {
434 struct ieee80211_roc_work *roc;
435 bool on_channel;
436
437 lockdep_assert_held(&local->mtx);
438
439 if (WARN_ON(local->ops->remain_on_channel))
440 return;
441
442 roc = list_first_entry_or_null(&local->roc_list,
443 struct ieee80211_roc_work, list);
444 if (!roc)
445 return;
446
447 if (!roc->started) {
448 WARN_ON(local->use_chanctx);
449 _ieee80211_start_next_roc(local);
450 } else {
451 on_channel = roc->on_channel;
452 if (ieee80211_recalc_sw_work(local, jiffies))
453 return;
454
455 /* careful - roc pointer became invalid during recalc */
456
457 if (!on_channel) {
458 ieee80211_flush_queues(local, NULL, false);
459
460 local->tmp_channel = NULL;
461 ieee80211_hw_config(local, 0);
462
463 ieee80211_offchannel_return(local);
464 }
465
466 ieee80211_recalc_idle(local);
467 ieee80211_start_next_roc(local);
468 }
469 }
470
ieee80211_roc_work(struct work_struct * work)471 static void ieee80211_roc_work(struct work_struct *work)
472 {
473 struct ieee80211_local *local =
474 container_of(work, struct ieee80211_local, roc_work.work);
475
476 mutex_lock(&local->mtx);
477 __ieee80211_roc_work(local);
478 mutex_unlock(&local->mtx);
479 }
480
ieee80211_hw_roc_done(struct work_struct * work)481 static void ieee80211_hw_roc_done(struct work_struct *work)
482 {
483 struct ieee80211_local *local =
484 container_of(work, struct ieee80211_local, hw_roc_done);
485
486 mutex_lock(&local->mtx);
487
488 ieee80211_end_finished_rocs(local, jiffies);
489
490 /* if there's another roc, start it now */
491 ieee80211_start_next_roc(local);
492
493 mutex_unlock(&local->mtx);
494 }
495
mac80211_remain_on_channel_expired(struct ieee80211_hw * hw)496 void mac80211_remain_on_channel_expired(struct ieee80211_hw *hw)
497 {
498 struct ieee80211_local *local = hw_to_local(hw);
499
500 trace_api_remain_on_channel_expired(local);
501
502 mac80211_queue_work(hw, &local->hw_roc_done);
503 }
504
505 static bool
ieee80211_coalesce_hw_started_roc(struct ieee80211_local * local,struct ieee80211_roc_work * new_roc,struct ieee80211_roc_work * cur_roc)506 ieee80211_coalesce_hw_started_roc(struct ieee80211_local *local,
507 struct ieee80211_roc_work *new_roc,
508 struct ieee80211_roc_work *cur_roc)
509 {
510 unsigned long now = jiffies;
511 unsigned long remaining;
512
513 if (WARN_ON(!cur_roc->started))
514 return false;
515
516 /* if it was scheduled in the hardware, but not started yet,
517 * we can only combine if the older one had a longer duration
518 */
519 if (!cur_roc->hw_begun && new_roc->duration > cur_roc->duration)
520 return false;
521
522 remaining = cur_roc->start_time +
523 msecs_to_jiffies(cur_roc->duration) -
524 now;
525
526 /* if it doesn't fit entirely, schedule a new one */
527 if (new_roc->duration > jiffies_to_msecs(remaining))
528 return false;
529
530 /* add just after the current one so we combine their finish later */
531 list_add(&new_roc->list, &cur_roc->list);
532
533 /* if the existing one has already begun then let this one also
534 * begin, otherwise they'll both be marked properly by the work
535 * struct that runs once the driver notifies us of the beginning
536 */
537 if (cur_roc->hw_begun) {
538 new_roc->hw_begun = true;
539 ieee80211_handle_roc_started(new_roc, now);
540 }
541
542 return true;
543 }
544
ieee80211_start_roc_work(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata,struct ieee80211_channel * channel,unsigned int duration,u64 * cookie,struct sk_buff * txskb,enum ieee80211_roc_type type)545 static int ieee80211_start_roc_work(struct ieee80211_local *local,
546 struct ieee80211_sub_if_data *sdata,
547 struct ieee80211_channel *channel,
548 unsigned int duration, u64 *cookie,
549 struct sk_buff *txskb,
550 enum ieee80211_roc_type type)
551 {
552 struct ieee80211_roc_work *roc, *tmp;
553 bool queued = false, combine_started = true;
554 int ret;
555
556 lockdep_assert_held(&local->mtx);
557
558 if (local->use_chanctx && !local->ops->remain_on_channel)
559 return -EOPNOTSUPP;
560
561 roc = kzalloc(sizeof(*roc), GFP_KERNEL);
562 if (!roc)
563 return -ENOMEM;
564
565 /*
566 * If the duration is zero, then the driver
567 * wouldn't actually do anything. Set it to
568 * 10 for now.
569 *
570 * TODO: cancel the off-channel operation
571 * when we get the SKB's TX status and
572 * the wait time was zero before.
573 */
574 if (!duration)
575 duration = 10;
576
577 roc->chan = channel;
578 roc->duration = duration;
579 roc->req_duration = duration;
580 roc->frame = txskb;
581 roc->type = type;
582 roc->sdata = sdata;
583
584 /*
585 * cookie is either the roc cookie (for normal roc)
586 * or the SKB (for mgmt TX)
587 */
588 if (!txskb) {
589 roc->cookie = ieee80211_mgmt_tx_cookie(local);
590 *cookie = roc->cookie;
591 } else {
592 roc->mgmt_tx_cookie = *cookie;
593 }
594
595 /* if there's no need to queue, handle it immediately */
596 if (list_empty(&local->roc_list) &&
597 !local->scanning && !ieee80211_is_radar_required(local)) {
598 /* if not HW assist, just queue & schedule work */
599 if (!local->ops->remain_on_channel) {
600 list_add_tail(&roc->list, &local->roc_list);
601 mac80211_queue_delayed_work(&local->hw,
602 &local->roc_work, 0);
603 } else {
604 /* otherwise actually kick it off here
605 * (for error handling)
606 */
607 ret = drv_remain_on_channel(local, sdata, channel,
608 duration, type);
609 if (ret) {
610 kfree(roc);
611 return ret;
612 }
613 roc->started = true;
614 list_add_tail(&roc->list, &local->roc_list);
615 }
616
617 return 0;
618 }
619
620 /* otherwise handle queueing */
621
622 list_for_each_entry(tmp, &local->roc_list, list) {
623 if (tmp->chan != channel || tmp->sdata != sdata)
624 continue;
625
626 /*
627 * Extend this ROC if possible: If it hasn't started, add
628 * just after the new one to combine.
629 */
630 if (!tmp->started) {
631 list_add(&roc->list, &tmp->list);
632 queued = true;
633 break;
634 }
635
636 if (!combine_started)
637 continue;
638
639 if (!local->ops->remain_on_channel) {
640 /* If there's no hardware remain-on-channel, and
641 * doing so won't push us over the maximum r-o-c
642 * we allow, then we can just add the new one to
643 * the list and mark it as having started now.
644 * If it would push over the limit, don't try to
645 * combine with other started ones (that haven't
646 * been running as long) but potentially sort it
647 * with others that had the same fate.
648 */
649 unsigned long now = jiffies;
650 u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
651 struct wiphy *wiphy = local->hw.wiphy;
652 u32 max_roc = wiphy->max_remain_on_channel_duration;
653
654 if (elapsed + roc->duration > max_roc) {
655 combine_started = false;
656 continue;
657 }
658
659 list_add(&roc->list, &tmp->list);
660 queued = true;
661 roc->on_channel = tmp->on_channel;
662 ieee80211_handle_roc_started(roc, now);
663 ieee80211_recalc_sw_work(local, now);
664 break;
665 }
666
667 queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
668 if (queued)
669 break;
670 /* if it wasn't queued, perhaps it can be combined with
671 * another that also couldn't get combined previously,
672 * but no need to check for already started ones, since
673 * that can't work.
674 */
675 combine_started = false;
676 }
677
678 if (!queued)
679 list_add_tail(&roc->list, &local->roc_list);
680
681 return 0;
682 }
683
ieee80211_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,struct ieee80211_channel * chan,unsigned int duration,u64 * cookie)684 int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
685 struct ieee80211_channel *chan,
686 unsigned int duration, u64 *cookie)
687 {
688 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
689 struct ieee80211_local *local = sdata->local;
690 int ret;
691
692 mutex_lock(&local->mtx);
693 ret = ieee80211_start_roc_work(local, sdata, chan,
694 duration, cookie, NULL,
695 IEEE80211_ROC_TYPE_NORMAL);
696 mutex_unlock(&local->mtx);
697
698 return ret;
699 }
700
ieee80211_cancel_roc(struct ieee80211_local * local,u64 cookie,bool mgmt_tx)701 static int ieee80211_cancel_roc(struct ieee80211_local *local,
702 u64 cookie, bool mgmt_tx)
703 {
704 struct ieee80211_roc_work *roc, *tmp, *found = NULL;
705 int ret;
706
707 if (!cookie)
708 return -ENOENT;
709
710 flush_work(&local->hw_roc_start);
711
712 mutex_lock(&local->mtx);
713 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
714 if (!mgmt_tx && roc->cookie != cookie)
715 continue;
716 else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
717 continue;
718
719 found = roc;
720 break;
721 }
722
723 if (!found) {
724 mutex_unlock(&local->mtx);
725 return -ENOENT;
726 }
727
728 if (!found->started) {
729 ieee80211_roc_notify_destroy(found);
730 goto out_unlock;
731 }
732
733 if (local->ops->remain_on_channel) {
734 ret = drv_cancel_remain_on_channel(local, roc->sdata);
735 if (WARN_ON_ONCE(ret)) {
736 mutex_unlock(&local->mtx);
737 return ret;
738 }
739
740 /* TODO:
741 * if multiple items were combined here then we really shouldn't
742 * cancel them all - we should wait for as much time as needed
743 * for the longest remaining one, and only then cancel ...
744 */
745 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
746 if (!roc->started)
747 break;
748 if (roc == found)
749 found = NULL;
750 ieee80211_roc_notify_destroy(roc);
751 }
752
753 /* that really must not happen - it was started */
754 WARN_ON(found);
755
756 ieee80211_start_next_roc(local);
757 } else {
758 /* go through work struct to return to the operating channel */
759 found->abort = true;
760 mod_delayed_work(local->workqueue, &local->roc_work, 0);
761 }
762
763 out_unlock:
764 mutex_unlock(&local->mtx);
765
766 return 0;
767 }
768
ieee80211_cancel_remain_on_channel(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)769 int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
770 struct wireless_dev *wdev, u64 cookie)
771 {
772 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
773 struct ieee80211_local *local = sdata->local;
774
775 return ieee80211_cancel_roc(local, cookie, false);
776 }
777
ieee80211_mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)778 int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
779 struct cfg80211_mgmt_tx_params *params, u64 *cookie)
780 {
781 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
782 struct ieee80211_local *local = sdata->local;
783 struct sk_buff *skb;
784 struct sta_info *sta;
785 const struct ieee80211_mgmt *mgmt = (void *)params->buf;
786 bool need_offchan = false;
787 u32 flags;
788 int ret;
789 u8 *data;
790
791 if (params->dont_wait_for_ack)
792 flags = IEEE80211_TX_CTL_NO_ACK;
793 else
794 flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
795 IEEE80211_TX_CTL_REQ_TX_STATUS;
796
797 if (params->no_cck)
798 flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
799
800 switch (sdata->vif.type) {
801 case NL80211_IFTYPE_ADHOC:
802 if (!sdata->vif.bss_conf.ibss_joined)
803 need_offchan = true;
804 #ifdef CONFIG_MAC80211_MESH
805 /* fall through */
806 case NL80211_IFTYPE_MESH_POINT:
807 if (ieee80211_vif_is_mesh(&sdata->vif) &&
808 !sdata->u.mesh.mesh_id_len)
809 need_offchan = true;
810 #endif
811 /* fall through */
812 case NL80211_IFTYPE_AP:
813 case NL80211_IFTYPE_AP_VLAN:
814 case NL80211_IFTYPE_P2P_GO:
815 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
816 !ieee80211_vif_is_mesh(&sdata->vif) &&
817 !rcu_access_pointer(sdata->bss->beacon))
818 need_offchan = true;
819 if (!ieee80211_is_action(mgmt->frame_control) ||
820 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
821 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
822 mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
823 break;
824 rcu_read_lock();
825 sta = sta_info_get_bss(sdata, mgmt->da);
826 rcu_read_unlock();
827 if (!sta)
828 return -ENOLINK;
829 break;
830 case NL80211_IFTYPE_STATION:
831 case NL80211_IFTYPE_P2P_CLIENT:
832 sdata_lock(sdata);
833 if (!sdata->u.mgd.associated ||
834 (params->offchan && params->wait &&
835 local->ops->remain_on_channel &&
836 memcmp(sdata->u.mgd.associated->bssid,
837 mgmt->bssid, ETH_ALEN)))
838 need_offchan = true;
839 sdata_unlock(sdata);
840 break;
841 case NL80211_IFTYPE_P2P_DEVICE:
842 need_offchan = true;
843 break;
844 case NL80211_IFTYPE_NAN:
845 default:
846 return -EOPNOTSUPP;
847 }
848
849 /* configurations requiring offchan cannot work if no channel has been
850 * specified
851 */
852 if (need_offchan && !params->chan)
853 return -EINVAL;
854
855 mutex_lock(&local->mtx);
856
857 /* Check if the operating channel is the requested channel */
858 if (!need_offchan) {
859 struct ieee80211_chanctx_conf *chanctx_conf;
860
861 rcu_read_lock();
862 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
863
864 if (chanctx_conf) {
865 need_offchan = params->chan &&
866 (params->chan !=
867 chanctx_conf->def.chan);
868 } else if (!params->chan) {
869 ret = -EINVAL;
870 rcu_read_unlock();
871 goto out_unlock;
872 } else {
873 need_offchan = true;
874 }
875 rcu_read_unlock();
876 }
877
878 if (need_offchan && !params->offchan) {
879 ret = -EBUSY;
880 goto out_unlock;
881 }
882
883 skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
884 if (!skb) {
885 ret = -ENOMEM;
886 goto out_unlock;
887 }
888 skb_reserve(skb, local->hw.extra_tx_headroom);
889
890 data = skb_put_data(skb, params->buf, params->len);
891
892 /* Update CSA counters */
893 if (sdata->vif.csa_active &&
894 (sdata->vif.type == NL80211_IFTYPE_AP ||
895 sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
896 sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
897 params->n_csa_offsets) {
898 int i;
899 struct beacon_data *beacon = NULL;
900
901 rcu_read_lock();
902
903 if (sdata->vif.type == NL80211_IFTYPE_AP)
904 beacon = rcu_dereference(sdata->u.ap.beacon);
905 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
906 beacon = rcu_dereference(sdata->u.ibss.presp);
907 else if (ieee80211_vif_is_mesh(&sdata->vif))
908 beacon = rcu_dereference(sdata->u.mesh.beacon);
909
910 if (beacon)
911 for (i = 0; i < params->n_csa_offsets; i++)
912 data[params->csa_offsets[i]] =
913 beacon->csa_current_counter;
914
915 rcu_read_unlock();
916 }
917
918 IEEE80211_SKB_CB(skb)->flags = flags;
919
920 skb->dev = sdata->dev;
921
922 if (!params->dont_wait_for_ack) {
923 /* make a copy to preserve the frame contents
924 * in case of encryption.
925 */
926 ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
927 if (ret) {
928 kfree_skb(skb);
929 goto out_unlock;
930 }
931 } else {
932 /* Assign a dummy non-zero cookie, it's not sent to
933 * userspace in this case but we rely on its value
934 * internally in the need_offchan case to distinguish
935 * mgmt-tx from remain-on-channel.
936 */
937 *cookie = 0xffffffff;
938 }
939
940 if (!need_offchan) {
941 ieee80211_tx_skb(sdata, skb);
942 ret = 0;
943 goto out_unlock;
944 }
945
946 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
947 IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
948 if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
949 IEEE80211_SKB_CB(skb)->hw_queue =
950 local->hw.offchannel_tx_hw_queue;
951
952 /* This will handle all kinds of coalescing and immediate TX */
953 ret = ieee80211_start_roc_work(local, sdata, params->chan,
954 params->wait, cookie, skb,
955 IEEE80211_ROC_TYPE_MGMT_TX);
956 if (ret)
957 mac80211_free_txskb(&local->hw, skb);
958 out_unlock:
959 mutex_unlock(&local->mtx);
960 return ret;
961 }
962
ieee80211_mgmt_tx_cancel_wait(struct wiphy * wiphy,struct wireless_dev * wdev,u64 cookie)963 int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
964 struct wireless_dev *wdev, u64 cookie)
965 {
966 struct ieee80211_local *local = wiphy_priv(wiphy);
967
968 return ieee80211_cancel_roc(local, cookie, true);
969 }
970
ieee80211_roc_setup(struct ieee80211_local * local)971 void ieee80211_roc_setup(struct ieee80211_local *local)
972 {
973 INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
974 INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
975 INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work);
976 INIT_LIST_HEAD(&local->roc_list);
977 }
978
ieee80211_roc_purge(struct ieee80211_local * local,struct ieee80211_sub_if_data * sdata)979 void ieee80211_roc_purge(struct ieee80211_local *local,
980 struct ieee80211_sub_if_data *sdata)
981 {
982 struct ieee80211_roc_work *roc, *tmp;
983 bool work_to_do = false;
984
985 mutex_lock(&local->mtx);
986 list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
987 if (sdata && roc->sdata != sdata)
988 continue;
989
990 if (roc->started) {
991 if (local->ops->remain_on_channel) {
992 /* can race, so ignore return value */
993 drv_cancel_remain_on_channel(local, sdata);
994 ieee80211_roc_notify_destroy(roc);
995 } else {
996 roc->abort = true;
997 work_to_do = true;
998 }
999 } else {
1000 ieee80211_roc_notify_destroy(roc);
1001 }
1002 }
1003 if (work_to_do)
1004 __ieee80211_roc_work(local);
1005 mutex_unlock(&local->mtx);
1006 }
1007