scan.c 45 KB

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