3 * Copyright (c) 2009-2015, 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"
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)
33 static void wpa_bss_set_hessid(struct wpa_bss *bss)
35 #ifdef CONFIG_INTERWORKING
36 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
37 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
38 os_memset(bss->hessid, 0, ETH_ALEN);
42 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
44 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
45 #endif /* CONFIG_INTERWORKING */
50 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
51 * Returns: Allocated ANQP data structure or %NULL on failure
53 * The allocated ANQP data structure has its users count set to 1. It may be
54 * shared by multiple BSS entries and each shared entry is freed with
55 * wpa_bss_anqp_free().
57 struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
59 struct wpa_bss_anqp *anqp;
60 anqp = os_zalloc(sizeof(*anqp));
69 * wpa_bss_anqp_clone - Clone an ANQP data structure
70 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
71 * Returns: Cloned ANQP data structure or %NULL on failure
73 static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
75 struct wpa_bss_anqp *n;
77 n = os_zalloc(sizeof(*n));
81 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
82 #ifdef CONFIG_INTERWORKING
83 ANQP_DUP(capability_list);
85 ANQP_DUP(network_auth_type);
86 ANQP_DUP(roaming_consortium);
87 ANQP_DUP(ip_addr_type_availability);
90 ANQP_DUP(domain_name);
91 #endif /* CONFIG_INTERWORKING */
93 ANQP_DUP(hs20_capability_list);
94 ANQP_DUP(hs20_operator_friendly_name);
95 ANQP_DUP(hs20_wan_metrics);
96 ANQP_DUP(hs20_connection_capability);
97 ANQP_DUP(hs20_operating_class);
98 ANQP_DUP(hs20_osu_providers_list);
99 #endif /* CONFIG_HS20 */
107 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
109 * Returns: 0 on success, -1 on failure
111 * This function ensures the specific BSS entry has an ANQP data structure that
112 * is not shared with any other BSS entry.
114 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
116 struct wpa_bss_anqp *anqp;
118 if (bss->anqp && bss->anqp->users > 1) {
119 /* allocated, but shared - clone an unshared copy */
120 anqp = wpa_bss_anqp_clone(bss->anqp);
130 return 0; /* already allocated and not shared */
132 /* not allocated - allocate a new storage area */
133 bss->anqp = wpa_bss_anqp_alloc();
134 return bss->anqp ? 0 : -1;
139 * wpa_bss_anqp_free - Free an ANQP data structure
140 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
142 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
148 if (anqp->users > 0) {
149 /* Another BSS entry holds a pointer to this ANQP info */
153 #ifdef CONFIG_INTERWORKING
154 wpabuf_free(anqp->capability_list);
155 wpabuf_free(anqp->venue_name);
156 wpabuf_free(anqp->network_auth_type);
157 wpabuf_free(anqp->roaming_consortium);
158 wpabuf_free(anqp->ip_addr_type_availability);
159 wpabuf_free(anqp->nai_realm);
160 wpabuf_free(anqp->anqp_3gpp);
161 wpabuf_free(anqp->domain_name);
162 #endif /* CONFIG_INTERWORKING */
164 wpabuf_free(anqp->hs20_capability_list);
165 wpabuf_free(anqp->hs20_operator_friendly_name);
166 wpabuf_free(anqp->hs20_wan_metrics);
167 wpabuf_free(anqp->hs20_connection_capability);
168 wpabuf_free(anqp->hs20_operating_class);
169 wpabuf_free(anqp->hs20_osu_providers_list);
170 #endif /* CONFIG_HS20 */
176 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
177 struct wpa_bss *old_bss,
178 struct wpa_bss *new_bss)
180 struct wpa_radio_work *work;
181 struct wpa_connect_work *cwork;
183 work = radio_work_pending(wpa_s, "sme-connect");
185 work = radio_work_pending(wpa_s, "connect");
190 if (cwork->bss != old_bss)
193 wpa_printf(MSG_DEBUG,
194 "Update BSS pointer for the pending connect radio work");
195 cwork->bss = new_bss;
197 cwork->bss_removed = 1;
201 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
204 if (wpa_s->last_scan_res) {
206 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
207 if (wpa_s->last_scan_res[i] == bss) {
208 os_memmove(&wpa_s->last_scan_res[i],
209 &wpa_s->last_scan_res[i + 1],
210 (wpa_s->last_scan_res_used - i - 1)
211 * sizeof(struct wpa_bss *));
212 wpa_s->last_scan_res_used--;
217 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
218 dl_list_del(&bss->list);
219 dl_list_del(&bss->list_id);
221 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
222 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
223 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
224 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
225 wpa_bss_anqp_free(bss->anqp);
231 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
232 * @wpa_s: Pointer to wpa_supplicant data
235 * @ssid_len: Length of @ssid
236 * Returns: Pointer to the BSS entry or %NULL if not found
238 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
239 const u8 *ssid, size_t ssid_len)
242 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
244 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
245 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
246 bss->ssid_len == ssid_len &&
247 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
254 static void calculate_update_time(const struct os_reltime *fetch_time,
256 struct os_reltime *update_time)
260 update_time->sec = fetch_time->sec;
261 update_time->usec = fetch_time->usec;
262 update_time->sec -= age_ms / 1000;
263 usec = (age_ms % 1000) * 1000;
264 if (update_time->usec < usec) {
266 update_time->usec += 1000000;
268 update_time->usec -= usec;
272 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
273 struct os_reltime *fetch_time)
275 dst->flags = src->flags;
276 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
277 dst->freq = src->freq;
278 dst->beacon_int = src->beacon_int;
279 dst->caps = src->caps;
280 dst->qual = src->qual;
281 dst->noise = src->noise;
282 dst->level = src->level;
284 dst->est_throughput = src->est_throughput;
287 calculate_update_time(fetch_time, src->age, &dst->last_update);
291 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
293 struct wpa_ssid *ssid;
295 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
296 if (ssid->ssid == NULL || ssid->ssid_len == 0)
298 if (ssid->ssid_len == bss->ssid_len &&
299 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
307 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
309 if (bss == wpa_s->current_bss)
312 if (wpa_s->current_bss &&
313 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
314 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
315 bss->ssid_len) != 0))
316 return 0; /* SSID has changed */
318 return !is_zero_ether_addr(bss->bssid) &&
319 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
320 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
324 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
328 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
329 if (!wpa_bss_known(wpa_s, bss)) {
330 wpa_bss_remove(wpa_s, bss, __func__);
339 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
344 * Remove the oldest entry that does not match with any configured
347 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
351 * Remove the oldest entry that isn't currently in use.
353 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
354 if (!wpa_bss_in_use(wpa_s, bss)) {
355 wpa_bss_remove(wpa_s, bss, __func__);
364 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
365 const u8 *ssid, size_t ssid_len,
366 struct wpa_scan_res *res,
367 struct os_reltime *fetch_time)
371 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
374 bss->id = wpa_s->bss_next_id++;
375 bss->last_update_idx = wpa_s->bss_update_idx;
376 wpa_bss_copy_res(bss, res, fetch_time);
377 os_memcpy(bss->ssid, ssid, ssid_len);
378 bss->ssid_len = ssid_len;
379 bss->ie_len = res->ie_len;
380 bss->beacon_ie_len = res->beacon_ie_len;
381 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
382 wpa_bss_set_hessid(bss);
384 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
385 wpa_bss_remove_oldest(wpa_s) != 0) {
386 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
387 "because all BSSes are in use. We should normally "
388 "not get here!", (int) wpa_s->num_bss + 1);
389 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
392 dl_list_add_tail(&wpa_s->bss, &bss->list);
393 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
395 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
397 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
398 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
403 static int are_ies_equal(const struct wpa_bss *old,
404 const struct wpa_scan_res *new_res, u32 ie)
406 const u8 *old_ie, *new_ie;
407 struct wpabuf *old_ie_buff = NULL;
408 struct wpabuf *new_ie_buff = NULL;
409 int new_ie_len, old_ie_len, ret, is_multi;
412 case WPA_IE_VENDOR_TYPE:
413 old_ie = wpa_bss_get_vendor_ie(old, ie);
414 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
417 case WPS_IE_VENDOR_TYPE:
418 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
419 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
423 case WLAN_EID_SUPP_RATES:
424 case WLAN_EID_EXT_SUPP_RATES:
425 old_ie = wpa_bss_get_ie(old, ie);
426 new_ie = wpa_scan_get_ie(new_res, ie);
430 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
435 /* in case of multiple IEs stored in buffer */
436 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
437 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
438 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
439 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
441 /* in case of single IE */
442 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
443 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
446 if (!old_ie || !new_ie)
447 ret = !old_ie && !new_ie;
449 ret = (old_ie_len == new_ie_len &&
450 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
452 wpabuf_free(old_ie_buff);
453 wpabuf_free(new_ie_buff);
459 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
460 const struct wpa_scan_res *new_res)
463 int caps_diff = old->caps ^ new_res->caps;
465 if (old->freq != new_res->freq)
466 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
468 if (old->level != new_res->level)
469 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
471 if (caps_diff & IEEE80211_CAP_PRIVACY)
472 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
474 if (caps_diff & IEEE80211_CAP_IBSS)
475 changes |= WPA_BSS_MODE_CHANGED_FLAG;
477 if (old->ie_len == new_res->ie_len &&
478 os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
480 changes |= WPA_BSS_IES_CHANGED_FLAG;
482 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
483 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
485 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
486 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
488 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
489 changes |= WPA_BSS_WPS_CHANGED_FLAG;
491 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
492 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
493 changes |= WPA_BSS_RATES_CHANGED_FLAG;
499 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
500 const struct wpa_bss *bss)
502 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
503 wpas_notify_bss_freq_changed(wpa_s, bss->id);
505 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
506 wpas_notify_bss_signal_changed(wpa_s, bss->id);
508 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
509 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
511 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
512 wpas_notify_bss_mode_changed(wpa_s, bss->id);
514 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
515 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
517 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
518 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
520 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
521 wpas_notify_bss_wps_changed(wpa_s, bss->id);
523 if (changes & WPA_BSS_IES_CHANGED_FLAG)
524 wpas_notify_bss_ies_changed(wpa_s, bss->id);
526 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
527 wpas_notify_bss_rates_changed(wpa_s, bss->id);
529 wpas_notify_bss_seen(wpa_s, bss->id);
533 static struct wpa_bss *
534 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
535 struct wpa_scan_res *res, struct os_reltime *fetch_time)
539 changes = wpa_bss_compare_res(bss, res);
540 bss->scan_miss_count = 0;
541 bss->last_update_idx = wpa_s->bss_update_idx;
542 wpa_bss_copy_res(bss, res, fetch_time);
543 /* Move the entry to the end of the list */
544 dl_list_del(&bss->list);
546 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
547 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
549 * This can happen when non-P2P station interface runs a scan
550 * without P2P IE in the Probe Request frame. P2P GO would reply
551 * to that with a Probe Response that does not include P2P IE.
552 * Do not update the IEs in this BSS entry to avoid such loss of
553 * information that may be needed for P2P operations to
554 * determine group information.
556 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
557 MACSTR " since that would remove P2P IE information",
558 MAC2STR(bss->bssid));
560 #endif /* CONFIG_P2P */
561 if (bss->ie_len + bss->beacon_ie_len >=
562 res->ie_len + res->beacon_ie_len) {
563 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
564 bss->ie_len = res->ie_len;
565 bss->beacon_ie_len = res->beacon_ie_len;
567 struct wpa_bss *nbss;
568 struct dl_list *prev = bss->list_id.prev;
569 dl_list_del(&bss->list_id);
570 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
574 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
575 if (wpa_s->last_scan_res[i] == bss) {
576 wpa_s->last_scan_res[i] = nbss;
580 if (wpa_s->current_bss == bss)
581 wpa_s->current_bss = nbss;
582 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
584 os_memcpy(bss + 1, res + 1,
585 res->ie_len + res->beacon_ie_len);
586 bss->ie_len = res->ie_len;
587 bss->beacon_ie_len = res->beacon_ie_len;
589 dl_list_add(prev, &bss->list_id);
591 if (changes & WPA_BSS_IES_CHANGED_FLAG)
592 wpa_bss_set_hessid(bss);
593 dl_list_add_tail(&wpa_s->bss, &bss->list);
595 notify_bss_changes(wpa_s, changes, bss);
602 * wpa_bss_update_start - Start a BSS table update from scan results
603 * @wpa_s: Pointer to wpa_supplicant data
605 * This function is called at the start of each BSS table update round for new
606 * scan results. The actual scan result entries are indicated with calls to
607 * wpa_bss_update_scan_res() and the update round is finished with a call to
608 * wpa_bss_update_end().
610 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
612 wpa_s->bss_update_idx++;
613 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
614 wpa_s->bss_update_idx);
615 wpa_s->last_scan_res_used = 0;
620 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
621 * @wpa_s: Pointer to wpa_supplicant data
623 * @fetch_time: Time when the result was fetched from the driver
625 * This function updates a BSS table entry (or adds one) based on a scan result.
626 * This is called separately for each scan result between the calls to
627 * wpa_bss_update_start() and wpa_bss_update_end().
629 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
630 struct wpa_scan_res *res,
631 struct os_reltime *fetch_time)
633 const u8 *ssid, *p2p, *mesh;
636 if (wpa_s->conf->ignore_old_scan_res) {
637 struct os_reltime update;
638 calculate_update_time(fetch_time, res->age, &update);
639 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
640 struct os_reltime age;
641 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
643 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
644 "table entry that is %u.%06u seconds older "
645 "than our scan trigger",
646 (unsigned int) age.sec,
647 (unsigned int) age.usec);
652 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
654 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
655 MACSTR, MAC2STR(res->bssid));
658 if (ssid[1] > SSID_MAX_LEN) {
659 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
660 MACSTR, MAC2STR(res->bssid));
664 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
667 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
669 * If it's a P2P specific interface, then don't update
670 * the scan result without a P2P IE.
672 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
673 " update for P2P interface", MAC2STR(res->bssid));
676 #endif /* CONFIG_P2P */
677 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
678 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
679 return; /* Skip P2P listen discovery results here */
681 /* TODO: add option for ignoring BSSes we are not interested in
682 * (to save memory) */
684 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
685 if (mesh && mesh[1] <= SSID_MAX_LEN)
688 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
690 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
692 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
693 if (wpa_s->last_scan_res) {
695 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
696 if (bss == wpa_s->last_scan_res[i]) {
697 /* Already in the list */
706 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
709 if (wpa_s->last_scan_res_size == 0)
712 siz = wpa_s->last_scan_res_size * 2;
713 n = os_realloc_array(wpa_s->last_scan_res, siz,
714 sizeof(struct wpa_bss *));
717 wpa_s->last_scan_res = n;
718 wpa_s->last_scan_res_size = siz;
721 if (wpa_s->last_scan_res)
722 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
726 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
727 const struct scan_info *info)
735 if (info->num_freqs) {
737 for (i = 0; i < info->num_freqs; i++) {
738 if (bss->freq == info->freqs[i]) {
747 if (info->num_ssids) {
749 for (i = 0; i < info->num_ssids; i++) {
750 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
751 if ((s->ssid == NULL || s->ssid_len == 0) ||
752 (s->ssid_len == bss->ssid_len &&
753 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
768 * wpa_bss_update_end - End a BSS table update from scan results
769 * @wpa_s: Pointer to wpa_supplicant data
770 * @info: Information about scan parameters
771 * @new_scan: Whether this update round was based on a new scan
773 * This function is called at the end of each BSS table update round for new
774 * scan results. The start of the update was indicated with a call to
775 * wpa_bss_update_start().
777 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
780 struct wpa_bss *bss, *n;
782 os_get_reltime(&wpa_s->last_scan);
784 return; /* do not expire entries without new scan */
786 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
787 if (wpa_bss_in_use(wpa_s, bss))
789 if (!wpa_bss_included_in_scan(bss, info))
790 continue; /* expire only BSSes that were scanned */
791 if (bss->last_update_idx < wpa_s->bss_update_idx)
792 bss->scan_miss_count++;
793 if (bss->scan_miss_count >=
794 wpa_s->conf->bss_expiration_scan_count) {
795 wpa_bss_remove(wpa_s, bss, "no match in scan");
799 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
800 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
805 * wpa_bss_flush_by_age - Flush old BSS entries
806 * @wpa_s: Pointer to wpa_supplicant data
807 * @age: Maximum entry age in seconds
809 * Remove BSS entries that have not been updated during the last @age seconds.
811 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
813 struct wpa_bss *bss, *n;
816 if (dl_list_empty(&wpa_s->bss))
822 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
823 if (wpa_bss_in_use(wpa_s, bss))
826 if (os_reltime_before(&bss->last_update, &t)) {
827 wpa_bss_remove(wpa_s, bss, __func__);
835 * wpa_bss_init - Initialize BSS table
836 * @wpa_s: Pointer to wpa_supplicant data
837 * Returns: 0 on success, -1 on failure
839 * This prepares BSS table lists and timer for periodic updates. The BSS table
840 * is deinitialized with wpa_bss_deinit() once not needed anymore.
842 int wpa_bss_init(struct wpa_supplicant *wpa_s)
844 dl_list_init(&wpa_s->bss);
845 dl_list_init(&wpa_s->bss_id);
851 * wpa_bss_flush - Flush all unused BSS entries
852 * @wpa_s: Pointer to wpa_supplicant data
854 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
856 struct wpa_bss *bss, *n;
858 wpa_s->clear_driver_scan_cache = 1;
860 if (wpa_s->bss.next == NULL)
861 return; /* BSS table not yet initialized */
863 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
864 if (wpa_bss_in_use(wpa_s, bss))
866 wpa_bss_remove(wpa_s, bss, __func__);
872 * wpa_bss_deinit - Deinitialize BSS table
873 * @wpa_s: Pointer to wpa_supplicant data
875 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
877 wpa_bss_flush(wpa_s);
882 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
883 * @wpa_s: Pointer to wpa_supplicant data
885 * Returns: Pointer to the BSS entry or %NULL if not found
887 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
891 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
893 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
894 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
902 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
903 * @wpa_s: Pointer to wpa_supplicant data
905 * Returns: Pointer to the BSS entry or %NULL if not found
907 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
908 * find the entry that has the most recent update. This can help in finding the
909 * correct entry in cases where the SSID of the AP may have changed recently
910 * (e.g., in WPS reconfiguration cases).
912 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
915 struct wpa_bss *bss, *found = NULL;
916 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
918 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
919 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
922 os_reltime_before(&found->last_update, &bss->last_update))
931 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
932 * @wpa_s: Pointer to wpa_supplicant data
933 * @dev_addr: P2P Device Address of the GO
934 * Returns: Pointer to the BSS entry or %NULL if not found
936 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
940 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
942 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
944 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
949 #endif /* CONFIG_P2P */
953 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
954 * @wpa_s: Pointer to wpa_supplicant data
955 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
956 * Returns: Pointer to the BSS entry or %NULL if not found
958 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
961 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
970 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
971 * @wpa_s: Pointer to wpa_supplicant data
972 * @idf: Smallest allowed identifier assigned for the entry
973 * @idf: Largest allowed identifier assigned for the entry
974 * Returns: Pointer to the BSS entry or %NULL if not found
976 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
977 * smallest id value to be fetched within the specified range without the
978 * caller having to know the exact id.
980 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
981 unsigned int idf, unsigned int idl)
984 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
985 if (bss->id >= idf && bss->id <= idl)
993 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
994 * @bss: BSS table entry
995 * @ie: Information element identitifier (WLAN_EID_*)
996 * Returns: Pointer to the information element (id field) or %NULL if not found
998 * This function returns the first matching information element in the BSS
1001 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1003 const u8 *end, *pos;
1005 pos = (const u8 *) (bss + 1);
1006 end = pos + bss->ie_len;
1008 while (pos + 1 < end) {
1009 if (pos + 2 + pos[1] > end)
1021 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1022 * @bss: BSS table entry
1023 * @vendor_type: Vendor type (four octets starting the IE payload)
1024 * Returns: Pointer to the information element (id field) or %NULL if not found
1026 * This function returns the first matching information element in the BSS
1029 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1031 const u8 *end, *pos;
1033 pos = (const u8 *) (bss + 1);
1034 end = pos + bss->ie_len;
1036 while (pos + 1 < end) {
1037 if (pos + 2 + pos[1] > end)
1039 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1040 vendor_type == WPA_GET_BE32(&pos[2]))
1050 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1051 * @bss: BSS table entry
1052 * @vendor_type: Vendor type (four octets starting the IE payload)
1053 * Returns: Pointer to the information element (id field) or %NULL if not found
1055 * This function returns the first matching information element in the BSS
1058 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1059 * from Beacon frames instead of either Beacon or Probe Response frames.
1061 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1064 const u8 *end, *pos;
1066 if (bss->beacon_ie_len == 0)
1069 pos = (const u8 *) (bss + 1);
1071 end = pos + bss->beacon_ie_len;
1073 while (pos + 1 < end) {
1074 if (pos + 2 + pos[1] > end)
1076 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1077 vendor_type == WPA_GET_BE32(&pos[2]))
1087 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1088 * @bss: BSS table entry
1089 * @vendor_type: Vendor type (four octets starting the IE payload)
1090 * Returns: Pointer to the information element payload or %NULL if not found
1092 * This function returns concatenated payload of possibly fragmented vendor
1093 * specific information elements in the BSS entry. The caller is responsible for
1094 * freeing the returned buffer.
1096 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1100 const u8 *end, *pos;
1102 buf = wpabuf_alloc(bss->ie_len);
1106 pos = (const u8 *) (bss + 1);
1107 end = pos + bss->ie_len;
1109 while (pos + 1 < end) {
1110 if (pos + 2 + pos[1] > end)
1112 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1113 vendor_type == WPA_GET_BE32(&pos[2]))
1114 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1118 if (wpabuf_len(buf) == 0) {
1128 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1129 * @bss: BSS table entry
1130 * @vendor_type: Vendor type (four octets starting the IE payload)
1131 * Returns: Pointer to the information element payload or %NULL if not found
1133 * This function returns concatenated payload of possibly fragmented vendor
1134 * specific information elements in the BSS entry. The caller is responsible for
1135 * freeing the returned buffer.
1137 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1138 * from Beacon frames instead of either Beacon or Probe Response frames.
1140 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1144 const u8 *end, *pos;
1146 buf = wpabuf_alloc(bss->beacon_ie_len);
1150 pos = (const u8 *) (bss + 1);
1152 end = pos + bss->beacon_ie_len;
1154 while (pos + 1 < end) {
1155 if (pos + 2 + pos[1] > end)
1157 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1158 vendor_type == WPA_GET_BE32(&pos[2]))
1159 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1163 if (wpabuf_len(buf) == 0) {
1173 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1174 * @bss: BSS table entry
1175 * Returns: Maximum legacy rate in units of 500 kbps
1177 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1183 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1184 for (i = 0; ie && i < ie[1]; i++) {
1185 if ((ie[i + 2] & 0x7f) > rate)
1186 rate = ie[i + 2] & 0x7f;
1189 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1190 for (i = 0; ie && i < ie[1]; i++) {
1191 if ((ie[i + 2] & 0x7f) > rate)
1192 rate = ie[i + 2] & 0x7f;
1200 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1201 * @bss: BSS table entry
1202 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1203 * Returns: number of legacy TX rates or -1 on failure
1205 * The caller is responsible for freeing the returned buffer with os_free() in
1208 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1215 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1216 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1218 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1224 for (i = 0; ie && i < ie[1]; i++)
1225 r[i] = ie[i + 2] & 0x7f;
1227 for (j = 0; ie2 && j < ie2[1]; j++)
1228 r[i + j] = ie2[j + 2] & 0x7f;