scan.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "config.h"
  18. #include "wpa_supplicant_i.h"
  19. #include "mlme.h"
  20. #include "wps/wps.h"
  21. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  22. {
  23. struct wpa_ssid *ssid;
  24. union wpa_event_data data;
  25. ssid = wpa_supplicant_get_ssid(wpa_s);
  26. if (ssid == NULL)
  27. return;
  28. if (wpa_s->current_ssid == NULL)
  29. wpa_s->current_ssid = ssid;
  30. wpa_supplicant_initiate_eapol(wpa_s);
  31. wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
  32. "generating associated event");
  33. os_memset(&data, 0, sizeof(data));
  34. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  35. }
  36. #ifdef CONFIG_WPS
  37. static int wpas_wps_in_use(struct wpa_config *conf)
  38. {
  39. struct wpa_ssid *ssid;
  40. int wps = 0;
  41. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  42. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  43. continue;
  44. wps = 1;
  45. if (!ssid->eap.phase1)
  46. continue;
  47. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  48. return 2;
  49. }
  50. return wps;
  51. }
  52. #endif /* CONFIG_WPS */
  53. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  54. {
  55. struct wpa_supplicant *wpa_s = eloop_ctx;
  56. struct wpa_ssid *ssid;
  57. int enabled, scan_req = 0, ret;
  58. struct wpabuf *wps_ie = NULL;
  59. const u8 *extra_ie = NULL;
  60. size_t extra_ie_len = 0;
  61. int wps = 0;
  62. if (wpa_s->disconnected && !wpa_s->scan_req)
  63. return;
  64. enabled = 0;
  65. ssid = wpa_s->conf->ssid;
  66. while (ssid) {
  67. if (!ssid->disabled) {
  68. enabled++;
  69. break;
  70. }
  71. ssid = ssid->next;
  72. }
  73. if (!enabled && !wpa_s->scan_req) {
  74. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  75. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  76. return;
  77. }
  78. scan_req = wpa_s->scan_req;
  79. wpa_s->scan_req = 0;
  80. if (wpa_s->conf->ap_scan != 0 &&
  81. wpa_s->driver && IS_WIRED(wpa_s->driver)) {
  82. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  83. "overriding ap_scan configuration");
  84. wpa_s->conf->ap_scan = 0;
  85. }
  86. if (wpa_s->conf->ap_scan == 0) {
  87. wpa_supplicant_gen_assoc_event(wpa_s);
  88. return;
  89. }
  90. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  91. wpa_s->wpa_state == WPA_INACTIVE)
  92. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  93. ssid = wpa_s->conf->ssid;
  94. if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
  95. while (ssid) {
  96. if (ssid == wpa_s->prev_scan_ssid) {
  97. ssid = ssid->next;
  98. break;
  99. }
  100. ssid = ssid->next;
  101. }
  102. }
  103. while (ssid) {
  104. if (!ssid->disabled &&
  105. (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
  106. break;
  107. ssid = ssid->next;
  108. }
  109. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  110. /*
  111. * ap_scan=2 mode - try to associate with each SSID instead of
  112. * scanning for each scan_ssid=1 network.
  113. */
  114. if (ssid == NULL) {
  115. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  116. "end of scan list - go back to beginning");
  117. wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
  118. wpa_supplicant_req_scan(wpa_s, 0, 0);
  119. return;
  120. }
  121. if (ssid->next) {
  122. /* Continue from the next SSID on the next attempt. */
  123. wpa_s->prev_scan_ssid = ssid;
  124. } else {
  125. /* Start from the beginning of the SSID list. */
  126. wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
  127. }
  128. wpa_supplicant_associate(wpa_s, NULL, ssid);
  129. return;
  130. }
  131. wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
  132. ssid ? "specific": "broadcast");
  133. if (ssid) {
  134. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  135. ssid->ssid, ssid->ssid_len);
  136. wpa_s->prev_scan_ssid = ssid;
  137. } else
  138. wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
  139. #ifdef CONFIG_WPS
  140. wps = wpas_wps_in_use(wpa_s->conf);
  141. #endif /* CONFIG_WPS */
  142. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  143. !wpa_s->use_client_mlme && wps != 2) {
  144. wpa_s->scan_res_tried++;
  145. wpa_s->scan_req = scan_req;
  146. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  147. "first without requesting a new scan to speed up "
  148. "initial association");
  149. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  150. return;
  151. }
  152. #ifdef CONFIG_WPS
  153. if (wps) {
  154. wps_ie = wps_enrollee_build_probe_req_ie(wps == 2,
  155. wpa_s->conf->uuid);
  156. if (wps_ie) {
  157. extra_ie = wpabuf_head(wps_ie);
  158. extra_ie_len = wpabuf_len(wps_ie);
  159. }
  160. }
  161. #endif /* CONFIG_WPS */
  162. if (wpa_s->use_client_mlme) {
  163. ieee80211_sta_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
  164. ret = ieee80211_sta_req_scan(wpa_s, ssid ? ssid->ssid : NULL,
  165. ssid ? ssid->ssid_len : 0);
  166. } else {
  167. wpa_drv_set_probe_req_ie(wpa_s, extra_ie, extra_ie_len);
  168. ret = wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
  169. ssid ? ssid->ssid_len : 0);
  170. }
  171. wpabuf_free(wps_ie);
  172. if (ret) {
  173. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  174. wpa_supplicant_req_scan(wpa_s, 10, 0);
  175. }
  176. }
  177. /**
  178. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  179. * @wpa_s: Pointer to wpa_supplicant data
  180. * @sec: Number of seconds after which to scan
  181. * @usec: Number of microseconds after which to scan
  182. *
  183. * This function is used to schedule a scan for neighboring access points after
  184. * the specified time.
  185. */
  186. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  187. {
  188. /* If there's at least one network that should be specifically scanned
  189. * then don't cancel the scan and reschedule. Some drivers do
  190. * background scanning which generates frequent scan results, and that
  191. * causes the specific SSID scan to get continually pushed back and
  192. * never happen, which causes hidden APs to never get probe-scanned.
  193. */
  194. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  195. wpa_s->conf->ap_scan == 1) {
  196. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  197. while (ssid) {
  198. if (!ssid->disabled && ssid->scan_ssid)
  199. break;
  200. ssid = ssid->next;
  201. }
  202. if (ssid) {
  203. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  204. "ensure that specific SSID scans occur");
  205. return;
  206. }
  207. }
  208. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  209. sec, usec);
  210. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  211. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  212. }
  213. /**
  214. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  215. * @wpa_s: Pointer to wpa_supplicant data
  216. *
  217. * This function is used to cancel a scan request scheduled with
  218. * wpa_supplicant_req_scan().
  219. */
  220. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  221. {
  222. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  223. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  224. }