scan.c 36 KB

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