scan.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2010, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "config.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "mlme.h"
  22. #include "wps_supplicant.h"
  23. #include "notify.h"
  24. #include "bss.h"
  25. #include "scan.h"
  26. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  27. {
  28. struct wpa_ssid *ssid;
  29. union wpa_event_data data;
  30. ssid = wpa_supplicant_get_ssid(wpa_s);
  31. if (ssid == NULL)
  32. return;
  33. if (wpa_s->current_ssid == NULL) {
  34. wpa_s->current_ssid = ssid;
  35. if (wpa_s->current_ssid != NULL)
  36. wpas_notify_network_changed(wpa_s);
  37. }
  38. wpa_supplicant_initiate_eapol(wpa_s);
  39. wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
  40. "generating associated event");
  41. os_memset(&data, 0, sizeof(data));
  42. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  43. }
  44. #ifdef CONFIG_WPS
  45. static int wpas_wps_in_use(struct wpa_config *conf,
  46. enum wps_request_type *req_type)
  47. {
  48. struct wpa_ssid *ssid;
  49. int wps = 0;
  50. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  51. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  52. continue;
  53. wps = 1;
  54. *req_type = wpas_wps_get_req_type(ssid);
  55. if (!ssid->eap.phase1)
  56. continue;
  57. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  58. return 2;
  59. }
  60. return wps;
  61. }
  62. #endif /* CONFIG_WPS */
  63. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  64. {
  65. struct wpa_ssid *ssid = conf->ssid;
  66. while (ssid) {
  67. if (!ssid->disabled)
  68. return 1;
  69. ssid = ssid->next;
  70. }
  71. return 0;
  72. }
  73. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  74. struct wpa_ssid *ssid)
  75. {
  76. while (ssid) {
  77. if (!ssid->disabled)
  78. break;
  79. ssid = ssid->next;
  80. }
  81. /* ap_scan=2 mode - try to associate with each SSID. */
  82. if (ssid == NULL) {
  83. wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
  84. "end of scan list - go back to beginning");
  85. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  86. wpa_supplicant_req_scan(wpa_s, 0, 0);
  87. return;
  88. }
  89. if (ssid->next) {
  90. /* Continue from the next SSID on the next attempt. */
  91. wpa_s->prev_scan_ssid = ssid;
  92. } else {
  93. /* Start from the beginning of the SSID list. */
  94. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  95. }
  96. wpa_supplicant_associate(wpa_s, NULL, ssid);
  97. }
  98. static int int_array_len(const int *a)
  99. {
  100. int i;
  101. for (i = 0; a && a[i]; i++)
  102. ;
  103. return i;
  104. }
  105. static void int_array_concat(int **res, const int *a)
  106. {
  107. int reslen, alen, i;
  108. int *n;
  109. reslen = int_array_len(*res);
  110. alen = int_array_len(a);
  111. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  112. if (n == NULL) {
  113. os_free(*res);
  114. *res = NULL;
  115. return;
  116. }
  117. for (i = 0; i <= alen; i++)
  118. n[reslen + i] = a[i];
  119. *res = n;
  120. }
  121. static int freq_cmp(const void *a, const void *b)
  122. {
  123. int _a = *(int *) a;
  124. int _b = *(int *) b;
  125. if (_a == 0)
  126. return 1;
  127. if (_b == 0)
  128. return -1;
  129. return _a - _b;
  130. }
  131. static void int_array_sort_unique(int *a)
  132. {
  133. int alen;
  134. int i, j;
  135. if (a == NULL)
  136. return;
  137. alen = int_array_len(a);
  138. qsort(a, alen, sizeof(int), freq_cmp);
  139. i = 0;
  140. j = 1;
  141. while (a[i] && a[j]) {
  142. if (a[i] == a[j]) {
  143. j++;
  144. continue;
  145. }
  146. a[++i] = a[j++];
  147. }
  148. if (a[i])
  149. i++;
  150. a[i] = 0;
  151. }
  152. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  153. struct wpa_driver_scan_params *params)
  154. {
  155. int ret;
  156. wpa_supplicant_notify_scanning(wpa_s, 1);
  157. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  158. ret = ieee80211_sta_req_scan(wpa_s, params);
  159. else
  160. ret = wpa_drv_scan(wpa_s, params);
  161. if (ret) {
  162. wpa_supplicant_notify_scanning(wpa_s, 0);
  163. wpas_notify_scan_done(wpa_s, 0);
  164. } else
  165. wpa_s->scan_runs++;
  166. return ret;
  167. }
  168. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  169. {
  170. struct wpa_supplicant *wpa_s = eloop_ctx;
  171. struct wpa_ssid *ssid;
  172. int scan_req = 0, ret;
  173. struct wpabuf *wps_ie = NULL;
  174. int wps = 0;
  175. #ifdef CONFIG_WPS
  176. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  177. #endif /* CONFIG_WPS */
  178. struct wpa_driver_scan_params params;
  179. size_t max_ssids;
  180. if (wpa_s->disconnected && !wpa_s->scan_req) {
  181. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  182. return;
  183. }
  184. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  185. !wpa_s->scan_req) {
  186. wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
  187. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  188. return;
  189. }
  190. if (wpa_s->conf->ap_scan != 0 &&
  191. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  192. wpa_printf(MSG_DEBUG, "Using wired authentication - "
  193. "overriding ap_scan configuration");
  194. wpa_s->conf->ap_scan = 0;
  195. wpas_notify_ap_scan_changed(wpa_s);
  196. }
  197. if (wpa_s->conf->ap_scan == 0) {
  198. wpa_supplicant_gen_assoc_event(wpa_s);
  199. return;
  200. }
  201. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  202. wpa_s->conf->ap_scan == 2)
  203. max_ssids = 1;
  204. else {
  205. max_ssids = wpa_s->max_scan_ssids;
  206. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  207. max_ssids = WPAS_MAX_SCAN_SSIDS;
  208. }
  209. #ifdef CONFIG_WPS
  210. wps = wpas_wps_in_use(wpa_s->conf, &req_type);
  211. #endif /* CONFIG_WPS */
  212. if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
  213. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
  214. wps != 2) {
  215. wpa_s->scan_res_tried++;
  216. wpa_printf(MSG_DEBUG, "Trying to get current scan results "
  217. "first without requesting a new scan to speed up "
  218. "initial association");
  219. wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
  220. return;
  221. }
  222. scan_req = wpa_s->scan_req;
  223. wpa_s->scan_req = 0;
  224. os_memset(&params, 0, sizeof(params));
  225. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  226. wpa_s->wpa_state == WPA_INACTIVE)
  227. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  228. /* Find the starting point from which to continue scanning */
  229. ssid = wpa_s->conf->ssid;
  230. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  231. while (ssid) {
  232. if (ssid == wpa_s->prev_scan_ssid) {
  233. ssid = ssid->next;
  234. break;
  235. }
  236. ssid = ssid->next;
  237. }
  238. }
  239. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  240. wpa_supplicant_assoc_try(wpa_s, ssid);
  241. return;
  242. } else if (wpa_s->conf->ap_scan == 2) {
  243. /*
  244. * User-initiated scan request in ap_scan == 2; scan with
  245. * wildcard SSID.
  246. */
  247. ssid = NULL;
  248. } else {
  249. struct wpa_ssid *start = ssid, *tssid;
  250. int freqs_set = 0;
  251. if (ssid == NULL && max_ssids > 1)
  252. ssid = wpa_s->conf->ssid;
  253. while (ssid) {
  254. if (!ssid->disabled && ssid->scan_ssid) {
  255. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  256. ssid->ssid, ssid->ssid_len);
  257. params.ssids[params.num_ssids].ssid =
  258. ssid->ssid;
  259. params.ssids[params.num_ssids].ssid_len =
  260. ssid->ssid_len;
  261. params.num_ssids++;
  262. if (params.num_ssids + 1 >= max_ssids)
  263. break;
  264. }
  265. ssid = ssid->next;
  266. if (ssid == start)
  267. break;
  268. if (ssid == NULL && max_ssids > 1 &&
  269. start != wpa_s->conf->ssid)
  270. ssid = wpa_s->conf->ssid;
  271. }
  272. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  273. if (tssid->disabled)
  274. continue;
  275. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  276. int_array_concat(&params.freqs,
  277. tssid->scan_freq);
  278. } else {
  279. os_free(params.freqs);
  280. params.freqs = NULL;
  281. }
  282. freqs_set = 1;
  283. }
  284. int_array_sort_unique(params.freqs);
  285. }
  286. if (ssid) {
  287. wpa_s->prev_scan_ssid = ssid;
  288. if (max_ssids > 1) {
  289. wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
  290. "scan request");
  291. params.num_ssids++;
  292. }
  293. wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
  294. } else {
  295. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  296. params.num_ssids++;
  297. wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
  298. }
  299. #ifdef CONFIG_WPS
  300. if (wps) {
  301. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  302. wpa_s->wps->uuid, req_type);
  303. if (wps_ie) {
  304. params.extra_ies = wpabuf_head(wps_ie);
  305. params.extra_ies_len = wpabuf_len(wps_ie);
  306. }
  307. }
  308. #endif /* CONFIG_WPS */
  309. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  310. wpabuf_free(wps_ie);
  311. os_free(params.freqs);
  312. if (ret) {
  313. wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
  314. wpa_supplicant_req_scan(wpa_s, 10, 0);
  315. } else
  316. wpa_s->scan_runs++;
  317. }
  318. /**
  319. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  320. * @wpa_s: Pointer to wpa_supplicant data
  321. * @sec: Number of seconds after which to scan
  322. * @usec: Number of microseconds after which to scan
  323. *
  324. * This function is used to schedule a scan for neighboring access points after
  325. * the specified time.
  326. */
  327. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  328. {
  329. /* If there's at least one network that should be specifically scanned
  330. * then don't cancel the scan and reschedule. Some drivers do
  331. * background scanning which generates frequent scan results, and that
  332. * causes the specific SSID scan to get continually pushed back and
  333. * never happen, which causes hidden APs to never get probe-scanned.
  334. */
  335. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  336. wpa_s->conf->ap_scan == 1) {
  337. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  338. while (ssid) {
  339. if (!ssid->disabled && ssid->scan_ssid)
  340. break;
  341. ssid = ssid->next;
  342. }
  343. if (ssid) {
  344. wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  345. "ensure that specific SSID scans occur");
  346. return;
  347. }
  348. }
  349. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  350. sec, usec);
  351. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  352. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  353. }
  354. /**
  355. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  356. * @wpa_s: Pointer to wpa_supplicant data
  357. *
  358. * This function is used to cancel a scan request scheduled with
  359. * wpa_supplicant_req_scan().
  360. */
  361. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  362. {
  363. wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  364. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  365. }
  366. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  367. int scanning)
  368. {
  369. if (wpa_s->scanning != scanning) {
  370. wpa_s->scanning = scanning;
  371. wpas_notify_scanning(wpa_s);
  372. }
  373. }
  374. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  375. {
  376. int rate = 0;
  377. const u8 *ie;
  378. int i;
  379. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  380. for (i = 0; ie && i < ie[1]; i++) {
  381. if ((ie[i + 2] & 0x7f) > rate)
  382. rate = ie[i + 2] & 0x7f;
  383. }
  384. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  385. for (i = 0; ie && i < ie[1]; i++) {
  386. if ((ie[i + 2] & 0x7f) > rate)
  387. rate = ie[i + 2] & 0x7f;
  388. }
  389. return rate;
  390. }
  391. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  392. {
  393. const u8 *end, *pos;
  394. pos = (const u8 *) (res + 1);
  395. end = pos + res->ie_len;
  396. while (pos + 1 < end) {
  397. if (pos + 2 + pos[1] > end)
  398. break;
  399. if (pos[0] == ie)
  400. return pos;
  401. pos += 2 + pos[1];
  402. }
  403. return NULL;
  404. }
  405. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  406. u32 vendor_type)
  407. {
  408. const u8 *end, *pos;
  409. pos = (const u8 *) (res + 1);
  410. end = pos + res->ie_len;
  411. while (pos + 1 < end) {
  412. if (pos + 2 + pos[1] > end)
  413. break;
  414. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  415. vendor_type == WPA_GET_BE32(&pos[2]))
  416. return pos;
  417. pos += 2 + pos[1];
  418. }
  419. return NULL;
  420. }
  421. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  422. u32 vendor_type)
  423. {
  424. struct wpabuf *buf;
  425. const u8 *end, *pos;
  426. buf = wpabuf_alloc(res->ie_len);
  427. if (buf == NULL)
  428. return NULL;
  429. pos = (const u8 *) (res + 1);
  430. end = pos + res->ie_len;
  431. while (pos + 1 < end) {
  432. if (pos + 2 + pos[1] > end)
  433. break;
  434. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  435. vendor_type == WPA_GET_BE32(&pos[2]))
  436. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  437. pos += 2 + pos[1];
  438. }
  439. if (wpabuf_len(buf) == 0) {
  440. wpabuf_free(buf);
  441. buf = NULL;
  442. }
  443. return buf;
  444. }
  445. /* Compare function for sorting scan results. Return >0 if @b is considered
  446. * better. */
  447. static int wpa_scan_result_compar(const void *a, const void *b)
  448. {
  449. struct wpa_scan_res **_wa = (void *) a;
  450. struct wpa_scan_res **_wb = (void *) b;
  451. struct wpa_scan_res *wa = *_wa;
  452. struct wpa_scan_res *wb = *_wb;
  453. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  454. /* WPA/WPA2 support preferred */
  455. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  456. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  457. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  458. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  459. if (wpa_b && !wpa_a)
  460. return 1;
  461. if (!wpa_b && wpa_a)
  462. return -1;
  463. /* privacy support preferred */
  464. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  465. (wb->caps & IEEE80211_CAP_PRIVACY))
  466. return 1;
  467. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  468. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  469. return -1;
  470. /* best/max rate preferred if signal level close enough XXX */
  471. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  472. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  473. maxrate_a = wpa_scan_get_max_rate(wa);
  474. maxrate_b = wpa_scan_get_max_rate(wb);
  475. if (maxrate_a != maxrate_b)
  476. return maxrate_b - maxrate_a;
  477. }
  478. /* use freq for channel preference */
  479. /* all things being equal, use signal level; if signal levels are
  480. * identical, use quality values since some drivers may only report
  481. * that value and leave the signal level zero */
  482. if (wb->level == wa->level)
  483. return wb->qual - wa->qual;
  484. return wb->level - wa->level;
  485. }
  486. /**
  487. * wpa_supplicant_get_scan_results - Get scan results
  488. * @wpa_s: Pointer to wpa_supplicant data
  489. * @info: Information about what was scanned or %NULL if not available
  490. * @new_scan: Whether a new scan was performed
  491. * Returns: Scan results, %NULL on failure
  492. *
  493. * This function request the current scan results from the driver and updates
  494. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  495. * results with wpa_scan_results_free().
  496. */
  497. struct wpa_scan_results *
  498. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  499. struct scan_info *info, int new_scan)
  500. {
  501. struct wpa_scan_results *scan_res;
  502. size_t i;
  503. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  504. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  505. else
  506. scan_res = wpa_drv_get_scan_results2(wpa_s);
  507. if (scan_res == NULL) {
  508. wpa_printf(MSG_DEBUG, "Failed to get scan results");
  509. return NULL;
  510. }
  511. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  512. wpa_scan_result_compar);
  513. wpa_bss_update_start(wpa_s);
  514. for (i = 0; i < scan_res->num; i++)
  515. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  516. wpa_bss_update_end(wpa_s, info, new_scan);
  517. return scan_res;
  518. }
  519. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  520. {
  521. struct wpa_scan_results *scan_res;
  522. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  523. if (scan_res == NULL)
  524. return -1;
  525. wpa_scan_results_free(scan_res);
  526. return 0;
  527. }
  528. void wpa_scan_results_free(struct wpa_scan_results *res)
  529. {
  530. size_t i;
  531. if (res == NULL)
  532. return;
  533. for (i = 0; i < res->num; i++)
  534. os_free(res->res[i]);
  535. os_free(res->res);
  536. os_free(res);
  537. }