offchannel.c 26 KB

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