bss.c 31 KB

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