scan.c 10 KB

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