scan.c 6.9 KB

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