bss.c 17 KB

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