scan.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "config.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "driver_i.h"
  15. #include "wps_supplicant.h"
  16. #include "p2p_supplicant.h"
  17. #include "p2p/p2p.h"
  18. #include "notify.h"
  19. #include "bss.h"
  20. #include "scan.h"
  21. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  22. {
  23. struct wpa_ssid *ssid;
  24. union wpa_event_data data;
  25. ssid = wpa_supplicant_get_ssid(wpa_s);
  26. if (ssid == NULL)
  27. return;
  28. if (wpa_s->current_ssid == NULL) {
  29. wpa_s->current_ssid = ssid;
  30. if (wpa_s->current_ssid != NULL)
  31. wpas_notify_network_changed(wpa_s);
  32. }
  33. wpa_supplicant_initiate_eapol(wpa_s);
  34. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  35. "network - generating associated event");
  36. os_memset(&data, 0, sizeof(data));
  37. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  38. }
  39. #ifdef CONFIG_WPS
  40. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  41. enum wps_request_type *req_type)
  42. {
  43. struct wpa_ssid *ssid;
  44. int wps = 0;
  45. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  46. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  47. continue;
  48. wps = 1;
  49. *req_type = wpas_wps_get_req_type(ssid);
  50. if (!ssid->eap.phase1)
  51. continue;
  52. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  53. return 2;
  54. }
  55. #ifdef CONFIG_P2P
  56. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) {
  57. wpa_s->wps->dev.p2p = 1;
  58. if (!wps) {
  59. wps = 1;
  60. *req_type = WPS_REQ_ENROLLEE_INFO;
  61. }
  62. }
  63. #endif /* CONFIG_P2P */
  64. return wps;
  65. }
  66. #endif /* CONFIG_WPS */
  67. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  68. {
  69. struct wpa_ssid *ssid = conf->ssid;
  70. int count = 0;
  71. while (ssid) {
  72. if (!ssid->disabled)
  73. count++;
  74. ssid = ssid->next;
  75. }
  76. return count;
  77. }
  78. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  79. struct wpa_ssid *ssid)
  80. {
  81. while (ssid) {
  82. if (!ssid->disabled)
  83. break;
  84. ssid = ssid->next;
  85. }
  86. /* ap_scan=2 mode - try to associate with each SSID. */
  87. if (ssid == NULL) {
  88. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  89. "end of scan list - go back to beginning");
  90. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  91. wpa_supplicant_req_scan(wpa_s, 0, 0);
  92. return;
  93. }
  94. if (ssid->next) {
  95. /* Continue from the next SSID on the next attempt. */
  96. wpa_s->prev_scan_ssid = ssid;
  97. } else {
  98. /* Start from the beginning of the SSID list. */
  99. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  100. }
  101. wpa_supplicant_associate(wpa_s, NULL, ssid);
  102. }
  103. static int int_array_len(const int *a)
  104. {
  105. int i;
  106. for (i = 0; a && a[i]; i++)
  107. ;
  108. return i;
  109. }
  110. static void int_array_concat(int **res, const int *a)
  111. {
  112. int reslen, alen, i;
  113. int *n;
  114. reslen = int_array_len(*res);
  115. alen = int_array_len(a);
  116. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  117. if (n == NULL) {
  118. os_free(*res);
  119. *res = NULL;
  120. return;
  121. }
  122. for (i = 0; i <= alen; i++)
  123. n[reslen + i] = a[i];
  124. *res = n;
  125. }
  126. static int freq_cmp(const void *a, const void *b)
  127. {
  128. int _a = *(int *) a;
  129. int _b = *(int *) b;
  130. if (_a == 0)
  131. return 1;
  132. if (_b == 0)
  133. return -1;
  134. return _a - _b;
  135. }
  136. static void int_array_sort_unique(int *a)
  137. {
  138. int alen;
  139. int i, j;
  140. if (a == NULL)
  141. return;
  142. alen = int_array_len(a);
  143. qsort(a, alen, sizeof(int), freq_cmp);
  144. i = 0;
  145. j = 1;
  146. while (a[i] && a[j]) {
  147. if (a[i] == a[j]) {
  148. j++;
  149. continue;
  150. }
  151. a[++i] = a[j++];
  152. }
  153. if (a[i])
  154. i++;
  155. a[i] = 0;
  156. }
  157. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  158. struct wpa_driver_scan_params *params)
  159. {
  160. int ret;
  161. wpa_supplicant_notify_scanning(wpa_s, 1);
  162. ret = wpa_drv_scan(wpa_s, params);
  163. if (ret) {
  164. wpa_supplicant_notify_scanning(wpa_s, 0);
  165. wpas_notify_scan_done(wpa_s, 0);
  166. } else {
  167. wpa_s->scan_runs++;
  168. wpa_s->normal_scans++;
  169. }
  170. return ret;
  171. }
  172. static void
  173. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  174. {
  175. struct wpa_supplicant *wpa_s = eloop_ctx;
  176. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  177. if (wpa_supplicant_req_sched_scan(wpa_s))
  178. wpa_supplicant_req_scan(wpa_s, 0, 0);
  179. }
  180. static void
  181. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  182. {
  183. struct wpa_supplicant *wpa_s = eloop_ctx;
  184. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  185. wpa_s->sched_scan_timed_out = 1;
  186. wpa_supplicant_cancel_sched_scan(wpa_s);
  187. }
  188. static int
  189. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  190. struct wpa_driver_scan_params *params,
  191. int interval)
  192. {
  193. int ret;
  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. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  281. {
  282. /* Optimize provisioning scan based on already known channel */
  283. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  284. wpa_s->wps_freq);
  285. params->freqs = os_zalloc(2 * sizeof(int));
  286. if (params->freqs)
  287. params->freqs[0] = wpa_s->wps_freq;
  288. wpa_s->known_wps_freq = 0; /* only do this once */
  289. }
  290. #endif /* CONFIG_WPS */
  291. }
  292. #ifdef CONFIG_INTERWORKING
  293. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  294. struct wpabuf *buf)
  295. {
  296. if (wpa_s->conf->interworking == 0)
  297. return;
  298. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  299. wpabuf_put_u8(buf, 4);
  300. wpabuf_put_u8(buf, 0x00);
  301. wpabuf_put_u8(buf, 0x00);
  302. wpabuf_put_u8(buf, 0x00);
  303. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  304. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  305. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  306. 1 + ETH_ALEN);
  307. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  308. /* No Venue Info */
  309. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  310. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  311. }
  312. #endif /* CONFIG_INTERWORKING */
  313. static struct wpabuf *
  314. wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
  315. struct wpa_driver_scan_params *params)
  316. {
  317. struct wpabuf *extra_ie = NULL;
  318. #ifdef CONFIG_WPS
  319. int wps = 0;
  320. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  321. #endif /* CONFIG_WPS */
  322. #ifdef CONFIG_INTERWORKING
  323. if (wpa_s->conf->interworking &&
  324. wpabuf_resize(&extra_ie, 100) == 0)
  325. wpas_add_interworking_elements(wpa_s, extra_ie);
  326. #endif /* CONFIG_INTERWORKING */
  327. #ifdef CONFIG_WPS
  328. wps = wpas_wps_in_use(wpa_s, &req_type);
  329. if (wps) {
  330. struct wpabuf *wps_ie;
  331. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  332. DEV_PW_DEFAULT,
  333. &wpa_s->wps->dev,
  334. wpa_s->wps->uuid, req_type,
  335. 0, NULL);
  336. if (wps_ie) {
  337. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  338. wpabuf_put_buf(extra_ie, wps_ie);
  339. wpabuf_free(wps_ie);
  340. }
  341. }
  342. #ifdef CONFIG_P2P
  343. if (wps) {
  344. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  345. if (wpabuf_resize(&extra_ie, ielen) == 0)
  346. wpas_p2p_scan_ie(wpa_s, extra_ie);
  347. }
  348. #endif /* CONFIG_P2P */
  349. #endif /* CONFIG_WPS */
  350. return extra_ie;
  351. }
  352. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  353. {
  354. struct wpa_supplicant *wpa_s = eloop_ctx;
  355. struct wpa_ssid *ssid;
  356. int scan_req = 0, ret;
  357. struct wpabuf *extra_ie;
  358. struct wpa_driver_scan_params params;
  359. size_t max_ssids;
  360. enum wpa_states prev_state;
  361. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  362. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  363. return;
  364. }
  365. if (wpa_s->disconnected && !wpa_s->scan_req) {
  366. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  367. return;
  368. }
  369. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  370. !wpa_s->scan_req) {
  371. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  372. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  373. return;
  374. }
  375. if (wpa_s->conf->ap_scan != 0 &&
  376. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  377. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  378. "overriding ap_scan configuration");
  379. wpa_s->conf->ap_scan = 0;
  380. wpas_notify_ap_scan_changed(wpa_s);
  381. }
  382. if (wpa_s->conf->ap_scan == 0) {
  383. wpa_supplicant_gen_assoc_event(wpa_s);
  384. return;
  385. }
  386. #ifdef CONFIG_P2P
  387. if (wpas_p2p_in_progress(wpa_s)) {
  388. if (wpa_s->wpa_state == WPA_SCANNING) {
  389. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  390. "while P2P operation is in progress");
  391. wpa_supplicant_req_scan(wpa_s, 5, 0);
  392. } else {
  393. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
  394. "P2P operation is in progress");
  395. }
  396. return;
  397. }
  398. #endif /* CONFIG_P2P */
  399. if (wpa_s->conf->ap_scan == 2)
  400. max_ssids = 1;
  401. else {
  402. max_ssids = wpa_s->max_scan_ssids;
  403. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  404. max_ssids = WPAS_MAX_SCAN_SSIDS;
  405. }
  406. scan_req = wpa_s->scan_req;
  407. wpa_s->scan_req = 0;
  408. os_memset(&params, 0, sizeof(params));
  409. prev_state = wpa_s->wpa_state;
  410. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  411. wpa_s->wpa_state == WPA_INACTIVE)
  412. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  413. if (scan_req != 2 && wpa_s->connect_without_scan) {
  414. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  415. if (ssid == wpa_s->connect_without_scan)
  416. break;
  417. }
  418. wpa_s->connect_without_scan = NULL;
  419. if (ssid) {
  420. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  421. "without scan step");
  422. wpa_supplicant_associate(wpa_s, NULL, ssid);
  423. return;
  424. }
  425. }
  426. #ifdef CONFIG_P2P
  427. if (wpa_s->p2p_in_provisioning && wpa_s->go_params) {
  428. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
  429. "P2P provisioning");
  430. params.ssids[0].ssid = wpa_s->go_params->ssid;
  431. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  432. params.num_ssids = 1;
  433. goto ssid_list_set;
  434. }
  435. #endif /* CONFIG_P2P */
  436. /* Find the starting point from which to continue scanning */
  437. ssid = wpa_s->conf->ssid;
  438. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  439. while (ssid) {
  440. if (ssid == wpa_s->prev_scan_ssid) {
  441. ssid = ssid->next;
  442. break;
  443. }
  444. ssid = ssid->next;
  445. }
  446. }
  447. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  448. wpa_s->connect_without_scan = NULL;
  449. wpa_s->prev_scan_wildcard = 0;
  450. wpa_supplicant_assoc_try(wpa_s, ssid);
  451. return;
  452. } else if (wpa_s->conf->ap_scan == 2) {
  453. /*
  454. * User-initiated scan request in ap_scan == 2; scan with
  455. * wildcard SSID.
  456. */
  457. ssid = NULL;
  458. } else {
  459. struct wpa_ssid *start = ssid, *tssid;
  460. int freqs_set = 0;
  461. if (ssid == NULL && max_ssids > 1)
  462. ssid = wpa_s->conf->ssid;
  463. while (ssid) {
  464. if (!ssid->disabled && ssid->scan_ssid) {
  465. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  466. ssid->ssid, ssid->ssid_len);
  467. params.ssids[params.num_ssids].ssid =
  468. ssid->ssid;
  469. params.ssids[params.num_ssids].ssid_len =
  470. ssid->ssid_len;
  471. params.num_ssids++;
  472. if (params.num_ssids + 1 >= max_ssids)
  473. break;
  474. }
  475. ssid = ssid->next;
  476. if (ssid == start)
  477. break;
  478. if (ssid == NULL && max_ssids > 1 &&
  479. start != wpa_s->conf->ssid)
  480. ssid = wpa_s->conf->ssid;
  481. }
  482. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  483. if (tssid->disabled)
  484. continue;
  485. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  486. int_array_concat(&params.freqs,
  487. tssid->scan_freq);
  488. } else {
  489. os_free(params.freqs);
  490. params.freqs = NULL;
  491. }
  492. freqs_set = 1;
  493. }
  494. int_array_sort_unique(params.freqs);
  495. }
  496. if (ssid && max_ssids == 1) {
  497. /*
  498. * If the driver is limited to 1 SSID at a time interleave
  499. * wildcard SSID scans with specific SSID scans to avoid
  500. * waiting a long time for a wildcard scan.
  501. */
  502. if (!wpa_s->prev_scan_wildcard) {
  503. params.ssids[0].ssid = NULL;
  504. params.ssids[0].ssid_len = 0;
  505. wpa_s->prev_scan_wildcard = 1;
  506. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  507. "wildcard SSID (Interleave with specific)");
  508. } else {
  509. wpa_s->prev_scan_ssid = ssid;
  510. wpa_s->prev_scan_wildcard = 0;
  511. wpa_dbg(wpa_s, MSG_DEBUG,
  512. "Starting AP scan for specific SSID: %s",
  513. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  514. }
  515. } else if (ssid) {
  516. /* max_ssids > 1 */
  517. wpa_s->prev_scan_ssid = ssid;
  518. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  519. "the scan request");
  520. params.num_ssids++;
  521. } else {
  522. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  523. params.num_ssids++;
  524. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  525. "SSID");
  526. }
  527. #ifdef CONFIG_P2P
  528. ssid_list_set:
  529. #endif /* CONFIG_P2P */
  530. wpa_supplicant_optimize_freqs(wpa_s, &params);
  531. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  532. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  533. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  534. "generated frequency list");
  535. params.freqs = wpa_s->next_scan_freqs;
  536. } else
  537. os_free(wpa_s->next_scan_freqs);
  538. wpa_s->next_scan_freqs = NULL;
  539. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  540. wpa_s->conf, &params.num_filter_ssids);
  541. if (extra_ie) {
  542. params.extra_ies = wpabuf_head(extra_ie);
  543. params.extra_ies_len = wpabuf_len(extra_ie);
  544. }
  545. #ifdef CONFIG_P2P
  546. if (wpa_s->p2p_in_provisioning) {
  547. /*
  548. * The interface may not yet be in P2P mode, so we have to
  549. * explicitly request P2P probe to disable CCK rates.
  550. */
  551. params.p2p_probe = 1;
  552. }
  553. #endif /* CONFIG_P2P */
  554. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  555. wpabuf_free(extra_ie);
  556. os_free(params.freqs);
  557. os_free(params.filter_ssids);
  558. if (ret) {
  559. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  560. if (prev_state != wpa_s->wpa_state)
  561. wpa_supplicant_set_state(wpa_s, prev_state);
  562. wpa_supplicant_req_scan(wpa_s, 1, 0);
  563. }
  564. }
  565. /**
  566. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  567. * @wpa_s: Pointer to wpa_supplicant data
  568. * @sec: Number of seconds after which to scan
  569. * @usec: Number of microseconds after which to scan
  570. *
  571. * This function is used to schedule a scan for neighboring access points after
  572. * the specified time.
  573. */
  574. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  575. {
  576. /* If there's at least one network that should be specifically scanned
  577. * then don't cancel the scan and reschedule. Some drivers do
  578. * background scanning which generates frequent scan results, and that
  579. * causes the specific SSID scan to get continually pushed back and
  580. * never happen, which causes hidden APs to never get probe-scanned.
  581. */
  582. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  583. wpa_s->conf->ap_scan == 1) {
  584. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  585. while (ssid) {
  586. if (!ssid->disabled && ssid->scan_ssid)
  587. break;
  588. ssid = ssid->next;
  589. }
  590. if (ssid) {
  591. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  592. "ensure that specific SSID scans occur");
  593. return;
  594. }
  595. }
  596. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  597. sec, usec);
  598. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  599. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  600. }
  601. /**
  602. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  603. * @wpa_s: Pointer to wpa_supplicant data
  604. * @sec: Number of seconds after which to scan
  605. * @usec: Number of microseconds after which to scan
  606. *
  607. * This function is used to schedule periodic scans for neighboring
  608. * access points after the specified time.
  609. */
  610. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  611. int sec, int usec)
  612. {
  613. if (!wpa_s->sched_scan_supported)
  614. return -1;
  615. eloop_register_timeout(sec, usec,
  616. wpa_supplicant_delayed_sched_scan_timeout,
  617. wpa_s, NULL);
  618. return 0;
  619. }
  620. /**
  621. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  622. * @wpa_s: Pointer to wpa_supplicant data
  623. *
  624. * This function is used to schedule periodic scans for neighboring
  625. * access points repeating the scan continuously.
  626. */
  627. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  628. {
  629. struct wpa_driver_scan_params params;
  630. enum wpa_states prev_state;
  631. struct wpa_ssid *ssid;
  632. struct wpabuf *wps_ie = NULL;
  633. int ret;
  634. unsigned int max_sched_scan_ssids;
  635. int wildcard = 0;
  636. int need_ssids;
  637. if (!wpa_s->sched_scan_supported)
  638. return -1;
  639. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  640. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  641. else
  642. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  643. if (max_sched_scan_ssids < 1)
  644. return -1;
  645. if (wpa_s->sched_scanning) {
  646. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  647. return 0;
  648. }
  649. need_ssids = 0;
  650. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  651. if (!ssid->disabled && !ssid->scan_ssid) {
  652. /* Use wildcard SSID to find this network */
  653. wildcard = 1;
  654. } else if (!ssid->disabled && ssid->ssid_len)
  655. need_ssids++;
  656. #ifdef CONFIG_WPS
  657. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  658. /*
  659. * Normal scan is more reliable and faster for WPS
  660. * operations and since these are for short periods of
  661. * time, the benefit of trying to use sched_scan would
  662. * be limited.
  663. */
  664. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  665. "sched_scan for WPS");
  666. return -1;
  667. }
  668. #endif /* CONFIG_WPS */
  669. }
  670. if (wildcard)
  671. need_ssids++;
  672. if (wpa_s->normal_scans < 3 &&
  673. (need_ssids <= wpa_s->max_scan_ssids ||
  674. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  675. /*
  676. * When normal scan can speed up operations, use that for the
  677. * first operations before starting the sched_scan to allow
  678. * user space sleep more. We do this only if the normal scan
  679. * has functionality that is suitable for this or if the
  680. * sched_scan does not have better support for multiple SSIDs.
  681. */
  682. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  683. "sched_scan for initial scans (normal_scans=%d)",
  684. wpa_s->normal_scans);
  685. return -1;
  686. }
  687. os_memset(&params, 0, sizeof(params));
  688. /* If we can't allocate space for the filters, we just don't filter */
  689. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  690. sizeof(struct wpa_driver_scan_filter));
  691. prev_state = wpa_s->wpa_state;
  692. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  693. wpa_s->wpa_state == WPA_INACTIVE)
  694. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  695. /* Find the starting point from which to continue scanning */
  696. ssid = wpa_s->conf->ssid;
  697. if (wpa_s->prev_sched_ssid) {
  698. while (ssid) {
  699. if (ssid == wpa_s->prev_sched_ssid) {
  700. ssid = ssid->next;
  701. break;
  702. }
  703. ssid = ssid->next;
  704. }
  705. }
  706. if (!ssid || !wpa_s->prev_sched_ssid) {
  707. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  708. wpa_s->sched_scan_interval = 10;
  709. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  710. wpa_s->first_sched_scan = 1;
  711. ssid = wpa_s->conf->ssid;
  712. wpa_s->prev_sched_ssid = ssid;
  713. }
  714. if (wildcard) {
  715. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  716. params.num_ssids++;
  717. }
  718. while (ssid) {
  719. if (ssid->disabled)
  720. goto next;
  721. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  722. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  723. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  724. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  725. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  726. ssid->ssid, ssid->ssid_len);
  727. params.filter_ssids[params.num_filter_ssids].ssid_len =
  728. ssid->ssid_len;
  729. params.num_filter_ssids++;
  730. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  731. {
  732. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  733. "filter for sched_scan - drop filter");
  734. os_free(params.filter_ssids);
  735. params.filter_ssids = NULL;
  736. params.num_filter_ssids = 0;
  737. }
  738. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  739. if (params.num_ssids == max_sched_scan_ssids)
  740. break; /* only room for broadcast SSID */
  741. wpa_dbg(wpa_s, MSG_DEBUG,
  742. "add to active scan ssid: %s",
  743. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  744. params.ssids[params.num_ssids].ssid =
  745. ssid->ssid;
  746. params.ssids[params.num_ssids].ssid_len =
  747. ssid->ssid_len;
  748. params.num_ssids++;
  749. if (params.num_ssids >= max_sched_scan_ssids) {
  750. wpa_s->prev_sched_ssid = ssid;
  751. break;
  752. }
  753. }
  754. next:
  755. wpa_s->prev_sched_ssid = ssid;
  756. ssid = ssid->next;
  757. }
  758. if (params.num_filter_ssids == 0) {
  759. os_free(params.filter_ssids);
  760. params.filter_ssids = NULL;
  761. }
  762. if (wpa_s->wps)
  763. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  764. if (ssid || !wpa_s->first_sched_scan) {
  765. wpa_dbg(wpa_s, MSG_DEBUG,
  766. "Starting sched scan: interval %d (no timeout)",
  767. wpa_s->sched_scan_interval);
  768. } else {
  769. wpa_dbg(wpa_s, MSG_DEBUG,
  770. "Starting sched scan: interval %d timeout %d",
  771. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  772. }
  773. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  774. wpa_s->sched_scan_interval);
  775. wpabuf_free(wps_ie);
  776. os_free(params.filter_ssids);
  777. if (ret) {
  778. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  779. if (prev_state != wpa_s->wpa_state)
  780. wpa_supplicant_set_state(wpa_s, prev_state);
  781. return ret;
  782. }
  783. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  784. if (ssid || !wpa_s->first_sched_scan) {
  785. wpa_s->sched_scan_timed_out = 0;
  786. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  787. wpa_supplicant_sched_scan_timeout,
  788. wpa_s, NULL);
  789. wpa_s->first_sched_scan = 0;
  790. wpa_s->sched_scan_timeout /= 2;
  791. wpa_s->sched_scan_interval *= 2;
  792. }
  793. return 0;
  794. }
  795. /**
  796. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  797. * @wpa_s: Pointer to wpa_supplicant data
  798. *
  799. * This function is used to cancel a scan request scheduled with
  800. * wpa_supplicant_req_scan().
  801. */
  802. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  803. {
  804. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  805. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  806. }
  807. /**
  808. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  809. * @wpa_s: Pointer to wpa_supplicant data
  810. *
  811. * This function is used to stop a periodic scheduled scan.
  812. */
  813. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  814. {
  815. if (!wpa_s->sched_scanning)
  816. return;
  817. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  818. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  819. wpa_supplicant_stop_sched_scan(wpa_s);
  820. }
  821. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  822. int scanning)
  823. {
  824. if (wpa_s->scanning != scanning) {
  825. wpa_s->scanning = scanning;
  826. wpas_notify_scanning(wpa_s);
  827. }
  828. }
  829. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  830. {
  831. int rate = 0;
  832. const u8 *ie;
  833. int i;
  834. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  835. for (i = 0; ie && i < ie[1]; i++) {
  836. if ((ie[i + 2] & 0x7f) > rate)
  837. rate = ie[i + 2] & 0x7f;
  838. }
  839. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  840. for (i = 0; ie && i < ie[1]; i++) {
  841. if ((ie[i + 2] & 0x7f) > rate)
  842. rate = ie[i + 2] & 0x7f;
  843. }
  844. return rate;
  845. }
  846. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  847. {
  848. const u8 *end, *pos;
  849. pos = (const u8 *) (res + 1);
  850. end = pos + res->ie_len;
  851. while (pos + 1 < end) {
  852. if (pos + 2 + pos[1] > end)
  853. break;
  854. if (pos[0] == ie)
  855. return pos;
  856. pos += 2 + pos[1];
  857. }
  858. return NULL;
  859. }
  860. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  861. u32 vendor_type)
  862. {
  863. const u8 *end, *pos;
  864. pos = (const u8 *) (res + 1);
  865. end = pos + res->ie_len;
  866. while (pos + 1 < end) {
  867. if (pos + 2 + pos[1] > end)
  868. break;
  869. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  870. vendor_type == WPA_GET_BE32(&pos[2]))
  871. return pos;
  872. pos += 2 + pos[1];
  873. }
  874. return NULL;
  875. }
  876. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  877. u32 vendor_type)
  878. {
  879. struct wpabuf *buf;
  880. const u8 *end, *pos;
  881. buf = wpabuf_alloc(res->ie_len);
  882. if (buf == NULL)
  883. return NULL;
  884. pos = (const u8 *) (res + 1);
  885. end = pos + res->ie_len;
  886. while (pos + 1 < end) {
  887. if (pos + 2 + pos[1] > end)
  888. break;
  889. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  890. vendor_type == WPA_GET_BE32(&pos[2]))
  891. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  892. pos += 2 + pos[1];
  893. }
  894. if (wpabuf_len(buf) == 0) {
  895. wpabuf_free(buf);
  896. buf = NULL;
  897. }
  898. return buf;
  899. }
  900. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  901. const struct wpa_scan_res *res, u32 vendor_type)
  902. {
  903. struct wpabuf *buf;
  904. const u8 *end, *pos;
  905. if (res->beacon_ie_len == 0)
  906. return NULL;
  907. buf = wpabuf_alloc(res->beacon_ie_len);
  908. if (buf == NULL)
  909. return NULL;
  910. pos = (const u8 *) (res + 1);
  911. pos += res->ie_len;
  912. end = pos + res->beacon_ie_len;
  913. while (pos + 1 < end) {
  914. if (pos + 2 + pos[1] > end)
  915. break;
  916. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  917. vendor_type == WPA_GET_BE32(&pos[2]))
  918. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  919. pos += 2 + pos[1];
  920. }
  921. if (wpabuf_len(buf) == 0) {
  922. wpabuf_free(buf);
  923. buf = NULL;
  924. }
  925. return buf;
  926. }
  927. /*
  928. * Channels with a great SNR can operate at full rate. What is a great SNR?
  929. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  930. * rule of thumb is that any SNR above 20 is good." This one
  931. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  932. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  933. * conservative value.
  934. */
  935. #define GREAT_SNR 30
  936. /* Compare function for sorting scan results. Return >0 if @b is considered
  937. * better. */
  938. static int wpa_scan_result_compar(const void *a, const void *b)
  939. {
  940. #define IS_5GHZ(n) (n > 4000)
  941. #define MIN(a,b) a < b ? a : b
  942. struct wpa_scan_res **_wa = (void *) a;
  943. struct wpa_scan_res **_wb = (void *) b;
  944. struct wpa_scan_res *wa = *_wa;
  945. struct wpa_scan_res *wb = *_wb;
  946. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  947. int snr_a, snr_b;
  948. /* WPA/WPA2 support preferred */
  949. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  950. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  951. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  952. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  953. if (wpa_b && !wpa_a)
  954. return 1;
  955. if (!wpa_b && wpa_a)
  956. return -1;
  957. /* privacy support preferred */
  958. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  959. (wb->caps & IEEE80211_CAP_PRIVACY))
  960. return 1;
  961. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  962. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  963. return -1;
  964. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  965. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  966. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  967. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  968. } else {
  969. /* Not suitable information to calculate SNR, so use level */
  970. snr_a = wa->level;
  971. snr_b = wb->level;
  972. }
  973. /* best/max rate preferred if SNR close enough */
  974. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  975. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  976. maxrate_a = wpa_scan_get_max_rate(wa);
  977. maxrate_b = wpa_scan_get_max_rate(wb);
  978. if (maxrate_a != maxrate_b)
  979. return maxrate_b - maxrate_a;
  980. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  981. return IS_5GHZ(wa->freq) ? -1 : 1;
  982. }
  983. /* use freq for channel preference */
  984. /* all things being equal, use SNR; if SNRs are
  985. * identical, use quality values since some drivers may only report
  986. * that value and leave the signal level zero */
  987. if (snr_b == snr_a)
  988. return wb->qual - wa->qual;
  989. return snr_b - snr_a;
  990. #undef MIN
  991. #undef IS_5GHZ
  992. }
  993. #ifdef CONFIG_WPS
  994. /* Compare function for sorting scan results when searching a WPS AP for
  995. * provisioning. Return >0 if @b is considered better. */
  996. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  997. {
  998. struct wpa_scan_res **_wa = (void *) a;
  999. struct wpa_scan_res **_wb = (void *) b;
  1000. struct wpa_scan_res *wa = *_wa;
  1001. struct wpa_scan_res *wb = *_wb;
  1002. int uses_wps_a, uses_wps_b;
  1003. struct wpabuf *wps_a, *wps_b;
  1004. int res;
  1005. /* Optimization - check WPS IE existence before allocated memory and
  1006. * doing full reassembly. */
  1007. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1008. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1009. if (uses_wps_a && !uses_wps_b)
  1010. return -1;
  1011. if (!uses_wps_a && uses_wps_b)
  1012. return 1;
  1013. if (uses_wps_a && uses_wps_b) {
  1014. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1015. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1016. res = wps_ap_priority_compar(wps_a, wps_b);
  1017. wpabuf_free(wps_a);
  1018. wpabuf_free(wps_b);
  1019. if (res)
  1020. return res;
  1021. }
  1022. /*
  1023. * Do not use current AP security policy as a sorting criteria during
  1024. * WPS provisioning step since the AP may get reconfigured at the
  1025. * completion of provisioning.
  1026. */
  1027. /* all things being equal, use signal level; if signal levels are
  1028. * identical, use quality values since some drivers may only report
  1029. * that value and leave the signal level zero */
  1030. if (wb->level == wa->level)
  1031. return wb->qual - wa->qual;
  1032. return wb->level - wa->level;
  1033. }
  1034. #endif /* CONFIG_WPS */
  1035. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1036. {
  1037. #ifndef CONFIG_NO_STDOUT_DEBUG
  1038. size_t i;
  1039. if (scan_res->res == NULL || scan_res->num == 0)
  1040. return;
  1041. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1042. for (i = 0; i < scan_res->num; i++) {
  1043. struct wpa_scan_res *r = scan_res->res[i];
  1044. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1045. == WPA_SCAN_LEVEL_DBM) {
  1046. int snr = r->level - r->noise;
  1047. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1048. "noise=%d level=%d snr=%d%s flags=0x%x",
  1049. MAC2STR(r->bssid), r->freq, r->qual,
  1050. r->noise, r->level, snr,
  1051. snr >= GREAT_SNR ? "*" : "", r->flags);
  1052. } else {
  1053. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1054. "noise=%d level=%d flags=0x%x",
  1055. MAC2STR(r->bssid), r->freq, r->qual,
  1056. r->noise, r->level, r->flags);
  1057. }
  1058. }
  1059. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1060. }
  1061. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1062. const u8 *bssid)
  1063. {
  1064. size_t i;
  1065. if (wpa_s->bssid_filter == NULL)
  1066. return 1;
  1067. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1068. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1069. ETH_ALEN) == 0)
  1070. return 1;
  1071. }
  1072. return 0;
  1073. }
  1074. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1075. struct wpa_scan_results *res)
  1076. {
  1077. size_t i, j;
  1078. if (wpa_s->bssid_filter == NULL)
  1079. return;
  1080. for (i = 0, j = 0; i < res->num; i++) {
  1081. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1082. res->res[i]->bssid)) {
  1083. res->res[j++] = res->res[i];
  1084. } else {
  1085. os_free(res->res[i]);
  1086. res->res[i] = NULL;
  1087. }
  1088. }
  1089. if (res->num != j) {
  1090. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1091. (int) (res->num - j));
  1092. res->num = j;
  1093. }
  1094. }
  1095. /**
  1096. * wpa_supplicant_get_scan_results - Get scan results
  1097. * @wpa_s: Pointer to wpa_supplicant data
  1098. * @info: Information about what was scanned or %NULL if not available
  1099. * @new_scan: Whether a new scan was performed
  1100. * Returns: Scan results, %NULL on failure
  1101. *
  1102. * This function request the current scan results from the driver and updates
  1103. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1104. * results with wpa_scan_results_free().
  1105. */
  1106. struct wpa_scan_results *
  1107. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1108. struct scan_info *info, int new_scan)
  1109. {
  1110. struct wpa_scan_results *scan_res;
  1111. size_t i;
  1112. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1113. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1114. if (scan_res == NULL) {
  1115. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1116. return NULL;
  1117. }
  1118. filter_scan_res(wpa_s, scan_res);
  1119. #ifdef CONFIG_WPS
  1120. if (wpas_wps_in_progress(wpa_s)) {
  1121. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1122. "provisioning rules");
  1123. compar = wpa_scan_result_wps_compar;
  1124. }
  1125. #endif /* CONFIG_WPS */
  1126. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1127. compar);
  1128. dump_scan_res(scan_res);
  1129. wpa_bss_update_start(wpa_s);
  1130. for (i = 0; i < scan_res->num; i++)
  1131. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1132. wpa_bss_update_end(wpa_s, info, new_scan);
  1133. return scan_res;
  1134. }
  1135. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1136. {
  1137. struct wpa_scan_results *scan_res;
  1138. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1139. if (scan_res == NULL)
  1140. return -1;
  1141. wpa_scan_results_free(scan_res);
  1142. return 0;
  1143. }