scan.c 6.8 KB

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