bss.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. if (wpa_s->current_bss == bss)
  272. wpa_s->current_bss = nbss;
  273. bss = nbss;
  274. os_memcpy(bss + 1, res + 1,
  275. res->ie_len + res->beacon_ie_len);
  276. bss->ie_len = res->ie_len;
  277. bss->beacon_ie_len = res->beacon_ie_len;
  278. }
  279. dl_list_add(prev, &bss->list_id);
  280. }
  281. dl_list_add_tail(&wpa_s->bss, &bss->list);
  282. notify_bss_changes(wpa_s, changes, bss);
  283. }
  284. static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
  285. {
  286. return bss == wpa_s->current_bss ||
  287. os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
  288. os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
  289. }
  290. void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
  291. {
  292. wpa_s->bss_update_idx++;
  293. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
  294. wpa_s->bss_update_idx);
  295. }
  296. void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
  297. struct wpa_scan_res *res)
  298. {
  299. const u8 *ssid, *p2p;
  300. struct wpa_bss *bss;
  301. ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
  302. if (ssid == NULL) {
  303. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
  304. MACSTR, MAC2STR(res->bssid));
  305. return;
  306. }
  307. if (ssid[1] > 32) {
  308. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
  309. MACSTR, MAC2STR(res->bssid));
  310. return;
  311. }
  312. p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
  313. #ifdef CONFIG_P2P
  314. if (p2p == NULL &&
  315. wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
  316. /*
  317. * If it's a P2P specific interface, then don't update
  318. * the scan result without a P2P IE.
  319. */
  320. wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
  321. " update for P2P interface", MAC2STR(res->bssid));
  322. return;
  323. }
  324. #endif /* CONFIG_P2P */
  325. if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
  326. os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
  327. return; /* Skip P2P listen discovery results here */
  328. /* TODO: add option for ignoring BSSes we are not interested in
  329. * (to save memory) */
  330. bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
  331. if (bss == NULL)
  332. wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
  333. else
  334. wpa_bss_update(wpa_s, bss, res);
  335. }
  336. static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
  337. const struct scan_info *info)
  338. {
  339. int found;
  340. size_t i;
  341. if (info == NULL)
  342. return 1;
  343. if (info->num_freqs) {
  344. found = 0;
  345. for (i = 0; i < info->num_freqs; i++) {
  346. if (bss->freq == info->freqs[i]) {
  347. found = 1;
  348. break;
  349. }
  350. }
  351. if (!found)
  352. return 0;
  353. }
  354. if (info->num_ssids) {
  355. found = 0;
  356. for (i = 0; i < info->num_ssids; i++) {
  357. const struct wpa_driver_scan_ssid *s = &info->ssids[i];
  358. if ((s->ssid == NULL || s->ssid_len == 0) ||
  359. (s->ssid_len == bss->ssid_len &&
  360. os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
  361. 0)) {
  362. found = 1;
  363. break;
  364. }
  365. }
  366. if (!found)
  367. return 0;
  368. }
  369. return 1;
  370. }
  371. void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
  372. int new_scan)
  373. {
  374. struct wpa_bss *bss, *n;
  375. if (!new_scan)
  376. return; /* do not expire entries without new scan */
  377. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  378. if (wpa_bss_in_use(wpa_s, bss))
  379. continue;
  380. if (!wpa_bss_included_in_scan(bss, info))
  381. continue; /* expire only BSSes that were scanned */
  382. if (bss->last_update_idx < wpa_s->bss_update_idx)
  383. bss->scan_miss_count++;
  384. if (bss->scan_miss_count >=
  385. wpa_s->conf->bss_expiration_scan_count) {
  386. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
  387. "no match in scan", bss->id);
  388. wpa_bss_remove(wpa_s, bss);
  389. }
  390. }
  391. }
  392. void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
  393. {
  394. struct wpa_bss *bss, *n;
  395. struct os_time t;
  396. if (dl_list_empty(&wpa_s->bss))
  397. return;
  398. os_get_time(&t);
  399. t.sec -= age;
  400. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  401. if (wpa_bss_in_use(wpa_s, bss))
  402. continue;
  403. if (os_time_before(&bss->last_update, &t)) {
  404. wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
  405. "age", bss->id);
  406. wpa_bss_remove(wpa_s, bss);
  407. } else
  408. break;
  409. }
  410. }
  411. static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
  412. {
  413. struct wpa_supplicant *wpa_s = eloop_ctx;
  414. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  415. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  416. wpa_bss_timeout, wpa_s, NULL);
  417. }
  418. int wpa_bss_init(struct wpa_supplicant *wpa_s)
  419. {
  420. dl_list_init(&wpa_s->bss);
  421. dl_list_init(&wpa_s->bss_id);
  422. eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
  423. wpa_bss_timeout, wpa_s, NULL);
  424. return 0;
  425. }
  426. void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  427. {
  428. struct wpa_bss *bss, *n;
  429. if (wpa_s->bss.next == NULL)
  430. return; /* BSS table not yet initialized */
  431. dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
  432. if (wpa_bss_in_use(wpa_s, bss))
  433. continue;
  434. wpa_bss_remove(wpa_s, bss);
  435. }
  436. }
  437. void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
  438. {
  439. eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
  440. wpa_bss_flush(wpa_s);
  441. }
  442. struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
  443. const u8 *bssid)
  444. {
  445. struct wpa_bss *bss;
  446. if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
  447. return NULL;
  448. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  449. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
  450. return bss;
  451. }
  452. return NULL;
  453. }
  454. #ifdef CONFIG_P2P
  455. struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
  456. const u8 *dev_addr)
  457. {
  458. struct wpa_bss *bss;
  459. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
  460. u8 addr[ETH_ALEN];
  461. if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
  462. addr) == 0 &&
  463. os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
  464. return bss;
  465. }
  466. return NULL;
  467. }
  468. #endif /* CONFIG_P2P */
  469. struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
  470. {
  471. struct wpa_bss *bss;
  472. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  473. if (bss->id == id)
  474. return bss;
  475. }
  476. return NULL;
  477. }
  478. const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
  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] == ie)
  487. return pos;
  488. pos += 2 + pos[1];
  489. }
  490. return NULL;
  491. }
  492. const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
  493. {
  494. const u8 *end, *pos;
  495. pos = (const u8 *) (bss + 1);
  496. end = pos + bss->ie_len;
  497. while (pos + 1 < end) {
  498. if (pos + 2 + pos[1] > end)
  499. break;
  500. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  501. vendor_type == WPA_GET_BE32(&pos[2]))
  502. return pos;
  503. pos += 2 + pos[1];
  504. }
  505. return NULL;
  506. }
  507. struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
  508. u32 vendor_type)
  509. {
  510. struct wpabuf *buf;
  511. const u8 *end, *pos;
  512. buf = wpabuf_alloc(bss->ie_len);
  513. if (buf == NULL)
  514. return NULL;
  515. pos = (const u8 *) (bss + 1);
  516. end = pos + bss->ie_len;
  517. while (pos + 1 < end) {
  518. if (pos + 2 + pos[1] > end)
  519. break;
  520. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  521. vendor_type == WPA_GET_BE32(&pos[2]))
  522. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  523. pos += 2 + pos[1];
  524. }
  525. if (wpabuf_len(buf) == 0) {
  526. wpabuf_free(buf);
  527. buf = NULL;
  528. }
  529. return buf;
  530. }
  531. int wpa_bss_get_max_rate(const struct wpa_bss *bss)
  532. {
  533. int rate = 0;
  534. const u8 *ie;
  535. int i;
  536. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  537. for (i = 0; ie && i < ie[1]; i++) {
  538. if ((ie[i + 2] & 0x7f) > rate)
  539. rate = ie[i + 2] & 0x7f;
  540. }
  541. ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  542. for (i = 0; ie && i < ie[1]; i++) {
  543. if ((ie[i + 2] & 0x7f) > rate)
  544. rate = ie[i + 2] & 0x7f;
  545. }
  546. return rate;
  547. }
  548. int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
  549. {
  550. const u8 *ie, *ie2;
  551. int i, j;
  552. unsigned int len;
  553. u8 *r;
  554. ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  555. ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  556. len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
  557. r = os_malloc(len);
  558. if (!r)
  559. return -1;
  560. for (i = 0; ie && i < ie[1]; i++)
  561. r[i] = ie[i + 2] & 0x7f;
  562. for (j = 0; ie2 && j < ie2[1]; j++)
  563. r[i + j] = ie2[j + 2] & 0x7f;
  564. *rates = r;
  565. return len;
  566. }