bss.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * BSS table
  3. * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "drivers/driver.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "config.h"
  21. #include "notify.h"
  22. #include "scan.h"
  23. #include "bss.h"
  24. /**
  25. * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
  26. */
  27. #define WPA_BSS_EXPIRATION_PERIOD 10
  28. #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
  29. #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
  30. #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
  31. #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
  32. #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
  33. #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
  34. #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
  35. #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
  36. #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
  37. static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  38. {
  39. dl_list_del(&bss->list);
  40. dl_list_del(&bss->list_id);
  41. wpa_s->num_bss--;
  42. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
  43. " SSID '%s'", bss->id, MAC2STR(bss->bssid),
  44. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  45. wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
  46. os_free(bss);
  47. }
  48. struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
  49. const u8 *ssid, size_t ssid_len)
  50. {
  51. struct wpa_bss *bss;
  52. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  53. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  54. bss->ssid_len == ssid_len &&
  55. os_memcmp(bss->ssid, ssid, ssid_len) == 0)
  56. return bss;
  57. }
  58. return NULL;
  59. }
  60. static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
  61. {
  62. os_time_t usec;
  63. dst->flags = src->flags;
  64. os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
  65. dst->freq = src->freq;
  66. dst->beacon_int = src->beacon_int;
  67. dst->caps = src->caps;
  68. dst->qual = src->qual;
  69. dst->noise = src->noise;
  70. dst->level = src->level;
  71. dst->tsf = src->tsf;
  72. os_get_time(&dst->last_update);
  73. dst->last_update.sec -= src->age / 1000;
  74. usec = (src->age % 1000) * 1000;
  75. if (dst->last_update.usec < usec) {
  76. dst->last_update.sec--;
  77. dst->last_update.usec += 1000000;
  78. }
  79. dst->last_update.usec -= usec;
  80. }
  81. static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  82. {
  83. struct wpa_ssid *ssid;
  84. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  85. if (ssid->ssid == NULL || ssid->ssid_len == 0)
  86. continue;
  87. if (ssid->ssid_len == bss->ssid_len &&
  88. os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
  89. return 1;
  90. }
  91. return 0;
  92. }
  93. static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
  94. {
  95. struct wpa_bss *bss;
  96. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  97. if (!wpa_bss_known(wpa_s, bss)) {
  98. wpa_bss_remove(wpa_s, bss);
  99. return 0;
  100. }
  101. }
  102. return -1;
  103. }
  104. static void wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
  105. {
  106. /*
  107. * Remove the oldest entry that does not match with any configured
  108. * network.
  109. */
  110. if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
  111. return;
  112. /*
  113. * Remove the oldest entry since no better candidate for removal was
  114. * found.
  115. */
  116. wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
  117. struct wpa_bss, list));
  118. }
  119. static void wpa_bss_add(struct wpa_supplicant *wpa_s,
  120. const u8 *ssid, size_t ssid_len,
  121. struct wpa_scan_res *res)
  122. {
  123. struct wpa_bss *bss;
  124. bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
  125. if (bss == NULL)
  126. return;
  127. bss->id = wpa_s->bss_next_id++;
  128. bss->last_update_idx = wpa_s->bss_update_idx;
  129. wpa_bss_copy_res(bss, res);
  130. os_memcpy(bss->ssid, ssid, ssid_len);
  131. bss->ssid_len = ssid_len;
  132. bss->ie_len = res->ie_len;
  133. bss->beacon_ie_len = res->beacon_ie_len;
  134. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  135. dl_list_add_tail(&wpa_s->bss, &bss->list);
  136. dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
  137. wpa_s->num_bss++;
  138. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
  139. " SSID '%s'",
  140. bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
  141. wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
  142. if (wpa_s->num_bss > wpa_s->conf->bss_max_count)
  143. wpa_bss_remove_oldest(wpa_s);
  144. }
  145. static int are_ies_equal(const struct wpa_bss *old,
  146. const struct wpa_scan_res *new, u32 ie)
  147. {
  148. const u8 *old_ie, *new_ie;
  149. struct wpabuf *old_ie_buff = NULL;
  150. struct wpabuf *new_ie_buff = NULL;
  151. int new_ie_len, old_ie_len, ret, is_multi;
  152. switch (ie) {
  153. case WPA_IE_VENDOR_TYPE:
  154. old_ie = wpa_bss_get_vendor_ie(old, ie);
  155. new_ie = wpa_scan_get_vendor_ie(new, ie);
  156. is_multi = 0;
  157. break;
  158. case WPS_IE_VENDOR_TYPE:
  159. old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
  160. new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
  161. is_multi = 1;
  162. break;
  163. case WLAN_EID_RSN:
  164. case WLAN_EID_SUPP_RATES:
  165. case WLAN_EID_EXT_SUPP_RATES:
  166. old_ie = wpa_bss_get_ie(old, ie);
  167. new_ie = wpa_scan_get_ie(new, ie);
  168. is_multi = 0;
  169. break;
  170. default:
  171. wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
  172. return 0;
  173. }
  174. if (is_multi) {
  175. /* in case of multiple IEs stored in buffer */
  176. old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
  177. new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
  178. old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
  179. new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
  180. } else {
  181. /* in case of single IE */
  182. old_ie_len = old_ie ? old_ie[1] + 2 : 0;
  183. new_ie_len = new_ie ? new_ie[1] + 2 : 0;
  184. }
  185. if (!old_ie || !new_ie)
  186. ret = !old_ie && !new_ie;
  187. else
  188. ret = (old_ie_len == new_ie_len &&
  189. os_memcmp(old_ie, new_ie, old_ie_len) == 0);
  190. wpabuf_free(old_ie_buff);
  191. wpabuf_free(new_ie_buff);
  192. return ret;
  193. }
  194. static u32 wpa_bss_compare_res(const struct wpa_bss *old,
  195. const struct wpa_scan_res *new)
  196. {
  197. u32 changes = 0;
  198. int caps_diff = old->caps ^ new->caps;
  199. if (old->freq != new->freq)
  200. changes |= WPA_BSS_FREQ_CHANGED_FLAG;
  201. if (old->level != new->level)
  202. changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
  203. if (caps_diff & IEEE80211_CAP_PRIVACY)
  204. changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
  205. if (caps_diff & IEEE80211_CAP_IBSS)
  206. changes |= WPA_BSS_MODE_CHANGED_FLAG;
  207. if (old->ie_len == new->ie_len &&
  208. os_memcmp(old + 1, new + 1, old->ie_len) == 0)
  209. return changes;
  210. changes |= WPA_BSS_IES_CHANGED_FLAG;
  211. if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
  212. changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
  213. if (!are_ies_equal(old, new, WLAN_EID_RSN))
  214. changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
  215. if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
  216. changes |= WPA_BSS_WPS_CHANGED_FLAG;
  217. if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
  218. !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
  219. changes |= WPA_BSS_RATES_CHANGED_FLAG;
  220. return changes;
  221. }
  222. static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
  223. const struct wpa_bss *bss)
  224. {
  225. if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
  226. wpas_notify_bss_freq_changed(wpa_s, bss->id);
  227. if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
  228. wpas_notify_bss_signal_changed(wpa_s, bss->id);
  229. if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
  230. wpas_notify_bss_privacy_changed(wpa_s, bss->id);
  231. if (changes & WPA_BSS_MODE_CHANGED_FLAG)
  232. wpas_notify_bss_mode_changed(wpa_s, bss->id);
  233. if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
  234. wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
  235. if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
  236. wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
  237. if (changes & WPA_BSS_WPS_CHANGED_FLAG)
  238. wpas_notify_bss_wps_changed(wpa_s, bss->id);
  239. if (changes & WPA_BSS_IES_CHANGED_FLAG)
  240. wpas_notify_bss_ies_changed(wpa_s, bss->id);
  241. if (changes & WPA_BSS_RATES_CHANGED_FLAG)
  242. wpas_notify_bss_rates_changed(wpa_s, bss->id);
  243. }
  244. static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  245. struct wpa_scan_res *res)
  246. {
  247. u32 changes;
  248. changes = wpa_bss_compare_res(bss, res);
  249. bss->scan_miss_count = 0;
  250. bss->last_update_idx = wpa_s->bss_update_idx;
  251. wpa_bss_copy_res(bss, res);
  252. /* Move the entry to the end of the list */
  253. dl_list_del(&bss->list);
  254. if (bss->ie_len + bss->beacon_ie_len >=
  255. res->ie_len + res->beacon_ie_len) {
  256. os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
  257. bss->ie_len = res->ie_len;
  258. bss->beacon_ie_len = res->beacon_ie_len;
  259. } else {
  260. struct wpa_bss *nbss;
  261. struct dl_list *prev = bss->list_id.prev;
  262. dl_list_del(&bss->list_id);
  263. nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
  264. res->beacon_ie_len);
  265. if (nbss) {
  266. bss = nbss;
  267. os_memcpy(bss + 1, res + 1,
  268. res->ie_len + res->beacon_ie_len);
  269. bss->ie_len = res->ie_len;
  270. bss->beacon_ie_len = res->beacon_ie_len;
  271. }
  272. dl_list_add(prev, &bss->list_id);
  273. }
  274. dl_list_add_tail(&wpa_s->bss, &bss->list);
  275. notify_bss_changes(wpa_s, changes, bss);
  276. }
  277. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  278. {
  279. return bss == wpa_s->current_bss ||
  280. os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  281. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
  282. }
  283. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  284. {
  285. wpa_s->bss_update_idx++;
  286. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  287. wpa_s->bss_update_idx);
  288. }
  289. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  290. struct wpa_scan_res *res)
  291. {
  292. const u8 *ssid, *p2p;
  293. struct wpa_bss *bss;
  294. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  295. if (ssid == NULL) {
  296. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  297. MACSTR, MAC2STR(res->bssid));
  298. return;
  299. }
  300. if (ssid[1] > 32) {
  301. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  302. MACSTR, MAC2STR(res->bssid));
  303. return;
  304. }
  305. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  306. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  307. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  308. return; /* Skip P2P listen discovery results here */
  309. /* TODO: add option for ignoring BSSes we are not interested in
  310. * (to save memory) */
  311. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  312. if (bss == NULL)
  313. wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  314. else
  315. wpa_bss_update(wpa_s, bss, res);
  316. }
  317. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  318. const struct scan_info *info)
  319. {
  320. int found;
  321. size_t i;
  322. if (info == NULL)
  323. return 1;
  324. if (info->num_freqs) {
  325. found = 0;
  326. for (i = 0; i < info->num_freqs; i++) {
  327. if (bss->freq == info->freqs[i]) {
  328. found = 1;
  329. break;
  330. }
  331. }
  332. if (!found)
  333. return 0;
  334. }
  335. if (info->num_ssids) {
  336. found = 0;
  337. for (i = 0; i < info->num_ssids; i++) {
  338. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  339. if ((s->ssid == NULL || s->ssid_len == 0) ||
  340. (s->ssid_len == bss->ssid_len &&
  341. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  342. 0)) {
  343. found = 1;
  344. break;
  345. }
  346. }
  347. if (!found)
  348. return 0;
  349. }
  350. return 1;
  351. }
  352. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  353. int new_scan)
  354. {
  355. struct wpa_bss *bss, *n;
  356. if (!new_scan)
  357. return; /* do not expire entries without new scan */
  358. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  359. if (wpa_bss_in_use(wpa_s, bss))
  360. continue;
  361. if (!wpa_bss_included_in_scan(bss, info))
  362. continue; /* expire only BSSes that were scanned */
  363. if (bss->last_update_idx < wpa_s->bss_update_idx)
  364. bss->scan_miss_count++;
  365. if (bss->scan_miss_count >=
  366. wpa_s->conf->bss_expiration_scan_count) {
  367. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
  368. "no match in scan", bss->id);
  369. wpa_bss_remove(wpa_s, bss);
  370. }
  371. }
  372. }
  373. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  374. {
  375. struct wpa_bss *bss, *n;
  376. struct os_time t;
  377. if (dl_list_empty(&wpa_s->bss))
  378. return;
  379. os_get_time(&t);
  380. t.sec -= age;
  381. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  382. if (wpa_bss_in_use(wpa_s, bss))
  383. continue;
  384. if (os_time_before(&bss->last_update, &t)) {
  385. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
  386. "age", bss->id);
  387. wpa_bss_remove(wpa_s, bss);
  388. } else
  389. break;
  390. }
  391. }
  392. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  393. {
  394. struct wpa_supplicant *wpa_s = eloop_ctx;
  395. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  396. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  397. wpa_bss_timeout, wpa_s, NULL);
  398. }
  399. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  400. {
  401. dl_list_init(&wpa_s->bss);
  402. dl_list_init(&wpa_s->bss_id);
  403. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  404. wpa_bss_timeout, wpa_s, NULL);
  405. return 0;
  406. }
  407. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  408. {
  409. struct wpa_bss *bss, *n;
  410. if (wpa_s->bss.next == NULL)
  411. return; /* BSS table not yet initialized */
  412. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  413. if (wpa_bss_in_use(wpa_s, bss))
  414. continue;
  415. wpa_bss_remove(wpa_s, bss);
  416. }
  417. }
  418. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  419. {
  420. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  421. wpa_bss_flush(wpa_s);
  422. }
  423. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  424. const u8 *bssid)
  425. {
  426. struct wpa_bss *bss;
  427. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  428. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  429. return bss;
  430. }
  431. return NULL;
  432. }
  433. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  434. {
  435. struct wpa_bss *bss;
  436. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  437. if (bss->id == id)
  438. return bss;
  439. }
  440. return NULL;
  441. }
  442. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  443. {
  444. const u8 *end, *pos;
  445. pos = (const u8 *) (bss + 1);
  446. end = pos + bss->ie_len;
  447. while (pos + 1 < end) {
  448. if (pos + 2 + pos[1] > end)
  449. break;
  450. if (pos[0] == ie)
  451. return pos;
  452. pos += 2 + pos[1];
  453. }
  454. return NULL;
  455. }
  456. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  457. {
  458. const u8 *end, *pos;
  459. pos = (const u8 *) (bss + 1);
  460. end = pos + bss->ie_len;
  461. while (pos + 1 < end) {
  462. if (pos + 2 + pos[1] > end)
  463. break;
  464. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  465. vendor_type == WPA_GET_BE32(&pos[2]))
  466. return pos;
  467. pos += 2 + pos[1];
  468. }
  469. return NULL;
  470. }
  471. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  472. u32 vendor_type)
  473. {
  474. struct wpabuf *buf;
  475. const u8 *end, *pos;
  476. buf = wpabuf_alloc(bss->ie_len);
  477. if (buf == NULL)
  478. return NULL;
  479. pos = (const u8 *) (bss + 1);
  480. end = pos + bss->ie_len;
  481. while (pos + 1 < end) {
  482. if (pos + 2 + pos[1] > end)
  483. break;
  484. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  485. vendor_type == WPA_GET_BE32(&pos[2]))
  486. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  487. pos += 2 + pos[1];
  488. }
  489. if (wpabuf_len(buf) == 0) {
  490. wpabuf_free(buf);
  491. buf = NULL;
  492. }
  493. return buf;
  494. }
  495. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  496. {
  497. int rate = 0;
  498. const u8 *ie;
  499. int i;
  500. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  501. for (i = 0; ie && i < ie[1]; i++) {
  502. if ((ie[i + 2] & 0x7f) > rate)
  503. rate = ie[i + 2] & 0x7f;
  504. }
  505. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  506. for (i = 0; ie && i < ie[1]; i++) {
  507. if ((ie[i + 2] & 0x7f) > rate)
  508. rate = ie[i + 2] & 0x7f;
  509. }
  510. return rate;
  511. }
  512. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  513. {
  514. const u8 *ie, *ie2;
  515. int i, j;
  516. unsigned int len;
  517. u8 *r;
  518. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  519. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  520. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  521. r = os_malloc(len);
  522. if (!r)
  523. return -1;
  524. for (i = 0; ie && i < ie[1]; i++)
  525. r[i] = ie[i + 2] & 0x7f;
  526. for (j = 0; ie2 && j < ie2[1]; j++)
  527. r[i + j] = ie2[j + 2] & 0x7f;
  528. *rates = r;
  529. return len;
  530. }