bss.c 32 KB

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