bss.c 16 KB

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