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