scan.c 34 KB

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