ieee80211softmac_scan.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Scanning routines.
  3. *
  4. * These are not exported because they're assigned to the function pointers.
  5. *
  6. * Copyright (c) 2005, 2006 Johannes Berg <johannes@sipsolutions.net>
  7. * Joseph Jezak <josejx@gentoo.org>
  8. * Larry Finger <Larry.Finger@lwfinger.net>
  9. * Danny van Dyk <kugelfang@gentoo.org>
  10. * Michael Buesch <mbuesch@freenet.de>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of version 2 of the GNU General Public License as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  19. * more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * The full GNU General Public License is included in this distribution in the
  26. * file called COPYING.
  27. */
  28. #include <linux/completion.h>
  29. #include "ieee80211softmac_priv.h"
  30. /* internal, use to trigger scanning if needed.
  31. * Returns -EBUSY if already scanning,
  32. * result of start_scan otherwise */
  33. int
  34. ieee80211softmac_start_scan(struct ieee80211softmac_device *sm)
  35. {
  36. unsigned long flags;
  37. int ret;
  38. spin_lock_irqsave(&sm->lock, flags);
  39. if (sm->scanning)
  40. {
  41. spin_unlock_irqrestore(&sm->lock, flags);
  42. return -EINPROGRESS;
  43. }
  44. sm->scanning = 1;
  45. spin_unlock_irqrestore(&sm->lock, flags);
  46. netif_tx_disable(sm->ieee->dev);
  47. ret = sm->start_scan(sm->dev);
  48. if (ret) {
  49. spin_lock_irqsave(&sm->lock, flags);
  50. sm->scanning = 0;
  51. spin_unlock_irqrestore(&sm->lock, flags);
  52. }
  53. return ret;
  54. }
  55. void
  56. ieee80211softmac_stop_scan(struct ieee80211softmac_device *sm)
  57. {
  58. unsigned long flags;
  59. spin_lock_irqsave(&sm->lock, flags);
  60. if (!sm->scanning) {
  61. spin_unlock_irqrestore(&sm->lock, flags);
  62. return;
  63. }
  64. spin_unlock_irqrestore(&sm->lock, flags);
  65. sm->stop_scan(sm->dev);
  66. }
  67. void
  68. ieee80211softmac_wait_for_scan(struct ieee80211softmac_device *sm)
  69. {
  70. unsigned long flags;
  71. spin_lock_irqsave(&sm->lock, flags);
  72. if (!sm->scanning) {
  73. spin_unlock_irqrestore(&sm->lock, flags);
  74. return;
  75. }
  76. spin_unlock_irqrestore(&sm->lock, flags);
  77. sm->wait_for_scan(sm->dev);
  78. }
  79. /* internal scanning implementation follows */
  80. void ieee80211softmac_scan(void *d)
  81. {
  82. int invalid_channel;
  83. u8 current_channel_idx;
  84. struct ieee80211softmac_device *sm = (struct ieee80211softmac_device *)d;
  85. struct ieee80211softmac_scaninfo *si = sm->scaninfo;
  86. unsigned long flags;
  87. while (!(si->stop) && (si->current_channel_idx < si->number_channels)) {
  88. current_channel_idx = si->current_channel_idx;
  89. si->current_channel_idx++; /* go to the next channel */
  90. invalid_channel = (si->skip_flags & si->channels[current_channel_idx].flags);
  91. if (!invalid_channel) {
  92. sm->set_channel(sm->dev, si->channels[current_channel_idx].channel);
  93. // FIXME make this user configurable (active/passive)
  94. if(ieee80211softmac_send_mgt_frame(sm, NULL, IEEE80211_STYPE_PROBE_REQ, 0))
  95. printkl(KERN_DEBUG PFX "Sending Probe Request Failed\n");
  96. /* also send directed management frame for the network we're looking for */
  97. // TODO: is this if correct, or should we do this only if scanning from assoc request?
  98. if (sm->associnfo.req_essid.len)
  99. ieee80211softmac_send_mgt_frame(sm, &sm->associnfo.req_essid, IEEE80211_STYPE_PROBE_REQ, 0);
  100. spin_lock_irqsave(&sm->lock, flags);
  101. if (unlikely(!sm->running)) {
  102. /* Prevent reschedule on workqueue flush */
  103. spin_unlock_irqrestore(&sm->lock, flags);
  104. break;
  105. }
  106. schedule_delayed_work(&si->softmac_scan, IEEE80211SOFTMAC_PROBE_DELAY);
  107. spin_unlock_irqrestore(&sm->lock, flags);
  108. return;
  109. } else {
  110. dprintk(PFX "Not probing Channel %d (not allowed here)\n", si->channels[current_channel_idx].channel);
  111. }
  112. }
  113. spin_lock_irqsave(&sm->lock, flags);
  114. cancel_delayed_work(&si->softmac_scan);
  115. si->started = 0;
  116. spin_unlock_irqrestore(&sm->lock, flags);
  117. dprintk(PFX "Scanning finished\n");
  118. ieee80211softmac_scan_finished(sm);
  119. complete_all(&sm->scaninfo->finished);
  120. }
  121. static inline struct ieee80211softmac_scaninfo *allocate_scaninfo(struct ieee80211softmac_device *mac)
  122. {
  123. /* ugh. can we call this without having the spinlock held? */
  124. struct ieee80211softmac_scaninfo *info = kmalloc(sizeof(struct ieee80211softmac_scaninfo), GFP_ATOMIC);
  125. if (unlikely(!info))
  126. return NULL;
  127. INIT_WORK(&info->softmac_scan, ieee80211softmac_scan, mac);
  128. init_completion(&info->finished);
  129. return info;
  130. }
  131. int ieee80211softmac_start_scan_implementation(struct net_device *dev)
  132. {
  133. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  134. unsigned long flags;
  135. if (!(dev->flags & IFF_UP))
  136. return -ENODEV;
  137. assert(ieee80211softmac_scan_handlers_check_self(sm));
  138. if (!ieee80211softmac_scan_handlers_check_self(sm))
  139. return -EINVAL;
  140. spin_lock_irqsave(&sm->lock, flags);
  141. /* it looks like we need to hold the lock here
  142. * to make sure we don't allocate two of these... */
  143. if (unlikely(!sm->scaninfo))
  144. sm->scaninfo = allocate_scaninfo(sm);
  145. if (unlikely(!sm->scaninfo)) {
  146. spin_unlock_irqrestore(&sm->lock, flags);
  147. return -ENOMEM;
  148. }
  149. sm->scaninfo->skip_flags = IEEE80211_CH_INVALID;
  150. if (0 /* not scanning in IEEE802.11b */)//TODO
  151. sm->scaninfo->skip_flags |= IEEE80211_CH_B_ONLY;
  152. if (0 /* IEEE802.11a */) {//TODO
  153. sm->scaninfo->channels = sm->ieee->geo.a;
  154. sm->scaninfo->number_channels = sm->ieee->geo.a_channels;
  155. } else {
  156. sm->scaninfo->channels = sm->ieee->geo.bg;
  157. sm->scaninfo->number_channels = sm->ieee->geo.bg_channels;
  158. }
  159. dprintk(PFX "Start scanning with channel: %d\n", sm->scaninfo->channels[0].channel);
  160. dprintk(PFX "Scanning %d channels\n", sm->scaninfo->number_channels);
  161. sm->scaninfo->current_channel_idx = 0;
  162. sm->scaninfo->started = 1;
  163. sm->scaninfo->stop = 0;
  164. INIT_COMPLETION(sm->scaninfo->finished);
  165. schedule_work(&sm->scaninfo->softmac_scan);
  166. spin_unlock_irqrestore(&sm->lock, flags);
  167. return 0;
  168. }
  169. void ieee80211softmac_stop_scan_implementation(struct net_device *dev)
  170. {
  171. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  172. unsigned long flags;
  173. assert(ieee80211softmac_scan_handlers_check_self(sm));
  174. if (!ieee80211softmac_scan_handlers_check_self(sm))
  175. return;
  176. spin_lock_irqsave(&sm->lock, flags);
  177. assert(sm->scaninfo != NULL);
  178. if (sm->scaninfo) {
  179. if (sm->scaninfo->started)
  180. sm->scaninfo->stop = 1;
  181. else
  182. complete_all(&sm->scaninfo->finished);
  183. }
  184. spin_unlock_irqrestore(&sm->lock, flags);
  185. }
  186. void ieee80211softmac_wait_for_scan_implementation(struct net_device *dev)
  187. {
  188. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  189. unsigned long flags;
  190. assert(ieee80211softmac_scan_handlers_check_self(sm));
  191. if (!ieee80211softmac_scan_handlers_check_self(sm))
  192. return;
  193. spin_lock_irqsave(&sm->lock, flags);
  194. if (!sm->scaninfo->started) {
  195. spin_unlock_irqrestore(&sm->lock, flags);
  196. return;
  197. }
  198. spin_unlock_irqrestore(&sm->lock, flags);
  199. wait_for_completion(&sm->scaninfo->finished);
  200. }
  201. /* this is what drivers (that do scanning) call when they're done */
  202. void ieee80211softmac_scan_finished(struct ieee80211softmac_device *sm)
  203. {
  204. unsigned long flags;
  205. spin_lock_irqsave(&sm->lock, flags);
  206. sm->scanning = 0;
  207. spin_unlock_irqrestore(&sm->lock, flags);
  208. if (sm->associnfo.bssvalid) {
  209. struct ieee80211softmac_network *net;
  210. net = ieee80211softmac_get_network_by_bssid(sm, sm->associnfo.bssid);
  211. if (net)
  212. sm->set_channel(sm->dev, net->channel);
  213. }
  214. netif_wake_queue(sm->ieee->dev);
  215. ieee80211softmac_call_events(sm, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, NULL);
  216. }
  217. EXPORT_SYMBOL_GPL(ieee80211softmac_scan_finished);