offchannel.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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-2024 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. del_timer_sync(&local->dynamic_ps_timer);
  31. del_timer_sync(&ifmgd->bcn_mon_timer);
  32. del_timer_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, 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. int ret;
  459. lockdep_assert_wiphy(local->hw.wiphy);
  460. if (channel->freq_offset)
  461. /* this may work, but is untested */
  462. return -EOPNOTSUPP;
  463. if (!local->emulate_chanctx && !local->ops->remain_on_channel)
  464. return -EOPNOTSUPP;
  465. roc = kzalloc(sizeof(*roc), GFP_KERNEL);
  466. if (!roc)
  467. return -ENOMEM;
  468. /*
  469. * If the duration is zero, then the driver
  470. * wouldn't actually do anything. Set it to
  471. * 10 for now.
  472. *
  473. * TODO: cancel the off-channel operation
  474. * when we get the SKB's TX status and
  475. * the wait time was zero before.
  476. */
  477. if (!duration)
  478. duration = 10;
  479. roc->chan = channel;
  480. roc->duration = duration;
  481. roc->req_duration = duration;
  482. roc->frame = txskb;
  483. roc->type = type;
  484. roc->sdata = sdata;
  485. /*
  486. * cookie is either the roc cookie (for normal roc)
  487. * or the SKB (for mgmt TX)
  488. */
  489. if (!txskb) {
  490. roc->cookie = ieee80211_mgmt_tx_cookie(local);
  491. *cookie = roc->cookie;
  492. } else {
  493. roc->mgmt_tx_cookie = *cookie;
  494. }
  495. /* if there's no need to queue, handle it immediately */
  496. if (list_empty(&local->roc_list) &&
  497. !local->scanning && !ieee80211_is_radar_required(local)) {
  498. /* if not HW assist, just queue & schedule work */
  499. if (!local->ops->remain_on_channel) {
  500. list_add_tail(&roc->list, &local->roc_list);
  501. wiphy_delayed_work_queue(local->hw.wiphy,
  502. &local->roc_work, 0);
  503. } else {
  504. /* otherwise actually kick it off here
  505. * (for error handling)
  506. */
  507. ret = drv_remain_on_channel(local, sdata, channel,
  508. duration, type);
  509. if (ret) {
  510. kfree(roc);
  511. return ret;
  512. }
  513. roc->started = true;
  514. list_add_tail(&roc->list, &local->roc_list);
  515. }
  516. return 0;
  517. }
  518. /* otherwise handle queueing */
  519. list_for_each_entry(tmp, &local->roc_list, list) {
  520. if (tmp->chan != channel || tmp->sdata != sdata)
  521. continue;
  522. /*
  523. * Extend this ROC if possible: If it hasn't started, add
  524. * just after the new one to combine.
  525. */
  526. if (!tmp->started) {
  527. list_add(&roc->list, &tmp->list);
  528. queued = true;
  529. break;
  530. }
  531. if (!combine_started)
  532. continue;
  533. if (!local->ops->remain_on_channel) {
  534. /* If there's no hardware remain-on-channel, and
  535. * doing so won't push us over the maximum r-o-c
  536. * we allow, then we can just add the new one to
  537. * the list and mark it as having started now.
  538. * If it would push over the limit, don't try to
  539. * combine with other started ones (that haven't
  540. * been running as long) but potentially sort it
  541. * with others that had the same fate.
  542. */
  543. unsigned long now = jiffies;
  544. u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
  545. struct wiphy *wiphy = local->hw.wiphy;
  546. u32 max_roc = wiphy->max_remain_on_channel_duration;
  547. if (elapsed + roc->duration > max_roc) {
  548. combine_started = false;
  549. continue;
  550. }
  551. list_add(&roc->list, &tmp->list);
  552. queued = true;
  553. roc->on_channel = tmp->on_channel;
  554. ieee80211_handle_roc_started(roc, now);
  555. ieee80211_recalc_sw_work(local, now);
  556. break;
  557. }
  558. queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
  559. if (queued)
  560. break;
  561. /* if it wasn't queued, perhaps it can be combined with
  562. * another that also couldn't get combined previously,
  563. * but no need to check for already started ones, since
  564. * that can't work.
  565. */
  566. combine_started = false;
  567. }
  568. if (!queued)
  569. list_add_tail(&roc->list, &local->roc_list);
  570. return 0;
  571. }
  572. int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  573. struct ieee80211_channel *chan,
  574. unsigned int duration, u64 *cookie)
  575. {
  576. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  577. struct ieee80211_local *local = sdata->local;
  578. lockdep_assert_wiphy(local->hw.wiphy);
  579. return ieee80211_start_roc_work(local, sdata, chan,
  580. duration, cookie, NULL,
  581. IEEE80211_ROC_TYPE_NORMAL);
  582. }
  583. static int ieee80211_cancel_roc(struct ieee80211_local *local,
  584. u64 cookie, bool mgmt_tx)
  585. {
  586. struct ieee80211_roc_work *roc, *tmp, *found = NULL;
  587. int ret;
  588. lockdep_assert_wiphy(local->hw.wiphy);
  589. if (!cookie)
  590. return -ENOENT;
  591. wiphy_work_flush(local->hw.wiphy, &local->hw_roc_start);
  592. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  593. if (!mgmt_tx && roc->cookie != cookie)
  594. continue;
  595. else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
  596. continue;
  597. found = roc;
  598. break;
  599. }
  600. if (!found) {
  601. return -ENOENT;
  602. }
  603. if (!found->started) {
  604. ieee80211_roc_notify_destroy(found);
  605. goto out_unlock;
  606. }
  607. if (local->ops->remain_on_channel) {
  608. ret = drv_cancel_remain_on_channel(local, roc->sdata);
  609. if (WARN_ON_ONCE(ret)) {
  610. return ret;
  611. }
  612. /*
  613. * We could be racing against the notification from the driver:
  614. * + driver is handling the notification on CPU0
  615. * + user space is cancelling the remain on channel and
  616. * schedules the hw_roc_done worker.
  617. *
  618. * Now hw_roc_done might start to run after the next roc will
  619. * start and mac80211 will think that this second roc has
  620. * ended prematurely.
  621. * Cancel the work to make sure that all the pending workers
  622. * have completed execution.
  623. * Note that this assumes that by the time the driver returns
  624. * from drv_cancel_remain_on_channel, it has completed all
  625. * the processing of related notifications.
  626. */
  627. wiphy_work_cancel(local->hw.wiphy, &local->hw_roc_done);
  628. /* TODO:
  629. * if multiple items were combined here then we really shouldn't
  630. * cancel them all - we should wait for as much time as needed
  631. * for the longest remaining one, and only then cancel ...
  632. */
  633. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  634. if (!roc->started)
  635. break;
  636. if (roc == found)
  637. found = NULL;
  638. ieee80211_roc_notify_destroy(roc);
  639. }
  640. /* that really must not happen - it was started */
  641. WARN_ON(found);
  642. ieee80211_start_next_roc(local);
  643. } else {
  644. /* go through work struct to return to the operating channel */
  645. found->abort = true;
  646. wiphy_delayed_work_queue(local->hw.wiphy, &local->roc_work, 0);
  647. }
  648. out_unlock:
  649. return 0;
  650. }
  651. int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
  652. struct wireless_dev *wdev, u64 cookie)
  653. {
  654. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  655. struct ieee80211_local *local = sdata->local;
  656. return ieee80211_cancel_roc(local, cookie, false);
  657. }
  658. int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
  659. struct cfg80211_mgmt_tx_params *params, u64 *cookie)
  660. {
  661. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  662. struct ieee80211_local *local = sdata->local;
  663. struct sk_buff *skb;
  664. struct sta_info *sta = NULL;
  665. const struct ieee80211_mgmt *mgmt = (void *)params->buf;
  666. bool need_offchan = false;
  667. bool mlo_sta = false;
  668. int link_id = -1;
  669. u32 flags;
  670. int ret;
  671. u8 *data;
  672. lockdep_assert_wiphy(local->hw.wiphy);
  673. if (params->dont_wait_for_ack)
  674. flags = IEEE80211_TX_CTL_NO_ACK;
  675. else
  676. flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  677. IEEE80211_TX_CTL_REQ_TX_STATUS;
  678. if (params->no_cck)
  679. flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  680. switch (sdata->vif.type) {
  681. case NL80211_IFTYPE_ADHOC:
  682. if (!sdata->vif.cfg.ibss_joined)
  683. need_offchan = true;
  684. #ifdef CONFIG_MAC80211_MESH
  685. fallthrough;
  686. case NL80211_IFTYPE_MESH_POINT:
  687. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  688. !sdata->u.mesh.mesh_id_len)
  689. need_offchan = true;
  690. #endif
  691. fallthrough;
  692. case NL80211_IFTYPE_AP:
  693. case NL80211_IFTYPE_AP_VLAN:
  694. case NL80211_IFTYPE_P2P_GO:
  695. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  696. !ieee80211_vif_is_mesh(&sdata->vif) &&
  697. !sdata->bss->active)
  698. need_offchan = true;
  699. rcu_read_lock();
  700. sta = sta_info_get_bss(sdata, mgmt->da);
  701. mlo_sta = sta && sta->sta.mlo;
  702. if (!ieee80211_is_action(mgmt->frame_control) ||
  703. mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
  704. mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
  705. mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  706. rcu_read_unlock();
  707. break;
  708. }
  709. if (!sta) {
  710. rcu_read_unlock();
  711. return -ENOLINK;
  712. }
  713. if (params->link_id >= 0 &&
  714. !(sta->sta.valid_links & BIT(params->link_id))) {
  715. rcu_read_unlock();
  716. return -ENOLINK;
  717. }
  718. link_id = params->link_id;
  719. rcu_read_unlock();
  720. break;
  721. case NL80211_IFTYPE_STATION:
  722. case NL80211_IFTYPE_P2P_CLIENT:
  723. if (!sdata->u.mgd.associated ||
  724. (params->offchan && params->wait &&
  725. local->ops->remain_on_channel &&
  726. memcmp(sdata->vif.cfg.ap_addr, mgmt->bssid, ETH_ALEN))) {
  727. need_offchan = true;
  728. } else if (sdata->u.mgd.associated &&
  729. ether_addr_equal(sdata->vif.cfg.ap_addr, mgmt->da)) {
  730. sta = sta_info_get_bss(sdata, mgmt->da);
  731. mlo_sta = sta && sta->sta.mlo;
  732. }
  733. break;
  734. case NL80211_IFTYPE_P2P_DEVICE:
  735. need_offchan = true;
  736. break;
  737. case NL80211_IFTYPE_NAN:
  738. default:
  739. return -EOPNOTSUPP;
  740. }
  741. /* configurations requiring offchan cannot work if no channel has been
  742. * specified
  743. */
  744. if (need_offchan && !params->chan)
  745. return -EINVAL;
  746. /* Check if the operating channel is the requested channel */
  747. if (!params->chan && mlo_sta) {
  748. need_offchan = false;
  749. } else if (!need_offchan) {
  750. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  751. int i;
  752. rcu_read_lock();
  753. /* Check all the links first */
  754. for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
  755. struct ieee80211_bss_conf *conf;
  756. conf = rcu_dereference(sdata->vif.link_conf[i]);
  757. if (!conf)
  758. continue;
  759. chanctx_conf = rcu_dereference(conf->chanctx_conf);
  760. if (!chanctx_conf)
  761. continue;
  762. if (mlo_sta && params->chan == chanctx_conf->def.chan &&
  763. ether_addr_equal(sdata->vif.addr, mgmt->sa)) {
  764. link_id = i;
  765. break;
  766. }
  767. if (ether_addr_equal(conf->addr, mgmt->sa)) {
  768. /* If userspace requested Tx on a specific link
  769. * use the same link id if the link bss is matching
  770. * the requested chan.
  771. */
  772. if (sdata->vif.valid_links &&
  773. params->link_id >= 0 && params->link_id == i &&
  774. params->chan == chanctx_conf->def.chan)
  775. link_id = i;
  776. break;
  777. }
  778. chanctx_conf = NULL;
  779. }
  780. if (chanctx_conf) {
  781. need_offchan = params->chan &&
  782. (params->chan !=
  783. chanctx_conf->def.chan);
  784. } else {
  785. need_offchan = true;
  786. }
  787. rcu_read_unlock();
  788. }
  789. if (need_offchan && !params->offchan) {
  790. ret = -EBUSY;
  791. goto out_unlock;
  792. }
  793. skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
  794. if (!skb) {
  795. ret = -ENOMEM;
  796. goto out_unlock;
  797. }
  798. skb_reserve(skb, local->hw.extra_tx_headroom);
  799. data = skb_put_data(skb, params->buf, params->len);
  800. /* Update CSA counters */
  801. if (sdata->vif.bss_conf.csa_active &&
  802. (sdata->vif.type == NL80211_IFTYPE_AP ||
  803. sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  804. sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
  805. params->n_csa_offsets) {
  806. int i;
  807. struct beacon_data *beacon = NULL;
  808. rcu_read_lock();
  809. if (sdata->vif.type == NL80211_IFTYPE_AP)
  810. beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
  811. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  812. beacon = rcu_dereference(sdata->u.ibss.presp);
  813. else if (ieee80211_vif_is_mesh(&sdata->vif))
  814. beacon = rcu_dereference(sdata->u.mesh.beacon);
  815. if (beacon)
  816. for (i = 0; i < params->n_csa_offsets; i++)
  817. data[params->csa_offsets[i]] =
  818. beacon->cntdwn_current_counter;
  819. rcu_read_unlock();
  820. }
  821. IEEE80211_SKB_CB(skb)->flags = flags;
  822. IEEE80211_SKB_CB(skb)->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK;
  823. skb->dev = sdata->dev;
  824. if (!params->dont_wait_for_ack) {
  825. /* make a copy to preserve the frame contents
  826. * in case of encryption.
  827. */
  828. ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
  829. if (ret) {
  830. kfree_skb(skb);
  831. goto out_unlock;
  832. }
  833. } else {
  834. /* Assign a dummy non-zero cookie, it's not sent to
  835. * userspace in this case but we rely on its value
  836. * internally in the need_offchan case to distinguish
  837. * mgmt-tx from remain-on-channel.
  838. */
  839. *cookie = 0xffffffff;
  840. }
  841. if (!need_offchan) {
  842. ieee80211_tx_skb_tid(sdata, skb, 7, link_id);
  843. ret = 0;
  844. goto out_unlock;
  845. }
  846. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
  847. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  848. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  849. IEEE80211_SKB_CB(skb)->hw_queue =
  850. local->hw.offchannel_tx_hw_queue;
  851. /* This will handle all kinds of coalescing and immediate TX */
  852. ret = ieee80211_start_roc_work(local, sdata, params->chan,
  853. params->wait, cookie, skb,
  854. IEEE80211_ROC_TYPE_MGMT_TX);
  855. if (ret)
  856. ieee80211_free_txskb(&local->hw, skb);
  857. out_unlock:
  858. return ret;
  859. }
  860. int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
  861. struct wireless_dev *wdev, u64 cookie)
  862. {
  863. struct ieee80211_local *local = wiphy_priv(wiphy);
  864. return ieee80211_cancel_roc(local, cookie, true);
  865. }
  866. void ieee80211_roc_setup(struct ieee80211_local *local)
  867. {
  868. wiphy_work_init(&local->hw_roc_start, ieee80211_hw_roc_start);
  869. wiphy_work_init(&local->hw_roc_done, ieee80211_hw_roc_done);
  870. wiphy_delayed_work_init(&local->roc_work, ieee80211_roc_work);
  871. INIT_LIST_HEAD(&local->roc_list);
  872. }
  873. void ieee80211_roc_purge(struct ieee80211_local *local,
  874. struct ieee80211_sub_if_data *sdata)
  875. {
  876. struct ieee80211_roc_work *roc, *tmp;
  877. bool work_to_do = false;
  878. lockdep_assert_wiphy(local->hw.wiphy);
  879. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  880. if (sdata && roc->sdata != sdata)
  881. continue;
  882. if (roc->started) {
  883. if (local->ops->remain_on_channel) {
  884. /* can race, so ignore return value */
  885. drv_cancel_remain_on_channel(local, roc->sdata);
  886. ieee80211_roc_notify_destroy(roc);
  887. } else {
  888. roc->abort = true;
  889. work_to_do = true;
  890. }
  891. } else {
  892. ieee80211_roc_notify_destroy(roc);
  893. }
  894. }
  895. if (work_to_do)
  896. __ieee80211_roc_work(local);
  897. }