scan.c 46 KB

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