scan.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  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->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  121. wpa_s->conf->ap_scan == 2)
  122. max_ssids = 1;
  123. else {
  124. max_ssids = wpa_s->max_scan_ssids;
  125. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  126. max_ssids = WPAS_MAX_SCAN_SSIDS;
  127. }
  128. #ifdef CONFIG_WPS
  129. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  130. #endif /* CONFIG_WPS */
  131. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  132. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
  133. wps != 2) {
  134. wpa_s->scan_res_tried++;
  135. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  136. "first without requesting a new scan to speed up "
  137. "initial association");
  138. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  139. return;
  140. }
  141. scan_req = wpa_s->scan_req;
  142. wpa_s->scan_req = 0;
  143. os_memset(&params, 0, sizeof(params));
  144. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  145. wpa_s->wpa_state == WPA_INACTIVE)
  146. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  147. /* Find the starting point from which to continue scanning */
  148. ssid = wpa_s->conf->ssid;
  149. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  150. while (ssid) {
  151. if (ssid == wpa_s->prev_scan_ssid) {
  152. ssid = ssid->next;
  153. break;
  154. }
  155. ssid = ssid->next;
  156. }
  157. }
  158. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  159. wpa_supplicant_assoc_try(wpa_s, ssid);
  160. return;
  161. } else if (wpa_s->conf->ap_scan == 2) {
  162. /*
  163. * User-initiated scan request in ap_scan == 2; scan with
  164. * wildcard SSID.
  165. */
  166. ssid = NULL;
  167. } else {
  168. struct wpa_ssid *start = ssid;
  169. if (ssid == NULL && max_ssids > 1)
  170. ssid = wpa_s->conf->ssid;
  171. while (ssid) {
  172. if (!ssid->disabled && ssid->scan_ssid) {
  173. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  174. ssid->ssid, ssid->ssid_len);
  175. params.ssids[params.num_ssids].ssid =
  176. ssid->ssid;
  177. params.ssids[params.num_ssids].ssid_len =
  178. ssid->ssid_len;
  179. params.num_ssids++;
  180. if (params.num_ssids + 1 >= max_ssids)
  181. break;
  182. }
  183. ssid = ssid->next;
  184. if (ssid == start)
  185. break;
  186. if (ssid == NULL && max_ssids > 1 &&
  187. start != wpa_s->conf->ssid)
  188. ssid = wpa_s->conf->ssid;
  189. }
  190. }
  191. if (ssid) {
  192. wpa_s->prev_scan_ssid = ssid;
  193. if (max_ssids > 1) {
  194. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  195. "scan request");
  196. params.num_ssids++;
  197. }
  198. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  199. } else {
  200. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  201. params.num_ssids++;
  202. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  203. }
  204. #ifdef CONFIG_WPS
  205. if (wps) {
  206. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  207. wpa_s->wps->uuid, req_type);
  208. if (wps_ie) {
  209. params.extra_ies = wpabuf_head(wps_ie);
  210. params.extra_ies_len = wpabuf_len(wps_ie);
  211. }
  212. }
  213. #endif /* CONFIG_WPS */
  214. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  215. ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
  216. params.extra_ies_len);
  217. ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
  218. params.ssids[0].ssid_len);
  219. } else {
  220. wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
  221. params.extra_ies_len);
  222. ret = wpa_drv_scan(wpa_s, &params);
  223. }
  224. wpabuf_free(wps_ie);
  225. if (ret) {
  226. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  227. wpa_supplicant_req_scan(wpa_s, 10, 0);
  228. } else
  229. wpa_s->scan_runs++;
  230. }
  231. /**
  232. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  233. * @wpa_s: Pointer to wpa_supplicant data
  234. * @sec: Number of seconds after which to scan
  235. * @usec: Number of microseconds after which to scan
  236. *
  237. * This function is used to schedule a scan for neighboring access points after
  238. * the specified time.
  239. */
  240. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  241. {
  242. /* If there's at least one network that should be specifically scanned
  243. * then don't cancel the scan and reschedule. Some drivers do
  244. * background scanning which generates frequent scan results, and that
  245. * causes the specific SSID scan to get continually pushed back and
  246. * never happen, which causes hidden APs to never get probe-scanned.
  247. */
  248. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  249. wpa_s->conf->ap_scan == 1) {
  250. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  251. while (ssid) {
  252. if (!ssid->disabled && ssid->scan_ssid)
  253. break;
  254. ssid = ssid->next;
  255. }
  256. if (ssid) {
  257. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  258. "ensure that specific SSID scans occur");
  259. return;
  260. }
  261. }
  262. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  263. sec, usec);
  264. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  265. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  266. }
  267. /**
  268. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  269. * @wpa_s: Pointer to wpa_supplicant data
  270. *
  271. * This function is used to cancel a scan request scheduled with
  272. * wpa_supplicant_req_scan().
  273. */
  274. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  275. {
  276. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  277. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  278. }