scan.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  56. {
  57. struct wpa_ssid *ssid = conf->ssid;
  58. while (ssid) {
  59. if (!ssid->disabled)
  60. return 1;
  61. ssid = ssid->next;
  62. }
  63. return 0;
  64. }
  65. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  66. struct wpa_ssid *ssid)
  67. {
  68. while (ssid) {
  69. if (!ssid->disabled)
  70. break;
  71. ssid = ssid->next;
  72. }
  73. /* ap_scan=2 mode - try to associate with each SSID. */
  74. if (ssid == NULL) {
  75. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  76. "end of scan list - go back to beginning");
  77. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  78. wpa_supplicant_req_scan(wpa_s, 0, 0);
  79. return;
  80. }
  81. if (ssid->next) {
  82. /* Continue from the next SSID on the next attempt. */
  83. wpa_s->prev_scan_ssid = ssid;
  84. } else {
  85. /* Start from the beginning of the SSID list. */
  86. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  87. }
  88. wpa_supplicant_associate(wpa_s, NULL, ssid);
  89. }
  90. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  91. {
  92. struct wpa_supplicant *wpa_s = eloop_ctx;
  93. struct wpa_ssid *ssid;
  94. int scan_req = 0, ret;
  95. struct wpabuf *wps_ie = NULL;
  96. int wps = 0;
  97. #ifdef CONFIG_WPS
  98. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  99. #endif /* CONFIG_WPS */
  100. struct wpa_driver_scan_params params;
  101. size_t max_ssids;
  102. if (wpa_s->disconnected && !wpa_s->scan_req)
  103. return;
  104. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  105. !wpa_s->scan_req) {
  106. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  107. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  108. return;
  109. }
  110. if (wpa_s->conf->ap_scan != 0 &&
  111. wpa_s->driver && IS_WIRED(wpa_s->driver)) {
  112. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  113. "overriding ap_scan configuration");
  114. wpa_s->conf->ap_scan = 0;
  115. }
  116. if (wpa_s->conf->ap_scan == 0) {
  117. wpa_supplicant_gen_assoc_event(wpa_s);
  118. return;
  119. }
  120. if (wpa_s->use_client_mlme || wpa_s->conf->ap_scan == 2)
  121. max_ssids = 1;
  122. else {
  123. max_ssids = wpa_s->max_scan_ssids;
  124. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  125. max_ssids = WPAS_MAX_SCAN_SSIDS;
  126. }
  127. #ifdef CONFIG_WPS
  128. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  129. #endif /* CONFIG_WPS */
  130. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  131. !wpa_s->use_client_mlme && wps != 2) {
  132. wpa_s->scan_res_tried++;
  133. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  134. "first without requesting a new scan to speed up "
  135. "initial association");
  136. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  137. return;
  138. }
  139. scan_req = wpa_s->scan_req;
  140. wpa_s->scan_req = 0;
  141. os_memset(&params, 0, sizeof(params));
  142. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  143. wpa_s->wpa_state == WPA_INACTIVE)
  144. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  145. /* Find the starting point from which to continue scanning */
  146. ssid = wpa_s->conf->ssid;
  147. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  148. while (ssid) {
  149. if (ssid == wpa_s->prev_scan_ssid) {
  150. ssid = ssid->next;
  151. break;
  152. }
  153. ssid = ssid->next;
  154. }
  155. }
  156. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  157. wpa_supplicant_assoc_try(wpa_s, ssid);
  158. return;
  159. } else if (wpa_s->conf->ap_scan == 2) {
  160. /*
  161. * User-initiated scan request in ap_scan == 2; scan with
  162. * wildcard SSID.
  163. */
  164. ssid = NULL;
  165. } else {
  166. struct wpa_ssid *start = ssid;
  167. if (ssid == NULL && max_ssids > 1)
  168. ssid = wpa_s->conf->ssid;
  169. while (ssid) {
  170. if (!ssid->disabled && ssid->scan_ssid) {
  171. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  172. ssid->ssid, ssid->ssid_len);
  173. params.ssids[params.num_ssids].ssid =
  174. ssid->ssid;
  175. params.ssids[params.num_ssids].ssid_len =
  176. ssid->ssid_len;
  177. params.num_ssids++;
  178. if (params.num_ssids + 1 >= max_ssids)
  179. break;
  180. }
  181. ssid = ssid->next;
  182. if (ssid == start)
  183. break;
  184. if (ssid == NULL && max_ssids > 1 &&
  185. start != wpa_s->conf->ssid)
  186. ssid = wpa_s->conf->ssid;
  187. }
  188. }
  189. if (ssid) {
  190. wpa_s->prev_scan_ssid = ssid;
  191. if (max_ssids > 1) {
  192. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  193. "scan request");
  194. params.num_ssids++;
  195. }
  196. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  197. } else {
  198. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  199. params.num_ssids++;
  200. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  201. }
  202. #ifdef CONFIG_WPS
  203. if (wps) {
  204. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  205. wpa_s->wps->uuid, req_type);
  206. if (wps_ie) {
  207. params.extra_ies = wpabuf_head(wps_ie);
  208. params.extra_ies_len = wpabuf_len(wps_ie);
  209. }
  210. }
  211. #endif /* CONFIG_WPS */
  212. if (wpa_s->use_client_mlme) {
  213. ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
  214. params.extra_ies_len);
  215. ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
  216. params.ssids[0].ssid_len);
  217. } else {
  218. wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
  219. params.extra_ies_len);
  220. ret = wpa_drv_scan(wpa_s, &params);
  221. }
  222. wpabuf_free(wps_ie);
  223. if (ret) {
  224. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  225. wpa_supplicant_req_scan(wpa_s, 10, 0);
  226. } else
  227. wpa_s->scan_runs++;
  228. }
  229. /**
  230. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  231. * @wpa_s: Pointer to wpa_supplicant data
  232. * @sec: Number of seconds after which to scan
  233. * @usec: Number of microseconds after which to scan
  234. *
  235. * This function is used to schedule a scan for neighboring access points after
  236. * the specified time.
  237. */
  238. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  239. {
  240. /* If there's at least one network that should be specifically scanned
  241. * then don't cancel the scan and reschedule. Some drivers do
  242. * background scanning which generates frequent scan results, and that
  243. * causes the specific SSID scan to get continually pushed back and
  244. * never happen, which causes hidden APs to never get probe-scanned.
  245. */
  246. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  247. wpa_s->conf->ap_scan == 1) {
  248. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  249. while (ssid) {
  250. if (!ssid->disabled && ssid->scan_ssid)
  251. break;
  252. ssid = ssid->next;
  253. }
  254. if (ssid) {
  255. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  256. "ensure that specific SSID scans occur");
  257. return;
  258. }
  259. }
  260. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  261. sec, usec);
  262. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  263. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  264. }
  265. /**
  266. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  267. * @wpa_s: Pointer to wpa_supplicant data
  268. *
  269. * This function is used to cancel a scan request scheduled with
  270. * wpa_supplicant_req_scan().
  271. */
  272. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  273. {
  274. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  275. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  276. }