offchannel.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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, 0);
  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. #ifdef CONFIG_MAC80211_MESH
  660. /* fall through */
  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. #endif
  666. /* fall through */
  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_data(skb, params->buf, params->len);
  738. /* Update CSA counters */
  739. if (sdata->vif.csa_active &&
  740. (sdata->vif.type == NL80211_IFTYPE_AP ||
  741. sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  742. sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
  743. params->n_csa_offsets) {
  744. int i;
  745. struct beacon_data *beacon = NULL;
  746. rcu_read_lock();
  747. if (sdata->vif.type == NL80211_IFTYPE_AP)
  748. beacon = rcu_dereference(sdata->u.ap.beacon);
  749. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  750. beacon = rcu_dereference(sdata->u.ibss.presp);
  751. else if (ieee80211_vif_is_mesh(&sdata->vif))
  752. beacon = rcu_dereference(sdata->u.mesh.beacon);
  753. if (beacon)
  754. for (i = 0; i < params->n_csa_offsets; i++)
  755. data[params->csa_offsets[i]] =
  756. beacon->csa_current_counter;
  757. rcu_read_unlock();
  758. }
  759. IEEE80211_SKB_CB(skb)->flags = flags;
  760. skb->dev = sdata->dev;
  761. if (!params->dont_wait_for_ack) {
  762. /* make a copy to preserve the frame contents
  763. * in case of encryption.
  764. */
  765. ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
  766. if (ret) {
  767. kfree_skb(skb);
  768. goto out_unlock;
  769. }
  770. } else {
  771. /* Assign a dummy non-zero cookie, it's not sent to
  772. * userspace in this case but we rely on its value
  773. * internally in the need_offchan case to distinguish
  774. * mgmt-tx from remain-on-channel.
  775. */
  776. *cookie = 0xffffffff;
  777. }
  778. if (!need_offchan) {
  779. ieee80211_tx_skb(sdata, skb);
  780. ret = 0;
  781. goto out_unlock;
  782. }
  783. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
  784. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  785. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  786. IEEE80211_SKB_CB(skb)->hw_queue =
  787. local->hw.offchannel_tx_hw_queue;
  788. /* This will handle all kinds of coalescing and immediate TX */
  789. ret = ieee80211_start_roc_work(local, sdata, params->chan,
  790. params->wait, cookie, skb,
  791. IEEE80211_ROC_TYPE_MGMT_TX);
  792. if (ret)
  793. ieee80211_free_txskb(&local->hw, skb);
  794. out_unlock:
  795. mutex_unlock(&local->mtx);
  796. return ret;
  797. }
  798. int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
  799. struct wireless_dev *wdev, u64 cookie)
  800. {
  801. struct ieee80211_local *local = wiphy_priv(wiphy);
  802. return ieee80211_cancel_roc(local, cookie, true);
  803. }
  804. void ieee80211_roc_setup(struct ieee80211_local *local)
  805. {
  806. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  807. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  808. INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work);
  809. INIT_LIST_HEAD(&local->roc_list);
  810. }
  811. void ieee80211_roc_purge(struct ieee80211_local *local,
  812. struct ieee80211_sub_if_data *sdata)
  813. {
  814. struct ieee80211_roc_work *roc, *tmp;
  815. bool work_to_do = false;
  816. mutex_lock(&local->mtx);
  817. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  818. if (sdata && roc->sdata != sdata)
  819. continue;
  820. if (roc->started) {
  821. if (local->ops->remain_on_channel) {
  822. /* can race, so ignore return value */
  823. drv_cancel_remain_on_channel(local);
  824. ieee80211_roc_notify_destroy(roc);
  825. } else {
  826. roc->abort = true;
  827. work_to_do = true;
  828. }
  829. } else {
  830. ieee80211_roc_notify_destroy(roc);
  831. }
  832. }
  833. if (work_to_do)
  834. __ieee80211_roc_work(local);
  835. mutex_unlock(&local->mtx);
  836. }