offchannel.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <net/mac80211.h>
  16. #include "ieee80211_i.h"
  17. #include "driver-trace.h"
  18. /*
  19. * inform AP that we will go to sleep so that it will buffer the frames
  20. * while we scan
  21. */
  22. static void ieee80211_offchannel_ps_enable(struct ieee80211_sub_if_data *sdata)
  23. {
  24. struct ieee80211_local *local = sdata->local;
  25. struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
  26. local->offchannel_ps_enabled = false;
  27. /* FIXME: what to do when local->pspolling is true? */
  28. del_timer_sync(&local->dynamic_ps_timer);
  29. del_timer_sync(&ifmgd->bcn_mon_timer);
  30. del_timer_sync(&ifmgd->conn_mon_timer);
  31. cancel_work_sync(&local->dynamic_ps_enable_work);
  32. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  33. local->offchannel_ps_enabled = true;
  34. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  35. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  36. }
  37. if (!(local->offchannel_ps_enabled) ||
  38. !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
  39. /*
  40. * If power save was enabled, no need to send a nullfunc
  41. * frame because AP knows that we are sleeping. But if the
  42. * hardware is creating the nullfunc frame for power save
  43. * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
  44. * enabled) and power save was enabled, the firmware just
  45. * sent a null frame with power save disabled. So we need
  46. * to send a new nullfunc frame to inform the AP that we
  47. * are again sleeping.
  48. */
  49. ieee80211_send_nullfunc(local, sdata, 1);
  50. }
  51. /* inform AP that we are awake again, unless power save is enabled */
  52. static void ieee80211_offchannel_ps_disable(struct ieee80211_sub_if_data *sdata)
  53. {
  54. struct ieee80211_local *local = sdata->local;
  55. if (!local->ps_sdata)
  56. ieee80211_send_nullfunc(local, sdata, 0);
  57. else if (local->offchannel_ps_enabled) {
  58. /*
  59. * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
  60. * will send a nullfunc frame with the powersave bit set
  61. * even though the AP already knows that we are sleeping.
  62. * This could be avoided by sending a null frame with power
  63. * save bit disabled before enabling the power save, but
  64. * this doesn't gain anything.
  65. *
  66. * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
  67. * to send a nullfunc frame because AP already knows that
  68. * we are sleeping, let's just enable power save mode in
  69. * hardware.
  70. */
  71. local->hw.conf.flags |= IEEE80211_CONF_PS;
  72. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  73. } else if (local->hw.conf.dynamic_ps_timeout > 0) {
  74. /*
  75. * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
  76. * had been running before leaving the operating channel,
  77. * restart the timer now and send a nullfunc frame to inform
  78. * the AP that we are awake.
  79. */
  80. ieee80211_send_nullfunc(local, sdata, 0);
  81. mod_timer(&local->dynamic_ps_timer, jiffies +
  82. msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
  83. }
  84. ieee80211_sta_reset_beacon_monitor(sdata);
  85. ieee80211_sta_reset_conn_monitor(sdata);
  86. }
  87. void ieee80211_offchannel_stop_beaconing(struct ieee80211_local *local)
  88. {
  89. struct ieee80211_sub_if_data *sdata;
  90. mutex_lock(&local->iflist_mtx);
  91. list_for_each_entry(sdata, &local->interfaces, list) {
  92. if (!ieee80211_sdata_running(sdata))
  93. continue;
  94. /* disable beaconing */
  95. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  96. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  97. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  98. ieee80211_bss_info_change_notify(
  99. sdata, BSS_CHANGED_BEACON_ENABLED);
  100. /*
  101. * only handle non-STA interfaces here, STA interfaces
  102. * are handled in ieee80211_offchannel_stop_station(),
  103. * e.g., from the background scan state machine.
  104. *
  105. * In addition, do not stop monitor interface to allow it to be
  106. * used from user space controlled off-channel operations.
  107. */
  108. if (sdata->vif.type != NL80211_IFTYPE_STATION &&
  109. sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  110. set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  111. netif_tx_stop_all_queues(sdata->dev);
  112. }
  113. }
  114. mutex_unlock(&local->iflist_mtx);
  115. }
  116. void ieee80211_offchannel_stop_station(struct ieee80211_local *local)
  117. {
  118. struct ieee80211_sub_if_data *sdata;
  119. /*
  120. * notify the AP about us leaving the channel and stop all STA interfaces
  121. */
  122. mutex_lock(&local->iflist_mtx);
  123. list_for_each_entry(sdata, &local->interfaces, list) {
  124. if (!ieee80211_sdata_running(sdata))
  125. continue;
  126. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  127. set_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  128. netif_tx_stop_all_queues(sdata->dev);
  129. if (sdata->u.mgd.associated)
  130. ieee80211_offchannel_ps_enable(sdata);
  131. }
  132. }
  133. mutex_unlock(&local->iflist_mtx);
  134. }
  135. void ieee80211_offchannel_return(struct ieee80211_local *local,
  136. bool enable_beaconing)
  137. {
  138. struct ieee80211_sub_if_data *sdata;
  139. mutex_lock(&local->iflist_mtx);
  140. list_for_each_entry(sdata, &local->interfaces, list) {
  141. if (!ieee80211_sdata_running(sdata))
  142. continue;
  143. /* Tell AP we're back */
  144. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  145. if (sdata->u.mgd.associated)
  146. ieee80211_offchannel_ps_disable(sdata);
  147. }
  148. if (sdata->vif.type != NL80211_IFTYPE_MONITOR) {
  149. clear_bit(SDATA_STATE_OFFCHANNEL, &sdata->state);
  150. /*
  151. * This may wake up queues even though the driver
  152. * currently has them stopped. This is not very
  153. * likely, since the driver won't have gotten any
  154. * (or hardly any) new packets while we weren't
  155. * on the right channel, and even if it happens
  156. * it will at most lead to queueing up one more
  157. * packet per queue in mac80211 rather than on
  158. * the interface qdisc.
  159. */
  160. netif_tx_wake_all_queues(sdata->dev);
  161. }
  162. /* re-enable beaconing */
  163. if (enable_beaconing &&
  164. (sdata->vif.type == NL80211_IFTYPE_AP ||
  165. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  166. sdata->vif.type == NL80211_IFTYPE_MESH_POINT))
  167. ieee80211_bss_info_change_notify(
  168. sdata, BSS_CHANGED_BEACON_ENABLED);
  169. }
  170. mutex_unlock(&local->iflist_mtx);
  171. }
  172. static void ieee80211_hw_roc_start(struct work_struct *work)
  173. {
  174. struct ieee80211_local *local =
  175. container_of(work, struct ieee80211_local, hw_roc_start);
  176. struct ieee80211_sub_if_data *sdata;
  177. mutex_lock(&local->mtx);
  178. if (!local->hw_roc_channel) {
  179. mutex_unlock(&local->mtx);
  180. return;
  181. }
  182. ieee80211_recalc_idle(local);
  183. if (local->hw_roc_skb) {
  184. sdata = IEEE80211_DEV_TO_SUB_IF(local->hw_roc_dev);
  185. ieee80211_tx_skb(sdata, local->hw_roc_skb);
  186. local->hw_roc_skb = NULL;
  187. } else {
  188. cfg80211_ready_on_channel(local->hw_roc_dev,
  189. local->hw_roc_cookie,
  190. local->hw_roc_channel,
  191. local->hw_roc_channel_type,
  192. local->hw_roc_duration,
  193. GFP_KERNEL);
  194. }
  195. mutex_unlock(&local->mtx);
  196. }
  197. void ieee80211_ready_on_channel(struct ieee80211_hw *hw)
  198. {
  199. struct ieee80211_local *local = hw_to_local(hw);
  200. trace_api_ready_on_channel(local);
  201. ieee80211_queue_work(hw, &local->hw_roc_start);
  202. }
  203. EXPORT_SYMBOL_GPL(ieee80211_ready_on_channel);
  204. static void ieee80211_hw_roc_done(struct work_struct *work)
  205. {
  206. struct ieee80211_local *local =
  207. container_of(work, struct ieee80211_local, hw_roc_done);
  208. mutex_lock(&local->mtx);
  209. if (!local->hw_roc_channel) {
  210. mutex_unlock(&local->mtx);
  211. return;
  212. }
  213. if (!local->hw_roc_for_tx)
  214. cfg80211_remain_on_channel_expired(local->hw_roc_dev,
  215. local->hw_roc_cookie,
  216. local->hw_roc_channel,
  217. local->hw_roc_channel_type,
  218. GFP_KERNEL);
  219. local->hw_roc_channel = NULL;
  220. local->hw_roc_cookie = 0;
  221. ieee80211_recalc_idle(local);
  222. mutex_unlock(&local->mtx);
  223. }
  224. void ieee80211_remain_on_channel_expired(struct ieee80211_hw *hw)
  225. {
  226. struct ieee80211_local *local = hw_to_local(hw);
  227. trace_api_remain_on_channel_expired(local);
  228. ieee80211_queue_work(hw, &local->hw_roc_done);
  229. }
  230. EXPORT_SYMBOL_GPL(ieee80211_remain_on_channel_expired);
  231. void ieee80211_hw_roc_setup(struct ieee80211_local *local)
  232. {
  233. INIT_WORK(&local->hw_roc_start, ieee80211_hw_roc_start);
  234. INIT_WORK(&local->hw_roc_done, ieee80211_hw_roc_done);
  235. }