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. ret = sm->start_scan(sm->dev);
  47. if (ret) {
  48. spin_lock_irqsave(&sm->lock, flags);
  49. sm->scanning = 0;
  50. spin_unlock_irqrestore(&sm->lock, flags);
  51. }
  52. return ret;
  53. }
  54. void
  55. ieee80211softmac_stop_scan(struct ieee80211softmac_device *sm)
  56. {
  57. unsigned long flags;
  58. spin_lock_irqsave(&sm->lock, flags);
  59. if (!sm->scanning) {
  60. spin_unlock_irqrestore(&sm->lock, flags);
  61. return;
  62. }
  63. spin_unlock_irqrestore(&sm->lock, flags);
  64. sm->stop_scan(sm->dev);
  65. }
  66. void
  67. ieee80211softmac_wait_for_scan(struct ieee80211softmac_device *sm)
  68. {
  69. unsigned long flags;
  70. spin_lock_irqsave(&sm->lock, flags);
  71. if (!sm->scanning) {
  72. spin_unlock_irqrestore(&sm->lock, flags);
  73. return;
  74. }
  75. spin_unlock_irqrestore(&sm->lock, flags);
  76. sm->wait_for_scan(sm->dev);
  77. }
  78. /* internal scanning implementation follows */
  79. void ieee80211softmac_scan(struct work_struct *work)
  80. {
  81. int invalid_channel;
  82. u8 current_channel_idx;
  83. struct ieee80211softmac_scaninfo *si =
  84. container_of(work, struct ieee80211softmac_scaninfo,
  85. softmac_scan.work);
  86. struct ieee80211softmac_device *sm = si->mac;
  87. unsigned long flags;
  88. while (!(si->stop) && (si->current_channel_idx < si->number_channels)) {
  89. current_channel_idx = si->current_channel_idx;
  90. si->current_channel_idx++; /* go to the next channel */
  91. invalid_channel = (si->skip_flags & si->channels[current_channel_idx].flags);
  92. if (!invalid_channel) {
  93. sm->set_channel(sm->dev, si->channels[current_channel_idx].channel);
  94. // FIXME make this user configurable (active/passive)
  95. if(ieee80211softmac_send_mgt_frame(sm, NULL, IEEE80211_STYPE_PROBE_REQ, 0))
  96. printkl(KERN_DEBUG PFX "Sending Probe Request Failed\n");
  97. /* also send directed management frame for the network we're looking for */
  98. // TODO: is this if correct, or should we do this only if scanning from assoc request?
  99. if (sm->associnfo.req_essid.len)
  100. ieee80211softmac_send_mgt_frame(sm, &sm->associnfo.req_essid, IEEE80211_STYPE_PROBE_REQ, 0);
  101. spin_lock_irqsave(&sm->lock, flags);
  102. if (unlikely(!sm->running)) {
  103. /* Prevent reschedule on workqueue flush */
  104. spin_unlock_irqrestore(&sm->lock, flags);
  105. break;
  106. }
  107. schedule_delayed_work(&si->softmac_scan, IEEE80211SOFTMAC_PROBE_DELAY);
  108. spin_unlock_irqrestore(&sm->lock, flags);
  109. return;
  110. } else {
  111. dprintk(PFX "Not probing Channel %d (not allowed here)\n", si->channels[current_channel_idx].channel);
  112. }
  113. }
  114. spin_lock_irqsave(&sm->lock, flags);
  115. cancel_delayed_work(&si->softmac_scan);
  116. si->started = 0;
  117. spin_unlock_irqrestore(&sm->lock, flags);
  118. dprintk(PFX "Scanning finished: scanned %d channels starting with channel %d\n",
  119. sm->scaninfo->number_channels, sm->scaninfo->channels[0].channel);
  120. ieee80211softmac_scan_finished(sm);
  121. complete_all(&sm->scaninfo->finished);
  122. }
  123. static inline struct ieee80211softmac_scaninfo *allocate_scaninfo(struct ieee80211softmac_device *mac)
  124. {
  125. /* ugh. can we call this without having the spinlock held? */
  126. struct ieee80211softmac_scaninfo *info = kmalloc(sizeof(struct ieee80211softmac_scaninfo), GFP_ATOMIC);
  127. if (unlikely(!info))
  128. return NULL;
  129. INIT_DELAYED_WORK(&info->softmac_scan, ieee80211softmac_scan);
  130. info->mac = mac;
  131. init_completion(&info->finished);
  132. return info;
  133. }
  134. int ieee80211softmac_start_scan_implementation(struct net_device *dev)
  135. {
  136. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  137. unsigned long flags;
  138. if (!(dev->flags & IFF_UP))
  139. return -ENODEV;
  140. assert(ieee80211softmac_scan_handlers_check_self(sm));
  141. if (!ieee80211softmac_scan_handlers_check_self(sm))
  142. return -EINVAL;
  143. spin_lock_irqsave(&sm->lock, flags);
  144. /* it looks like we need to hold the lock here
  145. * to make sure we don't allocate two of these... */
  146. if (unlikely(!sm->scaninfo))
  147. sm->scaninfo = allocate_scaninfo(sm);
  148. if (unlikely(!sm->scaninfo)) {
  149. spin_unlock_irqrestore(&sm->lock, flags);
  150. return -ENOMEM;
  151. }
  152. sm->scaninfo->skip_flags = IEEE80211_CH_INVALID;
  153. if (0 /* not scanning in IEEE802.11b */)//TODO
  154. sm->scaninfo->skip_flags |= IEEE80211_CH_B_ONLY;
  155. if (0 /* IEEE802.11a */) {//TODO
  156. sm->scaninfo->channels = sm->ieee->geo.a;
  157. sm->scaninfo->number_channels = sm->ieee->geo.a_channels;
  158. } else {
  159. sm->scaninfo->channels = sm->ieee->geo.bg;
  160. sm->scaninfo->number_channels = sm->ieee->geo.bg_channels;
  161. }
  162. sm->scaninfo->current_channel_idx = 0;
  163. sm->scaninfo->started = 1;
  164. sm->scaninfo->stop = 0;
  165. INIT_COMPLETION(sm->scaninfo->finished);
  166. schedule_delayed_work(&sm->scaninfo->softmac_scan, 0);
  167. spin_unlock_irqrestore(&sm->lock, flags);
  168. return 0;
  169. }
  170. void ieee80211softmac_stop_scan_implementation(struct net_device *dev)
  171. {
  172. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  173. unsigned long flags;
  174. assert(ieee80211softmac_scan_handlers_check_self(sm));
  175. if (!ieee80211softmac_scan_handlers_check_self(sm))
  176. return;
  177. spin_lock_irqsave(&sm->lock, flags);
  178. assert(sm->scaninfo != NULL);
  179. if (sm->scaninfo) {
  180. if (sm->scaninfo->started)
  181. sm->scaninfo->stop = 1;
  182. else
  183. complete_all(&sm->scaninfo->finished);
  184. }
  185. spin_unlock_irqrestore(&sm->lock, flags);
  186. }
  187. void ieee80211softmac_wait_for_scan_implementation(struct net_device *dev)
  188. {
  189. struct ieee80211softmac_device *sm = ieee80211_priv(dev);
  190. unsigned long flags;
  191. assert(ieee80211softmac_scan_handlers_check_self(sm));
  192. if (!ieee80211softmac_scan_handlers_check_self(sm))
  193. return;
  194. spin_lock_irqsave(&sm->lock, flags);
  195. if (!sm->scaninfo->started) {
  196. spin_unlock_irqrestore(&sm->lock, flags);
  197. return;
  198. }
  199. spin_unlock_irqrestore(&sm->lock, flags);
  200. wait_for_completion(&sm->scaninfo->finished);
  201. }
  202. /* this is what drivers (that do scanning) call when they're done */
  203. void ieee80211softmac_scan_finished(struct ieee80211softmac_device *sm)
  204. {
  205. unsigned long flags;
  206. spin_lock_irqsave(&sm->lock, flags);
  207. sm->scanning = 0;
  208. spin_unlock_irqrestore(&sm->lock, flags);
  209. if (sm->associnfo.bssvalid) {
  210. struct ieee80211softmac_network *net;
  211. net = ieee80211softmac_get_network_by_bssid(sm, sm->associnfo.bssid);
  212. if (net)
  213. sm->set_channel(sm->dev, net->channel);
  214. }
  215. ieee80211softmac_call_events(sm, IEEE80211SOFTMAC_EVENT_SCAN_FINISHED, NULL);
  216. }
  217. EXPORT_SYMBOL_GPL(ieee80211softmac_scan_finished);