scan.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  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. /* Restore scan_req since we will try to scan again */
  596. wpa_s->scan_req = scan_req;
  597. wpa_supplicant_req_scan(wpa_s, 1, 0);
  598. }
  599. }
  600. /**
  601. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  602. * @wpa_s: Pointer to wpa_supplicant data
  603. * @sec: Number of seconds after which to scan
  604. * @usec: Number of microseconds after which to scan
  605. *
  606. * This function is used to schedule a scan for neighboring access points after
  607. * the specified time.
  608. */
  609. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  610. {
  611. /* If there's at least one network that should be specifically scanned
  612. * then don't cancel the scan and reschedule. Some drivers do
  613. * background scanning which generates frequent scan results, and that
  614. * causes the specific SSID scan to get continually pushed back and
  615. * never happen, which causes hidden APs to never get probe-scanned.
  616. */
  617. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  618. wpa_s->conf->ap_scan == 1) {
  619. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  620. while (ssid) {
  621. if (!wpas_network_disabled(wpa_s, ssid) &&
  622. ssid->scan_ssid)
  623. break;
  624. ssid = ssid->next;
  625. }
  626. if (ssid) {
  627. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  628. "ensure that specific SSID scans occur");
  629. return;
  630. }
  631. }
  632. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  633. sec, usec);
  634. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  635. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  636. }
  637. /**
  638. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  639. * @wpa_s: Pointer to wpa_supplicant data
  640. * @sec: Number of seconds after which to scan
  641. * @usec: Number of microseconds after which to scan
  642. *
  643. * This function is used to schedule periodic scans for neighboring
  644. * access points after the specified time.
  645. */
  646. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  647. int sec, int usec)
  648. {
  649. if (!wpa_s->sched_scan_supported)
  650. return -1;
  651. eloop_register_timeout(sec, usec,
  652. wpa_supplicant_delayed_sched_scan_timeout,
  653. wpa_s, NULL);
  654. return 0;
  655. }
  656. /**
  657. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  658. * @wpa_s: Pointer to wpa_supplicant data
  659. *
  660. * This function is used to schedule periodic scans for neighboring
  661. * access points repeating the scan continuously.
  662. */
  663. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  664. {
  665. struct wpa_driver_scan_params params;
  666. struct wpa_driver_scan_params *scan_params;
  667. enum wpa_states prev_state;
  668. struct wpa_ssid *ssid = NULL;
  669. struct wpabuf *extra_ie = NULL;
  670. int ret;
  671. unsigned int max_sched_scan_ssids;
  672. int wildcard = 0;
  673. int need_ssids;
  674. if (!wpa_s->sched_scan_supported)
  675. return -1;
  676. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  677. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  678. else
  679. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  680. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  681. return -1;
  682. if (wpa_s->sched_scanning) {
  683. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  684. return 0;
  685. }
  686. need_ssids = 0;
  687. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  688. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  689. /* Use wildcard SSID to find this network */
  690. wildcard = 1;
  691. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  692. ssid->ssid_len)
  693. need_ssids++;
  694. #ifdef CONFIG_WPS
  695. if (!wpas_network_disabled(wpa_s, ssid) &&
  696. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  697. /*
  698. * Normal scan is more reliable and faster for WPS
  699. * operations and since these are for short periods of
  700. * time, the benefit of trying to use sched_scan would
  701. * be limited.
  702. */
  703. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  704. "sched_scan for WPS");
  705. return -1;
  706. }
  707. #endif /* CONFIG_WPS */
  708. }
  709. if (wildcard)
  710. need_ssids++;
  711. if (wpa_s->normal_scans < 3 &&
  712. (need_ssids <= wpa_s->max_scan_ssids ||
  713. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  714. /*
  715. * When normal scan can speed up operations, use that for the
  716. * first operations before starting the sched_scan to allow
  717. * user space sleep more. We do this only if the normal scan
  718. * has functionality that is suitable for this or if the
  719. * sched_scan does not have better support for multiple SSIDs.
  720. */
  721. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  722. "sched_scan for initial scans (normal_scans=%d)",
  723. wpa_s->normal_scans);
  724. return -1;
  725. }
  726. os_memset(&params, 0, sizeof(params));
  727. /* If we can't allocate space for the filters, we just don't filter */
  728. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  729. sizeof(struct wpa_driver_scan_filter));
  730. prev_state = wpa_s->wpa_state;
  731. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  732. wpa_s->wpa_state == WPA_INACTIVE)
  733. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  734. if (wpa_s->autoscan_params != NULL) {
  735. scan_params = wpa_s->autoscan_params;
  736. goto scan;
  737. }
  738. /* Find the starting point from which to continue scanning */
  739. ssid = wpa_s->conf->ssid;
  740. if (wpa_s->prev_sched_ssid) {
  741. while (ssid) {
  742. if (ssid == wpa_s->prev_sched_ssid) {
  743. ssid = ssid->next;
  744. break;
  745. }
  746. ssid = ssid->next;
  747. }
  748. }
  749. if (!ssid || !wpa_s->prev_sched_ssid) {
  750. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  751. if (wpa_s->sched_scan_interval == 0)
  752. wpa_s->sched_scan_interval = 10;
  753. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  754. wpa_s->first_sched_scan = 1;
  755. ssid = wpa_s->conf->ssid;
  756. wpa_s->prev_sched_ssid = ssid;
  757. }
  758. if (wildcard) {
  759. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  760. params.num_ssids++;
  761. }
  762. while (ssid) {
  763. if (wpas_network_disabled(wpa_s, ssid))
  764. goto next;
  765. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  766. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  767. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  768. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  769. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  770. ssid->ssid, ssid->ssid_len);
  771. params.filter_ssids[params.num_filter_ssids].ssid_len =
  772. ssid->ssid_len;
  773. params.num_filter_ssids++;
  774. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  775. {
  776. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  777. "filter for sched_scan - drop filter");
  778. os_free(params.filter_ssids);
  779. params.filter_ssids = NULL;
  780. params.num_filter_ssids = 0;
  781. }
  782. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  783. if (params.num_ssids == max_sched_scan_ssids)
  784. break; /* only room for broadcast SSID */
  785. wpa_dbg(wpa_s, MSG_DEBUG,
  786. "add to active scan ssid: %s",
  787. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  788. params.ssids[params.num_ssids].ssid =
  789. ssid->ssid;
  790. params.ssids[params.num_ssids].ssid_len =
  791. ssid->ssid_len;
  792. params.num_ssids++;
  793. if (params.num_ssids >= max_sched_scan_ssids) {
  794. wpa_s->prev_sched_ssid = ssid;
  795. do {
  796. ssid = ssid->next;
  797. } while (ssid &&
  798. (wpas_network_disabled(wpa_s, ssid) ||
  799. !ssid->scan_ssid));
  800. break;
  801. }
  802. }
  803. next:
  804. wpa_s->prev_sched_ssid = ssid;
  805. ssid = ssid->next;
  806. }
  807. if (params.num_filter_ssids == 0) {
  808. os_free(params.filter_ssids);
  809. params.filter_ssids = NULL;
  810. }
  811. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  812. if (extra_ie) {
  813. params.extra_ies = wpabuf_head(extra_ie);
  814. params.extra_ies_len = wpabuf_len(extra_ie);
  815. }
  816. scan_params = &params;
  817. scan:
  818. if (ssid || !wpa_s->first_sched_scan) {
  819. wpa_dbg(wpa_s, MSG_DEBUG,
  820. "Starting sched scan: interval %d timeout %d",
  821. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  822. } else {
  823. wpa_dbg(wpa_s, MSG_DEBUG,
  824. "Starting sched scan: interval %d (no timeout)",
  825. wpa_s->sched_scan_interval);
  826. }
  827. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  828. wpa_s->sched_scan_interval);
  829. wpabuf_free(extra_ie);
  830. os_free(params.filter_ssids);
  831. if (ret) {
  832. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  833. if (prev_state != wpa_s->wpa_state)
  834. wpa_supplicant_set_state(wpa_s, prev_state);
  835. return ret;
  836. }
  837. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  838. if (ssid || !wpa_s->first_sched_scan) {
  839. wpa_s->sched_scan_timed_out = 0;
  840. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  841. wpa_supplicant_sched_scan_timeout,
  842. wpa_s, NULL);
  843. wpa_s->first_sched_scan = 0;
  844. wpa_s->sched_scan_timeout /= 2;
  845. wpa_s->sched_scan_interval *= 2;
  846. }
  847. return 0;
  848. }
  849. /**
  850. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  851. * @wpa_s: Pointer to wpa_supplicant data
  852. *
  853. * This function is used to cancel a scan request scheduled with
  854. * wpa_supplicant_req_scan().
  855. */
  856. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  857. {
  858. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  859. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  860. }
  861. /**
  862. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  863. * @wpa_s: Pointer to wpa_supplicant data
  864. *
  865. * This function is used to stop a periodic scheduled scan.
  866. */
  867. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  868. {
  869. if (!wpa_s->sched_scanning)
  870. return;
  871. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  872. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  873. wpa_supplicant_stop_sched_scan(wpa_s);
  874. }
  875. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  876. int scanning)
  877. {
  878. if (wpa_s->scanning != scanning) {
  879. wpa_s->scanning = scanning;
  880. wpas_notify_scanning(wpa_s);
  881. }
  882. }
  883. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  884. {
  885. int rate = 0;
  886. const u8 *ie;
  887. int i;
  888. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  889. for (i = 0; ie && i < ie[1]; i++) {
  890. if ((ie[i + 2] & 0x7f) > rate)
  891. rate = ie[i + 2] & 0x7f;
  892. }
  893. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  894. for (i = 0; ie && i < ie[1]; i++) {
  895. if ((ie[i + 2] & 0x7f) > rate)
  896. rate = ie[i + 2] & 0x7f;
  897. }
  898. return rate;
  899. }
  900. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  901. {
  902. const u8 *end, *pos;
  903. pos = (const u8 *) (res + 1);
  904. end = pos + res->ie_len;
  905. while (pos + 1 < end) {
  906. if (pos + 2 + pos[1] > end)
  907. break;
  908. if (pos[0] == ie)
  909. return pos;
  910. pos += 2 + pos[1];
  911. }
  912. return NULL;
  913. }
  914. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  915. u32 vendor_type)
  916. {
  917. const u8 *end, *pos;
  918. pos = (const u8 *) (res + 1);
  919. end = pos + res->ie_len;
  920. while (pos + 1 < end) {
  921. if (pos + 2 + pos[1] > end)
  922. break;
  923. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  924. vendor_type == WPA_GET_BE32(&pos[2]))
  925. return pos;
  926. pos += 2 + pos[1];
  927. }
  928. return NULL;
  929. }
  930. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  931. u32 vendor_type)
  932. {
  933. struct wpabuf *buf;
  934. const u8 *end, *pos;
  935. buf = wpabuf_alloc(res->ie_len);
  936. if (buf == NULL)
  937. return NULL;
  938. pos = (const u8 *) (res + 1);
  939. end = pos + res->ie_len;
  940. while (pos + 1 < end) {
  941. if (pos + 2 + pos[1] > end)
  942. break;
  943. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  944. vendor_type == WPA_GET_BE32(&pos[2]))
  945. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  946. pos += 2 + pos[1];
  947. }
  948. if (wpabuf_len(buf) == 0) {
  949. wpabuf_free(buf);
  950. buf = NULL;
  951. }
  952. return buf;
  953. }
  954. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  955. const struct wpa_scan_res *res, u32 vendor_type)
  956. {
  957. struct wpabuf *buf;
  958. const u8 *end, *pos;
  959. if (res->beacon_ie_len == 0)
  960. return NULL;
  961. buf = wpabuf_alloc(res->beacon_ie_len);
  962. if (buf == NULL)
  963. return NULL;
  964. pos = (const u8 *) (res + 1);
  965. pos += res->ie_len;
  966. end = pos + res->beacon_ie_len;
  967. while (pos + 1 < end) {
  968. if (pos + 2 + pos[1] > end)
  969. break;
  970. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  971. vendor_type == WPA_GET_BE32(&pos[2]))
  972. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  973. pos += 2 + pos[1];
  974. }
  975. if (wpabuf_len(buf) == 0) {
  976. wpabuf_free(buf);
  977. buf = NULL;
  978. }
  979. return buf;
  980. }
  981. /*
  982. * Channels with a great SNR can operate at full rate. What is a great SNR?
  983. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  984. * rule of thumb is that any SNR above 20 is good." This one
  985. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  986. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  987. * conservative value.
  988. */
  989. #define GREAT_SNR 30
  990. /* Compare function for sorting scan results. Return >0 if @b is considered
  991. * better. */
  992. static int wpa_scan_result_compar(const void *a, const void *b)
  993. {
  994. #define IS_5GHZ(n) (n > 4000)
  995. #define MIN(a,b) a < b ? a : b
  996. struct wpa_scan_res **_wa = (void *) a;
  997. struct wpa_scan_res **_wb = (void *) b;
  998. struct wpa_scan_res *wa = *_wa;
  999. struct wpa_scan_res *wb = *_wb;
  1000. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1001. int snr_a, snr_b;
  1002. /* WPA/WPA2 support preferred */
  1003. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1004. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1005. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1006. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1007. if (wpa_b && !wpa_a)
  1008. return 1;
  1009. if (!wpa_b && wpa_a)
  1010. return -1;
  1011. /* privacy support preferred */
  1012. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1013. (wb->caps & IEEE80211_CAP_PRIVACY))
  1014. return 1;
  1015. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1016. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1017. return -1;
  1018. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1019. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1020. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1021. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1022. } else {
  1023. /* Not suitable information to calculate SNR, so use level */
  1024. snr_a = wa->level;
  1025. snr_b = wb->level;
  1026. }
  1027. /* best/max rate preferred if SNR close enough */
  1028. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1029. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1030. maxrate_a = wpa_scan_get_max_rate(wa);
  1031. maxrate_b = wpa_scan_get_max_rate(wb);
  1032. if (maxrate_a != maxrate_b)
  1033. return maxrate_b - maxrate_a;
  1034. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1035. return IS_5GHZ(wa->freq) ? -1 : 1;
  1036. }
  1037. /* use freq for channel preference */
  1038. /* all things being equal, use SNR; if SNRs are
  1039. * identical, use quality values since some drivers may only report
  1040. * that value and leave the signal level zero */
  1041. if (snr_b == snr_a)
  1042. return wb->qual - wa->qual;
  1043. return snr_b - snr_a;
  1044. #undef MIN
  1045. #undef IS_5GHZ
  1046. }
  1047. #ifdef CONFIG_WPS
  1048. /* Compare function for sorting scan results when searching a WPS AP for
  1049. * provisioning. Return >0 if @b is considered better. */
  1050. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1051. {
  1052. struct wpa_scan_res **_wa = (void *) a;
  1053. struct wpa_scan_res **_wb = (void *) b;
  1054. struct wpa_scan_res *wa = *_wa;
  1055. struct wpa_scan_res *wb = *_wb;
  1056. int uses_wps_a, uses_wps_b;
  1057. struct wpabuf *wps_a, *wps_b;
  1058. int res;
  1059. /* Optimization - check WPS IE existence before allocated memory and
  1060. * doing full reassembly. */
  1061. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1062. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1063. if (uses_wps_a && !uses_wps_b)
  1064. return -1;
  1065. if (!uses_wps_a && uses_wps_b)
  1066. return 1;
  1067. if (uses_wps_a && uses_wps_b) {
  1068. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1069. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1070. res = wps_ap_priority_compar(wps_a, wps_b);
  1071. wpabuf_free(wps_a);
  1072. wpabuf_free(wps_b);
  1073. if (res)
  1074. return res;
  1075. }
  1076. /*
  1077. * Do not use current AP security policy as a sorting criteria during
  1078. * WPS provisioning step since the AP may get reconfigured at the
  1079. * completion of provisioning.
  1080. */
  1081. /* all things being equal, use signal level; if signal levels are
  1082. * identical, use quality values since some drivers may only report
  1083. * that value and leave the signal level zero */
  1084. if (wb->level == wa->level)
  1085. return wb->qual - wa->qual;
  1086. return wb->level - wa->level;
  1087. }
  1088. #endif /* CONFIG_WPS */
  1089. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1090. {
  1091. #ifndef CONFIG_NO_STDOUT_DEBUG
  1092. size_t i;
  1093. if (scan_res->res == NULL || scan_res->num == 0)
  1094. return;
  1095. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1096. for (i = 0; i < scan_res->num; i++) {
  1097. struct wpa_scan_res *r = scan_res->res[i];
  1098. u8 *pos;
  1099. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1100. == WPA_SCAN_LEVEL_DBM) {
  1101. int snr = r->level - r->noise;
  1102. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1103. "noise=%d level=%d snr=%d%s flags=0x%x",
  1104. MAC2STR(r->bssid), r->freq, r->qual,
  1105. r->noise, r->level, snr,
  1106. snr >= GREAT_SNR ? "*" : "", r->flags);
  1107. } else {
  1108. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1109. "noise=%d level=%d flags=0x%x",
  1110. MAC2STR(r->bssid), r->freq, r->qual,
  1111. r->noise, r->level, r->flags);
  1112. }
  1113. pos = (u8 *) (r + 1);
  1114. if (r->ie_len)
  1115. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1116. pos += r->ie_len;
  1117. if (r->beacon_ie_len)
  1118. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1119. pos, r->beacon_ie_len);
  1120. }
  1121. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1122. }
  1123. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1124. const u8 *bssid)
  1125. {
  1126. size_t i;
  1127. if (wpa_s->bssid_filter == NULL)
  1128. return 1;
  1129. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1130. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1131. ETH_ALEN) == 0)
  1132. return 1;
  1133. }
  1134. return 0;
  1135. }
  1136. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1137. struct wpa_scan_results *res)
  1138. {
  1139. size_t i, j;
  1140. if (wpa_s->bssid_filter == NULL)
  1141. return;
  1142. for (i = 0, j = 0; i < res->num; i++) {
  1143. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1144. res->res[i]->bssid)) {
  1145. res->res[j++] = res->res[i];
  1146. } else {
  1147. os_free(res->res[i]);
  1148. res->res[i] = NULL;
  1149. }
  1150. }
  1151. if (res->num != j) {
  1152. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1153. (int) (res->num - j));
  1154. res->num = j;
  1155. }
  1156. }
  1157. /**
  1158. * wpa_supplicant_get_scan_results - Get scan results
  1159. * @wpa_s: Pointer to wpa_supplicant data
  1160. * @info: Information about what was scanned or %NULL if not available
  1161. * @new_scan: Whether a new scan was performed
  1162. * Returns: Scan results, %NULL on failure
  1163. *
  1164. * This function request the current scan results from the driver and updates
  1165. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1166. * results with wpa_scan_results_free().
  1167. */
  1168. struct wpa_scan_results *
  1169. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1170. struct scan_info *info, int new_scan)
  1171. {
  1172. struct wpa_scan_results *scan_res;
  1173. size_t i;
  1174. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1175. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1176. if (scan_res == NULL) {
  1177. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1178. return NULL;
  1179. }
  1180. filter_scan_res(wpa_s, scan_res);
  1181. #ifdef CONFIG_WPS
  1182. if (wpas_wps_in_progress(wpa_s)) {
  1183. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1184. "provisioning rules");
  1185. compar = wpa_scan_result_wps_compar;
  1186. }
  1187. #endif /* CONFIG_WPS */
  1188. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1189. compar);
  1190. dump_scan_res(scan_res);
  1191. wpa_bss_update_start(wpa_s);
  1192. for (i = 0; i < scan_res->num; i++)
  1193. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1194. wpa_bss_update_end(wpa_s, info, new_scan);
  1195. return scan_res;
  1196. }
  1197. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1198. {
  1199. struct wpa_scan_results *scan_res;
  1200. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1201. if (scan_res == NULL)
  1202. return -1;
  1203. wpa_scan_results_free(scan_res);
  1204. return 0;
  1205. }