scan.c 36 KB

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