scan.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. int use_wildcard = 0;
  535. unsigned int max_sched_scan_ssids;
  536. if (!wpa_s->sched_scan_supported)
  537. return -1;
  538. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  539. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  540. else
  541. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  542. if (wpa_s->sched_scanning)
  543. return 0;
  544. os_memset(&params, 0, sizeof(params));
  545. prev_state = wpa_s->wpa_state;
  546. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  547. wpa_s->wpa_state == WPA_INACTIVE)
  548. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  549. /* Find the starting point from which to continue scanning */
  550. ssid = wpa_s->conf->ssid;
  551. if (wpa_s->prev_sched_ssid) {
  552. while (ssid) {
  553. if (ssid == wpa_s->prev_sched_ssid) {
  554. ssid = ssid->next;
  555. break;
  556. }
  557. ssid = ssid->next;
  558. }
  559. }
  560. if (!ssid || !wpa_s->prev_sched_ssid) {
  561. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  562. wpa_s->sched_scan_interval = 2;
  563. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  564. wpa_s->first_sched_scan = 1;
  565. ssid = wpa_s->conf->ssid;
  566. wpa_s->prev_sched_ssid = ssid;
  567. }
  568. while (ssid) {
  569. if (ssid->disabled) {
  570. wpa_s->prev_sched_ssid = ssid;
  571. ssid = ssid->next;
  572. continue;
  573. }
  574. if (!ssid->scan_ssid)
  575. use_wildcard = 1;
  576. else {
  577. params.ssids[params.num_ssids].ssid =
  578. ssid->ssid;
  579. params.ssids[params.num_ssids].ssid_len =
  580. ssid->ssid_len;
  581. params.num_ssids++;
  582. if (params.num_ssids + 1 >= max_sched_scan_ssids) {
  583. wpa_s->prev_sched_ssid = ssid;
  584. break;
  585. }
  586. }
  587. wpa_s->prev_sched_ssid = ssid;
  588. ssid = ssid->next;
  589. }
  590. if (ssid || use_wildcard) {
  591. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  592. "the sched scan request");
  593. params.num_ssids++;
  594. } else {
  595. wpa_dbg(wpa_s, MSG_DEBUG, "ssid %p - list ended", ssid);
  596. }
  597. if (!params.num_ssids)
  598. return 0;
  599. if (wpa_s->wps)
  600. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  601. wpa_dbg(wpa_s, MSG_DEBUG,
  602. "Starting sched scan: interval %d timeout %d",
  603. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  604. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  605. wpa_s->sched_scan_interval);
  606. wpabuf_free(wps_ie);
  607. if (ret) {
  608. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  609. if (prev_state != wpa_s->wpa_state)
  610. wpa_supplicant_set_state(wpa_s, prev_state);
  611. return ret;
  612. }
  613. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  614. if (ssid || !wpa_s->first_sched_scan) {
  615. wpa_s->sched_scan_timed_out = 0;
  616. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  617. wpa_supplicant_sched_scan_timeout,
  618. wpa_s, NULL);
  619. wpa_s->first_sched_scan = 0;
  620. wpa_s->sched_scan_timeout /= 2;
  621. wpa_s->sched_scan_interval *= 2;
  622. }
  623. return 0;
  624. }
  625. /**
  626. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  627. * @wpa_s: Pointer to wpa_supplicant data
  628. *
  629. * This function is used to cancel a scan request scheduled with
  630. * wpa_supplicant_req_scan().
  631. */
  632. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  633. {
  634. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  635. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  636. }
  637. /**
  638. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  639. * @wpa_s: Pointer to wpa_supplicant data
  640. *
  641. * This function is used to stop a periodic scheduled scan.
  642. */
  643. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  644. {
  645. if (!wpa_s->sched_scanning)
  646. return;
  647. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  648. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  649. wpa_supplicant_stop_sched_scan(wpa_s);
  650. }
  651. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  652. int scanning)
  653. {
  654. if (wpa_s->scanning != scanning) {
  655. wpa_s->scanning = scanning;
  656. wpas_notify_scanning(wpa_s);
  657. }
  658. }
  659. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  660. {
  661. int rate = 0;
  662. const u8 *ie;
  663. int i;
  664. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  665. for (i = 0; ie && i < ie[1]; i++) {
  666. if ((ie[i + 2] & 0x7f) > rate)
  667. rate = ie[i + 2] & 0x7f;
  668. }
  669. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_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. return rate;
  675. }
  676. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  677. {
  678. const u8 *end, *pos;
  679. pos = (const u8 *) (res + 1);
  680. end = pos + res->ie_len;
  681. while (pos + 1 < end) {
  682. if (pos + 2 + pos[1] > end)
  683. break;
  684. if (pos[0] == ie)
  685. return pos;
  686. pos += 2 + pos[1];
  687. }
  688. return NULL;
  689. }
  690. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  691. u32 vendor_type)
  692. {
  693. const u8 *end, *pos;
  694. pos = (const u8 *) (res + 1);
  695. end = pos + res->ie_len;
  696. while (pos + 1 < end) {
  697. if (pos + 2 + pos[1] > end)
  698. break;
  699. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  700. vendor_type == WPA_GET_BE32(&pos[2]))
  701. return pos;
  702. pos += 2 + pos[1];
  703. }
  704. return NULL;
  705. }
  706. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  707. u32 vendor_type)
  708. {
  709. struct wpabuf *buf;
  710. const u8 *end, *pos;
  711. buf = wpabuf_alloc(res->ie_len);
  712. if (buf == NULL)
  713. return NULL;
  714. pos = (const u8 *) (res + 1);
  715. end = pos + res->ie_len;
  716. while (pos + 1 < end) {
  717. if (pos + 2 + pos[1] > end)
  718. break;
  719. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  720. vendor_type == WPA_GET_BE32(&pos[2]))
  721. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  722. pos += 2 + pos[1];
  723. }
  724. if (wpabuf_len(buf) == 0) {
  725. wpabuf_free(buf);
  726. buf = NULL;
  727. }
  728. return buf;
  729. }
  730. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  731. const struct wpa_scan_res *res, u32 vendor_type)
  732. {
  733. struct wpabuf *buf;
  734. const u8 *end, *pos;
  735. if (res->beacon_ie_len == 0)
  736. return NULL;
  737. buf = wpabuf_alloc(res->beacon_ie_len);
  738. if (buf == NULL)
  739. return NULL;
  740. pos = (const u8 *) (res + 1);
  741. pos += res->ie_len;
  742. end = pos + res->beacon_ie_len;
  743. while (pos + 1 < end) {
  744. if (pos + 2 + pos[1] > end)
  745. break;
  746. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  747. vendor_type == WPA_GET_BE32(&pos[2]))
  748. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  749. pos += 2 + pos[1];
  750. }
  751. if (wpabuf_len(buf) == 0) {
  752. wpabuf_free(buf);
  753. buf = NULL;
  754. }
  755. return buf;
  756. }
  757. /* Compare function for sorting scan results. Return >0 if @b is considered
  758. * better. */
  759. static int wpa_scan_result_compar(const void *a, const void *b)
  760. {
  761. struct wpa_scan_res **_wa = (void *) a;
  762. struct wpa_scan_res **_wb = (void *) b;
  763. struct wpa_scan_res *wa = *_wa;
  764. struct wpa_scan_res *wb = *_wb;
  765. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  766. /* WPA/WPA2 support preferred */
  767. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  768. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  769. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  770. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  771. if (wpa_b && !wpa_a)
  772. return 1;
  773. if (!wpa_b && wpa_a)
  774. return -1;
  775. /* privacy support preferred */
  776. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  777. (wb->caps & IEEE80211_CAP_PRIVACY))
  778. return 1;
  779. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  780. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  781. return -1;
  782. /* best/max rate preferred if signal level close enough XXX */
  783. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  784. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  785. maxrate_a = wpa_scan_get_max_rate(wa);
  786. maxrate_b = wpa_scan_get_max_rate(wb);
  787. if (maxrate_a != maxrate_b)
  788. return maxrate_b - maxrate_a;
  789. }
  790. /* use freq for channel preference */
  791. /* all things being equal, use signal level; if signal levels are
  792. * identical, use quality values since some drivers may only report
  793. * that value and leave the signal level zero */
  794. if (wb->level == wa->level)
  795. return wb->qual - wa->qual;
  796. return wb->level - wa->level;
  797. }
  798. #ifdef CONFIG_WPS
  799. /* Compare function for sorting scan results when searching a WPS AP for
  800. * provisioning. Return >0 if @b is considered better. */
  801. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  802. {
  803. struct wpa_scan_res **_wa = (void *) a;
  804. struct wpa_scan_res **_wb = (void *) b;
  805. struct wpa_scan_res *wa = *_wa;
  806. struct wpa_scan_res *wb = *_wb;
  807. int uses_wps_a, uses_wps_b;
  808. struct wpabuf *wps_a, *wps_b;
  809. int res;
  810. /* Optimization - check WPS IE existence before allocated memory and
  811. * doing full reassembly. */
  812. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  813. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  814. if (uses_wps_a && !uses_wps_b)
  815. return -1;
  816. if (!uses_wps_a && uses_wps_b)
  817. return 1;
  818. if (uses_wps_a && uses_wps_b) {
  819. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  820. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  821. res = wps_ap_priority_compar(wps_a, wps_b);
  822. wpabuf_free(wps_a);
  823. wpabuf_free(wps_b);
  824. if (res)
  825. return res;
  826. }
  827. /*
  828. * Do not use current AP security policy as a sorting criteria during
  829. * WPS provisioning step since the AP may get reconfigured at the
  830. * completion of provisioning.
  831. */
  832. /* all things being equal, use signal level; if signal levels are
  833. * identical, use quality values since some drivers may only report
  834. * that value and leave the signal level zero */
  835. if (wb->level == wa->level)
  836. return wb->qual - wa->qual;
  837. return wb->level - wa->level;
  838. }
  839. #endif /* CONFIG_WPS */
  840. /**
  841. * wpa_supplicant_get_scan_results - Get scan results
  842. * @wpa_s: Pointer to wpa_supplicant data
  843. * @info: Information about what was scanned or %NULL if not available
  844. * @new_scan: Whether a new scan was performed
  845. * Returns: Scan results, %NULL on failure
  846. *
  847. * This function request the current scan results from the driver and updates
  848. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  849. * results with wpa_scan_results_free().
  850. */
  851. struct wpa_scan_results *
  852. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  853. struct scan_info *info, int new_scan)
  854. {
  855. struct wpa_scan_results *scan_res;
  856. size_t i;
  857. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  858. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  859. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  860. else
  861. scan_res = wpa_drv_get_scan_results2(wpa_s);
  862. if (scan_res == NULL) {
  863. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  864. return NULL;
  865. }
  866. #ifdef CONFIG_WPS
  867. if (wpas_wps_in_progress(wpa_s)) {
  868. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  869. "provisioning rules");
  870. compar = wpa_scan_result_wps_compar;
  871. }
  872. #endif /* CONFIG_WPS */
  873. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  874. compar);
  875. wpa_bss_update_start(wpa_s);
  876. for (i = 0; i < scan_res->num; i++)
  877. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  878. wpa_bss_update_end(wpa_s, info, new_scan);
  879. return scan_res;
  880. }
  881. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  882. {
  883. struct wpa_scan_results *scan_res;
  884. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  885. if (scan_res == NULL)
  886. return -1;
  887. wpa_scan_results_free(scan_res);
  888. return 0;
  889. }
  890. void wpa_scan_results_free(struct wpa_scan_results *res)
  891. {
  892. size_t i;
  893. if (res == NULL)
  894. return;
  895. for (i = 0; i < res->num; i++)
  896. os_free(res->res[i]);
  897. os_free(res->res);
  898. os_free(res);
  899. }