bss.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009-2015, 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 "drivers/driver.h"
  13. #include "eap_peer/eap.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "config.h"
  16. #include "notify.h"
  17. #include "scan.h"
  18. #include "bss.h"
  19. #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
  20. #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
  21. #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
  22. #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
  23. #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
  24. #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
  25. #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
  26. #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
  27. #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
  28. static void wpa_bss_set_hessid(struct wpa_bss *bss)
  29. {
  30. #ifdef CONFIG_INTERWORKING
  31. const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
  32. if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
  33. os_memset(bss->hessid, 0, ETH_ALEN);
  34. return;
  35. }
  36. if (ie[1] == 7)
  37. os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
  38. else
  39. os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
  40. #endif /* CONFIG_INTERWORKING */
  41. }
  42. /**
  43. * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
  44. * Returns: Allocated ANQP data structure or %NULL on failure
  45. *
  46. * The allocated ANQP data structure has its users count set to 1. It may be
  47. * shared by multiple BSS entries and each shared entry is freed with
  48. * wpa_bss_anqp_free().
  49. */
  50. struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
  51. {
  52. struct wpa_bss_anqp *anqp;
  53. anqp = os_zalloc(sizeof(*anqp));
  54. if (anqp == NULL)
  55. return NULL;
  56. #ifdef CONFIG_INTERWORKING
  57. dl_list_init(&anqp->anqp_elems);
  58. #endif /* CONFIG_INTERWORKING */
  59. anqp->users = 1;
  60. return anqp;
  61. }
  62. /**
  63. * wpa_bss_anqp_clone - Clone an ANQP data structure
  64. * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
  65. * Returns: Cloned ANQP data structure or %NULL on failure
  66. */
  67. static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
  68. {
  69. struct wpa_bss_anqp *n;
  70. n = os_zalloc(sizeof(*n));
  71. if (n == NULL)
  72. return NULL;
  73. #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
  74. #ifdef CONFIG_INTERWORKING
  75. dl_list_init(&n->anqp_elems);
  76. ANQP_DUP(capability_list);
  77. ANQP_DUP(venue_name);
  78. ANQP_DUP(network_auth_type);
  79. ANQP_DUP(roaming_consortium);
  80. ANQP_DUP(ip_addr_type_availability);
  81. ANQP_DUP(nai_realm);
  82. ANQP_DUP(anqp_3gpp);
  83. ANQP_DUP(domain_name);
  84. #endif /* CONFIG_INTERWORKING */
  85. #ifdef CONFIG_HS20
  86. ANQP_DUP(hs20_capability_list);
  87. ANQP_DUP(hs20_operator_friendly_name);
  88. ANQP_DUP(hs20_wan_metrics);
  89. ANQP_DUP(hs20_connection_capability);
  90. ANQP_DUP(hs20_operating_class);
  91. ANQP_DUP(hs20_osu_providers_list);
  92. #endif /* CONFIG_HS20 */
  93. #undef ANQP_DUP
  94. return n;
  95. }
  96. /**
  97. * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
  98. * @bss: BSS entry
  99. * Returns: 0 on success, -1 on failure
  100. *
  101. * This function ensures the specific BSS entry has an ANQP data structure that
  102. * is not shared with any other BSS entry.
  103. */
  104. int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
  105. {
  106. struct wpa_bss_anqp *anqp;
  107. if (bss->anqp && bss->anqp->users > 1) {
  108. /* allocated, but shared - clone an unshared copy */
  109. anqp = wpa_bss_anqp_clone(bss->anqp);
  110. if (anqp == NULL)
  111. return -1;
  112. anqp->users = 1;
  113. bss->anqp->users--;
  114. bss->anqp = anqp;
  115. return 0;
  116. }
  117. if (bss->anqp)
  118. return 0; /* already allocated and not shared */
  119. /* not allocated - allocate a new storage area */
  120. bss->anqp = wpa_bss_anqp_alloc();
  121. return bss->anqp ? 0 : -1;
  122. }
  123. /**
  124. * wpa_bss_anqp_free - Free an ANQP data structure
  125. * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
  126. */
  127. static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
  128. {
  129. #ifdef CONFIG_INTERWORKING
  130. struct wpa_bss_anqp_elem *elem;
  131. #endif /* CONFIG_INTERWORKING */
  132. if (anqp == NULL)
  133. return;
  134. anqp->users--;
  135. if (anqp->users > 0) {
  136. /* Another BSS entry holds a pointer to this ANQP info */
  137. return;
  138. }
  139. #ifdef CONFIG_INTERWORKING
  140. wpabuf_free(anqp->capability_list);
  141. wpabuf_free(anqp->venue_name);
  142. wpabuf_free(anqp->network_auth_type);
  143. wpabuf_free(anqp->roaming_consortium);
  144. wpabuf_free(anqp->ip_addr_type_availability);
  145. wpabuf_free(anqp->nai_realm);
  146. wpabuf_free(anqp->anqp_3gpp);
  147. wpabuf_free(anqp->domain_name);
  148. while ((elem = dl_list_first(&anqp->anqp_elems,
  149. struct wpa_bss_anqp_elem, list))) {
  150. dl_list_del(&elem->list);
  151. wpabuf_free(elem->payload);
  152. os_free(elem);
  153. }
  154. #endif /* CONFIG_INTERWORKING */
  155. #ifdef CONFIG_HS20
  156. wpabuf_free(anqp->hs20_capability_list);
  157. wpabuf_free(anqp->hs20_operator_friendly_name);
  158. wpabuf_free(anqp->hs20_wan_metrics);
  159. wpabuf_free(anqp->hs20_connection_capability);
  160. wpabuf_free(anqp->hs20_operating_class);
  161. wpabuf_free(anqp->hs20_osu_providers_list);
  162. #endif /* CONFIG_HS20 */
  163. os_free(anqp);
  164. }
  165. static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
  166. struct wpa_bss *old_bss,
  167. struct wpa_bss *new_bss)
  168. {
  169. struct wpa_radio_work *work;
  170. struct wpa_connect_work *cwork;
  171. work = radio_work_pending(wpa_s, "sme-connect");
  172. if (!work)
  173. work = radio_work_pending(wpa_s, "connect");
  174. if (!work)
  175. return;
  176. cwork = work->ctx;
  177. if (cwork->bss != old_bss)
  178. return;
  179. wpa_printf(MSG_DEBUG,
  180. "Update BSS pointer for the pending connect radio work");
  181. cwork->bss = new_bss;
  182. if (!new_bss)
  183. cwork->bss_removed = 1;
  184. }
  185. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  186. const char *reason)
  187. {
  188. if (wpa_s->last_scan_res) {
  189. unsigned int i;
  190. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  191. if (wpa_s->last_scan_res[i] == bss) {
  192. os_memmove(&wpa_s->last_scan_res[i],
  193. &wpa_s->last_scan_res[i + 1],
  194. (wpa_s->last_scan_res_used - i - 1)
  195. * sizeof(struct wpa_bss *));
  196. wpa_s->last_scan_res_used--;
  197. break;
  198. }
  199. }
  200. }
  201. wpa_bss_update_pending_connect(wpa_s, bss, NULL);
  202. dl_list_del(&bss->list);
  203. dl_list_del(&bss->list_id);
  204. wpa_s->num_bss--;
  205. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
  206. " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
  207. wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
  208. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  209. wpa_bss_anqp_free(bss->anqp);
  210. os_free(bss);
  211. }
  212. /**
  213. * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
  214. * @wpa_s: Pointer to wpa_supplicant data
  215. * @bssid: BSSID
  216. * @ssid: SSID
  217. * @ssid_len: Length of @ssid
  218. * Returns: Pointer to the BSS entry or %NULL if not found
  219. */
  220. struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
  221. const u8 *ssid, size_t ssid_len)
  222. {
  223. struct wpa_bss *bss;
  224. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  225. return NULL;
  226. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  227. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  228. bss->ssid_len == ssid_len &&
  229. os_memcmp(bss->ssid, ssid, ssid_len) == 0)
  230. return bss;
  231. }
  232. return NULL;
  233. }
  234. static void calculate_update_time(const struct os_reltime *fetch_time,
  235. unsigned int age_ms,
  236. struct os_reltime *update_time)
  237. {
  238. os_time_t usec;
  239. update_time->sec = fetch_time->sec;
  240. update_time->usec = fetch_time->usec;
  241. update_time->sec -= age_ms / 1000;
  242. usec = (age_ms % 1000) * 1000;
  243. if (update_time->usec < usec) {
  244. update_time->sec--;
  245. update_time->usec += 1000000;
  246. }
  247. update_time->usec -= usec;
  248. }
  249. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
  250. struct os_reltime *fetch_time)
  251. {
  252. dst->flags = src->flags;
  253. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  254. dst->freq = src->freq;
  255. dst->beacon_int = src->beacon_int;
  256. dst->caps = src->caps;
  257. dst->qual = src->qual;
  258. dst->noise = src->noise;
  259. dst->level = src->level;
  260. dst->tsf = src->tsf;
  261. dst->est_throughput = src->est_throughput;
  262. dst->snr = src->snr;
  263. calculate_update_time(fetch_time, src->age, &dst->last_update);
  264. }
  265. static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
  266. struct wpa_bss *bss)
  267. {
  268. #ifdef CONFIG_WPS
  269. struct wpa_ssid *ssid;
  270. struct wpabuf *wps_ie;
  271. int pbc = 0, ret;
  272. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  273. if (!wps_ie)
  274. return 0;
  275. if (wps_is_selected_pbc_registrar(wps_ie)) {
  276. pbc = 1;
  277. } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
  278. wpabuf_free(wps_ie);
  279. return 0;
  280. }
  281. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  282. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  283. continue;
  284. if (ssid->ssid_len &&
  285. (ssid->ssid_len != bss->ssid_len ||
  286. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
  287. continue;
  288. if (pbc)
  289. ret = eap_is_wps_pbc_enrollee(&ssid->eap);
  290. else
  291. ret = eap_is_wps_pin_enrollee(&ssid->eap);
  292. wpabuf_free(wps_ie);
  293. return ret;
  294. }
  295. #endif /* CONFIG_WPS */
  296. return 0;
  297. }
  298. static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  299. {
  300. struct wpa_ssid *ssid;
  301. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  302. if (ssid->ssid == NULL || ssid->ssid_len == 0)
  303. continue;
  304. if (ssid->ssid_len == bss->ssid_len &&
  305. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
  306. return 1;
  307. }
  308. return 0;
  309. }
  310. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  311. {
  312. if (bss == wpa_s->current_bss)
  313. return 1;
  314. if (wpa_s->current_bss &&
  315. (bss->ssid_len != wpa_s->current_bss->ssid_len ||
  316. os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
  317. bss->ssid_len) != 0))
  318. return 0; /* SSID has changed */
  319. return !is_zero_ether_addr(bss->bssid) &&
  320. (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  321. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
  322. }
  323. static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
  324. {
  325. struct wpa_bss *bss;
  326. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  327. if (!wpa_bss_known(wpa_s, bss) &&
  328. !wpa_bss_is_wps_candidate(wpa_s, bss)) {
  329. wpa_bss_remove(wpa_s, bss, __func__);
  330. return 0;
  331. }
  332. }
  333. return -1;
  334. }
  335. static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
  336. {
  337. struct wpa_bss *bss;
  338. /*
  339. * Remove the oldest entry that does not match with any configured
  340. * network.
  341. */
  342. if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
  343. return 0;
  344. /*
  345. * Remove the oldest entry that isn't currently in use.
  346. */
  347. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  348. if (!wpa_bss_in_use(wpa_s, bss)) {
  349. wpa_bss_remove(wpa_s, bss, __func__);
  350. return 0;
  351. }
  352. }
  353. return -1;
  354. }
  355. static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
  356. const u8 *ssid, size_t ssid_len,
  357. struct wpa_scan_res *res,
  358. struct os_reltime *fetch_time)
  359. {
  360. struct wpa_bss *bss;
  361. bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
  362. if (bss == NULL)
  363. return NULL;
  364. bss->id = wpa_s->bss_next_id++;
  365. bss->last_update_idx = wpa_s->bss_update_idx;
  366. wpa_bss_copy_res(bss, res, fetch_time);
  367. os_memcpy(bss->ssid, ssid, ssid_len);
  368. bss->ssid_len = ssid_len;
  369. bss->ie_len = res->ie_len;
  370. bss->beacon_ie_len = res->beacon_ie_len;
  371. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  372. wpa_bss_set_hessid(bss);
  373. if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
  374. wpa_bss_remove_oldest(wpa_s) != 0) {
  375. wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
  376. "because all BSSes are in use. We should normally "
  377. "not get here!", (int) wpa_s->num_bss + 1);
  378. wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
  379. }
  380. dl_list_add_tail(&wpa_s->bss, &bss->list);
  381. dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
  382. wpa_s->num_bss++;
  383. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
  384. " SSID '%s' freq %d",
  385. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
  386. bss->freq);
  387. wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
  388. return bss;
  389. }
  390. static int are_ies_equal(const struct wpa_bss *old,
  391. const struct wpa_scan_res *new_res, u32 ie)
  392. {
  393. const u8 *old_ie, *new_ie;
  394. struct wpabuf *old_ie_buff = NULL;
  395. struct wpabuf *new_ie_buff = NULL;
  396. int new_ie_len, old_ie_len, ret, is_multi;
  397. switch (ie) {
  398. case WPA_IE_VENDOR_TYPE:
  399. old_ie = wpa_bss_get_vendor_ie(old, ie);
  400. new_ie = wpa_scan_get_vendor_ie(new_res, ie);
  401. is_multi = 0;
  402. break;
  403. case WPS_IE_VENDOR_TYPE:
  404. old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
  405. new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
  406. is_multi = 1;
  407. break;
  408. case WLAN_EID_RSN:
  409. case WLAN_EID_SUPP_RATES:
  410. case WLAN_EID_EXT_SUPP_RATES:
  411. old_ie = wpa_bss_get_ie(old, ie);
  412. new_ie = wpa_scan_get_ie(new_res, ie);
  413. is_multi = 0;
  414. break;
  415. default:
  416. wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
  417. return 0;
  418. }
  419. if (is_multi) {
  420. /* in case of multiple IEs stored in buffer */
  421. old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
  422. new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
  423. old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
  424. new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
  425. } else {
  426. /* in case of single IE */
  427. old_ie_len = old_ie ? old_ie[1] + 2 : 0;
  428. new_ie_len = new_ie ? new_ie[1] + 2 : 0;
  429. }
  430. if (!old_ie || !new_ie)
  431. ret = !old_ie && !new_ie;
  432. else
  433. ret = (old_ie_len == new_ie_len &&
  434. os_memcmp(old_ie, new_ie, old_ie_len) == 0);
  435. wpabuf_free(old_ie_buff);
  436. wpabuf_free(new_ie_buff);
  437. return ret;
  438. }
  439. static u32 wpa_bss_compare_res(const struct wpa_bss *old,
  440. const struct wpa_scan_res *new_res)
  441. {
  442. u32 changes = 0;
  443. int caps_diff = old->caps ^ new_res->caps;
  444. if (old->freq != new_res->freq)
  445. changes |= WPA_BSS_FREQ_CHANGED_FLAG;
  446. if (old->level != new_res->level)
  447. changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
  448. if (caps_diff & IEEE80211_CAP_PRIVACY)
  449. changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
  450. if (caps_diff & IEEE80211_CAP_IBSS)
  451. changes |= WPA_BSS_MODE_CHANGED_FLAG;
  452. if (old->ie_len == new_res->ie_len &&
  453. os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
  454. return changes;
  455. changes |= WPA_BSS_IES_CHANGED_FLAG;
  456. if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
  457. changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
  458. if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
  459. changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
  460. if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
  461. changes |= WPA_BSS_WPS_CHANGED_FLAG;
  462. if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
  463. !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
  464. changes |= WPA_BSS_RATES_CHANGED_FLAG;
  465. return changes;
  466. }
  467. static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
  468. const struct wpa_bss *bss)
  469. {
  470. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  471. wpas_notify_bss_freq_changed(wpa_s, bss->id);
  472. if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
  473. wpas_notify_bss_signal_changed(wpa_s, bss->id);
  474. if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
  475. wpas_notify_bss_privacy_changed(wpa_s, bss->id);
  476. if (changes & WPA_BSS_MODE_CHANGED_FLAG)
  477. wpas_notify_bss_mode_changed(wpa_s, bss->id);
  478. if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
  479. wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
  480. if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
  481. wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
  482. if (changes & WPA_BSS_WPS_CHANGED_FLAG)
  483. wpas_notify_bss_wps_changed(wpa_s, bss->id);
  484. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  485. wpas_notify_bss_ies_changed(wpa_s, bss->id);
  486. if (changes & WPA_BSS_RATES_CHANGED_FLAG)
  487. wpas_notify_bss_rates_changed(wpa_s, bss->id);
  488. wpas_notify_bss_seen(wpa_s, bss->id);
  489. }
  490. static struct wpa_bss *
  491. wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  492. struct wpa_scan_res *res, struct os_reltime *fetch_time)
  493. {
  494. u32 changes;
  495. changes = wpa_bss_compare_res(bss, res);
  496. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  497. wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
  498. MAC2STR(bss->bssid), bss->freq, res->freq);
  499. bss->scan_miss_count = 0;
  500. bss->last_update_idx = wpa_s->bss_update_idx;
  501. wpa_bss_copy_res(bss, res, fetch_time);
  502. /* Move the entry to the end of the list */
  503. dl_list_del(&bss->list);
  504. #ifdef CONFIG_P2P
  505. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  506. !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
  507. /*
  508. * This can happen when non-P2P station interface runs a scan
  509. * without P2P IE in the Probe Request frame. P2P GO would reply
  510. * to that with a Probe Response that does not include P2P IE.
  511. * Do not update the IEs in this BSS entry to avoid such loss of
  512. * information that may be needed for P2P operations to
  513. * determine group information.
  514. */
  515. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
  516. MACSTR " since that would remove P2P IE information",
  517. MAC2STR(bss->bssid));
  518. } else
  519. #endif /* CONFIG_P2P */
  520. if (bss->ie_len + bss->beacon_ie_len >=
  521. res->ie_len + res->beacon_ie_len) {
  522. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  523. bss->ie_len = res->ie_len;
  524. bss->beacon_ie_len = res->beacon_ie_len;
  525. } else {
  526. struct wpa_bss *nbss;
  527. struct dl_list *prev = bss->list_id.prev;
  528. dl_list_del(&bss->list_id);
  529. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  530. res->beacon_ie_len);
  531. if (nbss) {
  532. unsigned int i;
  533. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  534. if (wpa_s->last_scan_res[i] == bss) {
  535. wpa_s->last_scan_res[i] = nbss;
  536. break;
  537. }
  538. }
  539. if (wpa_s->current_bss == bss)
  540. wpa_s->current_bss = nbss;
  541. wpa_bss_update_pending_connect(wpa_s, bss, nbss);
  542. bss = nbss;
  543. os_memcpy(bss + 1, res + 1,
  544. res->ie_len + res->beacon_ie_len);
  545. bss->ie_len = res->ie_len;
  546. bss->beacon_ie_len = res->beacon_ie_len;
  547. }
  548. dl_list_add(prev, &bss->list_id);
  549. }
  550. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  551. wpa_bss_set_hessid(bss);
  552. dl_list_add_tail(&wpa_s->bss, &bss->list);
  553. notify_bss_changes(wpa_s, changes, bss);
  554. return bss;
  555. }
  556. /**
  557. * wpa_bss_update_start - Start a BSS table update from scan results
  558. * @wpa_s: Pointer to wpa_supplicant data
  559. *
  560. * This function is called at the start of each BSS table update round for new
  561. * scan results. The actual scan result entries are indicated with calls to
  562. * wpa_bss_update_scan_res() and the update round is finished with a call to
  563. * wpa_bss_update_end().
  564. */
  565. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  566. {
  567. wpa_s->bss_update_idx++;
  568. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  569. wpa_s->bss_update_idx);
  570. wpa_s->last_scan_res_used = 0;
  571. }
  572. /**
  573. * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
  574. * @wpa_s: Pointer to wpa_supplicant data
  575. * @res: Scan result
  576. * @fetch_time: Time when the result was fetched from the driver
  577. *
  578. * This function updates a BSS table entry (or adds one) based on a scan result.
  579. * This is called separately for each scan result between the calls to
  580. * wpa_bss_update_start() and wpa_bss_update_end().
  581. */
  582. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  583. struct wpa_scan_res *res,
  584. struct os_reltime *fetch_time)
  585. {
  586. const u8 *ssid, *p2p, *mesh;
  587. struct wpa_bss *bss;
  588. if (wpa_s->conf->ignore_old_scan_res) {
  589. struct os_reltime update;
  590. calculate_update_time(fetch_time, res->age, &update);
  591. if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
  592. struct os_reltime age;
  593. os_reltime_sub(&wpa_s->scan_trigger_time, &update,
  594. &age);
  595. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
  596. "table entry that is %u.%06u seconds older "
  597. "than our scan trigger",
  598. (unsigned int) age.sec,
  599. (unsigned int) age.usec);
  600. return;
  601. }
  602. }
  603. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  604. if (ssid == NULL) {
  605. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  606. MACSTR, MAC2STR(res->bssid));
  607. return;
  608. }
  609. if (ssid[1] > SSID_MAX_LEN) {
  610. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  611. MACSTR, MAC2STR(res->bssid));
  612. return;
  613. }
  614. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  615. #ifdef CONFIG_P2P
  616. if (p2p == NULL &&
  617. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  618. /*
  619. * If it's a P2P specific interface, then don't update
  620. * the scan result without a P2P IE.
  621. */
  622. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  623. " update for P2P interface", MAC2STR(res->bssid));
  624. return;
  625. }
  626. #endif /* CONFIG_P2P */
  627. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  628. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  629. return; /* Skip P2P listen discovery results here */
  630. /* TODO: add option for ignoring BSSes we are not interested in
  631. * (to save memory) */
  632. mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
  633. if (mesh && mesh[1] <= SSID_MAX_LEN)
  634. ssid = mesh;
  635. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  636. if (bss == NULL)
  637. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
  638. else {
  639. bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
  640. if (wpa_s->last_scan_res) {
  641. unsigned int i;
  642. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  643. if (bss == wpa_s->last_scan_res[i]) {
  644. /* Already in the list */
  645. return;
  646. }
  647. }
  648. }
  649. }
  650. if (bss == NULL)
  651. return;
  652. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  653. struct wpa_bss **n;
  654. unsigned int siz;
  655. if (wpa_s->last_scan_res_size == 0)
  656. siz = 32;
  657. else
  658. siz = wpa_s->last_scan_res_size * 2;
  659. n = os_realloc_array(wpa_s->last_scan_res, siz,
  660. sizeof(struct wpa_bss *));
  661. if (n == NULL)
  662. return;
  663. wpa_s->last_scan_res = n;
  664. wpa_s->last_scan_res_size = siz;
  665. }
  666. if (wpa_s->last_scan_res)
  667. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  668. }
  669. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  670. const struct scan_info *info)
  671. {
  672. int found;
  673. size_t i;
  674. if (info == NULL)
  675. return 1;
  676. if (info->num_freqs) {
  677. found = 0;
  678. for (i = 0; i < info->num_freqs; i++) {
  679. if (bss->freq == info->freqs[i]) {
  680. found = 1;
  681. break;
  682. }
  683. }
  684. if (!found)
  685. return 0;
  686. }
  687. if (info->num_ssids) {
  688. found = 0;
  689. for (i = 0; i < info->num_ssids; i++) {
  690. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  691. if ((s->ssid == NULL || s->ssid_len == 0) ||
  692. (s->ssid_len == bss->ssid_len &&
  693. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  694. 0)) {
  695. found = 1;
  696. break;
  697. }
  698. }
  699. if (!found)
  700. return 0;
  701. }
  702. return 1;
  703. }
  704. /**
  705. * wpa_bss_update_end - End a BSS table update from scan results
  706. * @wpa_s: Pointer to wpa_supplicant data
  707. * @info: Information about scan parameters
  708. * @new_scan: Whether this update round was based on a new scan
  709. *
  710. * This function is called at the end of each BSS table update round for new
  711. * scan results. The start of the update was indicated with a call to
  712. * wpa_bss_update_start().
  713. */
  714. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  715. int new_scan)
  716. {
  717. struct wpa_bss *bss, *n;
  718. os_get_reltime(&wpa_s->last_scan);
  719. if ((info && info->aborted) || !new_scan)
  720. return; /* do not expire entries without new scan */
  721. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  722. if (wpa_bss_in_use(wpa_s, bss))
  723. continue;
  724. if (!wpa_bss_included_in_scan(bss, info))
  725. continue; /* expire only BSSes that were scanned */
  726. if (bss->last_update_idx < wpa_s->bss_update_idx)
  727. bss->scan_miss_count++;
  728. if (bss->scan_miss_count >=
  729. wpa_s->conf->bss_expiration_scan_count) {
  730. wpa_bss_remove(wpa_s, bss, "no match in scan");
  731. }
  732. }
  733. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
  734. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
  735. }
  736. /**
  737. * wpa_bss_flush_by_age - Flush old BSS entries
  738. * @wpa_s: Pointer to wpa_supplicant data
  739. * @age: Maximum entry age in seconds
  740. *
  741. * Remove BSS entries that have not been updated during the last @age seconds.
  742. */
  743. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  744. {
  745. struct wpa_bss *bss, *n;
  746. struct os_reltime t;
  747. if (dl_list_empty(&wpa_s->bss))
  748. return;
  749. os_get_reltime(&t);
  750. t.sec -= age;
  751. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  752. if (wpa_bss_in_use(wpa_s, bss))
  753. continue;
  754. if (os_reltime_before(&bss->last_update, &t)) {
  755. wpa_bss_remove(wpa_s, bss, __func__);
  756. } else
  757. break;
  758. }
  759. }
  760. /**
  761. * wpa_bss_init - Initialize BSS table
  762. * @wpa_s: Pointer to wpa_supplicant data
  763. * Returns: 0 on success, -1 on failure
  764. *
  765. * This prepares BSS table lists and timer for periodic updates. The BSS table
  766. * is deinitialized with wpa_bss_deinit() once not needed anymore.
  767. */
  768. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  769. {
  770. dl_list_init(&wpa_s->bss);
  771. dl_list_init(&wpa_s->bss_id);
  772. return 0;
  773. }
  774. /**
  775. * wpa_bss_flush - Flush all unused BSS entries
  776. * @wpa_s: Pointer to wpa_supplicant data
  777. */
  778. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  779. {
  780. struct wpa_bss *bss, *n;
  781. wpa_s->clear_driver_scan_cache = 1;
  782. if (wpa_s->bss.next == NULL)
  783. return; /* BSS table not yet initialized */
  784. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  785. if (wpa_bss_in_use(wpa_s, bss))
  786. continue;
  787. wpa_bss_remove(wpa_s, bss, __func__);
  788. }
  789. }
  790. /**
  791. * wpa_bss_deinit - Deinitialize BSS table
  792. * @wpa_s: Pointer to wpa_supplicant data
  793. */
  794. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  795. {
  796. wpa_bss_flush(wpa_s);
  797. }
  798. /**
  799. * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
  800. * @wpa_s: Pointer to wpa_supplicant data
  801. * @bssid: BSSID
  802. * Returns: Pointer to the BSS entry or %NULL if not found
  803. */
  804. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  805. const u8 *bssid)
  806. {
  807. struct wpa_bss *bss;
  808. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  809. return NULL;
  810. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  811. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  812. return bss;
  813. }
  814. return NULL;
  815. }
  816. /**
  817. * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
  818. * @wpa_s: Pointer to wpa_supplicant data
  819. * @bssid: BSSID
  820. * Returns: Pointer to the BSS entry or %NULL if not found
  821. *
  822. * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
  823. * find the entry that has the most recent update. This can help in finding the
  824. * correct entry in cases where the SSID of the AP may have changed recently
  825. * (e.g., in WPS reconfiguration cases).
  826. */
  827. struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
  828. const u8 *bssid)
  829. {
  830. struct wpa_bss *bss, *found = NULL;
  831. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  832. return NULL;
  833. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  834. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
  835. continue;
  836. if (found == NULL ||
  837. os_reltime_before(&found->last_update, &bss->last_update))
  838. found = bss;
  839. }
  840. return found;
  841. }
  842. #ifdef CONFIG_P2P
  843. /**
  844. * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
  845. * @wpa_s: Pointer to wpa_supplicant data
  846. * @dev_addr: P2P Device Address of the GO
  847. * Returns: Pointer to the BSS entry or %NULL if not found
  848. */
  849. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  850. const u8 *dev_addr)
  851. {
  852. struct wpa_bss *bss;
  853. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  854. u8 addr[ETH_ALEN];
  855. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  856. addr) == 0 &&
  857. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  858. return bss;
  859. }
  860. return NULL;
  861. }
  862. #endif /* CONFIG_P2P */
  863. /**
  864. * wpa_bss_get_id - Fetch a BSS table entry based on identifier
  865. * @wpa_s: Pointer to wpa_supplicant data
  866. * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
  867. * Returns: Pointer to the BSS entry or %NULL if not found
  868. */
  869. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  870. {
  871. struct wpa_bss *bss;
  872. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  873. if (bss->id == id)
  874. return bss;
  875. }
  876. return NULL;
  877. }
  878. /**
  879. * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
  880. * @wpa_s: Pointer to wpa_supplicant data
  881. * @idf: Smallest allowed identifier assigned for the entry
  882. * @idf: Largest allowed identifier assigned for the entry
  883. * Returns: Pointer to the BSS entry or %NULL if not found
  884. *
  885. * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
  886. * smallest id value to be fetched within the specified range without the
  887. * caller having to know the exact id.
  888. */
  889. struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  890. unsigned int idf, unsigned int idl)
  891. {
  892. struct wpa_bss *bss;
  893. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  894. if (bss->id >= idf && bss->id <= idl)
  895. return bss;
  896. }
  897. return NULL;
  898. }
  899. /**
  900. * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
  901. * @bss: BSS table entry
  902. * @ie: Information element identitifier (WLAN_EID_*)
  903. * Returns: Pointer to the information element (id field) or %NULL if not found
  904. *
  905. * This function returns the first matching information element in the BSS
  906. * entry.
  907. */
  908. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  909. {
  910. return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
  911. }
  912. /**
  913. * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
  914. * @bss: BSS table entry
  915. * @vendor_type: Vendor type (four octets starting the IE payload)
  916. * Returns: Pointer to the information element (id field) or %NULL if not found
  917. *
  918. * This function returns the first matching information element in the BSS
  919. * entry.
  920. */
  921. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  922. {
  923. const u8 *end, *pos;
  924. pos = (const u8 *) (bss + 1);
  925. end = pos + bss->ie_len;
  926. while (end - pos > 1) {
  927. if (2 + pos[1] > end - pos)
  928. break;
  929. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  930. vendor_type == WPA_GET_BE32(&pos[2]))
  931. return pos;
  932. pos += 2 + pos[1];
  933. }
  934. return NULL;
  935. }
  936. /**
  937. * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
  938. * @bss: BSS table entry
  939. * @vendor_type: Vendor type (four octets starting the IE payload)
  940. * Returns: Pointer to the information element (id field) or %NULL if not found
  941. *
  942. * This function returns the first matching information element in the BSS
  943. * entry.
  944. *
  945. * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
  946. * from Beacon frames instead of either Beacon or Probe Response frames.
  947. */
  948. const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
  949. u32 vendor_type)
  950. {
  951. const u8 *end, *pos;
  952. if (bss->beacon_ie_len == 0)
  953. return NULL;
  954. pos = (const u8 *) (bss + 1);
  955. pos += bss->ie_len;
  956. end = pos + bss->beacon_ie_len;
  957. while (end - pos > 1) {
  958. if (2 + pos[1] > end - pos)
  959. break;
  960. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  961. vendor_type == WPA_GET_BE32(&pos[2]))
  962. return pos;
  963. pos += 2 + pos[1];
  964. }
  965. return NULL;
  966. }
  967. /**
  968. * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
  969. * @bss: BSS table entry
  970. * @vendor_type: Vendor type (four octets starting the IE payload)
  971. * Returns: Pointer to the information element payload or %NULL if not found
  972. *
  973. * This function returns concatenated payload of possibly fragmented vendor
  974. * specific information elements in the BSS entry. The caller is responsible for
  975. * freeing the returned buffer.
  976. */
  977. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  978. u32 vendor_type)
  979. {
  980. struct wpabuf *buf;
  981. const u8 *end, *pos;
  982. buf = wpabuf_alloc(bss->ie_len);
  983. if (buf == NULL)
  984. return NULL;
  985. pos = (const u8 *) (bss + 1);
  986. end = pos + bss->ie_len;
  987. while (end - pos > 1) {
  988. if (2 + pos[1] > end - pos)
  989. break;
  990. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  991. vendor_type == WPA_GET_BE32(&pos[2]))
  992. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  993. pos += 2 + pos[1];
  994. }
  995. if (wpabuf_len(buf) == 0) {
  996. wpabuf_free(buf);
  997. buf = NULL;
  998. }
  999. return buf;
  1000. }
  1001. /**
  1002. * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
  1003. * @bss: BSS table entry
  1004. * @vendor_type: Vendor type (four octets starting the IE payload)
  1005. * Returns: Pointer to the information element payload or %NULL if not found
  1006. *
  1007. * This function returns concatenated payload of possibly fragmented vendor
  1008. * specific information elements in the BSS entry. The caller is responsible for
  1009. * freeing the returned buffer.
  1010. *
  1011. * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
  1012. * from Beacon frames instead of either Beacon or Probe Response frames.
  1013. */
  1014. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  1015. u32 vendor_type)
  1016. {
  1017. struct wpabuf *buf;
  1018. const u8 *end, *pos;
  1019. buf = wpabuf_alloc(bss->beacon_ie_len);
  1020. if (buf == NULL)
  1021. return NULL;
  1022. pos = (const u8 *) (bss + 1);
  1023. pos += bss->ie_len;
  1024. end = pos + bss->beacon_ie_len;
  1025. while (end - pos > 1) {
  1026. if (2 + pos[1] > end - pos)
  1027. break;
  1028. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1029. vendor_type == WPA_GET_BE32(&pos[2]))
  1030. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1031. pos += 2 + pos[1];
  1032. }
  1033. if (wpabuf_len(buf) == 0) {
  1034. wpabuf_free(buf);
  1035. buf = NULL;
  1036. }
  1037. return buf;
  1038. }
  1039. /**
  1040. * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
  1041. * @bss: BSS table entry
  1042. * Returns: Maximum legacy rate in units of 500 kbps
  1043. */
  1044. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  1045. {
  1046. int rate = 0;
  1047. const u8 *ie;
  1048. int i;
  1049. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1050. for (i = 0; ie && i < ie[1]; i++) {
  1051. if ((ie[i + 2] & 0x7f) > rate)
  1052. rate = ie[i + 2] & 0x7f;
  1053. }
  1054. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1055. for (i = 0; ie && i < ie[1]; i++) {
  1056. if ((ie[i + 2] & 0x7f) > rate)
  1057. rate = ie[i + 2] & 0x7f;
  1058. }
  1059. return rate;
  1060. }
  1061. /**
  1062. * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
  1063. * @bss: BSS table entry
  1064. * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
  1065. * Returns: number of legacy TX rates or -1 on failure
  1066. *
  1067. * The caller is responsible for freeing the returned buffer with os_free() in
  1068. * case of success.
  1069. */
  1070. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  1071. {
  1072. const u8 *ie, *ie2;
  1073. int i, j;
  1074. unsigned int len;
  1075. u8 *r;
  1076. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1077. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1078. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  1079. r = os_malloc(len);
  1080. if (!r)
  1081. return -1;
  1082. for (i = 0; ie && i < ie[1]; i++)
  1083. r[i] = ie[i + 2] & 0x7f;
  1084. for (j = 0; ie2 && j < ie2[1]; j++)
  1085. r[i + j] = ie2[j + 2] & 0x7f;
  1086. *rates = r;
  1087. return len;
  1088. }