scan.c 42 KB

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