scan.c 43 KB

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