ieee80211softmac_scan.c 7.4 KB

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