scan.c 35 KB

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