3 * Copyright (c) 2009-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "drivers/driver.h"
15 #include "wpa_supplicant_i.h"
23 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
25 #define WPA_BSS_EXPIRATION_PERIOD 10
27 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
28 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
29 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
30 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
31 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
32 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
33 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
34 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
35 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
38 static void wpa_bss_set_hessid(struct wpa_bss *bss)
40 #ifdef CONFIG_INTERWORKING
41 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
42 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
43 os_memset(bss->hessid, 0, ETH_ALEN);
47 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
49 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
50 #endif /* CONFIG_INTERWORKING */
55 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
56 * Returns: Allocated ANQP data structure or %NULL on failure
58 * The allocated ANQP data structure has its users count set to 1. It may be
59 * shared by multiple BSS entries and each shared entry is freed with
60 * wpa_bss_anqp_free().
62 struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
64 struct wpa_bss_anqp *anqp;
65 anqp = os_zalloc(sizeof(*anqp));
74 * wpa_bss_anqp_clone - Clone an ANQP data structure
75 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
76 * Returns: Cloned ANQP data structure or %NULL on failure
78 static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
80 struct wpa_bss_anqp *n;
82 n = os_zalloc(sizeof(*n));
86 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
87 #ifdef CONFIG_INTERWORKING
89 ANQP_DUP(network_auth_type);
90 ANQP_DUP(roaming_consortium);
91 ANQP_DUP(ip_addr_type_availability);
94 ANQP_DUP(domain_name);
95 #endif /* CONFIG_INTERWORKING */
97 ANQP_DUP(hs20_operator_friendly_name);
98 ANQP_DUP(hs20_wan_metrics);
99 ANQP_DUP(hs20_connection_capability);
100 ANQP_DUP(hs20_operating_class);
101 ANQP_DUP(hs20_osu_providers_list);
102 #endif /* CONFIG_HS20 */
110 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
112 * Returns: 0 on success, -1 on failure
114 * This function ensures the specific BSS entry has an ANQP data structure that
115 * is not shared with any other BSS entry.
117 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
119 struct wpa_bss_anqp *anqp;
121 if (bss->anqp && bss->anqp->users > 1) {
122 /* allocated, but shared - clone an unshared copy */
123 anqp = wpa_bss_anqp_clone(bss->anqp);
133 return 0; /* already allocated and not shared */
135 /* not allocated - allocate a new storage area */
136 bss->anqp = wpa_bss_anqp_alloc();
137 return bss->anqp ? 0 : -1;
142 * wpa_bss_anqp_free - Free an ANQP data structure
143 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
145 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
151 if (anqp->users > 0) {
152 /* Another BSS entry holds a pointer to this ANQP info */
156 #ifdef CONFIG_INTERWORKING
157 wpabuf_free(anqp->venue_name);
158 wpabuf_free(anqp->network_auth_type);
159 wpabuf_free(anqp->roaming_consortium);
160 wpabuf_free(anqp->ip_addr_type_availability);
161 wpabuf_free(anqp->nai_realm);
162 wpabuf_free(anqp->anqp_3gpp);
163 wpabuf_free(anqp->domain_name);
164 #endif /* CONFIG_INTERWORKING */
166 wpabuf_free(anqp->hs20_operator_friendly_name);
167 wpabuf_free(anqp->hs20_wan_metrics);
168 wpabuf_free(anqp->hs20_connection_capability);
169 wpabuf_free(anqp->hs20_operating_class);
170 wpabuf_free(anqp->hs20_osu_providers_list);
171 #endif /* CONFIG_HS20 */
177 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
180 if (wpa_s->last_scan_res) {
182 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
183 if (wpa_s->last_scan_res[i] == bss) {
184 os_memmove(&wpa_s->last_scan_res[i],
185 &wpa_s->last_scan_res[i + 1],
186 (wpa_s->last_scan_res_used - i - 1)
187 * sizeof(struct wpa_bss *));
188 wpa_s->last_scan_res_used--;
193 dl_list_del(&bss->list);
194 dl_list_del(&bss->list_id);
196 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
197 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
198 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
199 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
200 wpa_bss_anqp_free(bss->anqp);
206 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
207 * @wpa_s: Pointer to wpa_supplicant data
210 * @ssid_len: Length of @ssid
211 * Returns: Pointer to the BSS entry or %NULL if not found
213 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
214 const u8 *ssid, size_t ssid_len)
217 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
219 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
220 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
221 bss->ssid_len == ssid_len &&
222 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
229 static void calculate_update_time(const struct os_reltime *fetch_time,
231 struct os_reltime *update_time)
235 update_time->sec = fetch_time->sec;
236 update_time->usec = fetch_time->usec;
237 update_time->sec -= age_ms / 1000;
238 usec = (age_ms % 1000) * 1000;
239 if (update_time->usec < usec) {
241 update_time->usec += 1000000;
243 update_time->usec -= usec;
247 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
248 struct os_reltime *fetch_time)
250 dst->flags = src->flags;
251 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
252 dst->freq = src->freq;
253 dst->beacon_int = src->beacon_int;
254 dst->caps = src->caps;
255 dst->qual = src->qual;
256 dst->noise = src->noise;
257 dst->level = src->level;
260 calculate_update_time(fetch_time, src->age, &dst->last_update);
264 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
266 struct wpa_ssid *ssid;
268 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
269 if (ssid->ssid == NULL || ssid->ssid_len == 0)
271 if (ssid->ssid_len == bss->ssid_len &&
272 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
280 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
282 return bss == wpa_s->current_bss ||
283 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
284 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
288 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
292 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
293 if (!wpa_bss_known(wpa_s, bss)) {
294 wpa_bss_remove(wpa_s, bss, __func__);
303 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
308 * Remove the oldest entry that does not match with any configured
311 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
315 * Remove the oldest entry that isn't currently in use.
317 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
318 if (!wpa_bss_in_use(wpa_s, bss)) {
319 wpa_bss_remove(wpa_s, bss, __func__);
328 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
329 const u8 *ssid, size_t ssid_len,
330 struct wpa_scan_res *res,
331 struct os_reltime *fetch_time)
335 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
338 bss->id = wpa_s->bss_next_id++;
339 bss->last_update_idx = wpa_s->bss_update_idx;
340 wpa_bss_copy_res(bss, res, fetch_time);
341 os_memcpy(bss->ssid, ssid, ssid_len);
342 bss->ssid_len = ssid_len;
343 bss->ie_len = res->ie_len;
344 bss->beacon_ie_len = res->beacon_ie_len;
345 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
346 wpa_bss_set_hessid(bss);
348 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
349 wpa_bss_remove_oldest(wpa_s) != 0) {
350 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
351 "because all BSSes are in use. We should normally "
352 "not get here!", (int) wpa_s->num_bss + 1);
353 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
356 dl_list_add_tail(&wpa_s->bss, &bss->list);
357 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
359 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
361 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
362 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
367 static int are_ies_equal(const struct wpa_bss *old,
368 const struct wpa_scan_res *new, u32 ie)
370 const u8 *old_ie, *new_ie;
371 struct wpabuf *old_ie_buff = NULL;
372 struct wpabuf *new_ie_buff = NULL;
373 int new_ie_len, old_ie_len, ret, is_multi;
376 case WPA_IE_VENDOR_TYPE:
377 old_ie = wpa_bss_get_vendor_ie(old, ie);
378 new_ie = wpa_scan_get_vendor_ie(new, ie);
381 case WPS_IE_VENDOR_TYPE:
382 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
383 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
387 case WLAN_EID_SUPP_RATES:
388 case WLAN_EID_EXT_SUPP_RATES:
389 old_ie = wpa_bss_get_ie(old, ie);
390 new_ie = wpa_scan_get_ie(new, ie);
394 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
399 /* in case of multiple IEs stored in buffer */
400 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
401 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
402 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
403 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
405 /* in case of single IE */
406 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
407 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
410 if (!old_ie || !new_ie)
411 ret = !old_ie && !new_ie;
413 ret = (old_ie_len == new_ie_len &&
414 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
416 wpabuf_free(old_ie_buff);
417 wpabuf_free(new_ie_buff);
423 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
424 const struct wpa_scan_res *new)
427 int caps_diff = old->caps ^ new->caps;
429 if (old->freq != new->freq)
430 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
432 if (old->level != new->level)
433 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
435 if (caps_diff & IEEE80211_CAP_PRIVACY)
436 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
438 if (caps_diff & IEEE80211_CAP_IBSS)
439 changes |= WPA_BSS_MODE_CHANGED_FLAG;
441 if (old->ie_len == new->ie_len &&
442 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
444 changes |= WPA_BSS_IES_CHANGED_FLAG;
446 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
447 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
449 if (!are_ies_equal(old, new, WLAN_EID_RSN))
450 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
452 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
453 changes |= WPA_BSS_WPS_CHANGED_FLAG;
455 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
456 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
457 changes |= WPA_BSS_RATES_CHANGED_FLAG;
463 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
464 const struct wpa_bss *bss)
466 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
467 wpas_notify_bss_freq_changed(wpa_s, bss->id);
469 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
470 wpas_notify_bss_signal_changed(wpa_s, bss->id);
472 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
473 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
475 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
476 wpas_notify_bss_mode_changed(wpa_s, bss->id);
478 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
479 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
481 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
482 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
484 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
485 wpas_notify_bss_wps_changed(wpa_s, bss->id);
487 if (changes & WPA_BSS_IES_CHANGED_FLAG)
488 wpas_notify_bss_ies_changed(wpa_s, bss->id);
490 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
491 wpas_notify_bss_rates_changed(wpa_s, bss->id);
495 static struct wpa_bss *
496 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
497 struct wpa_scan_res *res, struct os_reltime *fetch_time)
501 changes = wpa_bss_compare_res(bss, res);
502 bss->scan_miss_count = 0;
503 bss->last_update_idx = wpa_s->bss_update_idx;
504 wpa_bss_copy_res(bss, res, fetch_time);
505 /* Move the entry to the end of the list */
506 dl_list_del(&bss->list);
508 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
509 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
511 * This can happen when non-P2P station interface runs a scan
512 * without P2P IE in the Probe Request frame. P2P GO would reply
513 * to that with a Probe Response that does not include P2P IE.
514 * Do not update the IEs in this BSS entry to avoid such loss of
515 * information that may be needed for P2P operations to
516 * determine group information.
518 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
519 MACSTR " since that would remove P2P IE information",
520 MAC2STR(bss->bssid));
522 #endif /* CONFIG_P2P */
523 if (bss->ie_len + bss->beacon_ie_len >=
524 res->ie_len + res->beacon_ie_len) {
525 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
526 bss->ie_len = res->ie_len;
527 bss->beacon_ie_len = res->beacon_ie_len;
529 struct wpa_bss *nbss;
530 struct dl_list *prev = bss->list_id.prev;
531 dl_list_del(&bss->list_id);
532 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
536 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
537 if (wpa_s->last_scan_res[i] == bss) {
538 wpa_s->last_scan_res[i] = nbss;
542 if (wpa_s->current_bss == bss)
543 wpa_s->current_bss = nbss;
545 os_memcpy(bss + 1, res + 1,
546 res->ie_len + res->beacon_ie_len);
547 bss->ie_len = res->ie_len;
548 bss->beacon_ie_len = res->beacon_ie_len;
550 dl_list_add(prev, &bss->list_id);
552 if (changes & WPA_BSS_IES_CHANGED_FLAG)
553 wpa_bss_set_hessid(bss);
554 dl_list_add_tail(&wpa_s->bss, &bss->list);
556 notify_bss_changes(wpa_s, changes, bss);
563 * wpa_bss_update_start - Start a BSS table update from scan results
564 * @wpa_s: Pointer to wpa_supplicant data
566 * This function is called at the start of each BSS table update round for new
567 * scan results. The actual scan result entries are indicated with calls to
568 * wpa_bss_update_scan_res() and the update round is finished with a call to
569 * wpa_bss_update_end().
571 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
573 wpa_s->bss_update_idx++;
574 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
575 wpa_s->bss_update_idx);
576 wpa_s->last_scan_res_used = 0;
581 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
582 * @wpa_s: Pointer to wpa_supplicant data
584 * @fetch_time: Time when the result was fetched from the driver
586 * This function updates a BSS table entry (or adds one) based on a scan result.
587 * This is called separately for each scan result between the calls to
588 * wpa_bss_update_start() and wpa_bss_update_end().
590 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
591 struct wpa_scan_res *res,
592 struct os_reltime *fetch_time)
594 const u8 *ssid, *p2p;
597 if (wpa_s->conf->ignore_old_scan_res) {
598 struct os_reltime update;
599 calculate_update_time(fetch_time, res->age, &update);
600 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
601 struct os_reltime age;
602 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
604 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
605 "table entry that is %u.%06u seconds older "
606 "than our scan trigger",
607 (unsigned int) age.sec,
608 (unsigned int) age.usec);
613 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
615 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
616 MACSTR, MAC2STR(res->bssid));
620 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
621 MACSTR, MAC2STR(res->bssid));
625 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
628 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
630 * If it's a P2P specific interface, then don't update
631 * the scan result without a P2P IE.
633 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
634 " update for P2P interface", MAC2STR(res->bssid));
637 #endif /* CONFIG_P2P */
638 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
639 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
640 return; /* Skip P2P listen discovery results here */
642 /* TODO: add option for ignoring BSSes we are not interested in
643 * (to save memory) */
644 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
646 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
648 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
649 if (wpa_s->last_scan_res) {
651 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
652 if (bss == wpa_s->last_scan_res[i]) {
653 /* Already in the list */
662 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
665 if (wpa_s->last_scan_res_size == 0)
668 siz = wpa_s->last_scan_res_size * 2;
669 n = os_realloc_array(wpa_s->last_scan_res, siz,
670 sizeof(struct wpa_bss *));
673 wpa_s->last_scan_res = n;
674 wpa_s->last_scan_res_size = siz;
677 if (wpa_s->last_scan_res)
678 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
682 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
683 const struct scan_info *info)
691 if (info->num_freqs) {
693 for (i = 0; i < info->num_freqs; i++) {
694 if (bss->freq == info->freqs[i]) {
703 if (info->num_ssids) {
705 for (i = 0; i < info->num_ssids; i++) {
706 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
707 if ((s->ssid == NULL || s->ssid_len == 0) ||
708 (s->ssid_len == bss->ssid_len &&
709 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
724 * wpa_bss_update_end - End a BSS table update from scan results
725 * @wpa_s: Pointer to wpa_supplicant data
726 * @info: Information about scan parameters
727 * @new_scan: Whether this update round was based on a new scan
729 * This function is called at the end of each BSS table update round for new
730 * scan results. The start of the update was indicated with a call to
731 * wpa_bss_update_start().
733 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
736 struct wpa_bss *bss, *n;
738 os_get_reltime(&wpa_s->last_scan);
740 return; /* do not expire entries without new scan */
742 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
743 if (wpa_bss_in_use(wpa_s, bss))
745 if (!wpa_bss_included_in_scan(bss, info))
746 continue; /* expire only BSSes that were scanned */
747 if (bss->last_update_idx < wpa_s->bss_update_idx)
748 bss->scan_miss_count++;
749 if (bss->scan_miss_count >=
750 wpa_s->conf->bss_expiration_scan_count) {
751 wpa_bss_remove(wpa_s, bss, "no match in scan");
755 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
756 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
761 * wpa_bss_flush_by_age - Flush old BSS entries
762 * @wpa_s: Pointer to wpa_supplicant data
763 * @age: Maximum entry age in seconds
765 * Remove BSS entries that have not been updated during the last @age seconds.
767 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
769 struct wpa_bss *bss, *n;
772 if (dl_list_empty(&wpa_s->bss))
778 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
779 if (wpa_bss_in_use(wpa_s, bss))
782 if (os_reltime_before(&bss->last_update, &t)) {
783 wpa_bss_remove(wpa_s, bss, __func__);
790 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
792 struct wpa_supplicant *wpa_s = eloop_ctx;
794 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
795 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
796 wpa_bss_timeout, wpa_s, NULL);
801 * wpa_bss_init - Initialize BSS table
802 * @wpa_s: Pointer to wpa_supplicant data
803 * Returns: 0 on success, -1 on failure
805 * This prepares BSS table lists and timer for periodic updates. The BSS table
806 * is deinitialized with wpa_bss_deinit() once not needed anymore.
808 int wpa_bss_init(struct wpa_supplicant *wpa_s)
810 dl_list_init(&wpa_s->bss);
811 dl_list_init(&wpa_s->bss_id);
812 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
813 wpa_bss_timeout, wpa_s, NULL);
819 * wpa_bss_flush - Flush all unused BSS entries
820 * @wpa_s: Pointer to wpa_supplicant data
822 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
824 struct wpa_bss *bss, *n;
826 wpa_s->clear_driver_scan_cache = 1;
828 if (wpa_s->bss.next == NULL)
829 return; /* BSS table not yet initialized */
831 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
832 if (wpa_bss_in_use(wpa_s, bss))
834 wpa_bss_remove(wpa_s, bss, __func__);
840 * wpa_bss_deinit - Deinitialize BSS table
841 * @wpa_s: Pointer to wpa_supplicant data
843 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
845 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
846 wpa_bss_flush(wpa_s);
851 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
852 * @wpa_s: Pointer to wpa_supplicant data
854 * Returns: Pointer to the BSS entry or %NULL if not found
856 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
860 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
862 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
863 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
871 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
872 * @wpa_s: Pointer to wpa_supplicant data
874 * Returns: Pointer to the BSS entry or %NULL if not found
876 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
877 * find the entry that has the most recent update. This can help in finding the
878 * correct entry in cases where the SSID of the AP may have changed recently
879 * (e.g., in WPS reconfiguration cases).
881 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
884 struct wpa_bss *bss, *found = NULL;
885 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
887 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
888 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
891 os_reltime_before(&found->last_update, &bss->last_update))
900 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
901 * @wpa_s: Pointer to wpa_supplicant data
902 * @dev_addr: P2P Device Address of the GO
903 * Returns: Pointer to the BSS entry or %NULL if not found
905 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
909 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
911 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
913 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
918 #endif /* CONFIG_P2P */
922 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
923 * @wpa_s: Pointer to wpa_supplicant data
924 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
925 * Returns: Pointer to the BSS entry or %NULL if not found
927 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
930 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
939 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
940 * @wpa_s: Pointer to wpa_supplicant data
941 * @idf: Smallest allowed identifier assigned for the entry
942 * @idf: Largest allowed identifier assigned for the entry
943 * Returns: Pointer to the BSS entry or %NULL if not found
945 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
946 * smallest id value to be fetched within the specified range without the
947 * caller having to know the exact id.
949 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
950 unsigned int idf, unsigned int idl)
953 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
954 if (bss->id >= idf && bss->id <= idl)
962 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
963 * @bss: BSS table entry
964 * @ie: Information element identitifier (WLAN_EID_*)
965 * Returns: Pointer to the information element (id field) or %NULL if not found
967 * This function returns the first matching information element in the BSS
970 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
974 pos = (const u8 *) (bss + 1);
975 end = pos + bss->ie_len;
977 while (pos + 1 < end) {
978 if (pos + 2 + pos[1] > end)
990 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
991 * @bss: BSS table entry
992 * @vendor_type: Vendor type (four octets starting the IE payload)
993 * Returns: Pointer to the information element (id field) or %NULL if not found
995 * This function returns the first matching information element in the BSS
998 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1000 const u8 *end, *pos;
1002 pos = (const u8 *) (bss + 1);
1003 end = pos + bss->ie_len;
1005 while (pos + 1 < end) {
1006 if (pos + 2 + pos[1] > end)
1008 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1009 vendor_type == WPA_GET_BE32(&pos[2]))
1019 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1020 * @bss: BSS table entry
1021 * @vendor_type: Vendor type (four octets starting the IE payload)
1022 * Returns: Pointer to the information element (id field) or %NULL if not found
1024 * This function returns the first matching information element in the BSS
1027 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1028 * from Beacon frames instead of either Beacon or Probe Response frames.
1030 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1033 const u8 *end, *pos;
1035 if (bss->beacon_ie_len == 0)
1038 pos = (const u8 *) (bss + 1);
1040 end = pos + bss->beacon_ie_len;
1042 while (pos + 1 < end) {
1043 if (pos + 2 + pos[1] > end)
1045 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1046 vendor_type == WPA_GET_BE32(&pos[2]))
1056 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1057 * @bss: BSS table entry
1058 * @vendor_type: Vendor type (four octets starting the IE payload)
1059 * Returns: Pointer to the information element payload or %NULL if not found
1061 * This function returns concatenated payload of possibly fragmented vendor
1062 * specific information elements in the BSS entry. The caller is responsible for
1063 * freeing the returned buffer.
1065 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1069 const u8 *end, *pos;
1071 buf = wpabuf_alloc(bss->ie_len);
1075 pos = (const u8 *) (bss + 1);
1076 end = pos + bss->ie_len;
1078 while (pos + 1 < end) {
1079 if (pos + 2 + pos[1] > end)
1081 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1082 vendor_type == WPA_GET_BE32(&pos[2]))
1083 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1087 if (wpabuf_len(buf) == 0) {
1097 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1098 * @bss: BSS table entry
1099 * @vendor_type: Vendor type (four octets starting the IE payload)
1100 * Returns: Pointer to the information element payload or %NULL if not found
1102 * This function returns concatenated payload of possibly fragmented vendor
1103 * specific information elements in the BSS entry. The caller is responsible for
1104 * freeing the returned buffer.
1106 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1107 * from Beacon frames instead of either Beacon or Probe Response frames.
1109 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1113 const u8 *end, *pos;
1115 buf = wpabuf_alloc(bss->beacon_ie_len);
1119 pos = (const u8 *) (bss + 1);
1121 end = pos + bss->beacon_ie_len;
1123 while (pos + 1 < end) {
1124 if (pos + 2 + pos[1] > end)
1126 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1127 vendor_type == WPA_GET_BE32(&pos[2]))
1128 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1132 if (wpabuf_len(buf) == 0) {
1142 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1143 * @bss: BSS table entry
1144 * Returns: Maximum legacy rate in units of 500 kbps
1146 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1152 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1153 for (i = 0; ie && i < ie[1]; i++) {
1154 if ((ie[i + 2] & 0x7f) > rate)
1155 rate = ie[i + 2] & 0x7f;
1158 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1159 for (i = 0; ie && i < ie[1]; i++) {
1160 if ((ie[i + 2] & 0x7f) > rate)
1161 rate = ie[i + 2] & 0x7f;
1169 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1170 * @bss: BSS table entry
1171 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1172 * Returns: number of legacy TX rates or -1 on failure
1174 * The caller is responsible for freeing the returned buffer with os_free() in
1177 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1184 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1185 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1187 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1193 for (i = 0; ie && i < ie[1]; i++)
1194 r[i] = ie[i + 2] & 0x7f;
1196 for (j = 0; ie2 && j < ie2[1]; j++)
1197 r[i + j] = ie2[j + 2] & 0x7f;