bss.c 16 KB

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