scan.c 30 KB

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