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_capability_list);
98 ANQP_DUP(hs20_operator_friendly_name);
99 ANQP_DUP(hs20_wan_metrics);
100 ANQP_DUP(hs20_connection_capability);
101 ANQP_DUP(hs20_operating_class);
102 ANQP_DUP(hs20_osu_providers_list);
103 #endif /* CONFIG_HS20 */
111 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
113 * Returns: 0 on success, -1 on failure
115 * This function ensures the specific BSS entry has an ANQP data structure that
116 * is not shared with any other BSS entry.
118 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
120 struct wpa_bss_anqp *anqp;
122 if (bss->anqp && bss->anqp->users > 1) {
123 /* allocated, but shared - clone an unshared copy */
124 anqp = wpa_bss_anqp_clone(bss->anqp);
134 return 0; /* already allocated and not shared */
136 /* not allocated - allocate a new storage area */
137 bss->anqp = wpa_bss_anqp_alloc();
138 return bss->anqp ? 0 : -1;
143 * wpa_bss_anqp_free - Free an ANQP data structure
144 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
146 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
152 if (anqp->users > 0) {
153 /* Another BSS entry holds a pointer to this ANQP info */
157 #ifdef CONFIG_INTERWORKING
158 wpabuf_free(anqp->venue_name);
159 wpabuf_free(anqp->network_auth_type);
160 wpabuf_free(anqp->roaming_consortium);
161 wpabuf_free(anqp->ip_addr_type_availability);
162 wpabuf_free(anqp->nai_realm);
163 wpabuf_free(anqp->anqp_3gpp);
164 wpabuf_free(anqp->domain_name);
165 #endif /* CONFIG_INTERWORKING */
167 wpabuf_free(anqp->hs20_capability_list);
168 wpabuf_free(anqp->hs20_operator_friendly_name);
169 wpabuf_free(anqp->hs20_wan_metrics);
170 wpabuf_free(anqp->hs20_connection_capability);
171 wpabuf_free(anqp->hs20_operating_class);
172 wpabuf_free(anqp->hs20_osu_providers_list);
173 #endif /* CONFIG_HS20 */
179 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
180 struct wpa_bss *old_bss,
181 struct wpa_bss *new_bss)
183 struct wpa_radio_work *work;
184 struct wpa_connect_work *cwork;
186 work = radio_work_pending(wpa_s, "sme-connect");
188 work = radio_work_pending(wpa_s, "connect");
193 if (cwork->bss != old_bss)
196 wpa_printf(MSG_DEBUG,
197 "Update BSS pointer for the pending connect radio work");
198 cwork->bss = new_bss;
200 cwork->bss_removed = 1;
204 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
207 if (wpa_s->last_scan_res) {
209 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
210 if (wpa_s->last_scan_res[i] == bss) {
211 os_memmove(&wpa_s->last_scan_res[i],
212 &wpa_s->last_scan_res[i + 1],
213 (wpa_s->last_scan_res_used - i - 1)
214 * sizeof(struct wpa_bss *));
215 wpa_s->last_scan_res_used--;
220 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
221 dl_list_del(&bss->list);
222 dl_list_del(&bss->list_id);
224 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
225 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
226 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
227 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
228 wpa_bss_anqp_free(bss->anqp);
234 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
235 * @wpa_s: Pointer to wpa_supplicant data
238 * @ssid_len: Length of @ssid
239 * Returns: Pointer to the BSS entry or %NULL if not found
241 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
242 const u8 *ssid, size_t ssid_len)
245 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
247 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
248 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
249 bss->ssid_len == ssid_len &&
250 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
257 static void calculate_update_time(const struct os_reltime *fetch_time,
259 struct os_reltime *update_time)
263 update_time->sec = fetch_time->sec;
264 update_time->usec = fetch_time->usec;
265 update_time->sec -= age_ms / 1000;
266 usec = (age_ms % 1000) * 1000;
267 if (update_time->usec < usec) {
269 update_time->usec += 1000000;
271 update_time->usec -= usec;
275 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
276 struct os_reltime *fetch_time)
278 dst->flags = src->flags;
279 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
280 dst->freq = src->freq;
281 dst->beacon_int = src->beacon_int;
282 dst->caps = src->caps;
283 dst->qual = src->qual;
284 dst->noise = src->noise;
285 dst->level = src->level;
288 calculate_update_time(fetch_time, src->age, &dst->last_update);
292 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
294 struct wpa_ssid *ssid;
296 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
297 if (ssid->ssid == NULL || ssid->ssid_len == 0)
299 if (ssid->ssid_len == bss->ssid_len &&
300 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
308 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
310 return bss == wpa_s->current_bss ||
311 (!is_zero_ether_addr(bss->bssid) &&
312 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
313 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0));
317 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
321 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
322 if (!wpa_bss_known(wpa_s, bss)) {
323 wpa_bss_remove(wpa_s, bss, __func__);
332 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
337 * Remove the oldest entry that does not match with any configured
340 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
344 * Remove the oldest entry that isn't currently in use.
346 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
347 if (!wpa_bss_in_use(wpa_s, bss)) {
348 wpa_bss_remove(wpa_s, bss, __func__);
357 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
358 const u8 *ssid, size_t ssid_len,
359 struct wpa_scan_res *res,
360 struct os_reltime *fetch_time)
364 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
367 bss->id = wpa_s->bss_next_id++;
368 bss->last_update_idx = wpa_s->bss_update_idx;
369 wpa_bss_copy_res(bss, res, fetch_time);
370 os_memcpy(bss->ssid, ssid, ssid_len);
371 bss->ssid_len = ssid_len;
372 bss->ie_len = res->ie_len;
373 bss->beacon_ie_len = res->beacon_ie_len;
374 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
375 wpa_bss_set_hessid(bss);
377 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
378 wpa_bss_remove_oldest(wpa_s) != 0) {
379 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
380 "because all BSSes are in use. We should normally "
381 "not get here!", (int) wpa_s->num_bss + 1);
382 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
385 dl_list_add_tail(&wpa_s->bss, &bss->list);
386 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
388 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
390 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
391 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
396 static int are_ies_equal(const struct wpa_bss *old,
397 const struct wpa_scan_res *new, u32 ie)
399 const u8 *old_ie, *new_ie;
400 struct wpabuf *old_ie_buff = NULL;
401 struct wpabuf *new_ie_buff = NULL;
402 int new_ie_len, old_ie_len, ret, is_multi;
405 case WPA_IE_VENDOR_TYPE:
406 old_ie = wpa_bss_get_vendor_ie(old, ie);
407 new_ie = wpa_scan_get_vendor_ie(new, ie);
410 case WPS_IE_VENDOR_TYPE:
411 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
412 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
416 case WLAN_EID_SUPP_RATES:
417 case WLAN_EID_EXT_SUPP_RATES:
418 old_ie = wpa_bss_get_ie(old, ie);
419 new_ie = wpa_scan_get_ie(new, ie);
423 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
428 /* in case of multiple IEs stored in buffer */
429 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
430 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
431 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
432 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
434 /* in case of single IE */
435 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
436 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
439 if (!old_ie || !new_ie)
440 ret = !old_ie && !new_ie;
442 ret = (old_ie_len == new_ie_len &&
443 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
445 wpabuf_free(old_ie_buff);
446 wpabuf_free(new_ie_buff);
452 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
453 const struct wpa_scan_res *new)
456 int caps_diff = old->caps ^ new->caps;
458 if (old->freq != new->freq)
459 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
461 if (old->level != new->level)
462 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
464 if (caps_diff & IEEE80211_CAP_PRIVACY)
465 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
467 if (caps_diff & IEEE80211_CAP_IBSS)
468 changes |= WPA_BSS_MODE_CHANGED_FLAG;
470 if (old->ie_len == new->ie_len &&
471 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
473 changes |= WPA_BSS_IES_CHANGED_FLAG;
475 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
476 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
478 if (!are_ies_equal(old, new, WLAN_EID_RSN))
479 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
481 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
482 changes |= WPA_BSS_WPS_CHANGED_FLAG;
484 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
485 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
486 changes |= WPA_BSS_RATES_CHANGED_FLAG;
492 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
493 const struct wpa_bss *bss)
495 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
496 wpas_notify_bss_freq_changed(wpa_s, bss->id);
498 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
499 wpas_notify_bss_signal_changed(wpa_s, bss->id);
501 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
502 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
504 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
505 wpas_notify_bss_mode_changed(wpa_s, bss->id);
507 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
508 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
510 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
511 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
513 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
514 wpas_notify_bss_wps_changed(wpa_s, bss->id);
516 if (changes & WPA_BSS_IES_CHANGED_FLAG)
517 wpas_notify_bss_ies_changed(wpa_s, bss->id);
519 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
520 wpas_notify_bss_rates_changed(wpa_s, bss->id);
522 wpas_notify_bss_seen(wpa_s, bss->id);
526 static struct wpa_bss *
527 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
528 struct wpa_scan_res *res, struct os_reltime *fetch_time)
532 changes = wpa_bss_compare_res(bss, res);
533 bss->scan_miss_count = 0;
534 bss->last_update_idx = wpa_s->bss_update_idx;
535 wpa_bss_copy_res(bss, res, fetch_time);
536 /* Move the entry to the end of the list */
537 dl_list_del(&bss->list);
539 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
540 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
542 * This can happen when non-P2P station interface runs a scan
543 * without P2P IE in the Probe Request frame. P2P GO would reply
544 * to that with a Probe Response that does not include P2P IE.
545 * Do not update the IEs in this BSS entry to avoid such loss of
546 * information that may be needed for P2P operations to
547 * determine group information.
549 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
550 MACSTR " since that would remove P2P IE information",
551 MAC2STR(bss->bssid));
553 #endif /* CONFIG_P2P */
554 if (bss->ie_len + bss->beacon_ie_len >=
555 res->ie_len + res->beacon_ie_len) {
556 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
557 bss->ie_len = res->ie_len;
558 bss->beacon_ie_len = res->beacon_ie_len;
560 struct wpa_bss *nbss;
561 struct dl_list *prev = bss->list_id.prev;
562 dl_list_del(&bss->list_id);
563 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
567 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
568 if (wpa_s->last_scan_res[i] == bss) {
569 wpa_s->last_scan_res[i] = nbss;
573 if (wpa_s->current_bss == bss)
574 wpa_s->current_bss = nbss;
575 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
577 os_memcpy(bss + 1, res + 1,
578 res->ie_len + res->beacon_ie_len);
579 bss->ie_len = res->ie_len;
580 bss->beacon_ie_len = res->beacon_ie_len;
582 dl_list_add(prev, &bss->list_id);
584 if (changes & WPA_BSS_IES_CHANGED_FLAG)
585 wpa_bss_set_hessid(bss);
586 dl_list_add_tail(&wpa_s->bss, &bss->list);
588 notify_bss_changes(wpa_s, changes, bss);
595 * wpa_bss_update_start - Start a BSS table update from scan results
596 * @wpa_s: Pointer to wpa_supplicant data
598 * This function is called at the start of each BSS table update round for new
599 * scan results. The actual scan result entries are indicated with calls to
600 * wpa_bss_update_scan_res() and the update round is finished with a call to
601 * wpa_bss_update_end().
603 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
605 wpa_s->bss_update_idx++;
606 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
607 wpa_s->bss_update_idx);
608 wpa_s->last_scan_res_used = 0;
613 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
614 * @wpa_s: Pointer to wpa_supplicant data
616 * @fetch_time: Time when the result was fetched from the driver
618 * This function updates a BSS table entry (or adds one) based on a scan result.
619 * This is called separately for each scan result between the calls to
620 * wpa_bss_update_start() and wpa_bss_update_end().
622 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
623 struct wpa_scan_res *res,
624 struct os_reltime *fetch_time)
626 const u8 *ssid, *p2p, *mesh;
629 if (wpa_s->conf->ignore_old_scan_res) {
630 struct os_reltime update;
631 calculate_update_time(fetch_time, res->age, &update);
632 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
633 struct os_reltime age;
634 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
636 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
637 "table entry that is %u.%06u seconds older "
638 "than our scan trigger",
639 (unsigned int) age.sec,
640 (unsigned int) age.usec);
645 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
647 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
648 MACSTR, MAC2STR(res->bssid));
652 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
653 MACSTR, MAC2STR(res->bssid));
657 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
660 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
662 * If it's a P2P specific interface, then don't update
663 * the scan result without a P2P IE.
665 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
666 " update for P2P interface", MAC2STR(res->bssid));
669 #endif /* CONFIG_P2P */
670 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
671 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
672 return; /* Skip P2P listen discovery results here */
674 /* TODO: add option for ignoring BSSes we are not interested in
675 * (to save memory) */
677 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
678 if (mesh && mesh[1] <= 32)
681 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
683 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
685 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
686 if (wpa_s->last_scan_res) {
688 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
689 if (bss == wpa_s->last_scan_res[i]) {
690 /* Already in the list */
699 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
702 if (wpa_s->last_scan_res_size == 0)
705 siz = wpa_s->last_scan_res_size * 2;
706 n = os_realloc_array(wpa_s->last_scan_res, siz,
707 sizeof(struct wpa_bss *));
710 wpa_s->last_scan_res = n;
711 wpa_s->last_scan_res_size = siz;
714 if (wpa_s->last_scan_res)
715 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
719 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
720 const struct scan_info *info)
728 if (info->num_freqs) {
730 for (i = 0; i < info->num_freqs; i++) {
731 if (bss->freq == info->freqs[i]) {
740 if (info->num_ssids) {
742 for (i = 0; i < info->num_ssids; i++) {
743 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
744 if ((s->ssid == NULL || s->ssid_len == 0) ||
745 (s->ssid_len == bss->ssid_len &&
746 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
761 * wpa_bss_update_end - End a BSS table update from scan results
762 * @wpa_s: Pointer to wpa_supplicant data
763 * @info: Information about scan parameters
764 * @new_scan: Whether this update round was based on a new scan
766 * This function is called at the end of each BSS table update round for new
767 * scan results. The start of the update was indicated with a call to
768 * wpa_bss_update_start().
770 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
773 struct wpa_bss *bss, *n;
775 os_get_reltime(&wpa_s->last_scan);
777 return; /* do not expire entries without new scan */
779 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
780 if (wpa_bss_in_use(wpa_s, bss))
782 if (!wpa_bss_included_in_scan(bss, info))
783 continue; /* expire only BSSes that were scanned */
784 if (bss->last_update_idx < wpa_s->bss_update_idx)
785 bss->scan_miss_count++;
786 if (bss->scan_miss_count >=
787 wpa_s->conf->bss_expiration_scan_count) {
788 wpa_bss_remove(wpa_s, bss, "no match in scan");
792 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
793 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
798 * wpa_bss_flush_by_age - Flush old BSS entries
799 * @wpa_s: Pointer to wpa_supplicant data
800 * @age: Maximum entry age in seconds
802 * Remove BSS entries that have not been updated during the last @age seconds.
804 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
806 struct wpa_bss *bss, *n;
809 if (dl_list_empty(&wpa_s->bss))
815 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
816 if (wpa_bss_in_use(wpa_s, bss))
819 if (os_reltime_before(&bss->last_update, &t)) {
820 wpa_bss_remove(wpa_s, bss, __func__);
827 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
829 struct wpa_supplicant *wpa_s = eloop_ctx;
831 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
832 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
833 wpa_bss_timeout, wpa_s, NULL);
838 * wpa_bss_init - Initialize BSS table
839 * @wpa_s: Pointer to wpa_supplicant data
840 * Returns: 0 on success, -1 on failure
842 * This prepares BSS table lists and timer for periodic updates. The BSS table
843 * is deinitialized with wpa_bss_deinit() once not needed anymore.
845 int wpa_bss_init(struct wpa_supplicant *wpa_s)
847 dl_list_init(&wpa_s->bss);
848 dl_list_init(&wpa_s->bss_id);
849 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
850 wpa_bss_timeout, wpa_s, NULL);
856 * wpa_bss_flush - Flush all unused BSS entries
857 * @wpa_s: Pointer to wpa_supplicant data
859 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
861 struct wpa_bss *bss, *n;
863 wpa_s->clear_driver_scan_cache = 1;
865 if (wpa_s->bss.next == NULL)
866 return; /* BSS table not yet initialized */
868 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
869 if (wpa_bss_in_use(wpa_s, bss))
871 wpa_bss_remove(wpa_s, bss, __func__);
877 * wpa_bss_deinit - Deinitialize BSS table
878 * @wpa_s: Pointer to wpa_supplicant data
880 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
882 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
883 wpa_bss_flush(wpa_s);
888 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
889 * @wpa_s: Pointer to wpa_supplicant data
891 * Returns: Pointer to the BSS entry or %NULL if not found
893 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
897 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
899 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
900 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
908 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
909 * @wpa_s: Pointer to wpa_supplicant data
911 * Returns: Pointer to the BSS entry or %NULL if not found
913 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
914 * find the entry that has the most recent update. This can help in finding the
915 * correct entry in cases where the SSID of the AP may have changed recently
916 * (e.g., in WPS reconfiguration cases).
918 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
921 struct wpa_bss *bss, *found = NULL;
922 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
924 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
925 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
928 os_reltime_before(&found->last_update, &bss->last_update))
937 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
938 * @wpa_s: Pointer to wpa_supplicant data
939 * @dev_addr: P2P Device Address of the GO
940 * Returns: Pointer to the BSS entry or %NULL if not found
942 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
946 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
948 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
950 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
955 #endif /* CONFIG_P2P */
959 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
960 * @wpa_s: Pointer to wpa_supplicant data
961 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
962 * Returns: Pointer to the BSS entry or %NULL if not found
964 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
967 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
976 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
977 * @wpa_s: Pointer to wpa_supplicant data
978 * @idf: Smallest allowed identifier assigned for the entry
979 * @idf: Largest allowed identifier assigned for the entry
980 * Returns: Pointer to the BSS entry or %NULL if not found
982 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
983 * smallest id value to be fetched within the specified range without the
984 * caller having to know the exact id.
986 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
987 unsigned int idf, unsigned int idl)
990 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
991 if (bss->id >= idf && bss->id <= idl)
999 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1000 * @bss: BSS table entry
1001 * @ie: Information element identitifier (WLAN_EID_*)
1002 * Returns: Pointer to the information element (id field) or %NULL if not found
1004 * This function returns the first matching information element in the BSS
1007 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1009 const u8 *end, *pos;
1011 pos = (const u8 *) (bss + 1);
1012 end = pos + bss->ie_len;
1014 while (pos + 1 < end) {
1015 if (pos + 2 + pos[1] > end)
1027 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1028 * @bss: BSS table entry
1029 * @vendor_type: Vendor type (four octets starting the IE payload)
1030 * Returns: Pointer to the information element (id field) or %NULL if not found
1032 * This function returns the first matching information element in the BSS
1035 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1037 const u8 *end, *pos;
1039 pos = (const u8 *) (bss + 1);
1040 end = pos + bss->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_beacon - Fetch a vendor information 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 (id field) or %NULL if not found
1061 * This function returns the first matching information element in the BSS
1064 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1065 * from Beacon frames instead of either Beacon or Probe Response frames.
1067 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1070 const u8 *end, *pos;
1072 if (bss->beacon_ie_len == 0)
1075 pos = (const u8 *) (bss + 1);
1077 end = pos + bss->beacon_ie_len;
1079 while (pos + 1 < end) {
1080 if (pos + 2 + pos[1] > end)
1082 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1083 vendor_type == WPA_GET_BE32(&pos[2]))
1093 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1094 * @bss: BSS table entry
1095 * @vendor_type: Vendor type (four octets starting the IE payload)
1096 * Returns: Pointer to the information element payload or %NULL if not found
1098 * This function returns concatenated payload of possibly fragmented vendor
1099 * specific information elements in the BSS entry. The caller is responsible for
1100 * freeing the returned buffer.
1102 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1106 const u8 *end, *pos;
1108 buf = wpabuf_alloc(bss->ie_len);
1112 pos = (const u8 *) (bss + 1);
1113 end = pos + bss->ie_len;
1115 while (pos + 1 < end) {
1116 if (pos + 2 + pos[1] > end)
1118 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1119 vendor_type == WPA_GET_BE32(&pos[2]))
1120 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1124 if (wpabuf_len(buf) == 0) {
1134 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1135 * @bss: BSS table entry
1136 * @vendor_type: Vendor type (four octets starting the IE payload)
1137 * Returns: Pointer to the information element payload or %NULL if not found
1139 * This function returns concatenated payload of possibly fragmented vendor
1140 * specific information elements in the BSS entry. The caller is responsible for
1141 * freeing the returned buffer.
1143 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1144 * from Beacon frames instead of either Beacon or Probe Response frames.
1146 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1150 const u8 *end, *pos;
1152 buf = wpabuf_alloc(bss->beacon_ie_len);
1156 pos = (const u8 *) (bss + 1);
1158 end = pos + bss->beacon_ie_len;
1160 while (pos + 1 < end) {
1161 if (pos + 2 + pos[1] > end)
1163 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1164 vendor_type == WPA_GET_BE32(&pos[2]))
1165 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1169 if (wpabuf_len(buf) == 0) {
1179 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1180 * @bss: BSS table entry
1181 * Returns: Maximum legacy rate in units of 500 kbps
1183 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1189 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1190 for (i = 0; ie && i < ie[1]; i++) {
1191 if ((ie[i + 2] & 0x7f) > rate)
1192 rate = ie[i + 2] & 0x7f;
1195 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1196 for (i = 0; ie && i < ie[1]; i++) {
1197 if ((ie[i + 2] & 0x7f) > rate)
1198 rate = ie[i + 2] & 0x7f;
1206 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1207 * @bss: BSS table entry
1208 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1209 * Returns: number of legacy TX rates or -1 on failure
1211 * The caller is responsible for freeing the returned buffer with os_free() in
1214 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1221 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1222 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1224 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1230 for (i = 0; ie && i < ie[1]; i++)
1231 r[i] = ie[i + 2] & 0x7f;
1233 for (j = 0; ie2 && j < ie2[1]; j++)
1234 r[i + j] = ie2[j + 2] & 0x7f;