offchannel.c 26 KB

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