scan.c 45 KB

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