scan.c 53 KB

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