scan.c 38 KB

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