bss.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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. wpas_notify_bss_seen(wpa_s, bss->id);
  410. }
  411. static struct wpa_bss *
  412. wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  413. struct wpa_scan_res *res, struct os_reltime *fetch_time)
  414. {
  415. u32 changes;
  416. changes = wpa_bss_compare_res(bss, res);
  417. bss->scan_miss_count = 0;
  418. bss->last_update_idx = wpa_s->bss_update_idx;
  419. wpa_bss_copy_res(bss, res, fetch_time);
  420. /* Move the entry to the end of the list */
  421. dl_list_del(&bss->list);
  422. #ifdef CONFIG_P2P
  423. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
  424. !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
  425. /*
  426. * This can happen when non-P2P station interface runs a scan
  427. * without P2P IE in the Probe Request frame. P2P GO would reply
  428. * to that with a Probe Response that does not include P2P IE.
  429. * Do not update the IEs in this BSS entry to avoid such loss of
  430. * information that may be needed for P2P operations to
  431. * determine group information.
  432. */
  433. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
  434. MACSTR " since that would remove P2P IE information",
  435. MAC2STR(bss->bssid));
  436. } else
  437. #endif /* CONFIG_P2P */
  438. if (bss->ie_len + bss->beacon_ie_len >=
  439. res->ie_len + res->beacon_ie_len) {
  440. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  441. bss->ie_len = res->ie_len;
  442. bss->beacon_ie_len = res->beacon_ie_len;
  443. } else {
  444. struct wpa_bss *nbss;
  445. struct dl_list *prev = bss->list_id.prev;
  446. dl_list_del(&bss->list_id);
  447. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  448. res->beacon_ie_len);
  449. if (nbss) {
  450. unsigned int i;
  451. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  452. if (wpa_s->last_scan_res[i] == bss) {
  453. wpa_s->last_scan_res[i] = nbss;
  454. break;
  455. }
  456. }
  457. if (wpa_s->current_bss == bss)
  458. wpa_s->current_bss = nbss;
  459. bss = nbss;
  460. os_memcpy(bss + 1, res + 1,
  461. res->ie_len + res->beacon_ie_len);
  462. bss->ie_len = res->ie_len;
  463. bss->beacon_ie_len = res->beacon_ie_len;
  464. }
  465. dl_list_add(prev, &bss->list_id);
  466. }
  467. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  468. wpa_bss_set_hessid(bss);
  469. dl_list_add_tail(&wpa_s->bss, &bss->list);
  470. notify_bss_changes(wpa_s, changes, bss);
  471. return bss;
  472. }
  473. /**
  474. * wpa_bss_update_start - Start a BSS table update from scan results
  475. * @wpa_s: Pointer to wpa_supplicant data
  476. *
  477. * This function is called at the start of each BSS table update round for new
  478. * scan results. The actual scan result entries are indicated with calls to
  479. * wpa_bss_update_scan_res() and the update round is finished with a call to
  480. * wpa_bss_update_end().
  481. */
  482. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  483. {
  484. wpa_s->bss_update_idx++;
  485. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  486. wpa_s->bss_update_idx);
  487. wpa_s->last_scan_res_used = 0;
  488. }
  489. /**
  490. * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
  491. * @wpa_s: Pointer to wpa_supplicant data
  492. * @res: Scan result
  493. * @fetch_time: Time when the result was fetched from the driver
  494. *
  495. * This function updates a BSS table entry (or adds one) based on a scan result.
  496. * This is called separately for each scan result between the calls to
  497. * wpa_bss_update_start() and wpa_bss_update_end().
  498. */
  499. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  500. struct wpa_scan_res *res,
  501. struct os_reltime *fetch_time)
  502. {
  503. const u8 *ssid, *p2p;
  504. struct wpa_bss *bss;
  505. if (wpa_s->conf->ignore_old_scan_res) {
  506. struct os_reltime update;
  507. calculate_update_time(fetch_time, res->age, &update);
  508. if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
  509. struct os_reltime age;
  510. os_reltime_sub(&wpa_s->scan_trigger_time, &update,
  511. &age);
  512. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
  513. "table entry that is %u.%06u seconds older "
  514. "than our scan trigger",
  515. (unsigned int) age.sec,
  516. (unsigned int) age.usec);
  517. return;
  518. }
  519. }
  520. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  521. if (ssid == NULL) {
  522. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  523. MACSTR, MAC2STR(res->bssid));
  524. return;
  525. }
  526. if (ssid[1] > 32) {
  527. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  528. MACSTR, MAC2STR(res->bssid));
  529. return;
  530. }
  531. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  532. #ifdef CONFIG_P2P
  533. if (p2p == NULL &&
  534. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  535. /*
  536. * If it's a P2P specific interface, then don't update
  537. * the scan result without a P2P IE.
  538. */
  539. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  540. " update for P2P interface", MAC2STR(res->bssid));
  541. return;
  542. }
  543. #endif /* CONFIG_P2P */
  544. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  545. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  546. return; /* Skip P2P listen discovery results here */
  547. /* TODO: add option for ignoring BSSes we are not interested in
  548. * (to save memory) */
  549. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  550. if (bss == NULL)
  551. bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
  552. else {
  553. bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
  554. if (wpa_s->last_scan_res) {
  555. unsigned int i;
  556. for (i = 0; i < wpa_s->last_scan_res_used; i++) {
  557. if (bss == wpa_s->last_scan_res[i]) {
  558. /* Already in the list */
  559. return;
  560. }
  561. }
  562. }
  563. }
  564. if (bss == NULL)
  565. return;
  566. if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
  567. struct wpa_bss **n;
  568. unsigned int siz;
  569. if (wpa_s->last_scan_res_size == 0)
  570. siz = 32;
  571. else
  572. siz = wpa_s->last_scan_res_size * 2;
  573. n = os_realloc_array(wpa_s->last_scan_res, siz,
  574. sizeof(struct wpa_bss *));
  575. if (n == NULL)
  576. return;
  577. wpa_s->last_scan_res = n;
  578. wpa_s->last_scan_res_size = siz;
  579. }
  580. if (wpa_s->last_scan_res)
  581. wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
  582. }
  583. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  584. const struct scan_info *info)
  585. {
  586. int found;
  587. size_t i;
  588. if (info == NULL)
  589. return 1;
  590. if (info->num_freqs) {
  591. found = 0;
  592. for (i = 0; i < info->num_freqs; i++) {
  593. if (bss->freq == info->freqs[i]) {
  594. found = 1;
  595. break;
  596. }
  597. }
  598. if (!found)
  599. return 0;
  600. }
  601. if (info->num_ssids) {
  602. found = 0;
  603. for (i = 0; i < info->num_ssids; i++) {
  604. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  605. if ((s->ssid == NULL || s->ssid_len == 0) ||
  606. (s->ssid_len == bss->ssid_len &&
  607. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  608. 0)) {
  609. found = 1;
  610. break;
  611. }
  612. }
  613. if (!found)
  614. return 0;
  615. }
  616. return 1;
  617. }
  618. /**
  619. * wpa_bss_update_end - End a BSS table update from scan results
  620. * @wpa_s: Pointer to wpa_supplicant data
  621. * @info: Information about scan parameters
  622. * @new_scan: Whether this update round was based on a new scan
  623. *
  624. * This function is called at the end of each BSS table update round for new
  625. * scan results. The start of the update was indicated with a call to
  626. * wpa_bss_update_start().
  627. */
  628. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  629. int new_scan)
  630. {
  631. struct wpa_bss *bss, *n;
  632. os_get_reltime(&wpa_s->last_scan);
  633. if (!new_scan)
  634. return; /* do not expire entries without new scan */
  635. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  636. if (wpa_bss_in_use(wpa_s, bss))
  637. continue;
  638. if (!wpa_bss_included_in_scan(bss, info))
  639. continue; /* expire only BSSes that were scanned */
  640. if (bss->last_update_idx < wpa_s->bss_update_idx)
  641. bss->scan_miss_count++;
  642. if (bss->scan_miss_count >=
  643. wpa_s->conf->bss_expiration_scan_count) {
  644. wpa_bss_remove(wpa_s, bss, "no match in scan");
  645. }
  646. }
  647. wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
  648. wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
  649. }
  650. /**
  651. * wpa_bss_flush_by_age - Flush old BSS entries
  652. * @wpa_s: Pointer to wpa_supplicant data
  653. * @age: Maximum entry age in seconds
  654. *
  655. * Remove BSS entries that have not been updated during the last @age seconds.
  656. */
  657. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  658. {
  659. struct wpa_bss *bss, *n;
  660. struct os_reltime t;
  661. if (dl_list_empty(&wpa_s->bss))
  662. return;
  663. os_get_reltime(&t);
  664. t.sec -= age;
  665. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  666. if (wpa_bss_in_use(wpa_s, bss))
  667. continue;
  668. if (os_reltime_before(&bss->last_update, &t)) {
  669. wpa_bss_remove(wpa_s, bss, __func__);
  670. } else
  671. break;
  672. }
  673. }
  674. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  675. {
  676. struct wpa_supplicant *wpa_s = eloop_ctx;
  677. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  678. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  679. wpa_bss_timeout, wpa_s, NULL);
  680. }
  681. /**
  682. * wpa_bss_init - Initialize BSS table
  683. * @wpa_s: Pointer to wpa_supplicant data
  684. * Returns: 0 on success, -1 on failure
  685. *
  686. * This prepares BSS table lists and timer for periodic updates. The BSS table
  687. * is deinitialized with wpa_bss_deinit() once not needed anymore.
  688. */
  689. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  690. {
  691. dl_list_init(&wpa_s->bss);
  692. dl_list_init(&wpa_s->bss_id);
  693. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  694. wpa_bss_timeout, wpa_s, NULL);
  695. return 0;
  696. }
  697. /**
  698. * wpa_bss_flush - Flush all unused BSS entries
  699. * @wpa_s: Pointer to wpa_supplicant data
  700. */
  701. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  702. {
  703. struct wpa_bss *bss, *n;
  704. wpa_s->clear_driver_scan_cache = 1;
  705. if (wpa_s->bss.next == NULL)
  706. return; /* BSS table not yet initialized */
  707. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  708. if (wpa_bss_in_use(wpa_s, bss))
  709. continue;
  710. wpa_bss_remove(wpa_s, bss, __func__);
  711. }
  712. }
  713. /**
  714. * wpa_bss_deinit - Deinitialize BSS table
  715. * @wpa_s: Pointer to wpa_supplicant data
  716. */
  717. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  718. {
  719. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  720. wpa_bss_flush(wpa_s);
  721. }
  722. /**
  723. * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
  724. * @wpa_s: Pointer to wpa_supplicant data
  725. * @bssid: BSSID
  726. * Returns: Pointer to the BSS entry or %NULL if not found
  727. */
  728. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  729. const u8 *bssid)
  730. {
  731. struct wpa_bss *bss;
  732. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  733. return NULL;
  734. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  735. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  736. return bss;
  737. }
  738. return NULL;
  739. }
  740. /**
  741. * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
  742. * @wpa_s: Pointer to wpa_supplicant data
  743. * @bssid: BSSID
  744. * Returns: Pointer to the BSS entry or %NULL if not found
  745. *
  746. * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
  747. * find the entry that has the most recent update. This can help in finding the
  748. * correct entry in cases where the SSID of the AP may have changed recently
  749. * (e.g., in WPS reconfiguration cases).
  750. */
  751. struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
  752. const u8 *bssid)
  753. {
  754. struct wpa_bss *bss, *found = NULL;
  755. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  756. return NULL;
  757. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  758. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
  759. continue;
  760. if (found == NULL ||
  761. os_reltime_before(&found->last_update, &bss->last_update))
  762. found = bss;
  763. }
  764. return found;
  765. }
  766. #ifdef CONFIG_P2P
  767. /**
  768. * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
  769. * @wpa_s: Pointer to wpa_supplicant data
  770. * @dev_addr: P2P Device Address of the GO
  771. * Returns: Pointer to the BSS entry or %NULL if not found
  772. */
  773. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  774. const u8 *dev_addr)
  775. {
  776. struct wpa_bss *bss;
  777. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  778. u8 addr[ETH_ALEN];
  779. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  780. addr) == 0 &&
  781. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  782. return bss;
  783. }
  784. return NULL;
  785. }
  786. #endif /* CONFIG_P2P */
  787. /**
  788. * wpa_bss_get_id - Fetch a BSS table entry based on identifier
  789. * @wpa_s: Pointer to wpa_supplicant data
  790. * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
  791. * Returns: Pointer to the BSS entry or %NULL if not found
  792. */
  793. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  794. {
  795. struct wpa_bss *bss;
  796. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  797. if (bss->id == id)
  798. return bss;
  799. }
  800. return NULL;
  801. }
  802. /**
  803. * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
  804. * @wpa_s: Pointer to wpa_supplicant data
  805. * @idf: Smallest allowed identifier assigned for the entry
  806. * @idf: Largest allowed identifier assigned for the entry
  807. * Returns: Pointer to the BSS entry or %NULL if not found
  808. *
  809. * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
  810. * smallest id value to be fetched within the specified range without the
  811. * caller having to know the exact id.
  812. */
  813. struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  814. unsigned int idf, unsigned int idl)
  815. {
  816. struct wpa_bss *bss;
  817. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  818. if (bss->id >= idf && bss->id <= idl)
  819. return bss;
  820. }
  821. return NULL;
  822. }
  823. /**
  824. * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
  825. * @bss: BSS table entry
  826. * @ie: Information element identitifier (WLAN_EID_*)
  827. * Returns: Pointer to the information element (id field) or %NULL if not found
  828. *
  829. * This function returns the first matching information element in the BSS
  830. * entry.
  831. */
  832. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  833. {
  834. const u8 *end, *pos;
  835. pos = (const u8 *) (bss + 1);
  836. end = pos + bss->ie_len;
  837. while (pos + 1 < end) {
  838. if (pos + 2 + pos[1] > end)
  839. break;
  840. if (pos[0] == ie)
  841. return pos;
  842. pos += 2 + pos[1];
  843. }
  844. return NULL;
  845. }
  846. /**
  847. * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
  848. * @bss: BSS table entry
  849. * @vendor_type: Vendor type (four octets starting the IE payload)
  850. * Returns: Pointer to the information element (id field) or %NULL if not found
  851. *
  852. * This function returns the first matching information element in the BSS
  853. * entry.
  854. */
  855. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  856. {
  857. const u8 *end, *pos;
  858. pos = (const u8 *) (bss + 1);
  859. end = pos + bss->ie_len;
  860. while (pos + 1 < end) {
  861. if (pos + 2 + pos[1] > end)
  862. break;
  863. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  864. vendor_type == WPA_GET_BE32(&pos[2]))
  865. return pos;
  866. pos += 2 + pos[1];
  867. }
  868. return NULL;
  869. }
  870. /**
  871. * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
  872. * @bss: BSS table entry
  873. * @vendor_type: Vendor type (four octets starting the IE payload)
  874. * Returns: Pointer to the information element (id field) or %NULL if not found
  875. *
  876. * This function returns the first matching information element in the BSS
  877. * entry.
  878. *
  879. * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
  880. * from Beacon frames instead of either Beacon or Probe Response frames.
  881. */
  882. const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
  883. u32 vendor_type)
  884. {
  885. const u8 *end, *pos;
  886. if (bss->beacon_ie_len == 0)
  887. return NULL;
  888. pos = (const u8 *) (bss + 1);
  889. pos += bss->ie_len;
  890. end = pos + bss->beacon_ie_len;
  891. while (pos + 1 < end) {
  892. if (pos + 2 + pos[1] > end)
  893. break;
  894. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  895. vendor_type == WPA_GET_BE32(&pos[2]))
  896. return pos;
  897. pos += 2 + pos[1];
  898. }
  899. return NULL;
  900. }
  901. /**
  902. * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
  903. * @bss: BSS table entry
  904. * @vendor_type: Vendor type (four octets starting the IE payload)
  905. * Returns: Pointer to the information element payload or %NULL if not found
  906. *
  907. * This function returns concatenated payload of possibly fragmented vendor
  908. * specific information elements in the BSS entry. The caller is responsible for
  909. * freeing the returned buffer.
  910. */
  911. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  912. u32 vendor_type)
  913. {
  914. struct wpabuf *buf;
  915. const u8 *end, *pos;
  916. buf = wpabuf_alloc(bss->ie_len);
  917. if (buf == NULL)
  918. return NULL;
  919. pos = (const u8 *) (bss + 1);
  920. end = pos + bss->ie_len;
  921. while (pos + 1 < end) {
  922. if (pos + 2 + pos[1] > end)
  923. break;
  924. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  925. vendor_type == WPA_GET_BE32(&pos[2]))
  926. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  927. pos += 2 + pos[1];
  928. }
  929. if (wpabuf_len(buf) == 0) {
  930. wpabuf_free(buf);
  931. buf = NULL;
  932. }
  933. return buf;
  934. }
  935. /**
  936. * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
  937. * @bss: BSS table entry
  938. * @vendor_type: Vendor type (four octets starting the IE payload)
  939. * Returns: Pointer to the information element payload or %NULL if not found
  940. *
  941. * This function returns concatenated payload of possibly fragmented vendor
  942. * specific information elements in the BSS entry. The caller is responsible for
  943. * freeing the returned buffer.
  944. *
  945. * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
  946. * from Beacon frames instead of either Beacon or Probe Response frames.
  947. */
  948. struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
  949. u32 vendor_type)
  950. {
  951. struct wpabuf *buf;
  952. const u8 *end, *pos;
  953. buf = wpabuf_alloc(bss->beacon_ie_len);
  954. if (buf == NULL)
  955. return NULL;
  956. pos = (const u8 *) (bss + 1);
  957. pos += bss->ie_len;
  958. end = pos + bss->beacon_ie_len;
  959. while (pos + 1 < end) {
  960. if (pos + 2 + pos[1] > end)
  961. break;
  962. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  963. vendor_type == WPA_GET_BE32(&pos[2]))
  964. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  965. pos += 2 + pos[1];
  966. }
  967. if (wpabuf_len(buf) == 0) {
  968. wpabuf_free(buf);
  969. buf = NULL;
  970. }
  971. return buf;
  972. }
  973. /**
  974. * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
  975. * @bss: BSS table entry
  976. * Returns: Maximum legacy rate in units of 500 kbps
  977. */
  978. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  979. {
  980. int rate = 0;
  981. const u8 *ie;
  982. int i;
  983. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  984. for (i = 0; ie && i < ie[1]; i++) {
  985. if ((ie[i + 2] & 0x7f) > rate)
  986. rate = ie[i + 2] & 0x7f;
  987. }
  988. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  989. for (i = 0; ie && i < ie[1]; i++) {
  990. if ((ie[i + 2] & 0x7f) > rate)
  991. rate = ie[i + 2] & 0x7f;
  992. }
  993. return rate;
  994. }
  995. /**
  996. * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
  997. * @bss: BSS table entry
  998. * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
  999. * Returns: number of legacy TX rates or -1 on failure
  1000. *
  1001. * The caller is responsible for freeing the returned buffer with os_free() in
  1002. * case of success.
  1003. */
  1004. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  1005. {
  1006. const u8 *ie, *ie2;
  1007. int i, j;
  1008. unsigned int len;
  1009. u8 *r;
  1010. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1011. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  1012. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  1013. r = os_malloc(len);
  1014. if (!r)
  1015. return -1;
  1016. for (i = 0; ie && i < ie[1]; i++)
  1017. r[i] = ie[i + 2] & 0x7f;
  1018. for (j = 0; ie2 && j < ie2[1]; j++)
  1019. r[i + j] = ie2[j + 2] & 0x7f;
  1020. *rates = r;
  1021. return len;
  1022. }