bss.c 35 KB

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