scan.c 35 KB

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