offchannel.c 29 KB

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