scan.c 32 KB

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