offchannel.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Off-channel operation helpers
  4. *
  5. * Copyright 2003, Jouni Malinen <[email protected]>
  6. * Copyright 2004, Instant802 Networks, Inc.
  7. * Copyright 2005, Devicescape Software, Inc.
  8. * Copyright 2006-2007 Jiri Benc <[email protected]>
  9. * Copyright 2007, Michael Wu <[email protected]>
  10. * Copyright 2009 Johannes Berg <[email protected]>
  11. * Copyright (C) 2019, 2022 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. cancel_work_sync(&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. if (WARN_ON(local->use_chanctx))
  77. return;
  78. /*
  79. * notify the AP about us leaving the channel and stop all
  80. * STA interfaces.
  81. */
  82. /*
  83. * Stop queues and transmit all frames queued by the driver
  84. * before sending nullfunc to enable powersave at the AP.
  85. */
  86. ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  87. IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
  88. false);
  89. ieee80211_flush_queues(local, NULL, false);
  90. mutex_lock(&local->iflist_mtx);
  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. mutex_unlock(&local->iflist_mtx);
  113. }
  114. void ieee80211_offchannel_return(struct ieee80211_local *local)
  115. {
  116. struct ieee80211_sub_if_data *sdata;
  117. if (WARN_ON(local->use_chanctx))
  118. return;
  119. mutex_lock(&local->iflist_mtx);
  120. list_for_each_entry(sdata, &local->interfaces, list) {
  121. if (sdata->vif.type == NL80211_IFTYPE_P2P_DEVICE)
  122. continue;
  123. if (sdata->vif.type != NL80211_IFTYPE_MONITOR)
  124. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  125. if (!ieee80211_sdata_running(sdata))
  126. continue;
  127. /* Tell AP we're back */
  128. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  129. sdata->u.mgd.associated)
  130. ieee80211_offchannel_ps_disable(sdata);
  131. if (test_and_clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED,
  132. &sdata->state)) {
  133. sdata->vif.bss_conf.enable_beacon = true;
  134. ieee80211_link_info_change_notify(
  135. sdata, &sdata->deflink,
  136. BSS_CHANGED_BEACON_ENABLED);
  137. }
  138. }
  139. mutex_unlock(&local->iflist_mtx);
  140. ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
  141. IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL,
  142. false);
  143. }
  144. static void ieee80211_roc_notify_destroy(struct ieee80211_roc_work *roc)
  145. {
  146. /* was never transmitted */
  147. if (roc->frame) {
  148. cfg80211_mgmt_tx_status(&roc->sdata->wdev, roc->mgmt_tx_cookie,
  149. roc->frame->data, roc->frame->len,
  150. false, GFP_KERNEL);
  151. ieee80211_free_txskb(&roc->sdata->local->hw, roc->frame);
  152. }
  153. if (!roc->mgmt_tx_cookie)
  154. cfg80211_remain_on_channel_expired(&roc->sdata->wdev,
  155. roc->cookie, roc->chan,
  156. GFP_KERNEL);
  157. else
  158. cfg80211_tx_mgmt_expired(&roc->sdata->wdev,
  159. roc->mgmt_tx_cookie,
  160. roc->chan, GFP_KERNEL);
  161. list_del(&roc->list);
  162. kfree(roc);
  163. }
  164. static unsigned long ieee80211_end_finished_rocs(struct ieee80211_local *local,
  165. unsigned long now)
  166. {
  167. struct ieee80211_roc_work *roc, *tmp;
  168. long remaining_dur_min = LONG_MAX;
  169. lockdep_assert_held(&local->mtx);
  170. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  171. long remaining;
  172. if (!roc->started)
  173. break;
  174. remaining = roc->start_time +
  175. msecs_to_jiffies(roc->duration) -
  176. now;
  177. /* In case of HW ROC, it is possible that the HW finished the
  178. * ROC session before the actual requested time. In such a case
  179. * end the ROC session (disregarding the remaining time).
  180. */
  181. if (roc->abort || roc->hw_begun || remaining <= 0)
  182. ieee80211_roc_notify_destroy(roc);
  183. else
  184. remaining_dur_min = min(remaining_dur_min, remaining);
  185. }
  186. return remaining_dur_min;
  187. }
  188. static bool ieee80211_recalc_sw_work(struct ieee80211_local *local,
  189. unsigned long now)
  190. {
  191. long dur = ieee80211_end_finished_rocs(local, now);
  192. if (dur == LONG_MAX)
  193. return false;
  194. wiphy_delayed_work_queue(local->hw.wiphy, &local->roc_work, dur);
  195. return true;
  196. }
  197. static void ieee80211_handle_roc_started(struct ieee80211_roc_work *roc,
  198. unsigned long start_time)
  199. {
  200. if (WARN_ON(roc->notified))
  201. return;
  202. roc->start_time = start_time;
  203. roc->started = true;
  204. if (roc->mgmt_tx_cookie) {
  205. if (!WARN_ON(!roc->frame)) {
  206. ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
  207. roc->chan->band);
  208. roc->frame = NULL;
  209. }
  210. } else {
  211. cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
  212. roc->chan, roc->req_duration,
  213. GFP_KERNEL);
  214. }
  215. roc->notified = true;
  216. }
  217. static void ieee80211_hw_roc_start(struct wiphy *wiphy, struct wiphy_work *work)
  218. {
  219. struct ieee80211_local *local =
  220. container_of(work, struct ieee80211_local, hw_roc_start);
  221. struct ieee80211_roc_work *roc;
  222. mutex_lock(&local->mtx);
  223. list_for_each_entry(roc, &local->roc_list, list) {
  224. if (!roc->started)
  225. break;
  226. roc->hw_begun = true;
  227. ieee80211_handle_roc_started(roc, local->hw_roc_start_time);
  228. }
  229. mutex_unlock(&local->mtx);
  230. }
  231. void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
  232. {
  233. struct ieee80211_local *local = hw_to_local(hw);
  234. local->hw_roc_start_time = jiffies;
  235. trace_api_ready_on_channel(local);
  236. wiphy_work_queue(hw->wiphy, &local->hw_roc_start);
  237. }
  238. EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
  239. static void _ieee80211_start_next_roc(struct ieee80211_local *local)
  240. {
  241. struct ieee80211_roc_work *roc, *tmp;
  242. enum ieee80211_roc_type type;
  243. u32 min_dur, max_dur;
  244. lockdep_assert_held(&local->mtx);
  245. if (WARN_ON(list_empty(&local->roc_list)))
  246. return;
  247. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  248. list);
  249. if (WARN_ON(roc->started))
  250. return;
  251. min_dur = roc->duration;
  252. max_dur = roc->duration;
  253. type = roc->type;
  254. list_for_each_entry(tmp, &local->roc_list, list) {
  255. if (tmp == roc)
  256. continue;
  257. if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
  258. break;
  259. max_dur = max(tmp->duration, max_dur);
  260. min_dur = min(tmp->duration, min_dur);
  261. type = max(tmp->type, type);
  262. }
  263. if (local->ops->remain_on_channel) {
  264. int ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
  265. max_dur, type);
  266. if (ret) {
  267. wiphy_warn(local->hw.wiphy,
  268. "failed to start next HW ROC (%d)\n", ret);
  269. /*
  270. * queue the work struct again to avoid recursion
  271. * when multiple failures occur
  272. */
  273. list_for_each_entry(tmp, &local->roc_list, list) {
  274. if (tmp->sdata != roc->sdata ||
  275. tmp->chan != roc->chan)
  276. break;
  277. tmp->started = true;
  278. tmp->abort = true;
  279. }
  280. wiphy_work_queue(local->hw.wiphy, &local->hw_roc_done);
  281. return;
  282. }
  283. /* we'll notify about the start once the HW calls back */
  284. list_for_each_entry(tmp, &local->roc_list, list) {
  285. if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
  286. break;
  287. tmp->started = true;
  288. }
  289. } else {
  290. /* If actually operating on the desired channel (with at least
  291. * 20 MHz channel width) don't stop all the operations but still
  292. * treat it as though the ROC operation started properly, so
  293. * other ROC operations won't interfere with this one.
  294. */
  295. roc->on_channel = roc->chan == local->_oper_chandef.chan &&
  296. local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
  297. local->_oper_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_config(local, 0);
  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_held(&local->mtx);
  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. static void __ieee80211_roc_work(struct ieee80211_local *local)
  340. {
  341. struct ieee80211_roc_work *roc;
  342. bool on_channel;
  343. lockdep_assert_held(&local->mtx);
  344. if (WARN_ON(local->ops->remain_on_channel))
  345. return;
  346. roc = list_first_entry_or_null(&local->roc_list,
  347. struct ieee80211_roc_work, list);
  348. if (!roc)
  349. return;
  350. if (!roc->started) {
  351. WARN_ON(local->use_chanctx);
  352. _ieee80211_start_next_roc(local);
  353. } else {
  354. on_channel = roc->on_channel;
  355. if (ieee80211_recalc_sw_work(local, jiffies))
  356. return;
  357. /* careful - roc pointer became invalid during recalc */
  358. if (!on_channel) {
  359. ieee80211_flush_queues(local, NULL, false);
  360. local->tmp_channel = NULL;
  361. ieee80211_hw_config(local, 0);
  362. ieee80211_offchannel_return(local);
  363. }
  364. ieee80211_recalc_idle(local);
  365. ieee80211_start_next_roc(local);
  366. }
  367. }
  368. static void ieee80211_roc_work(struct wiphy *wiphy, struct wiphy_work *work)
  369. {
  370. struct ieee80211_local *local =
  371. container_of(work, struct ieee80211_local, roc_work.work);
  372. mutex_lock(&local->mtx);
  373. __ieee80211_roc_work(local);
  374. mutex_unlock(&local->mtx);
  375. }
  376. static void ieee80211_hw_roc_done(struct wiphy *wiphy, struct wiphy_work *work)
  377. {
  378. struct ieee80211_local *local =
  379. container_of(work, struct ieee80211_local, hw_roc_done);
  380. mutex_lock(&local->mtx);
  381. ieee80211_end_finished_rocs(local, jiffies);
  382. /* if there's another roc, start it now */
  383. ieee80211_start_next_roc(local);
  384. mutex_unlock(&local->mtx);
  385. }
  386. void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
  387. {
  388. struct ieee80211_local *local = hw_to_local(hw);
  389. trace_api_remain_on_channel_expired(local);
  390. wiphy_work_queue(hw->wiphy, &local->hw_roc_done);
  391. }
  392. EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
  393. static bool
  394. ieee80211_coalesce_hw_started_roc(struct ieee80211_local *local,
  395. struct ieee80211_roc_work *new_roc,
  396. struct ieee80211_roc_work *cur_roc)
  397. {
  398. unsigned long now = jiffies;
  399. unsigned long remaining;
  400. if (WARN_ON(!cur_roc->started))
  401. return false;
  402. /* if it was scheduled in the hardware, but not started yet,
  403. * we can only combine if the older one had a longer duration
  404. */
  405. if (!cur_roc->hw_begun && new_roc->duration > cur_roc->duration)
  406. return false;
  407. remaining = cur_roc->start_time +
  408. msecs_to_jiffies(cur_roc->duration) -
  409. now;
  410. /* if it doesn't fit entirely, schedule a new one */
  411. if (new_roc->duration > jiffies_to_msecs(remaining))
  412. return false;
  413. /* add just after the current one so we combine their finish later */
  414. list_add(&new_roc->list, &cur_roc->list);
  415. /* if the existing one has already begun then let this one also
  416. * begin, otherwise they'll both be marked properly by the work
  417. * struct that runs once the driver notifies us of the beginning
  418. */
  419. if (cur_roc->hw_begun) {
  420. new_roc->hw_begun = true;
  421. ieee80211_handle_roc_started(new_roc, now);
  422. }
  423. return true;
  424. }
  425. static int ieee80211_start_roc_work(struct ieee80211_local *local,
  426. struct ieee80211_sub_if_data *sdata,
  427. struct ieee80211_channel *channel,
  428. unsigned int duration, u64 *cookie,
  429. struct sk_buff *txskb,
  430. enum ieee80211_roc_type type)
  431. {
  432. struct ieee80211_roc_work *roc, *tmp;
  433. bool queued = false, combine_started = true;
  434. int ret;
  435. lockdep_assert_held(&local->mtx);
  436. if (channel->freq_offset)
  437. /* this may work, but is untested */
  438. return -EOPNOTSUPP;
  439. if (local->use_chanctx && !local->ops->remain_on_channel)
  440. return -EOPNOTSUPP;
  441. roc = kzalloc(sizeof(*roc), GFP_KERNEL);
  442. if (!roc)
  443. return -ENOMEM;
  444. /*
  445. * If the duration is zero, then the driver
  446. * wouldn't actually do anything. Set it to
  447. * 10 for now.
  448. *
  449. * TODO: cancel the off-channel operation
  450. * when we get the SKB's TX status and
  451. * the wait time was zero before.
  452. */
  453. if (!duration)
  454. duration = 10;
  455. roc->chan = channel;
  456. roc->duration = duration;
  457. roc->req_duration = duration;
  458. roc->frame = txskb;
  459. roc->type = type;
  460. roc->sdata = sdata;
  461. /*
  462. * cookie is either the roc cookie (for normal roc)
  463. * or the SKB (for mgmt TX)
  464. */
  465. if (!txskb) {
  466. roc->cookie = ieee80211_mgmt_tx_cookie(local);
  467. *cookie = roc->cookie;
  468. } else {
  469. roc->mgmt_tx_cookie = *cookie;
  470. }
  471. /* if there's no need to queue, handle it immediately */
  472. if (list_empty(&local->roc_list) &&
  473. !local->scanning && !ieee80211_is_radar_required(local)) {
  474. /* if not HW assist, just queue & schedule work */
  475. if (!local->ops->remain_on_channel) {
  476. list_add_tail(&roc->list, &local->roc_list);
  477. wiphy_delayed_work_queue(local->hw.wiphy,
  478. &local->roc_work, 0);
  479. } else {
  480. /* otherwise actually kick it off here
  481. * (for error handling)
  482. */
  483. ret = drv_remain_on_channel(local, sdata, channel,
  484. duration, type);
  485. if (ret) {
  486. kfree(roc);
  487. return ret;
  488. }
  489. roc->started = true;
  490. list_add_tail(&roc->list, &local->roc_list);
  491. }
  492. return 0;
  493. }
  494. /* otherwise handle queueing */
  495. list_for_each_entry(tmp, &local->roc_list, list) {
  496. if (tmp->chan != channel || tmp->sdata != sdata)
  497. continue;
  498. /*
  499. * Extend this ROC if possible: If it hasn't started, add
  500. * just after the new one to combine.
  501. */
  502. if (!tmp->started) {
  503. list_add(&roc->list, &tmp->list);
  504. queued = true;
  505. break;
  506. }
  507. if (!combine_started)
  508. continue;
  509. if (!local->ops->remain_on_channel) {
  510. /* If there's no hardware remain-on-channel, and
  511. * doing so won't push us over the maximum r-o-c
  512. * we allow, then we can just add the new one to
  513. * the list and mark it as having started now.
  514. * If it would push over the limit, don't try to
  515. * combine with other started ones (that haven't
  516. * been running as long) but potentially sort it
  517. * with others that had the same fate.
  518. */
  519. unsigned long now = jiffies;
  520. u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
  521. struct wiphy *wiphy = local->hw.wiphy;
  522. u32 max_roc = wiphy->max_remain_on_channel_duration;
  523. if (elapsed + roc->duration > max_roc) {
  524. combine_started = false;
  525. continue;
  526. }
  527. list_add(&roc->list, &tmp->list);
  528. queued = true;
  529. roc->on_channel = tmp->on_channel;
  530. ieee80211_handle_roc_started(roc, now);
  531. ieee80211_recalc_sw_work(local, now);
  532. break;
  533. }
  534. queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
  535. if (queued)
  536. break;
  537. /* if it wasn't queued, perhaps it can be combined with
  538. * another that also couldn't get combined previously,
  539. * but no need to check for already started ones, since
  540. * that can't work.
  541. */
  542. combine_started = false;
  543. }
  544. if (!queued)
  545. list_add_tail(&roc->list, &local->roc_list);
  546. return 0;
  547. }
  548. int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  549. struct ieee80211_channel *chan,
  550. unsigned int duration, u64 *cookie)
  551. {
  552. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  553. struct ieee80211_local *local = sdata->local;
  554. int ret;
  555. mutex_lock(&local->mtx);
  556. ret = ieee80211_start_roc_work(local, sdata, chan,
  557. duration, cookie, NULL,
  558. IEEE80211_ROC_TYPE_NORMAL);
  559. mutex_unlock(&local->mtx);
  560. return ret;
  561. }
  562. static int ieee80211_cancel_roc(struct ieee80211_local *local,
  563. u64 cookie, bool mgmt_tx)
  564. {
  565. struct ieee80211_roc_work *roc, *tmp, *found = NULL;
  566. int ret;
  567. if (!cookie)
  568. return -ENOENT;
  569. wiphy_work_flush(local->hw.wiphy, &local->hw_roc_start);
  570. mutex_lock(&local->mtx);
  571. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  572. if (!mgmt_tx && roc->cookie != cookie)
  573. continue;
  574. else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
  575. continue;
  576. found = roc;
  577. break;
  578. }
  579. if (!found) {
  580. mutex_unlock(&local->mtx);
  581. return -ENOENT;
  582. }
  583. if (!found->started) {
  584. ieee80211_roc_notify_destroy(found);
  585. goto out_unlock;
  586. }
  587. if (local->ops->remain_on_channel) {
  588. ret = drv_cancel_remain_on_channel(local, roc->sdata);
  589. if (WARN_ON_ONCE(ret)) {
  590. mutex_unlock(&local->mtx);
  591. return ret;
  592. }
  593. /* TODO:
  594. * if multiple items were combined here then we really shouldn't
  595. * cancel them all - we should wait for as much time as needed
  596. * for the longest remaining one, and only then cancel ...
  597. */
  598. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  599. if (!roc->started)
  600. break;
  601. if (roc == found)
  602. found = NULL;
  603. ieee80211_roc_notify_destroy(roc);
  604. }
  605. /* that really must not happen - it was started */
  606. WARN_ON(found);
  607. ieee80211_start_next_roc(local);
  608. } else {
  609. /* go through work struct to return to the operating channel */
  610. found->abort = true;
  611. wiphy_delayed_work_queue(local->hw.wiphy, &local->roc_work, 0);
  612. }
  613. out_unlock:
  614. mutex_unlock(&local->mtx);
  615. return 0;
  616. }
  617. int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
  618. struct wireless_dev *wdev, u64 cookie)
  619. {
  620. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  621. struct ieee80211_local *local = sdata->local;
  622. return ieee80211_cancel_roc(local, cookie, false);
  623. }
  624. int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
  625. struct cfg80211_mgmt_tx_params *params, u64 *cookie)
  626. {
  627. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  628. struct ieee80211_local *local = sdata->local;
  629. struct sk_buff *skb;
  630. struct sta_info *sta = NULL;
  631. const struct ieee80211_mgmt *mgmt = (void *)params->buf;
  632. bool need_offchan = false;
  633. bool mlo_sta = false;
  634. int link_id = -1;
  635. u32 flags;
  636. int ret;
  637. u8 *data;
  638. if (params->dont_wait_for_ack)
  639. flags = IEEE80211_TX_CTL_NO_ACK;
  640. else
  641. flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  642. IEEE80211_TX_CTL_REQ_TX_STATUS;
  643. if (params->no_cck)
  644. flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  645. switch (sdata->vif.type) {
  646. case NL80211_IFTYPE_ADHOC:
  647. if (!sdata->vif.cfg.ibss_joined)
  648. need_offchan = true;
  649. #ifdef CONFIG_MAC80211_MESH
  650. fallthrough;
  651. case NL80211_IFTYPE_MESH_POINT:
  652. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  653. !sdata->u.mesh.mesh_id_len)
  654. need_offchan = true;
  655. #endif
  656. fallthrough;
  657. case NL80211_IFTYPE_AP:
  658. case NL80211_IFTYPE_AP_VLAN:
  659. case NL80211_IFTYPE_P2P_GO:
  660. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  661. !ieee80211_vif_is_mesh(&sdata->vif) &&
  662. !sdata->bss->active)
  663. need_offchan = true;
  664. rcu_read_lock();
  665. sta = sta_info_get_bss(sdata, mgmt->da);
  666. mlo_sta = sta && sta->sta.mlo;
  667. if (!ieee80211_is_action(mgmt->frame_control) ||
  668. mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
  669. mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
  670. mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) {
  671. rcu_read_unlock();
  672. break;
  673. }
  674. if (!sta) {
  675. rcu_read_unlock();
  676. return -ENOLINK;
  677. }
  678. if (params->link_id >= 0 &&
  679. !(sta->sta.valid_links & BIT(params->link_id))) {
  680. rcu_read_unlock();
  681. return -ENOLINK;
  682. }
  683. link_id = params->link_id;
  684. rcu_read_unlock();
  685. break;
  686. case NL80211_IFTYPE_STATION:
  687. case NL80211_IFTYPE_P2P_CLIENT:
  688. sdata_lock(sdata);
  689. if (!sdata->u.mgd.associated ||
  690. (params->offchan && params->wait &&
  691. local->ops->remain_on_channel &&
  692. memcmp(sdata->vif.cfg.ap_addr, mgmt->bssid, ETH_ALEN)))
  693. need_offchan = true;
  694. sdata_unlock(sdata);
  695. break;
  696. case NL80211_IFTYPE_P2P_DEVICE:
  697. need_offchan = true;
  698. break;
  699. case NL80211_IFTYPE_NAN:
  700. default:
  701. return -EOPNOTSUPP;
  702. }
  703. /* configurations requiring offchan cannot work if no channel has been
  704. * specified
  705. */
  706. if (need_offchan && !params->chan)
  707. return -EINVAL;
  708. mutex_lock(&local->mtx);
  709. /* Check if the operating channel is the requested channel */
  710. if (!params->chan && mlo_sta) {
  711. need_offchan = false;
  712. } else if (!need_offchan) {
  713. struct ieee80211_chanctx_conf *chanctx_conf = NULL;
  714. int i;
  715. rcu_read_lock();
  716. /* Check all the links first */
  717. for (i = 0; i < ARRAY_SIZE(sdata->vif.link_conf); i++) {
  718. struct ieee80211_bss_conf *conf;
  719. conf = rcu_dereference(sdata->vif.link_conf[i]);
  720. if (!conf)
  721. continue;
  722. chanctx_conf = rcu_dereference(conf->chanctx_conf);
  723. if (!chanctx_conf)
  724. continue;
  725. if (mlo_sta && params->chan == chanctx_conf->def.chan &&
  726. ether_addr_equal(sdata->vif.addr, mgmt->sa)) {
  727. link_id = i;
  728. break;
  729. }
  730. if (ether_addr_equal(conf->addr, mgmt->sa))
  731. break;
  732. chanctx_conf = NULL;
  733. }
  734. if (chanctx_conf) {
  735. need_offchan = params->chan &&
  736. (params->chan !=
  737. chanctx_conf->def.chan);
  738. } else {
  739. need_offchan = true;
  740. }
  741. rcu_read_unlock();
  742. }
  743. if (need_offchan && !params->offchan) {
  744. ret = -EBUSY;
  745. goto out_unlock;
  746. }
  747. skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
  748. if (!skb) {
  749. ret = -ENOMEM;
  750. goto out_unlock;
  751. }
  752. skb_reserve(skb, local->hw.extra_tx_headroom);
  753. data = skb_put_data(skb, params->buf, params->len);
  754. /* Update CSA counters */
  755. if (sdata->vif.bss_conf.csa_active &&
  756. (sdata->vif.type == NL80211_IFTYPE_AP ||
  757. sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  758. sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
  759. params->n_csa_offsets) {
  760. int i;
  761. struct beacon_data *beacon = NULL;
  762. rcu_read_lock();
  763. if (sdata->vif.type == NL80211_IFTYPE_AP)
  764. beacon = rcu_dereference(sdata->deflink.u.ap.beacon);
  765. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  766. beacon = rcu_dereference(sdata->u.ibss.presp);
  767. else if (ieee80211_vif_is_mesh(&sdata->vif))
  768. beacon = rcu_dereference(sdata->u.mesh.beacon);
  769. if (beacon)
  770. for (i = 0; i < params->n_csa_offsets; i++)
  771. data[params->csa_offsets[i]] =
  772. beacon->cntdwn_current_counter;
  773. rcu_read_unlock();
  774. }
  775. IEEE80211_SKB_CB(skb)->flags = flags;
  776. skb->dev = sdata->dev;
  777. if (!params->dont_wait_for_ack) {
  778. /* make a copy to preserve the frame contents
  779. * in case of encryption.
  780. */
  781. ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
  782. if (ret) {
  783. kfree_skb(skb);
  784. goto out_unlock;
  785. }
  786. } else {
  787. /* Assign a dummy non-zero cookie, it's not sent to
  788. * userspace in this case but we rely on its value
  789. * internally in the need_offchan case to distinguish
  790. * mgmt-tx from remain-on-channel.
  791. */
  792. *cookie = 0xffffffff;
  793. }
  794. if (!need_offchan) {
  795. ieee80211_tx_skb_tid(sdata, skb, 7, link_id);
  796. ret = 0;
  797. goto out_unlock;
  798. }
  799. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
  800. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  801. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  802. IEEE80211_SKB_CB(skb)->hw_queue =
  803. local->hw.offchannel_tx_hw_queue;
  804. /* This will handle all kinds of coalescing and immediate TX */
  805. ret = ieee80211_start_roc_work(local, sdata, params->chan,
  806. params->wait, cookie, skb,
  807. IEEE80211_ROC_TYPE_MGMT_TX);
  808. if (ret)
  809. ieee80211_free_txskb(&local->hw, skb);
  810. out_unlock:
  811. mutex_unlock(&local->mtx);
  812. return ret;
  813. }
  814. int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
  815. struct wireless_dev *wdev, u64 cookie)
  816. {
  817. struct ieee80211_local *local = wiphy_priv(wiphy);
  818. return ieee80211_cancel_roc(local, cookie, true);
  819. }
  820. void ieee80211_roc_setup(struct ieee80211_local *local)
  821. {
  822. wiphy_work_init(&local->hw_roc_start, ieee80211_hw_roc_start);
  823. wiphy_work_init(&local->hw_roc_done, ieee80211_hw_roc_done);
  824. wiphy_delayed_work_init(&local->roc_work, ieee80211_roc_work);
  825. INIT_LIST_HEAD(&local->roc_list);
  826. }
  827. void ieee80211_roc_purge(struct ieee80211_local *local,
  828. struct ieee80211_sub_if_data *sdata)
  829. {
  830. struct ieee80211_roc_work *roc, *tmp;
  831. bool work_to_do = false;
  832. mutex_lock(&local->mtx);
  833. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  834. if (sdata && roc->sdata != sdata)
  835. continue;
  836. if (roc->started) {
  837. if (local->ops->remain_on_channel) {
  838. /* can race, so ignore return value */
  839. drv_cancel_remain_on_channel(local, sdata);
  840. ieee80211_roc_notify_destroy(roc);
  841. } else {
  842. roc->abort = true;
  843. work_to_do = true;
  844. }
  845. } else {
  846. ieee80211_roc_notify_destroy(roc);
  847. }
  848. }
  849. if (work_to_do)
  850. __ieee80211_roc_work(local);
  851. mutex_unlock(&local->mtx);
  852. }