bss.c 33 KB

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