scan.c 9.8 KB

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