scan.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 "p2p_supplicant.h"
  24. #include "p2p/p2p.h"
  25. #include "notify.h"
  26. #include "bss.h"
  27. #include "scan.h"
  28. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  29. {
  30. struct wpa_ssid *ssid;
  31. union wpa_event_data data;
  32. ssid = wpa_supplicant_get_ssid(wpa_s);
  33. if (ssid == NULL)
  34. return;
  35. if (wpa_s->current_ssid == NULL) {
  36. wpa_s->current_ssid = ssid;
  37. if (wpa_s->current_ssid != NULL)
  38. wpas_notify_network_changed(wpa_s);
  39. }
  40. wpa_supplicant_initiate_eapol(wpa_s);
  41. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  42. "network - generating associated event");
  43. os_memset(&data, 0, sizeof(data));
  44. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  45. }
  46. #ifdef CONFIG_WPS
  47. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  48. enum wps_request_type *req_type)
  49. {
  50. struct wpa_ssid *ssid;
  51. int wps = 0;
  52. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  53. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  54. continue;
  55. wps = 1;
  56. *req_type = wpas_wps_get_req_type(ssid);
  57. if (!ssid->eap.phase1)
  58. continue;
  59. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  60. return 2;
  61. }
  62. #ifdef CONFIG_P2P
  63. wpa_s->wps->dev.p2p = 1;
  64. if (!wps) {
  65. wps = 1;
  66. *req_type = WPS_REQ_ENROLLEE_INFO;
  67. }
  68. #endif /* CONFIG_P2P */
  69. return wps;
  70. }
  71. #endif /* CONFIG_WPS */
  72. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  73. {
  74. struct wpa_ssid *ssid = conf->ssid;
  75. int count = 0;
  76. while (ssid) {
  77. if (!ssid->disabled)
  78. count++;
  79. ssid = ssid->next;
  80. }
  81. return count;
  82. }
  83. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  84. struct wpa_ssid *ssid)
  85. {
  86. while (ssid) {
  87. if (!ssid->disabled)
  88. break;
  89. ssid = ssid->next;
  90. }
  91. /* ap_scan=2 mode - try to associate with each SSID. */
  92. if (ssid == NULL) {
  93. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  94. "end of scan list - go back to beginning");
  95. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  96. wpa_supplicant_req_scan(wpa_s, 0, 0);
  97. return;
  98. }
  99. if (ssid->next) {
  100. /* Continue from the next SSID on the next attempt. */
  101. wpa_s->prev_scan_ssid = ssid;
  102. } else {
  103. /* Start from the beginning of the SSID list. */
  104. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  105. }
  106. wpa_supplicant_associate(wpa_s, NULL, ssid);
  107. }
  108. static int int_array_len(const int *a)
  109. {
  110. int i;
  111. for (i = 0; a && a[i]; i++)
  112. ;
  113. return i;
  114. }
  115. static void int_array_concat(int **res, const int *a)
  116. {
  117. int reslen, alen, i;
  118. int *n;
  119. reslen = int_array_len(*res);
  120. alen = int_array_len(a);
  121. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  122. if (n == NULL) {
  123. os_free(*res);
  124. *res = NULL;
  125. return;
  126. }
  127. for (i = 0; i <= alen; i++)
  128. n[reslen + i] = a[i];
  129. *res = n;
  130. }
  131. static int freq_cmp(const void *a, const void *b)
  132. {
  133. int _a = *(int *) a;
  134. int _b = *(int *) b;
  135. if (_a == 0)
  136. return 1;
  137. if (_b == 0)
  138. return -1;
  139. return _a - _b;
  140. }
  141. static void int_array_sort_unique(int *a)
  142. {
  143. int alen;
  144. int i, j;
  145. if (a == NULL)
  146. return;
  147. alen = int_array_len(a);
  148. qsort(a, alen, sizeof(int), freq_cmp);
  149. i = 0;
  150. j = 1;
  151. while (a[i] && a[j]) {
  152. if (a[i] == a[j]) {
  153. j++;
  154. continue;
  155. }
  156. a[++i] = a[j++];
  157. }
  158. if (a[i])
  159. i++;
  160. a[i] = 0;
  161. }
  162. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  163. struct wpa_driver_scan_params *params)
  164. {
  165. int ret;
  166. wpa_supplicant_notify_scanning(wpa_s, 1);
  167. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  168. ret = ieee80211_sta_req_scan(wpa_s, params);
  169. else
  170. ret = wpa_drv_scan(wpa_s, params);
  171. if (ret) {
  172. wpa_supplicant_notify_scanning(wpa_s, 0);
  173. wpas_notify_scan_done(wpa_s, 0);
  174. } else
  175. wpa_s->scan_runs++;
  176. return ret;
  177. }
  178. static void
  179. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  180. {
  181. struct wpa_supplicant *wpa_s = eloop_ctx;
  182. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  183. wpa_s->sched_scan_timed_out = 1;
  184. wpa_supplicant_cancel_sched_scan(wpa_s);
  185. }
  186. static int
  187. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  188. struct wpa_driver_scan_params *params,
  189. int interval)
  190. {
  191. int ret;
  192. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  193. return -1;
  194. wpa_supplicant_notify_scanning(wpa_s, 1);
  195. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  196. if (ret)
  197. wpa_supplicant_notify_scanning(wpa_s, 0);
  198. else
  199. wpa_s->sched_scanning = 1;
  200. return ret;
  201. }
  202. static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  203. {
  204. int ret;
  205. ret = wpa_drv_stop_sched_scan(wpa_s);
  206. if (ret) {
  207. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  208. /* TODO: what to do if stopping fails? */
  209. return -1;
  210. }
  211. return ret;
  212. }
  213. static struct wpa_driver_scan_filter *
  214. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  215. {
  216. struct wpa_driver_scan_filter *ssids;
  217. struct wpa_ssid *ssid;
  218. size_t count;
  219. *num_ssids = 0;
  220. if (!conf->filter_ssids)
  221. return NULL;
  222. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  223. if (ssid->ssid && ssid->ssid_len)
  224. count++;
  225. }
  226. if (count == 0)
  227. return NULL;
  228. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  229. if (ssids == NULL)
  230. return NULL;
  231. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  232. if (!ssid->ssid || !ssid->ssid_len)
  233. continue;
  234. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  235. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  236. (*num_ssids)++;
  237. }
  238. return ssids;
  239. }
  240. static void wpa_supplicant_optimize_freqs(
  241. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  242. {
  243. #ifdef CONFIG_P2P
  244. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  245. wpa_s->go_params) {
  246. /* Optimize provisioning state scan based on GO information */
  247. if (wpa_s->p2p_in_provisioning < 5 &&
  248. wpa_s->go_params->freq > 0) {
  249. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  250. "preferred frequency %d MHz",
  251. wpa_s->go_params->freq);
  252. params->freqs = os_zalloc(2 * sizeof(int));
  253. if (params->freqs)
  254. params->freqs[0] = wpa_s->go_params->freq;
  255. } else if (wpa_s->p2p_in_provisioning < 8 &&
  256. wpa_s->go_params->freq_list[0]) {
  257. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  258. "channels");
  259. int_array_concat(&params->freqs,
  260. wpa_s->go_params->freq_list);
  261. if (params->freqs)
  262. int_array_sort_unique(params->freqs);
  263. }
  264. wpa_s->p2p_in_provisioning++;
  265. }
  266. #endif /* CONFIG_P2P */
  267. #ifdef CONFIG_WPS
  268. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  269. /*
  270. * Optimize post-provisioning scan based on channel used
  271. * during provisioning.
  272. */
  273. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  274. "that was used during provisioning", wpa_s->wps_freq);
  275. params->freqs = os_zalloc(2 * sizeof(int));
  276. if (params->freqs)
  277. params->freqs[0] = wpa_s->wps_freq;
  278. wpa_s->after_wps--;
  279. }
  280. #endif /* CONFIG_WPS */
  281. }
  282. static struct wpabuf *
  283. wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
  284. struct wpa_driver_scan_params *params)
  285. {
  286. struct wpabuf *wps_ie = NULL;
  287. #ifdef CONFIG_WPS
  288. int wps = 0;
  289. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  290. wps = wpas_wps_in_use(wpa_s, &req_type);
  291. if (wps) {
  292. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  293. wpa_s->wps->uuid, req_type,
  294. 0, NULL);
  295. if (wps_ie) {
  296. params->extra_ies = wpabuf_head(wps_ie);
  297. params->extra_ies_len = wpabuf_len(wps_ie);
  298. }
  299. }
  300. #ifdef CONFIG_P2P
  301. if (wps_ie) {
  302. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  303. if (wpabuf_resize(&wps_ie, ielen) == 0) {
  304. wpas_p2p_scan_ie(wpa_s, wps_ie);
  305. params->extra_ies = wpabuf_head(wps_ie);
  306. params->extra_ies_len = wpabuf_len(wps_ie);
  307. }
  308. }
  309. #endif /* CONFIG_P2P */
  310. #endif /* CONFIG_WPS */
  311. return wps_ie;
  312. }
  313. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  314. {
  315. struct wpa_supplicant *wpa_s = eloop_ctx;
  316. struct wpa_ssid *ssid;
  317. int scan_req = 0, ret;
  318. struct wpabuf *wps_ie;
  319. struct wpa_driver_scan_params params;
  320. size_t max_ssids;
  321. enum wpa_states prev_state;
  322. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  323. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  324. return;
  325. }
  326. if (wpa_s->disconnected && !wpa_s->scan_req) {
  327. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  328. return;
  329. }
  330. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  331. !wpa_s->scan_req) {
  332. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  333. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  334. return;
  335. }
  336. if (wpa_s->conf->ap_scan != 0 &&
  337. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  338. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  339. "overriding ap_scan configuration");
  340. wpa_s->conf->ap_scan = 0;
  341. wpas_notify_ap_scan_changed(wpa_s);
  342. }
  343. if (wpa_s->conf->ap_scan == 0) {
  344. wpa_supplicant_gen_assoc_event(wpa_s);
  345. return;
  346. }
  347. #ifdef CONFIG_P2P
  348. if (wpas_p2p_in_progress(wpa_s)) {
  349. if (wpa_s->wpa_state == WPA_SCANNING) {
  350. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  351. "while P2P operation is in progress");
  352. wpa_supplicant_req_scan(wpa_s, 5, 0);
  353. } else {
  354. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
  355. "P2P operation is in progress");
  356. }
  357. return;
  358. }
  359. #endif /* CONFIG_P2P */
  360. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  361. wpa_s->conf->ap_scan == 2)
  362. max_ssids = 1;
  363. else {
  364. max_ssids = wpa_s->max_scan_ssids;
  365. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  366. max_ssids = WPAS_MAX_SCAN_SSIDS;
  367. }
  368. scan_req = wpa_s->scan_req;
  369. wpa_s->scan_req = 0;
  370. os_memset(&params, 0, sizeof(params));
  371. prev_state = wpa_s->wpa_state;
  372. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  373. wpa_s->wpa_state == WPA_INACTIVE)
  374. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  375. if (scan_req != 2 && wpa_s->connect_without_scan) {
  376. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  377. if (ssid == wpa_s->connect_without_scan)
  378. break;
  379. }
  380. wpa_s->connect_without_scan = NULL;
  381. if (ssid) {
  382. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  383. "without scan step");
  384. wpa_supplicant_associate(wpa_s, NULL, ssid);
  385. return;
  386. }
  387. }
  388. /* Find the starting point from which to continue scanning */
  389. ssid = wpa_s->conf->ssid;
  390. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  391. while (ssid) {
  392. if (ssid == wpa_s->prev_scan_ssid) {
  393. ssid = ssid->next;
  394. break;
  395. }
  396. ssid = ssid->next;
  397. }
  398. }
  399. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  400. wpa_s->connect_without_scan = NULL;
  401. wpa_supplicant_assoc_try(wpa_s, ssid);
  402. return;
  403. } else if (wpa_s->conf->ap_scan == 2) {
  404. /*
  405. * User-initiated scan request in ap_scan == 2; scan with
  406. * wildcard SSID.
  407. */
  408. ssid = NULL;
  409. } else {
  410. struct wpa_ssid *start = ssid, *tssid;
  411. int freqs_set = 0;
  412. if (ssid == NULL && max_ssids > 1)
  413. ssid = wpa_s->conf->ssid;
  414. while (ssid) {
  415. if (!ssid->disabled && ssid->scan_ssid) {
  416. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  417. ssid->ssid, ssid->ssid_len);
  418. params.ssids[params.num_ssids].ssid =
  419. ssid->ssid;
  420. params.ssids[params.num_ssids].ssid_len =
  421. ssid->ssid_len;
  422. params.num_ssids++;
  423. if (params.num_ssids + 1 >= max_ssids)
  424. break;
  425. }
  426. ssid = ssid->next;
  427. if (ssid == start)
  428. break;
  429. if (ssid == NULL && max_ssids > 1 &&
  430. start != wpa_s->conf->ssid)
  431. ssid = wpa_s->conf->ssid;
  432. }
  433. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  434. if (tssid->disabled)
  435. continue;
  436. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  437. int_array_concat(&params.freqs,
  438. tssid->scan_freq);
  439. } else {
  440. os_free(params.freqs);
  441. params.freqs = NULL;
  442. }
  443. freqs_set = 1;
  444. }
  445. int_array_sort_unique(params.freqs);
  446. }
  447. if (ssid) {
  448. wpa_s->prev_scan_ssid = ssid;
  449. if (max_ssids > 1) {
  450. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  451. "the scan request");
  452. params.num_ssids++;
  453. }
  454. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
  455. "SSID(s)");
  456. } else {
  457. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  458. params.num_ssids++;
  459. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  460. "SSID");
  461. }
  462. wpa_supplicant_optimize_freqs(wpa_s, &params);
  463. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  464. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  465. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  466. "generated frequency list");
  467. params.freqs = wpa_s->next_scan_freqs;
  468. } else
  469. os_free(wpa_s->next_scan_freqs);
  470. wpa_s->next_scan_freqs = NULL;
  471. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  472. wpa_s->conf, &params.num_filter_ssids);
  473. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  474. wpabuf_free(wps_ie);
  475. os_free(params.freqs);
  476. os_free(params.filter_ssids);
  477. if (ret) {
  478. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  479. if (prev_state != wpa_s->wpa_state)
  480. wpa_supplicant_set_state(wpa_s, prev_state);
  481. wpa_supplicant_req_scan(wpa_s, 1, 0);
  482. }
  483. }
  484. /**
  485. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  486. * @wpa_s: Pointer to wpa_supplicant data
  487. * @sec: Number of seconds after which to scan
  488. * @usec: Number of microseconds after which to scan
  489. *
  490. * This function is used to schedule a scan for neighboring access points after
  491. * the specified time.
  492. */
  493. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  494. {
  495. /* If there's at least one network that should be specifically scanned
  496. * then don't cancel the scan and reschedule. Some drivers do
  497. * background scanning which generates frequent scan results, and that
  498. * causes the specific SSID scan to get continually pushed back and
  499. * never happen, which causes hidden APs to never get probe-scanned.
  500. */
  501. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  502. wpa_s->conf->ap_scan == 1) {
  503. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  504. while (ssid) {
  505. if (!ssid->disabled && ssid->scan_ssid)
  506. break;
  507. ssid = ssid->next;
  508. }
  509. if (ssid) {
  510. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  511. "ensure that specific SSID scans occur");
  512. return;
  513. }
  514. }
  515. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  516. sec, usec);
  517. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  518. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  519. }
  520. /**
  521. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  522. * @wpa_s: Pointer to wpa_supplicant data
  523. *
  524. * This function is used to schedule periodic scans for neighboring
  525. * access points repeating the scan continuously.
  526. */
  527. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  528. {
  529. struct wpa_driver_scan_params params;
  530. enum wpa_states prev_state;
  531. struct wpa_ssid *ssid;
  532. struct wpabuf *wps_ie = NULL;
  533. int ret;
  534. unsigned int max_sched_scan_ssids;
  535. if (!wpa_s->sched_scan_supported)
  536. return -1;
  537. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  538. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  539. else
  540. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  541. if (wpa_s->sched_scanning)
  542. return 0;
  543. os_memset(&params, 0, sizeof(params));
  544. /* If we can't allocate space for the filters, we just don't filter */
  545. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  546. sizeof(struct wpa_driver_scan_filter));
  547. prev_state = wpa_s->wpa_state;
  548. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  549. wpa_s->wpa_state == WPA_INACTIVE)
  550. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  551. /* Find the starting point from which to continue scanning */
  552. ssid = wpa_s->conf->ssid;
  553. if (wpa_s->prev_sched_ssid) {
  554. while (ssid) {
  555. if (ssid == wpa_s->prev_sched_ssid) {
  556. ssid = ssid->next;
  557. break;
  558. }
  559. ssid = ssid->next;
  560. }
  561. }
  562. if (!ssid || !wpa_s->prev_sched_ssid) {
  563. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  564. wpa_s->sched_scan_interval = 2;
  565. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  566. wpa_s->first_sched_scan = 1;
  567. ssid = wpa_s->conf->ssid;
  568. wpa_s->prev_sched_ssid = ssid;
  569. }
  570. while (ssid) {
  571. if (ssid->disabled) {
  572. wpa_s->prev_sched_ssid = ssid;
  573. ssid = ssid->next;
  574. continue;
  575. }
  576. if (params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  577. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  578. ssid->ssid, ssid->ssid_len);
  579. params.filter_ssids[params.num_filter_ssids].ssid_len =
  580. ssid->ssid_len;
  581. params.num_filter_ssids++;
  582. }
  583. if (ssid->scan_ssid) {
  584. params.ssids[params.num_ssids].ssid =
  585. ssid->ssid;
  586. params.ssids[params.num_ssids].ssid_len =
  587. ssid->ssid_len;
  588. params.num_ssids++;
  589. if (params.num_ssids >= max_sched_scan_ssids) {
  590. wpa_s->prev_sched_ssid = ssid;
  591. break;
  592. }
  593. }
  594. if (params.num_filter_ssids >= wpa_s->max_match_sets)
  595. break;
  596. wpa_s->prev_sched_ssid = ssid;
  597. ssid = ssid->next;
  598. }
  599. if (!params.num_ssids) {
  600. os_free(params.filter_ssids);
  601. return 0;
  602. }
  603. if (wpa_s->wps)
  604. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  605. wpa_dbg(wpa_s, MSG_DEBUG,
  606. "Starting sched scan: interval %d timeout %d",
  607. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  608. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  609. wpa_s->sched_scan_interval);
  610. wpabuf_free(wps_ie);
  611. os_free(params.filter_ssids);
  612. if (ret) {
  613. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  614. if (prev_state != wpa_s->wpa_state)
  615. wpa_supplicant_set_state(wpa_s, prev_state);
  616. return ret;
  617. }
  618. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  619. if (ssid || !wpa_s->first_sched_scan) {
  620. wpa_s->sched_scan_timed_out = 0;
  621. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  622. wpa_supplicant_sched_scan_timeout,
  623. wpa_s, NULL);
  624. wpa_s->first_sched_scan = 0;
  625. wpa_s->sched_scan_timeout /= 2;
  626. wpa_s->sched_scan_interval *= 2;
  627. }
  628. return 0;
  629. }
  630. /**
  631. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  632. * @wpa_s: Pointer to wpa_supplicant data
  633. *
  634. * This function is used to cancel a scan request scheduled with
  635. * wpa_supplicant_req_scan().
  636. */
  637. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  638. {
  639. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  640. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  641. }
  642. /**
  643. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  644. * @wpa_s: Pointer to wpa_supplicant data
  645. *
  646. * This function is used to stop a periodic scheduled scan.
  647. */
  648. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  649. {
  650. if (!wpa_s->sched_scanning)
  651. return;
  652. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  653. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  654. wpa_supplicant_stop_sched_scan(wpa_s);
  655. }
  656. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  657. int scanning)
  658. {
  659. if (wpa_s->scanning != scanning) {
  660. wpa_s->scanning = scanning;
  661. wpas_notify_scanning(wpa_s);
  662. }
  663. }
  664. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  665. {
  666. int rate = 0;
  667. const u8 *ie;
  668. int i;
  669. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  670. for (i = 0; ie && i < ie[1]; i++) {
  671. if ((ie[i + 2] & 0x7f) > rate)
  672. rate = ie[i + 2] & 0x7f;
  673. }
  674. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  675. for (i = 0; ie && i < ie[1]; i++) {
  676. if ((ie[i + 2] & 0x7f) > rate)
  677. rate = ie[i + 2] & 0x7f;
  678. }
  679. return rate;
  680. }
  681. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  682. {
  683. const u8 *end, *pos;
  684. pos = (const u8 *) (res + 1);
  685. end = pos + res->ie_len;
  686. while (pos + 1 < end) {
  687. if (pos + 2 + pos[1] > end)
  688. break;
  689. if (pos[0] == ie)
  690. return pos;
  691. pos += 2 + pos[1];
  692. }
  693. return NULL;
  694. }
  695. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  696. u32 vendor_type)
  697. {
  698. const u8 *end, *pos;
  699. pos = (const u8 *) (res + 1);
  700. end = pos + res->ie_len;
  701. while (pos + 1 < end) {
  702. if (pos + 2 + pos[1] > end)
  703. break;
  704. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  705. vendor_type == WPA_GET_BE32(&pos[2]))
  706. return pos;
  707. pos += 2 + pos[1];
  708. }
  709. return NULL;
  710. }
  711. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  712. u32 vendor_type)
  713. {
  714. struct wpabuf *buf;
  715. const u8 *end, *pos;
  716. buf = wpabuf_alloc(res->ie_len);
  717. if (buf == NULL)
  718. return NULL;
  719. pos = (const u8 *) (res + 1);
  720. end = pos + res->ie_len;
  721. while (pos + 1 < end) {
  722. if (pos + 2 + pos[1] > end)
  723. break;
  724. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  725. vendor_type == WPA_GET_BE32(&pos[2]))
  726. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  727. pos += 2 + pos[1];
  728. }
  729. if (wpabuf_len(buf) == 0) {
  730. wpabuf_free(buf);
  731. buf = NULL;
  732. }
  733. return buf;
  734. }
  735. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  736. const struct wpa_scan_res *res, u32 vendor_type)
  737. {
  738. struct wpabuf *buf;
  739. const u8 *end, *pos;
  740. if (res->beacon_ie_len == 0)
  741. return NULL;
  742. buf = wpabuf_alloc(res->beacon_ie_len);
  743. if (buf == NULL)
  744. return NULL;
  745. pos = (const u8 *) (res + 1);
  746. pos += res->ie_len;
  747. end = pos + res->beacon_ie_len;
  748. while (pos + 1 < end) {
  749. if (pos + 2 + pos[1] > end)
  750. break;
  751. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  752. vendor_type == WPA_GET_BE32(&pos[2]))
  753. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  754. pos += 2 + pos[1];
  755. }
  756. if (wpabuf_len(buf) == 0) {
  757. wpabuf_free(buf);
  758. buf = NULL;
  759. }
  760. return buf;
  761. }
  762. /* Compare function for sorting scan results. Return >0 if @b is considered
  763. * better. */
  764. static int wpa_scan_result_compar(const void *a, const void *b)
  765. {
  766. struct wpa_scan_res **_wa = (void *) a;
  767. struct wpa_scan_res **_wb = (void *) b;
  768. struct wpa_scan_res *wa = *_wa;
  769. struct wpa_scan_res *wb = *_wb;
  770. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  771. /* WPA/WPA2 support preferred */
  772. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  773. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  774. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  775. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  776. if (wpa_b && !wpa_a)
  777. return 1;
  778. if (!wpa_b && wpa_a)
  779. return -1;
  780. /* privacy support preferred */
  781. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  782. (wb->caps & IEEE80211_CAP_PRIVACY))
  783. return 1;
  784. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  785. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  786. return -1;
  787. /* best/max rate preferred if signal level close enough XXX */
  788. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  789. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  790. maxrate_a = wpa_scan_get_max_rate(wa);
  791. maxrate_b = wpa_scan_get_max_rate(wb);
  792. if (maxrate_a != maxrate_b)
  793. return maxrate_b - maxrate_a;
  794. }
  795. /* use freq for channel preference */
  796. /* all things being equal, use signal level; if signal levels are
  797. * identical, use quality values since some drivers may only report
  798. * that value and leave the signal level zero */
  799. if (wb->level == wa->level)
  800. return wb->qual - wa->qual;
  801. return wb->level - wa->level;
  802. }
  803. #ifdef CONFIG_WPS
  804. /* Compare function for sorting scan results when searching a WPS AP for
  805. * provisioning. Return >0 if @b is considered better. */
  806. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  807. {
  808. struct wpa_scan_res **_wa = (void *) a;
  809. struct wpa_scan_res **_wb = (void *) b;
  810. struct wpa_scan_res *wa = *_wa;
  811. struct wpa_scan_res *wb = *_wb;
  812. int uses_wps_a, uses_wps_b;
  813. struct wpabuf *wps_a, *wps_b;
  814. int res;
  815. /* Optimization - check WPS IE existence before allocated memory and
  816. * doing full reassembly. */
  817. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  818. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  819. if (uses_wps_a && !uses_wps_b)
  820. return -1;
  821. if (!uses_wps_a && uses_wps_b)
  822. return 1;
  823. if (uses_wps_a && uses_wps_b) {
  824. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  825. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  826. res = wps_ap_priority_compar(wps_a, wps_b);
  827. wpabuf_free(wps_a);
  828. wpabuf_free(wps_b);
  829. if (res)
  830. return res;
  831. }
  832. /*
  833. * Do not use current AP security policy as a sorting criteria during
  834. * WPS provisioning step since the AP may get reconfigured at the
  835. * completion of provisioning.
  836. */
  837. /* all things being equal, use signal level; if signal levels are
  838. * identical, use quality values since some drivers may only report
  839. * that value and leave the signal level zero */
  840. if (wb->level == wa->level)
  841. return wb->qual - wa->qual;
  842. return wb->level - wa->level;
  843. }
  844. #endif /* CONFIG_WPS */
  845. /**
  846. * wpa_supplicant_get_scan_results - Get scan results
  847. * @wpa_s: Pointer to wpa_supplicant data
  848. * @info: Information about what was scanned or %NULL if not available
  849. * @new_scan: Whether a new scan was performed
  850. * Returns: Scan results, %NULL on failure
  851. *
  852. * This function request the current scan results from the driver and updates
  853. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  854. * results with wpa_scan_results_free().
  855. */
  856. struct wpa_scan_results *
  857. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  858. struct scan_info *info, int new_scan)
  859. {
  860. struct wpa_scan_results *scan_res;
  861. size_t i;
  862. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  863. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  864. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  865. else
  866. scan_res = wpa_drv_get_scan_results2(wpa_s);
  867. if (scan_res == NULL) {
  868. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  869. return NULL;
  870. }
  871. #ifdef CONFIG_WPS
  872. if (wpas_wps_in_progress(wpa_s)) {
  873. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  874. "provisioning rules");
  875. compar = wpa_scan_result_wps_compar;
  876. }
  877. #endif /* CONFIG_WPS */
  878. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  879. compar);
  880. wpa_bss_update_start(wpa_s);
  881. for (i = 0; i < scan_res->num; i++)
  882. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  883. wpa_bss_update_end(wpa_s, info, new_scan);
  884. return scan_res;
  885. }
  886. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  887. {
  888. struct wpa_scan_results *scan_res;
  889. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  890. if (scan_res == NULL)
  891. return -1;
  892. wpa_scan_results_free(scan_res);
  893. return 0;
  894. }
  895. void wpa_scan_results_free(struct wpa_scan_results *res)
  896. {
  897. size_t i;
  898. if (res == NULL)
  899. return;
  900. for (i = 0; i < res->num; i++)
  901. os_free(res->res[i]);
  902. os_free(res->res);
  903. os_free(res);
  904. }