scan.c 55 KB

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