offchannel.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. struct ieee80211_local *local = roc->sdata->local;
  215. if (WARN_ON(roc->notified))
  216. return;
  217. roc->start_time = start_time;
  218. roc->started = true;
  219. roc->hw_begun = true;
  220. if (roc->mgmt_tx_cookie) {
  221. if (!WARN_ON(!roc->frame)) {
  222. ieee80211_tx_skb_tid_band(roc->sdata, roc->frame, 7,
  223. roc->chan->band);
  224. roc->frame = NULL;
  225. }
  226. } else {
  227. cfg80211_ready_on_channel(&roc->sdata->wdev, roc->cookie,
  228. roc->chan, roc->req_duration,
  229. GFP_KERNEL);
  230. }
  231. roc->notified = true;
  232. if (!local->ops->remain_on_channel)
  233. ieee80211_recalc_sw_work(local, start_time);
  234. }
  235. static void ieee80211_hw_roc_start(struct work_struct *work)
  236. {
  237. struct ieee80211_local *local =
  238. container_of(work, struct ieee80211_local, hw_roc_start);
  239. struct ieee80211_roc_work *roc;
  240. mutex_lock(&local->mtx);
  241. list_for_each_entry(roc, &local->roc_list, list) {
  242. if (!roc->started)
  243. break;
  244. ieee80211_handle_roc_started(roc, local->hw_roc_start_time);
  245. }
  246. mutex_unlock(&local->mtx);
  247. }
  248. void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
  249. {
  250. struct ieee80211_local *local = hw_to_local(hw);
  251. local->hw_roc_start_time = jiffies;
  252. trace_api_ready_on_channel(local);
  253. ieee80211_queue_work(hw, &local->hw_roc_start);
  254. }
  255. EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
  256. static void _ieee80211_start_next_roc(struct ieee80211_local *local)
  257. {
  258. struct ieee80211_roc_work *roc, *tmp;
  259. enum ieee80211_roc_type type;
  260. u32 min_dur, max_dur;
  261. lockdep_assert_held(&local->mtx);
  262. if (WARN_ON(list_empty(&local->roc_list)))
  263. return;
  264. roc = list_first_entry(&local->roc_list, struct ieee80211_roc_work,
  265. list);
  266. if (WARN_ON(roc->started))
  267. return;
  268. min_dur = roc->duration;
  269. max_dur = roc->duration;
  270. type = roc->type;
  271. list_for_each_entry(tmp, &local->roc_list, list) {
  272. if (tmp == roc)
  273. continue;
  274. if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
  275. break;
  276. max_dur = max(tmp->duration, max_dur);
  277. min_dur = min(tmp->duration, min_dur);
  278. type = max(tmp->type, type);
  279. }
  280. if (local->ops->remain_on_channel) {
  281. int ret = drv_remain_on_channel(local, roc->sdata, roc->chan,
  282. max_dur, type);
  283. if (ret) {
  284. wiphy_warn(local->hw.wiphy,
  285. "failed to start next HW ROC (%d)\n", ret);
  286. /*
  287. * queue the work struct again to avoid recursion
  288. * when multiple failures occur
  289. */
  290. list_for_each_entry(tmp, &local->roc_list, list) {
  291. if (tmp->sdata != roc->sdata ||
  292. tmp->chan != roc->chan)
  293. break;
  294. tmp->started = true;
  295. tmp->abort = true;
  296. }
  297. ieee80211_queue_work(&local->hw, &local->hw_roc_done);
  298. return;
  299. }
  300. /* we'll notify about the start once the HW calls back */
  301. list_for_each_entry(tmp, &local->roc_list, list) {
  302. if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
  303. break;
  304. tmp->started = true;
  305. }
  306. } else {
  307. /* If actually operating on the desired channel (with at least
  308. * 20 MHz channel width) don't stop all the operations but still
  309. * treat it as though the ROC operation started properly, so
  310. * other ROC operations won't interfere with this one.
  311. */
  312. roc->on_channel = roc->chan == local->_oper_chandef.chan &&
  313. local->_oper_chandef.width != NL80211_CHAN_WIDTH_5 &&
  314. local->_oper_chandef.width != NL80211_CHAN_WIDTH_10;
  315. /* start this ROC */
  316. ieee80211_recalc_idle(local);
  317. if (!roc->on_channel) {
  318. ieee80211_offchannel_stop_vifs(local);
  319. local->tmp_channel = roc->chan;
  320. ieee80211_hw_config(local, 0);
  321. }
  322. ieee80211_queue_delayed_work(&local->hw, &local->roc_work,
  323. msecs_to_jiffies(min_dur));
  324. /* tell userspace or send frame(s) */
  325. list_for_each_entry(tmp, &local->roc_list, list) {
  326. if (tmp->sdata != roc->sdata || tmp->chan != roc->chan)
  327. break;
  328. tmp->on_channel = roc->on_channel;
  329. ieee80211_handle_roc_started(tmp, jiffies);
  330. }
  331. }
  332. }
  333. void ieee80211_start_next_roc(struct ieee80211_local *local)
  334. {
  335. struct ieee80211_roc_work *roc;
  336. lockdep_assert_held(&local->mtx);
  337. if (list_empty(&local->roc_list)) {
  338. ieee80211_run_deferred_scan(local);
  339. return;
  340. }
  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. ieee80211_handle_roc_started(new_roc, now);
  435. return true;
  436. }
  437. static int ieee80211_start_roc_work(struct ieee80211_local *local,
  438. struct ieee80211_sub_if_data *sdata,
  439. struct ieee80211_channel *channel,
  440. unsigned int duration, u64 *cookie,
  441. struct sk_buff *txskb,
  442. enum ieee80211_roc_type type)
  443. {
  444. struct ieee80211_roc_work *roc, *tmp;
  445. bool queued = false, combine_started = true;
  446. int ret;
  447. lockdep_assert_held(&local->mtx);
  448. if (local->use_chanctx && !local->ops->remain_on_channel)
  449. return -EOPNOTSUPP;
  450. roc = kzalloc(sizeof(*roc), GFP_KERNEL);
  451. if (!roc)
  452. return -ENOMEM;
  453. /*
  454. * If the duration is zero, then the driver
  455. * wouldn't actually do anything. Set it to
  456. * 10 for now.
  457. *
  458. * TODO: cancel the off-channel operation
  459. * when we get the SKB's TX status and
  460. * the wait time was zero before.
  461. */
  462. if (!duration)
  463. duration = 10;
  464. roc->chan = channel;
  465. roc->duration = duration;
  466. roc->req_duration = duration;
  467. roc->frame = txskb;
  468. roc->type = type;
  469. roc->sdata = sdata;
  470. /*
  471. * cookie is either the roc cookie (for normal roc)
  472. * or the SKB (for mgmt TX)
  473. */
  474. if (!txskb) {
  475. roc->cookie = ieee80211_mgmt_tx_cookie(local);
  476. *cookie = roc->cookie;
  477. } else {
  478. roc->mgmt_tx_cookie = *cookie;
  479. }
  480. /* if there's no need to queue, handle it immediately */
  481. if (list_empty(&local->roc_list) &&
  482. !local->scanning && !ieee80211_is_radar_required(local)) {
  483. /* if not HW assist, just queue & schedule work */
  484. if (!local->ops->remain_on_channel) {
  485. list_add_tail(&roc->list, &local->roc_list);
  486. ieee80211_queue_delayed_work(&local->hw,
  487. &local->roc_work, 0);
  488. } else {
  489. /* otherwise actually kick it off here
  490. * (for error handling)
  491. */
  492. ret = drv_remain_on_channel(local, sdata, channel,
  493. duration, type);
  494. if (ret) {
  495. kfree(roc);
  496. return ret;
  497. }
  498. roc->started = true;
  499. list_add_tail(&roc->list, &local->roc_list);
  500. }
  501. return 0;
  502. }
  503. /* otherwise handle queueing */
  504. list_for_each_entry(tmp, &local->roc_list, list) {
  505. if (tmp->chan != channel || tmp->sdata != sdata)
  506. continue;
  507. /*
  508. * Extend this ROC if possible: If it hasn't started, add
  509. * just after the new one to combine.
  510. */
  511. if (!tmp->started) {
  512. list_add(&roc->list, &tmp->list);
  513. queued = true;
  514. break;
  515. }
  516. if (!combine_started)
  517. continue;
  518. if (!local->ops->remain_on_channel) {
  519. /* If there's no hardware remain-on-channel, and
  520. * doing so won't push us over the maximum r-o-c
  521. * we allow, then we can just add the new one to
  522. * the list and mark it as having started now.
  523. * If it would push over the limit, don't try to
  524. * combine with other started ones (that haven't
  525. * been running as long) but potentially sort it
  526. * with others that had the same fate.
  527. */
  528. unsigned long now = jiffies;
  529. u32 elapsed = jiffies_to_msecs(now - tmp->start_time);
  530. struct wiphy *wiphy = local->hw.wiphy;
  531. u32 max_roc = wiphy->max_remain_on_channel_duration;
  532. if (elapsed + roc->duration > max_roc) {
  533. combine_started = false;
  534. continue;
  535. }
  536. list_add(&roc->list, &tmp->list);
  537. queued = true;
  538. roc->on_channel = tmp->on_channel;
  539. ieee80211_handle_roc_started(roc, now);
  540. break;
  541. }
  542. queued = ieee80211_coalesce_hw_started_roc(local, roc, tmp);
  543. if (queued)
  544. break;
  545. /* if it wasn't queued, perhaps it can be combined with
  546. * another that also couldn't get combined previously,
  547. * but no need to check for already started ones, since
  548. * that can't work.
  549. */
  550. combine_started = false;
  551. }
  552. if (!queued)
  553. list_add_tail(&roc->list, &local->roc_list);
  554. return 0;
  555. }
  556. int ieee80211_remain_on_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
  557. struct ieee80211_channel *chan,
  558. unsigned int duration, u64 *cookie)
  559. {
  560. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  561. struct ieee80211_local *local = sdata->local;
  562. int ret;
  563. mutex_lock(&local->mtx);
  564. ret = ieee80211_start_roc_work(local, sdata, chan,
  565. duration, cookie, NULL,
  566. IEEE80211_ROC_TYPE_NORMAL);
  567. mutex_unlock(&local->mtx);
  568. return ret;
  569. }
  570. static int ieee80211_cancel_roc(struct ieee80211_local *local,
  571. u64 cookie, bool mgmt_tx)
  572. {
  573. struct ieee80211_roc_work *roc, *tmp, *found = NULL;
  574. int ret;
  575. if (!cookie)
  576. return -ENOENT;
  577. mutex_lock(&local->mtx);
  578. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  579. if (!mgmt_tx && roc->cookie != cookie)
  580. continue;
  581. else if (mgmt_tx && roc->mgmt_tx_cookie != cookie)
  582. continue;
  583. found = roc;
  584. break;
  585. }
  586. if (!found) {
  587. mutex_unlock(&local->mtx);
  588. return -ENOENT;
  589. }
  590. if (!found->started) {
  591. ieee80211_roc_notify_destroy(found);
  592. goto out_unlock;
  593. }
  594. if (local->ops->remain_on_channel) {
  595. ret = drv_cancel_remain_on_channel(local);
  596. if (WARN_ON_ONCE(ret)) {
  597. mutex_unlock(&local->mtx);
  598. return ret;
  599. }
  600. /* TODO:
  601. * if multiple items were combined here then we really shouldn't
  602. * cancel them all - we should wait for as much time as needed
  603. * for the longest remaining one, and only then cancel ...
  604. */
  605. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  606. if (!roc->started)
  607. break;
  608. if (roc == found)
  609. found = NULL;
  610. ieee80211_roc_notify_destroy(roc);
  611. }
  612. /* that really must not happen - it was started */
  613. WARN_ON(found);
  614. ieee80211_start_next_roc(local);
  615. } else {
  616. /* go through work struct to return to the operating channel */
  617. found->abort = true;
  618. mod_delayed_work(local->workqueue, &local->roc_work, 0);
  619. }
  620. out_unlock:
  621. mutex_unlock(&local->mtx);
  622. return 0;
  623. }
  624. int ieee80211_cancel_remain_on_channel(struct wiphy *wiphy,
  625. struct wireless_dev *wdev, u64 cookie)
  626. {
  627. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  628. struct ieee80211_local *local = sdata->local;
  629. return ieee80211_cancel_roc(local, cookie, false);
  630. }
  631. int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
  632. struct cfg80211_mgmt_tx_params *params, u64 *cookie)
  633. {
  634. struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
  635. struct ieee80211_local *local = sdata->local;
  636. struct sk_buff *skb;
  637. struct sta_info *sta;
  638. const struct ieee80211_mgmt *mgmt = (void *)params->buf;
  639. bool need_offchan = false;
  640. u32 flags;
  641. int ret;
  642. u8 *data;
  643. if (params->dont_wait_for_ack)
  644. flags = IEEE80211_TX_CTL_NO_ACK;
  645. else
  646. flags = IEEE80211_TX_INTFL_NL80211_FRAME_TX |
  647. IEEE80211_TX_CTL_REQ_TX_STATUS;
  648. if (params->no_cck)
  649. flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  650. switch (sdata->vif.type) {
  651. case NL80211_IFTYPE_ADHOC:
  652. if (!sdata->vif.bss_conf.ibss_joined)
  653. need_offchan = true;
  654. /* fall through */
  655. #ifdef CONFIG_MAC80211_MESH
  656. case NL80211_IFTYPE_MESH_POINT:
  657. if (ieee80211_vif_is_mesh(&sdata->vif) &&
  658. !sdata->u.mesh.mesh_id_len)
  659. need_offchan = true;
  660. /* fall through */
  661. #endif
  662. case NL80211_IFTYPE_AP:
  663. case NL80211_IFTYPE_AP_VLAN:
  664. case NL80211_IFTYPE_P2P_GO:
  665. if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
  666. !ieee80211_vif_is_mesh(&sdata->vif) &&
  667. !rcu_access_pointer(sdata->bss->beacon))
  668. need_offchan = true;
  669. if (!ieee80211_is_action(mgmt->frame_control) ||
  670. mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
  671. mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
  672. mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
  673. break;
  674. rcu_read_lock();
  675. sta = sta_info_get(sdata, mgmt->da);
  676. rcu_read_unlock();
  677. if (!sta)
  678. return -ENOLINK;
  679. break;
  680. case NL80211_IFTYPE_STATION:
  681. case NL80211_IFTYPE_P2P_CLIENT:
  682. sdata_lock(sdata);
  683. if (!sdata->u.mgd.associated ||
  684. (params->offchan && params->wait &&
  685. local->ops->remain_on_channel &&
  686. memcmp(sdata->u.mgd.associated->bssid,
  687. mgmt->bssid, ETH_ALEN)))
  688. need_offchan = true;
  689. sdata_unlock(sdata);
  690. break;
  691. case NL80211_IFTYPE_P2P_DEVICE:
  692. need_offchan = true;
  693. break;
  694. default:
  695. return -EOPNOTSUPP;
  696. }
  697. /* configurations requiring offchan cannot work if no channel has been
  698. * specified
  699. */
  700. if (need_offchan && !params->chan)
  701. return -EINVAL;
  702. mutex_lock(&local->mtx);
  703. /* Check if the operating channel is the requested channel */
  704. if (!need_offchan) {
  705. struct ieee80211_chanctx_conf *chanctx_conf;
  706. rcu_read_lock();
  707. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  708. if (chanctx_conf) {
  709. need_offchan = params->chan &&
  710. (params->chan !=
  711. chanctx_conf->def.chan);
  712. } else if (!params->chan) {
  713. ret = -EINVAL;
  714. rcu_read_unlock();
  715. goto out_unlock;
  716. } else {
  717. need_offchan = true;
  718. }
  719. rcu_read_unlock();
  720. }
  721. if (need_offchan && !params->offchan) {
  722. ret = -EBUSY;
  723. goto out_unlock;
  724. }
  725. skb = dev_alloc_skb(local->hw.extra_tx_headroom + params->len);
  726. if (!skb) {
  727. ret = -ENOMEM;
  728. goto out_unlock;
  729. }
  730. skb_reserve(skb, local->hw.extra_tx_headroom);
  731. data = skb_put(skb, params->len);
  732. memcpy(data, params->buf, params->len);
  733. /* Update CSA counters */
  734. if (sdata->vif.csa_active &&
  735. (sdata->vif.type == NL80211_IFTYPE_AP ||
  736. sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
  737. sdata->vif.type == NL80211_IFTYPE_ADHOC) &&
  738. params->n_csa_offsets) {
  739. int i;
  740. struct beacon_data *beacon = NULL;
  741. rcu_read_lock();
  742. if (sdata->vif.type == NL80211_IFTYPE_AP)
  743. beacon = rcu_dereference(sdata->u.ap.beacon);
  744. else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  745. beacon = rcu_dereference(sdata->u.ibss.presp);
  746. else if (ieee80211_vif_is_mesh(&sdata->vif))
  747. beacon = rcu_dereference(sdata->u.mesh.beacon);
  748. if (beacon)
  749. for (i = 0; i < params->n_csa_offsets; i++)
  750. data[params->csa_offsets[i]] =
  751. beacon->csa_current_counter;
  752. rcu_read_unlock();
  753. }
  754. IEEE80211_SKB_CB(skb)->flags = flags;
  755. skb->dev = sdata->dev;
  756. if (!params->dont_wait_for_ack) {
  757. /* make a copy to preserve the frame contents
  758. * in case of encryption.
  759. */
  760. ret = ieee80211_attach_ack_skb(local, skb, cookie, GFP_KERNEL);
  761. if (ret) {
  762. kfree_skb(skb);
  763. goto out_unlock;
  764. }
  765. } else {
  766. /* Assign a dummy non-zero cookie, it's not sent to
  767. * userspace in this case but we rely on its value
  768. * internally in the need_offchan case to distinguish
  769. * mgmt-tx from remain-on-channel.
  770. */
  771. *cookie = 0xffffffff;
  772. }
  773. if (!need_offchan) {
  774. ieee80211_tx_skb(sdata, skb);
  775. ret = 0;
  776. goto out_unlock;
  777. }
  778. IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_TX_OFFCHAN |
  779. IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  780. if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
  781. IEEE80211_SKB_CB(skb)->hw_queue =
  782. local->hw.offchannel_tx_hw_queue;
  783. /* This will handle all kinds of coalescing and immediate TX */
  784. ret = ieee80211_start_roc_work(local, sdata, params->chan,
  785. params->wait, cookie, skb,
  786. IEEE80211_ROC_TYPE_MGMT_TX);
  787. if (ret)
  788. ieee80211_free_txskb(&local->hw, skb);
  789. out_unlock:
  790. mutex_unlock(&local->mtx);
  791. return ret;
  792. }
  793. int ieee80211_mgmt_tx_cancel_wait(struct wiphy *wiphy,
  794. struct wireless_dev *wdev, u64 cookie)
  795. {
  796. struct ieee80211_local *local = wiphy_priv(wiphy);
  797. return ieee80211_cancel_roc(local, cookie, true);
  798. }
  799. void ieee80211_roc_setup(struct ieee80211_local *local)
  800. {
  801. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  802. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  803. INIT_DELAYED_WORK(&local->roc_work, ieee80211_roc_work);
  804. INIT_LIST_HEAD(&local->roc_list);
  805. }
  806. void ieee80211_roc_purge(struct ieee80211_local *local,
  807. struct ieee80211_sub_if_data *sdata)
  808. {
  809. struct ieee80211_roc_work *roc, *tmp;
  810. bool work_to_do = false;
  811. mutex_lock(&local->mtx);
  812. list_for_each_entry_safe(roc, tmp, &local->roc_list, list) {
  813. if (sdata && roc->sdata != sdata)
  814. continue;
  815. if (roc->started) {
  816. if (local->ops->remain_on_channel) {
  817. /* can race, so ignore return value */
  818. drv_cancel_remain_on_channel(local);
  819. ieee80211_roc_notify_destroy(roc);
  820. } else {
  821. roc->abort = true;
  822. work_to_do = true;
  823. }
  824. } else {
  825. ieee80211_roc_notify_destroy(roc);
  826. }
  827. }
  828. if (work_to_do)
  829. __ieee80211_roc_work(local);
  830. mutex_unlock(&local->mtx);
  831. }