scan.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  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->show_group_started) &&
  428. wpa_s->go_params) {
  429. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
  430. "P2P group formation");
  431. params.ssids[0].ssid = wpa_s->go_params->ssid;
  432. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  433. params.num_ssids = 1;
  434. goto ssid_list_set;
  435. }
  436. #endif /* CONFIG_P2P */
  437. /* Find the starting point from which to continue scanning */
  438. ssid = wpa_s->conf->ssid;
  439. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  440. while (ssid) {
  441. if (ssid == wpa_s->prev_scan_ssid) {
  442. ssid = ssid->next;
  443. break;
  444. }
  445. ssid = ssid->next;
  446. }
  447. }
  448. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  449. wpa_s->connect_without_scan = NULL;
  450. wpa_s->prev_scan_wildcard = 0;
  451. wpa_supplicant_assoc_try(wpa_s, ssid);
  452. return;
  453. } else if (wpa_s->conf->ap_scan == 2) {
  454. /*
  455. * User-initiated scan request in ap_scan == 2; scan with
  456. * wildcard SSID.
  457. */
  458. ssid = NULL;
  459. } else {
  460. struct wpa_ssid *start = ssid, *tssid;
  461. int freqs_set = 0;
  462. if (ssid == NULL && max_ssids > 1)
  463. ssid = wpa_s->conf->ssid;
  464. while (ssid) {
  465. if (!ssid->disabled && ssid->scan_ssid) {
  466. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  467. ssid->ssid, ssid->ssid_len);
  468. params.ssids[params.num_ssids].ssid =
  469. ssid->ssid;
  470. params.ssids[params.num_ssids].ssid_len =
  471. ssid->ssid_len;
  472. params.num_ssids++;
  473. if (params.num_ssids + 1 >= max_ssids)
  474. break;
  475. }
  476. ssid = ssid->next;
  477. if (ssid == start)
  478. break;
  479. if (ssid == NULL && max_ssids > 1 &&
  480. start != wpa_s->conf->ssid)
  481. ssid = wpa_s->conf->ssid;
  482. }
  483. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  484. if (tssid->disabled)
  485. continue;
  486. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  487. int_array_concat(&params.freqs,
  488. tssid->scan_freq);
  489. } else {
  490. os_free(params.freqs);
  491. params.freqs = NULL;
  492. }
  493. freqs_set = 1;
  494. }
  495. int_array_sort_unique(params.freqs);
  496. }
  497. if (ssid && max_ssids == 1) {
  498. /*
  499. * If the driver is limited to 1 SSID at a time interleave
  500. * wildcard SSID scans with specific SSID scans to avoid
  501. * waiting a long time for a wildcard scan.
  502. */
  503. if (!wpa_s->prev_scan_wildcard) {
  504. params.ssids[0].ssid = NULL;
  505. params.ssids[0].ssid_len = 0;
  506. wpa_s->prev_scan_wildcard = 1;
  507. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  508. "wildcard SSID (Interleave with specific)");
  509. } else {
  510. wpa_s->prev_scan_ssid = ssid;
  511. wpa_s->prev_scan_wildcard = 0;
  512. wpa_dbg(wpa_s, MSG_DEBUG,
  513. "Starting AP scan for specific SSID: %s",
  514. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  515. }
  516. } else if (ssid) {
  517. /* max_ssids > 1 */
  518. wpa_s->prev_scan_ssid = ssid;
  519. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  520. "the scan request");
  521. params.num_ssids++;
  522. } else {
  523. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  524. params.num_ssids++;
  525. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  526. "SSID");
  527. }
  528. #ifdef CONFIG_P2P
  529. ssid_list_set:
  530. #endif /* CONFIG_P2P */
  531. wpa_supplicant_optimize_freqs(wpa_s, &params);
  532. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  533. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  534. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  535. "generated frequency list");
  536. params.freqs = wpa_s->next_scan_freqs;
  537. } else
  538. os_free(wpa_s->next_scan_freqs);
  539. wpa_s->next_scan_freqs = NULL;
  540. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  541. wpa_s->conf, &params.num_filter_ssids);
  542. if (extra_ie) {
  543. params.extra_ies = wpabuf_head(extra_ie);
  544. params.extra_ies_len = wpabuf_len(extra_ie);
  545. }
  546. #ifdef CONFIG_P2P
  547. if (wpa_s->p2p_in_provisioning ||
  548. (wpa_s->show_group_started && wpa_s->go_params)) {
  549. /*
  550. * The interface may not yet be in P2P mode, so we have to
  551. * explicitly request P2P probe to disable CCK rates.
  552. */
  553. params.p2p_probe = 1;
  554. }
  555. #endif /* CONFIG_P2P */
  556. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  557. wpabuf_free(extra_ie);
  558. os_free(params.freqs);
  559. os_free(params.filter_ssids);
  560. if (ret) {
  561. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  562. if (prev_state != wpa_s->wpa_state)
  563. wpa_supplicant_set_state(wpa_s, prev_state);
  564. wpa_supplicant_req_scan(wpa_s, 1, 0);
  565. }
  566. }
  567. /**
  568. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  569. * @wpa_s: Pointer to wpa_supplicant data
  570. * @sec: Number of seconds after which to scan
  571. * @usec: Number of microseconds after which to scan
  572. *
  573. * This function is used to schedule a scan for neighboring access points after
  574. * the specified time.
  575. */
  576. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  577. {
  578. /* If there's at least one network that should be specifically scanned
  579. * then don't cancel the scan and reschedule. Some drivers do
  580. * background scanning which generates frequent scan results, and that
  581. * causes the specific SSID scan to get continually pushed back and
  582. * never happen, which causes hidden APs to never get probe-scanned.
  583. */
  584. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  585. wpa_s->conf->ap_scan == 1) {
  586. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  587. while (ssid) {
  588. if (!ssid->disabled && ssid->scan_ssid)
  589. break;
  590. ssid = ssid->next;
  591. }
  592. if (ssid) {
  593. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  594. "ensure that specific SSID scans occur");
  595. return;
  596. }
  597. }
  598. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  599. sec, usec);
  600. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  601. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  602. }
  603. /**
  604. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  605. * @wpa_s: Pointer to wpa_supplicant data
  606. * @sec: Number of seconds after which to scan
  607. * @usec: Number of microseconds after which to scan
  608. *
  609. * This function is used to schedule periodic scans for neighboring
  610. * access points after the specified time.
  611. */
  612. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  613. int sec, int usec)
  614. {
  615. if (!wpa_s->sched_scan_supported)
  616. return -1;
  617. eloop_register_timeout(sec, usec,
  618. wpa_supplicant_delayed_sched_scan_timeout,
  619. wpa_s, NULL);
  620. return 0;
  621. }
  622. /**
  623. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  624. * @wpa_s: Pointer to wpa_supplicant data
  625. *
  626. * This function is used to schedule periodic scans for neighboring
  627. * access points repeating the scan continuously.
  628. */
  629. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  630. {
  631. struct wpa_driver_scan_params params;
  632. enum wpa_states prev_state;
  633. struct wpa_ssid *ssid;
  634. struct wpabuf *wps_ie = NULL;
  635. int ret;
  636. unsigned int max_sched_scan_ssids;
  637. int wildcard = 0;
  638. int need_ssids;
  639. if (!wpa_s->sched_scan_supported)
  640. return -1;
  641. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  642. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  643. else
  644. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  645. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  646. return -1;
  647. if (wpa_s->sched_scanning) {
  648. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  649. return 0;
  650. }
  651. need_ssids = 0;
  652. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  653. if (!ssid->disabled && !ssid->scan_ssid) {
  654. /* Use wildcard SSID to find this network */
  655. wildcard = 1;
  656. } else if (!ssid->disabled && ssid->ssid_len)
  657. need_ssids++;
  658. #ifdef CONFIG_WPS
  659. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  660. /*
  661. * Normal scan is more reliable and faster for WPS
  662. * operations and since these are for short periods of
  663. * time, the benefit of trying to use sched_scan would
  664. * be limited.
  665. */
  666. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  667. "sched_scan for WPS");
  668. return -1;
  669. }
  670. #endif /* CONFIG_WPS */
  671. }
  672. if (wildcard)
  673. need_ssids++;
  674. if (wpa_s->normal_scans < 3 &&
  675. (need_ssids <= wpa_s->max_scan_ssids ||
  676. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  677. /*
  678. * When normal scan can speed up operations, use that for the
  679. * first operations before starting the sched_scan to allow
  680. * user space sleep more. We do this only if the normal scan
  681. * has functionality that is suitable for this or if the
  682. * sched_scan does not have better support for multiple SSIDs.
  683. */
  684. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  685. "sched_scan for initial scans (normal_scans=%d)",
  686. wpa_s->normal_scans);
  687. return -1;
  688. }
  689. os_memset(&params, 0, sizeof(params));
  690. /* If we can't allocate space for the filters, we just don't filter */
  691. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  692. sizeof(struct wpa_driver_scan_filter));
  693. prev_state = wpa_s->wpa_state;
  694. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  695. wpa_s->wpa_state == WPA_INACTIVE)
  696. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  697. /* Find the starting point from which to continue scanning */
  698. ssid = wpa_s->conf->ssid;
  699. if (wpa_s->prev_sched_ssid) {
  700. while (ssid) {
  701. if (ssid == wpa_s->prev_sched_ssid) {
  702. ssid = ssid->next;
  703. break;
  704. }
  705. ssid = ssid->next;
  706. }
  707. }
  708. if (!ssid || !wpa_s->prev_sched_ssid) {
  709. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  710. wpa_s->sched_scan_interval = 10;
  711. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  712. wpa_s->first_sched_scan = 1;
  713. ssid = wpa_s->conf->ssid;
  714. wpa_s->prev_sched_ssid = ssid;
  715. }
  716. if (wildcard) {
  717. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  718. params.num_ssids++;
  719. }
  720. while (ssid) {
  721. if (ssid->disabled)
  722. goto next;
  723. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  724. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  725. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  726. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  727. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  728. ssid->ssid, ssid->ssid_len);
  729. params.filter_ssids[params.num_filter_ssids].ssid_len =
  730. ssid->ssid_len;
  731. params.num_filter_ssids++;
  732. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  733. {
  734. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  735. "filter for sched_scan - drop filter");
  736. os_free(params.filter_ssids);
  737. params.filter_ssids = NULL;
  738. params.num_filter_ssids = 0;
  739. }
  740. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  741. if (params.num_ssids == max_sched_scan_ssids)
  742. break; /* only room for broadcast SSID */
  743. wpa_dbg(wpa_s, MSG_DEBUG,
  744. "add to active scan ssid: %s",
  745. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  746. params.ssids[params.num_ssids].ssid =
  747. ssid->ssid;
  748. params.ssids[params.num_ssids].ssid_len =
  749. ssid->ssid_len;
  750. params.num_ssids++;
  751. if (params.num_ssids >= max_sched_scan_ssids) {
  752. wpa_s->prev_sched_ssid = ssid;
  753. do {
  754. ssid = ssid->next;
  755. } while (ssid &&
  756. (ssid->disabled || !ssid->scan_ssid));
  757. break;
  758. }
  759. }
  760. next:
  761. wpa_s->prev_sched_ssid = ssid;
  762. ssid = ssid->next;
  763. }
  764. if (params.num_filter_ssids == 0) {
  765. os_free(params.filter_ssids);
  766. params.filter_ssids = NULL;
  767. }
  768. if (wpa_s->wps)
  769. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  770. if (ssid || !wpa_s->first_sched_scan) {
  771. wpa_dbg(wpa_s, MSG_DEBUG,
  772. "Starting sched scan: interval %d timeout %d",
  773. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  774. } else {
  775. wpa_dbg(wpa_s, MSG_DEBUG,
  776. "Starting sched scan: interval %d (no timeout)",
  777. wpa_s->sched_scan_interval);
  778. }
  779. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  780. wpa_s->sched_scan_interval);
  781. wpabuf_free(wps_ie);
  782. os_free(params.filter_ssids);
  783. if (ret) {
  784. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  785. if (prev_state != wpa_s->wpa_state)
  786. wpa_supplicant_set_state(wpa_s, prev_state);
  787. return ret;
  788. }
  789. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  790. if (ssid || !wpa_s->first_sched_scan) {
  791. wpa_s->sched_scan_timed_out = 0;
  792. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  793. wpa_supplicant_sched_scan_timeout,
  794. wpa_s, NULL);
  795. wpa_s->first_sched_scan = 0;
  796. wpa_s->sched_scan_timeout /= 2;
  797. wpa_s->sched_scan_interval *= 2;
  798. }
  799. return 0;
  800. }
  801. /**
  802. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  803. * @wpa_s: Pointer to wpa_supplicant data
  804. *
  805. * This function is used to cancel a scan request scheduled with
  806. * wpa_supplicant_req_scan().
  807. */
  808. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  809. {
  810. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  811. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  812. }
  813. /**
  814. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  815. * @wpa_s: Pointer to wpa_supplicant data
  816. *
  817. * This function is used to stop a periodic scheduled scan.
  818. */
  819. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  820. {
  821. if (!wpa_s->sched_scanning)
  822. return;
  823. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  824. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  825. wpa_supplicant_stop_sched_scan(wpa_s);
  826. }
  827. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  828. int scanning)
  829. {
  830. if (wpa_s->scanning != scanning) {
  831. wpa_s->scanning = scanning;
  832. wpas_notify_scanning(wpa_s);
  833. }
  834. }
  835. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  836. {
  837. int rate = 0;
  838. const u8 *ie;
  839. int i;
  840. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  841. for (i = 0; ie && i < ie[1]; i++) {
  842. if ((ie[i + 2] & 0x7f) > rate)
  843. rate = ie[i + 2] & 0x7f;
  844. }
  845. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  846. for (i = 0; ie && i < ie[1]; i++) {
  847. if ((ie[i + 2] & 0x7f) > rate)
  848. rate = ie[i + 2] & 0x7f;
  849. }
  850. return rate;
  851. }
  852. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  853. {
  854. const u8 *end, *pos;
  855. pos = (const u8 *) (res + 1);
  856. end = pos + res->ie_len;
  857. while (pos + 1 < end) {
  858. if (pos + 2 + pos[1] > end)
  859. break;
  860. if (pos[0] == ie)
  861. return pos;
  862. pos += 2 + pos[1];
  863. }
  864. return NULL;
  865. }
  866. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  867. u32 vendor_type)
  868. {
  869. const u8 *end, *pos;
  870. pos = (const u8 *) (res + 1);
  871. end = pos + res->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. return pos;
  878. pos += 2 + pos[1];
  879. }
  880. return NULL;
  881. }
  882. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  883. u32 vendor_type)
  884. {
  885. struct wpabuf *buf;
  886. const u8 *end, *pos;
  887. buf = wpabuf_alloc(res->ie_len);
  888. if (buf == NULL)
  889. return NULL;
  890. pos = (const u8 *) (res + 1);
  891. end = pos + res->ie_len;
  892. while (pos + 1 < end) {
  893. if (pos + 2 + pos[1] > end)
  894. break;
  895. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  896. vendor_type == WPA_GET_BE32(&pos[2]))
  897. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  898. pos += 2 + pos[1];
  899. }
  900. if (wpabuf_len(buf) == 0) {
  901. wpabuf_free(buf);
  902. buf = NULL;
  903. }
  904. return buf;
  905. }
  906. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  907. const struct wpa_scan_res *res, u32 vendor_type)
  908. {
  909. struct wpabuf *buf;
  910. const u8 *end, *pos;
  911. if (res->beacon_ie_len == 0)
  912. return NULL;
  913. buf = wpabuf_alloc(res->beacon_ie_len);
  914. if (buf == NULL)
  915. return NULL;
  916. pos = (const u8 *) (res + 1);
  917. pos += res->ie_len;
  918. end = pos + res->beacon_ie_len;
  919. while (pos + 1 < end) {
  920. if (pos + 2 + pos[1] > end)
  921. break;
  922. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  923. vendor_type == WPA_GET_BE32(&pos[2]))
  924. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  925. pos += 2 + pos[1];
  926. }
  927. if (wpabuf_len(buf) == 0) {
  928. wpabuf_free(buf);
  929. buf = NULL;
  930. }
  931. return buf;
  932. }
  933. /*
  934. * Channels with a great SNR can operate at full rate. What is a great SNR?
  935. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  936. * rule of thumb is that any SNR above 20 is good." This one
  937. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  938. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  939. * conservative value.
  940. */
  941. #define GREAT_SNR 30
  942. /* Compare function for sorting scan results. Return >0 if @b is considered
  943. * better. */
  944. static int wpa_scan_result_compar(const void *a, const void *b)
  945. {
  946. #define IS_5GHZ(n) (n > 4000)
  947. #define MIN(a,b) a < b ? a : b
  948. struct wpa_scan_res **_wa = (void *) a;
  949. struct wpa_scan_res **_wb = (void *) b;
  950. struct wpa_scan_res *wa = *_wa;
  951. struct wpa_scan_res *wb = *_wb;
  952. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  953. int snr_a, snr_b;
  954. /* WPA/WPA2 support preferred */
  955. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  956. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  957. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  958. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  959. if (wpa_b && !wpa_a)
  960. return 1;
  961. if (!wpa_b && wpa_a)
  962. return -1;
  963. /* privacy support preferred */
  964. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  965. (wb->caps & IEEE80211_CAP_PRIVACY))
  966. return 1;
  967. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  968. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  969. return -1;
  970. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  971. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  972. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  973. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  974. } else {
  975. /* Not suitable information to calculate SNR, so use level */
  976. snr_a = wa->level;
  977. snr_b = wb->level;
  978. }
  979. /* best/max rate preferred if SNR close enough */
  980. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  981. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  982. maxrate_a = wpa_scan_get_max_rate(wa);
  983. maxrate_b = wpa_scan_get_max_rate(wb);
  984. if (maxrate_a != maxrate_b)
  985. return maxrate_b - maxrate_a;
  986. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  987. return IS_5GHZ(wa->freq) ? -1 : 1;
  988. }
  989. /* use freq for channel preference */
  990. /* all things being equal, use SNR; if SNRs are
  991. * identical, use quality values since some drivers may only report
  992. * that value and leave the signal level zero */
  993. if (snr_b == snr_a)
  994. return wb->qual - wa->qual;
  995. return snr_b - snr_a;
  996. #undef MIN
  997. #undef IS_5GHZ
  998. }
  999. #ifdef CONFIG_WPS
  1000. /* Compare function for sorting scan results when searching a WPS AP for
  1001. * provisioning. Return >0 if @b is considered better. */
  1002. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1003. {
  1004. struct wpa_scan_res **_wa = (void *) a;
  1005. struct wpa_scan_res **_wb = (void *) b;
  1006. struct wpa_scan_res *wa = *_wa;
  1007. struct wpa_scan_res *wb = *_wb;
  1008. int uses_wps_a, uses_wps_b;
  1009. struct wpabuf *wps_a, *wps_b;
  1010. int res;
  1011. /* Optimization - check WPS IE existence before allocated memory and
  1012. * doing full reassembly. */
  1013. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1014. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1015. if (uses_wps_a && !uses_wps_b)
  1016. return -1;
  1017. if (!uses_wps_a && uses_wps_b)
  1018. return 1;
  1019. if (uses_wps_a && uses_wps_b) {
  1020. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1021. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1022. res = wps_ap_priority_compar(wps_a, wps_b);
  1023. wpabuf_free(wps_a);
  1024. wpabuf_free(wps_b);
  1025. if (res)
  1026. return res;
  1027. }
  1028. /*
  1029. * Do not use current AP security policy as a sorting criteria during
  1030. * WPS provisioning step since the AP may get reconfigured at the
  1031. * completion of provisioning.
  1032. */
  1033. /* all things being equal, use signal level; if signal levels are
  1034. * identical, use quality values since some drivers may only report
  1035. * that value and leave the signal level zero */
  1036. if (wb->level == wa->level)
  1037. return wb->qual - wa->qual;
  1038. return wb->level - wa->level;
  1039. }
  1040. #endif /* CONFIG_WPS */
  1041. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1042. {
  1043. #ifndef CONFIG_NO_STDOUT_DEBUG
  1044. size_t i;
  1045. if (scan_res->res == NULL || scan_res->num == 0)
  1046. return;
  1047. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1048. for (i = 0; i < scan_res->num; i++) {
  1049. struct wpa_scan_res *r = scan_res->res[i];
  1050. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1051. == WPA_SCAN_LEVEL_DBM) {
  1052. int snr = r->level - r->noise;
  1053. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1054. "noise=%d level=%d snr=%d%s flags=0x%x",
  1055. MAC2STR(r->bssid), r->freq, r->qual,
  1056. r->noise, r->level, snr,
  1057. snr >= GREAT_SNR ? "*" : "", r->flags);
  1058. } else {
  1059. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1060. "noise=%d level=%d flags=0x%x",
  1061. MAC2STR(r->bssid), r->freq, r->qual,
  1062. r->noise, r->level, r->flags);
  1063. }
  1064. }
  1065. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1066. }
  1067. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1068. const u8 *bssid)
  1069. {
  1070. size_t i;
  1071. if (wpa_s->bssid_filter == NULL)
  1072. return 1;
  1073. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1074. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1075. ETH_ALEN) == 0)
  1076. return 1;
  1077. }
  1078. return 0;
  1079. }
  1080. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1081. struct wpa_scan_results *res)
  1082. {
  1083. size_t i, j;
  1084. if (wpa_s->bssid_filter == NULL)
  1085. return;
  1086. for (i = 0, j = 0; i < res->num; i++) {
  1087. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1088. res->res[i]->bssid)) {
  1089. res->res[j++] = res->res[i];
  1090. } else {
  1091. os_free(res->res[i]);
  1092. res->res[i] = NULL;
  1093. }
  1094. }
  1095. if (res->num != j) {
  1096. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1097. (int) (res->num - j));
  1098. res->num = j;
  1099. }
  1100. }
  1101. /**
  1102. * wpa_supplicant_get_scan_results - Get scan results
  1103. * @wpa_s: Pointer to wpa_supplicant data
  1104. * @info: Information about what was scanned or %NULL if not available
  1105. * @new_scan: Whether a new scan was performed
  1106. * Returns: Scan results, %NULL on failure
  1107. *
  1108. * This function request the current scan results from the driver and updates
  1109. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1110. * results with wpa_scan_results_free().
  1111. */
  1112. struct wpa_scan_results *
  1113. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1114. struct scan_info *info, int new_scan)
  1115. {
  1116. struct wpa_scan_results *scan_res;
  1117. size_t i;
  1118. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1119. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1120. if (scan_res == NULL) {
  1121. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1122. return NULL;
  1123. }
  1124. filter_scan_res(wpa_s, scan_res);
  1125. #ifdef CONFIG_WPS
  1126. if (wpas_wps_in_progress(wpa_s)) {
  1127. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1128. "provisioning rules");
  1129. compar = wpa_scan_result_wps_compar;
  1130. }
  1131. #endif /* CONFIG_WPS */
  1132. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1133. compar);
  1134. dump_scan_res(scan_res);
  1135. wpa_bss_update_start(wpa_s);
  1136. for (i = 0; i < scan_res->num; i++)
  1137. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1138. wpa_bss_update_end(wpa_s, info, new_scan);
  1139. return scan_res;
  1140. }
  1141. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1142. {
  1143. struct wpa_scan_results *scan_res;
  1144. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1145. if (scan_res == NULL)
  1146. return -1;
  1147. wpa_scan_results_free(scan_res);
  1148. return 0;
  1149. }