scan.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 "driver_i.h"
  20. #include "mlme.h"
  21. #include "wps_supplicant.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,
  39. enum wps_request_type *req_type)
  40. {
  41. struct wpa_ssid *ssid;
  42. int wps = 0;
  43. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  44. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  45. continue;
  46. wps = 1;
  47. *req_type = wpas_wps_get_req_type(ssid);
  48. if (!ssid->eap.phase1)
  49. continue;
  50. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  51. return 2;
  52. }
  53. return wps;
  54. }
  55. #endif /* CONFIG_WPS */
  56. static int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  57. {
  58. struct wpa_ssid *ssid = conf->ssid;
  59. while (ssid) {
  60. if (!ssid->disabled)
  61. return 1;
  62. ssid = ssid->next;
  63. }
  64. return 0;
  65. }
  66. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  67. struct wpa_ssid *ssid)
  68. {
  69. while (ssid) {
  70. if (!ssid->disabled)
  71. break;
  72. ssid = ssid->next;
  73. }
  74. /* ap_scan=2 mode - try to associate with each SSID. */
  75. if (ssid == NULL) {
  76. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  77. "end of scan list - go back to beginning");
  78. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  79. wpa_supplicant_req_scan(wpa_s, 0, 0);
  80. return;
  81. }
  82. if (ssid->next) {
  83. /* Continue from the next SSID on the next attempt. */
  84. wpa_s->prev_scan_ssid = ssid;
  85. } else {
  86. /* Start from the beginning of the SSID list. */
  87. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  88. }
  89. wpa_supplicant_associate(wpa_s, NULL, ssid);
  90. }
  91. static int int_array_len(const int *a)
  92. {
  93. int i;
  94. for (i = 0; a && a[i]; i++)
  95. ;
  96. return i;
  97. }
  98. static void int_array_concat(int **res, const int *a)
  99. {
  100. int reslen, alen, i;
  101. int *n;
  102. reslen = int_array_len(*res);
  103. alen = int_array_len(a);
  104. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  105. if (n == NULL) {
  106. os_free(*res);
  107. *res = NULL;
  108. }
  109. for (i = 0; i <= alen; i++)
  110. n[reslen + i] = a[i];
  111. *res = n;
  112. }
  113. static int freq_cmp(const void *a, const void *b)
  114. {
  115. int _a = *(int *) a;
  116. int _b = *(int *) b;
  117. if (_a == 0)
  118. return 1;
  119. if (_b == 0)
  120. return -1;
  121. return _a - _b;
  122. }
  123. static void int_array_sort_unique(int *a)
  124. {
  125. int alen;
  126. int i, j;
  127. if (a == NULL)
  128. return;
  129. alen = int_array_len(a);
  130. qsort(a, alen, sizeof(int), freq_cmp);
  131. i = 0;
  132. j = 1;
  133. while (a[i] && a[j]) {
  134. if (a[i] == a[j]) {
  135. j++;
  136. continue;
  137. }
  138. a[++i] = a[j++];
  139. }
  140. if (a[i])
  141. i++;
  142. a[i] = 0;
  143. }
  144. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  145. {
  146. struct wpa_supplicant *wpa_s = eloop_ctx;
  147. struct wpa_ssid *ssid;
  148. int scan_req = 0, ret;
  149. struct wpabuf *wps_ie = NULL;
  150. int wps = 0;
  151. #ifdef CONFIG_WPS
  152. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  153. #endif /* CONFIG_WPS */
  154. struct wpa_driver_scan_params params;
  155. size_t max_ssids;
  156. if (wpa_s->disconnected && !wpa_s->scan_req)
  157. return;
  158. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  159. !wpa_s->scan_req) {
  160. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  161. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  162. return;
  163. }
  164. if (wpa_s->conf->ap_scan != 0 &&
  165. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  166. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  167. "overriding ap_scan configuration");
  168. wpa_s->conf->ap_scan = 0;
  169. }
  170. if (wpa_s->conf->ap_scan == 0) {
  171. wpa_supplicant_gen_assoc_event(wpa_s);
  172. return;
  173. }
  174. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  175. wpa_s->conf->ap_scan == 2)
  176. max_ssids = 1;
  177. else {
  178. max_ssids = wpa_s->max_scan_ssids;
  179. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  180. max_ssids = WPAS_MAX_SCAN_SSIDS;
  181. }
  182. #ifdef CONFIG_WPS
  183. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  184. #endif /* CONFIG_WPS */
  185. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  186. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
  187. wps != 2) {
  188. wpa_s->scan_res_tried++;
  189. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  190. "first without requesting a new scan to speed up "
  191. "initial association");
  192. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  193. return;
  194. }
  195. scan_req = wpa_s->scan_req;
  196. wpa_s->scan_req = 0;
  197. os_memset(&params, 0, sizeof(params));
  198. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  199. wpa_s->wpa_state == WPA_INACTIVE)
  200. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  201. /* Find the starting point from which to continue scanning */
  202. ssid = wpa_s->conf->ssid;
  203. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  204. while (ssid) {
  205. if (ssid == wpa_s->prev_scan_ssid) {
  206. ssid = ssid->next;
  207. break;
  208. }
  209. ssid = ssid->next;
  210. }
  211. }
  212. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  213. wpa_supplicant_assoc_try(wpa_s, ssid);
  214. return;
  215. } else if (wpa_s->conf->ap_scan == 2) {
  216. /*
  217. * User-initiated scan request in ap_scan == 2; scan with
  218. * wildcard SSID.
  219. */
  220. ssid = NULL;
  221. } else {
  222. struct wpa_ssid *start = ssid;
  223. int freqs_set = 0;
  224. if (ssid == NULL && max_ssids > 1)
  225. ssid = wpa_s->conf->ssid;
  226. while (ssid) {
  227. if (!ssid->disabled && ssid->scan_ssid) {
  228. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  229. ssid->ssid, ssid->ssid_len);
  230. params.ssids[params.num_ssids].ssid =
  231. ssid->ssid;
  232. params.ssids[params.num_ssids].ssid_len =
  233. ssid->ssid_len;
  234. params.num_ssids++;
  235. if (params.num_ssids + 1 >= max_ssids)
  236. break;
  237. }
  238. ssid = ssid->next;
  239. if (ssid == start)
  240. break;
  241. if (ssid == NULL && max_ssids > 1 &&
  242. start != wpa_s->conf->ssid)
  243. ssid = wpa_s->conf->ssid;
  244. }
  245. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  246. if (ssid->disabled)
  247. continue;
  248. if ((params.freqs || !freqs_set) && ssid->scan_freq) {
  249. int_array_concat(&params.freqs,
  250. ssid->scan_freq);
  251. } else {
  252. os_free(params.freqs);
  253. params.freqs = NULL;
  254. }
  255. freqs_set = 1;
  256. }
  257. int_array_sort_unique(params.freqs);
  258. }
  259. if (ssid) {
  260. wpa_s->prev_scan_ssid = ssid;
  261. if (max_ssids > 1) {
  262. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  263. "scan request");
  264. params.num_ssids++;
  265. }
  266. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  267. } else {
  268. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  269. params.num_ssids++;
  270. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  271. }
  272. #ifdef CONFIG_WPS
  273. if (wps) {
  274. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  275. wpa_s->wps->uuid, req_type);
  276. if (wps_ie) {
  277. params.extra_ies = wpabuf_head(wps_ie);
  278. params.extra_ies_len = wpabuf_len(wps_ie);
  279. }
  280. }
  281. #endif /* CONFIG_WPS */
  282. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
  283. ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
  284. params.extra_ies_len);
  285. ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
  286. params.ssids[0].ssid_len);
  287. } else {
  288. wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
  289. params.extra_ies_len);
  290. ret = wpa_drv_scan(wpa_s, &params);
  291. }
  292. wpabuf_free(wps_ie);
  293. os_free(params.freqs);
  294. if (ret) {
  295. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  296. wpa_supplicant_req_scan(wpa_s, 10, 0);
  297. } else
  298. wpa_s->scan_runs++;
  299. }
  300. /**
  301. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  302. * @wpa_s: Pointer to wpa_supplicant data
  303. * @sec: Number of seconds after which to scan
  304. * @usec: Number of microseconds after which to scan
  305. *
  306. * This function is used to schedule a scan for neighboring access points after
  307. * the specified time.
  308. */
  309. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  310. {
  311. /* If there's at least one network that should be specifically scanned
  312. * then don't cancel the scan and reschedule. Some drivers do
  313. * background scanning which generates frequent scan results, and that
  314. * causes the specific SSID scan to get continually pushed back and
  315. * never happen, which causes hidden APs to never get probe-scanned.
  316. */
  317. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  318. wpa_s->conf->ap_scan == 1) {
  319. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  320. while (ssid) {
  321. if (!ssid->disabled && ssid->scan_ssid)
  322. break;
  323. ssid = ssid->next;
  324. }
  325. if (ssid) {
  326. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  327. "ensure that specific SSID scans occur");
  328. return;
  329. }
  330. }
  331. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  332. sec, usec);
  333. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  334. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  335. }
  336. /**
  337. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  338. * @wpa_s: Pointer to wpa_supplicant data
  339. *
  340. * This function is used to cancel a scan request scheduled with
  341. * wpa_supplicant_req_scan().
  342. */
  343. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  344. {
  345. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  346. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  347. }