scan.c 35 KB

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