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 "eap_peer/eap.h"
16 #include "wpa_supplicant_i.h"
23 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
24 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
25 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
26 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
27 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
28 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
29 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
30 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
31 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
34 static void wpa_bss_set_hessid(struct wpa_bss *bss)
36 #ifdef CONFIG_INTERWORKING
37 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
38 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
39 os_memset(bss->hessid, 0, ETH_ALEN);
43 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
45 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
46 #endif /* CONFIG_INTERWORKING */
51 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
52 * Returns: Allocated ANQP data structure or %NULL on failure
54 * The allocated ANQP data structure has its users count set to 1. It may be
55 * shared by multiple BSS entries and each shared entry is freed with
56 * wpa_bss_anqp_free().
58 struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
60 struct wpa_bss_anqp *anqp;
61 anqp = os_zalloc(sizeof(*anqp));
64 #ifdef CONFIG_INTERWORKING
65 dl_list_init(&anqp->anqp_elems);
66 #endif /* CONFIG_INTERWORKING */
73 * wpa_bss_anqp_clone - Clone an ANQP data structure
74 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
75 * Returns: Cloned ANQP data structure or %NULL on failure
77 static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
79 struct wpa_bss_anqp *n;
81 n = os_zalloc(sizeof(*n));
85 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
86 #ifdef CONFIG_INTERWORKING
87 dl_list_init(&n->anqp_elems);
88 ANQP_DUP(capability_list);
90 ANQP_DUP(network_auth_type);
91 ANQP_DUP(roaming_consortium);
92 ANQP_DUP(ip_addr_type_availability);
95 ANQP_DUP(domain_name);
96 #endif /* CONFIG_INTERWORKING */
98 ANQP_DUP(hs20_capability_list);
99 ANQP_DUP(hs20_operator_friendly_name);
100 ANQP_DUP(hs20_wan_metrics);
101 ANQP_DUP(hs20_connection_capability);
102 ANQP_DUP(hs20_operating_class);
103 ANQP_DUP(hs20_osu_providers_list);
104 #endif /* CONFIG_HS20 */
112 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
114 * Returns: 0 on success, -1 on failure
116 * This function ensures the specific BSS entry has an ANQP data structure that
117 * is not shared with any other BSS entry.
119 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
121 struct wpa_bss_anqp *anqp;
123 if (bss->anqp && bss->anqp->users > 1) {
124 /* allocated, but shared - clone an unshared copy */
125 anqp = wpa_bss_anqp_clone(bss->anqp);
135 return 0; /* already allocated and not shared */
137 /* not allocated - allocate a new storage area */
138 bss->anqp = wpa_bss_anqp_alloc();
139 return bss->anqp ? 0 : -1;
144 * wpa_bss_anqp_free - Free an ANQP data structure
145 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
147 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
149 #ifdef CONFIG_INTERWORKING
150 struct wpa_bss_anqp_elem *elem;
151 #endif /* CONFIG_INTERWORKING */
157 if (anqp->users > 0) {
158 /* Another BSS entry holds a pointer to this ANQP info */
162 #ifdef CONFIG_INTERWORKING
163 wpabuf_free(anqp->capability_list);
164 wpabuf_free(anqp->venue_name);
165 wpabuf_free(anqp->network_auth_type);
166 wpabuf_free(anqp->roaming_consortium);
167 wpabuf_free(anqp->ip_addr_type_availability);
168 wpabuf_free(anqp->nai_realm);
169 wpabuf_free(anqp->anqp_3gpp);
170 wpabuf_free(anqp->domain_name);
172 while ((elem = dl_list_first(&anqp->anqp_elems,
173 struct wpa_bss_anqp_elem, list))) {
174 dl_list_del(&elem->list);
175 wpabuf_free(elem->payload);
178 #endif /* CONFIG_INTERWORKING */
180 wpabuf_free(anqp->hs20_capability_list);
181 wpabuf_free(anqp->hs20_operator_friendly_name);
182 wpabuf_free(anqp->hs20_wan_metrics);
183 wpabuf_free(anqp->hs20_connection_capability);
184 wpabuf_free(anqp->hs20_operating_class);
185 wpabuf_free(anqp->hs20_osu_providers_list);
186 #endif /* CONFIG_HS20 */
192 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
193 struct wpa_bss *old_bss,
194 struct wpa_bss *new_bss)
196 struct wpa_radio_work *work;
197 struct wpa_connect_work *cwork;
199 work = radio_work_pending(wpa_s, "sme-connect");
201 work = radio_work_pending(wpa_s, "connect");
206 if (cwork->bss != old_bss)
209 wpa_printf(MSG_DEBUG,
210 "Update BSS pointer for the pending connect radio work");
211 cwork->bss = new_bss;
213 cwork->bss_removed = 1;
217 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
220 if (wpa_s->last_scan_res) {
222 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
223 if (wpa_s->last_scan_res[i] == bss) {
224 os_memmove(&wpa_s->last_scan_res[i],
225 &wpa_s->last_scan_res[i + 1],
226 (wpa_s->last_scan_res_used - i - 1)
227 * sizeof(struct wpa_bss *));
228 wpa_s->last_scan_res_used--;
233 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
234 dl_list_del(&bss->list);
235 dl_list_del(&bss->list_id);
237 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
238 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
239 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
240 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
241 wpa_bss_anqp_free(bss->anqp);
247 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
248 * @wpa_s: Pointer to wpa_supplicant data
251 * @ssid_len: Length of @ssid
252 * Returns: Pointer to the BSS entry or %NULL if not found
254 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
255 const u8 *ssid, size_t ssid_len)
258 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
260 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
261 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
262 bss->ssid_len == ssid_len &&
263 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
270 static void calculate_update_time(const struct os_reltime *fetch_time,
272 struct os_reltime *update_time)
276 update_time->sec = fetch_time->sec;
277 update_time->usec = fetch_time->usec;
278 update_time->sec -= age_ms / 1000;
279 usec = (age_ms % 1000) * 1000;
280 if (update_time->usec < usec) {
282 update_time->usec += 1000000;
284 update_time->usec -= usec;
288 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
289 struct os_reltime *fetch_time)
291 dst->flags = src->flags;
292 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
293 dst->freq = src->freq;
294 dst->beacon_int = src->beacon_int;
295 dst->caps = src->caps;
296 dst->qual = src->qual;
297 dst->noise = src->noise;
298 dst->level = src->level;
300 dst->est_throughput = src->est_throughput;
303 calculate_update_time(fetch_time, src->age, &dst->last_update);
307 static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
311 struct wpa_ssid *ssid;
312 struct wpabuf *wps_ie;
315 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
319 if (wps_is_selected_pbc_registrar(wps_ie)) {
321 } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
326 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
327 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
329 if (ssid->ssid_len &&
330 (ssid->ssid_len != bss->ssid_len ||
331 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
335 ret = eap_is_wps_pbc_enrollee(&ssid->eap);
337 ret = eap_is_wps_pin_enrollee(&ssid->eap);
341 #endif /* CONFIG_WPS */
347 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
349 struct wpa_ssid *ssid;
351 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
352 if (ssid->ssid == NULL || ssid->ssid_len == 0)
354 if (ssid->ssid_len == bss->ssid_len &&
355 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
363 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
365 if (bss == wpa_s->current_bss)
368 if (wpa_s->current_bss &&
369 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
370 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
371 bss->ssid_len) != 0))
372 return 0; /* SSID has changed */
374 return !is_zero_ether_addr(bss->bssid) &&
375 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
376 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
380 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
384 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
385 if (!wpa_bss_known(wpa_s, bss) &&
386 !wpa_bss_is_wps_candidate(wpa_s, bss)) {
387 wpa_bss_remove(wpa_s, bss, __func__);
396 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
401 * Remove the oldest entry that does not match with any configured
404 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
408 * Remove the oldest entry that isn't currently in use.
410 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
411 if (!wpa_bss_in_use(wpa_s, bss)) {
412 wpa_bss_remove(wpa_s, bss, __func__);
421 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
422 const u8 *ssid, size_t ssid_len,
423 struct wpa_scan_res *res,
424 struct os_reltime *fetch_time)
428 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
431 bss->id = wpa_s->bss_next_id++;
432 bss->last_update_idx = wpa_s->bss_update_idx;
433 wpa_bss_copy_res(bss, res, fetch_time);
434 os_memcpy(bss->ssid, ssid, ssid_len);
435 bss->ssid_len = ssid_len;
436 bss->ie_len = res->ie_len;
437 bss->beacon_ie_len = res->beacon_ie_len;
438 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
439 wpa_bss_set_hessid(bss);
441 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
442 wpa_bss_remove_oldest(wpa_s) != 0) {
443 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
444 "because all BSSes are in use. We should normally "
445 "not get here!", (int) wpa_s->num_bss + 1);
446 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
449 dl_list_add_tail(&wpa_s->bss, &bss->list);
450 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
452 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
453 " SSID '%s' freq %d",
454 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
456 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
461 static int are_ies_equal(const struct wpa_bss *old,
462 const struct wpa_scan_res *new_res, u32 ie)
464 const u8 *old_ie, *new_ie;
465 struct wpabuf *old_ie_buff = NULL;
466 struct wpabuf *new_ie_buff = NULL;
467 int new_ie_len, old_ie_len, ret, is_multi;
470 case WPA_IE_VENDOR_TYPE:
471 old_ie = wpa_bss_get_vendor_ie(old, ie);
472 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
475 case WPS_IE_VENDOR_TYPE:
476 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
477 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
481 case WLAN_EID_SUPP_RATES:
482 case WLAN_EID_EXT_SUPP_RATES:
483 old_ie = wpa_bss_get_ie(old, ie);
484 new_ie = wpa_scan_get_ie(new_res, ie);
488 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
493 /* in case of multiple IEs stored in buffer */
494 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
495 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
496 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
497 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
499 /* in case of single IE */
500 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
501 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
504 if (!old_ie || !new_ie)
505 ret = !old_ie && !new_ie;
507 ret = (old_ie_len == new_ie_len &&
508 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
510 wpabuf_free(old_ie_buff);
511 wpabuf_free(new_ie_buff);
517 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
518 const struct wpa_scan_res *new_res)
521 int caps_diff = old->caps ^ new_res->caps;
523 if (old->freq != new_res->freq)
524 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
526 if (old->level != new_res->level)
527 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
529 if (caps_diff & IEEE80211_CAP_PRIVACY)
530 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
532 if (caps_diff & IEEE80211_CAP_IBSS)
533 changes |= WPA_BSS_MODE_CHANGED_FLAG;
535 if (old->ie_len == new_res->ie_len &&
536 os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
538 changes |= WPA_BSS_IES_CHANGED_FLAG;
540 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
541 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
543 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
544 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
546 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
547 changes |= WPA_BSS_WPS_CHANGED_FLAG;
549 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
550 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
551 changes |= WPA_BSS_RATES_CHANGED_FLAG;
557 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
558 const struct wpa_bss *bss)
560 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
561 wpas_notify_bss_freq_changed(wpa_s, bss->id);
563 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
564 wpas_notify_bss_signal_changed(wpa_s, bss->id);
566 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
567 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
569 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
570 wpas_notify_bss_mode_changed(wpa_s, bss->id);
572 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
573 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
575 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
576 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
578 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
579 wpas_notify_bss_wps_changed(wpa_s, bss->id);
581 if (changes & WPA_BSS_IES_CHANGED_FLAG)
582 wpas_notify_bss_ies_changed(wpa_s, bss->id);
584 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
585 wpas_notify_bss_rates_changed(wpa_s, bss->id);
587 wpas_notify_bss_seen(wpa_s, bss->id);
591 static struct wpa_bss *
592 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
593 struct wpa_scan_res *res, struct os_reltime *fetch_time)
597 changes = wpa_bss_compare_res(bss, res);
598 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
599 wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
600 MAC2STR(bss->bssid), bss->freq, res->freq);
601 bss->scan_miss_count = 0;
602 bss->last_update_idx = wpa_s->bss_update_idx;
603 wpa_bss_copy_res(bss, res, fetch_time);
604 /* Move the entry to the end of the list */
605 dl_list_del(&bss->list);
607 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
608 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
610 * This can happen when non-P2P station interface runs a scan
611 * without P2P IE in the Probe Request frame. P2P GO would reply
612 * to that with a Probe Response that does not include P2P IE.
613 * Do not update the IEs in this BSS entry to avoid such loss of
614 * information that may be needed for P2P operations to
615 * determine group information.
617 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
618 MACSTR " since that would remove P2P IE information",
619 MAC2STR(bss->bssid));
621 #endif /* CONFIG_P2P */
622 if (bss->ie_len + bss->beacon_ie_len >=
623 res->ie_len + res->beacon_ie_len) {
624 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
625 bss->ie_len = res->ie_len;
626 bss->beacon_ie_len = res->beacon_ie_len;
628 struct wpa_bss *nbss;
629 struct dl_list *prev = bss->list_id.prev;
630 dl_list_del(&bss->list_id);
631 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
635 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
636 if (wpa_s->last_scan_res[i] == bss) {
637 wpa_s->last_scan_res[i] = nbss;
641 if (wpa_s->current_bss == bss)
642 wpa_s->current_bss = nbss;
643 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
645 os_memcpy(bss + 1, res + 1,
646 res->ie_len + res->beacon_ie_len);
647 bss->ie_len = res->ie_len;
648 bss->beacon_ie_len = res->beacon_ie_len;
650 dl_list_add(prev, &bss->list_id);
652 if (changes & WPA_BSS_IES_CHANGED_FLAG)
653 wpa_bss_set_hessid(bss);
654 dl_list_add_tail(&wpa_s->bss, &bss->list);
656 notify_bss_changes(wpa_s, changes, bss);
663 * wpa_bss_update_start - Start a BSS table update from scan results
664 * @wpa_s: Pointer to wpa_supplicant data
666 * This function is called at the start of each BSS table update round for new
667 * scan results. The actual scan result entries are indicated with calls to
668 * wpa_bss_update_scan_res() and the update round is finished with a call to
669 * wpa_bss_update_end().
671 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
673 wpa_s->bss_update_idx++;
674 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
675 wpa_s->bss_update_idx);
676 wpa_s->last_scan_res_used = 0;
681 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
682 * @wpa_s: Pointer to wpa_supplicant data
684 * @fetch_time: Time when the result was fetched from the driver
686 * This function updates a BSS table entry (or adds one) based on a scan result.
687 * This is called separately for each scan result between the calls to
688 * wpa_bss_update_start() and wpa_bss_update_end().
690 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
691 struct wpa_scan_res *res,
692 struct os_reltime *fetch_time)
694 const u8 *ssid, *p2p, *mesh;
697 if (wpa_s->conf->ignore_old_scan_res) {
698 struct os_reltime update;
699 calculate_update_time(fetch_time, res->age, &update);
700 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
701 struct os_reltime age;
702 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
704 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
705 "table entry that is %u.%06u seconds older "
706 "than our scan trigger",
707 (unsigned int) age.sec,
708 (unsigned int) age.usec);
713 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
715 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
716 MACSTR, MAC2STR(res->bssid));
719 if (ssid[1] > SSID_MAX_LEN) {
720 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
721 MACSTR, MAC2STR(res->bssid));
725 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
728 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
730 * If it's a P2P specific interface, then don't update
731 * the scan result without a P2P IE.
733 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
734 " update for P2P interface", MAC2STR(res->bssid));
737 #endif /* CONFIG_P2P */
738 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
739 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
740 return; /* Skip P2P listen discovery results here */
742 /* TODO: add option for ignoring BSSes we are not interested in
743 * (to save memory) */
745 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
746 if (mesh && mesh[1] <= SSID_MAX_LEN)
749 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
751 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
753 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
754 if (wpa_s->last_scan_res) {
756 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
757 if (bss == wpa_s->last_scan_res[i]) {
758 /* Already in the list */
767 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
770 if (wpa_s->last_scan_res_size == 0)
773 siz = wpa_s->last_scan_res_size * 2;
774 n = os_realloc_array(wpa_s->last_scan_res, siz,
775 sizeof(struct wpa_bss *));
778 wpa_s->last_scan_res = n;
779 wpa_s->last_scan_res_size = siz;
782 if (wpa_s->last_scan_res)
783 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
787 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
788 const struct scan_info *info)
796 if (info->num_freqs) {
798 for (i = 0; i < info->num_freqs; i++) {
799 if (bss->freq == info->freqs[i]) {
808 if (info->num_ssids) {
810 for (i = 0; i < info->num_ssids; i++) {
811 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
812 if ((s->ssid == NULL || s->ssid_len == 0) ||
813 (s->ssid_len == bss->ssid_len &&
814 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
829 * wpa_bss_update_end - End a BSS table update from scan results
830 * @wpa_s: Pointer to wpa_supplicant data
831 * @info: Information about scan parameters
832 * @new_scan: Whether this update round was based on a new scan
834 * This function is called at the end of each BSS table update round for new
835 * scan results. The start of the update was indicated with a call to
836 * wpa_bss_update_start().
838 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
841 struct wpa_bss *bss, *n;
843 os_get_reltime(&wpa_s->last_scan);
844 if ((info && info->aborted) || !new_scan)
845 return; /* do not expire entries without new scan */
847 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
848 if (wpa_bss_in_use(wpa_s, bss))
850 if (!wpa_bss_included_in_scan(bss, info))
851 continue; /* expire only BSSes that were scanned */
852 if (bss->last_update_idx < wpa_s->bss_update_idx)
853 bss->scan_miss_count++;
854 if (bss->scan_miss_count >=
855 wpa_s->conf->bss_expiration_scan_count) {
856 wpa_bss_remove(wpa_s, bss, "no match in scan");
860 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
861 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
866 * wpa_bss_flush_by_age - Flush old BSS entries
867 * @wpa_s: Pointer to wpa_supplicant data
868 * @age: Maximum entry age in seconds
870 * Remove BSS entries that have not been updated during the last @age seconds.
872 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
874 struct wpa_bss *bss, *n;
877 if (dl_list_empty(&wpa_s->bss))
883 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
884 if (wpa_bss_in_use(wpa_s, bss))
887 if (os_reltime_before(&bss->last_update, &t)) {
888 wpa_bss_remove(wpa_s, bss, __func__);
896 * wpa_bss_init - Initialize BSS table
897 * @wpa_s: Pointer to wpa_supplicant data
898 * Returns: 0 on success, -1 on failure
900 * This prepares BSS table lists and timer for periodic updates. The BSS table
901 * is deinitialized with wpa_bss_deinit() once not needed anymore.
903 int wpa_bss_init(struct wpa_supplicant *wpa_s)
905 dl_list_init(&wpa_s->bss);
906 dl_list_init(&wpa_s->bss_id);
912 * wpa_bss_flush - Flush all unused BSS entries
913 * @wpa_s: Pointer to wpa_supplicant data
915 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
917 struct wpa_bss *bss, *n;
919 wpa_s->clear_driver_scan_cache = 1;
921 if (wpa_s->bss.next == NULL)
922 return; /* BSS table not yet initialized */
924 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
925 if (wpa_bss_in_use(wpa_s, bss))
927 wpa_bss_remove(wpa_s, bss, __func__);
933 * wpa_bss_deinit - Deinitialize BSS table
934 * @wpa_s: Pointer to wpa_supplicant data
936 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
938 wpa_bss_flush(wpa_s);
943 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
944 * @wpa_s: Pointer to wpa_supplicant data
946 * Returns: Pointer to the BSS entry or %NULL if not found
948 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
952 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
954 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
955 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
963 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
964 * @wpa_s: Pointer to wpa_supplicant data
966 * Returns: Pointer to the BSS entry or %NULL if not found
968 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
969 * find the entry that has the most recent update. This can help in finding the
970 * correct entry in cases where the SSID of the AP may have changed recently
971 * (e.g., in WPS reconfiguration cases).
973 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
976 struct wpa_bss *bss, *found = NULL;
977 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
979 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
980 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
983 os_reltime_before(&found->last_update, &bss->last_update))
992 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
993 * @wpa_s: Pointer to wpa_supplicant data
994 * @dev_addr: P2P Device Address of the GO
995 * Returns: Pointer to the BSS entry or %NULL if not found
997 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1000 struct wpa_bss *bss;
1001 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1003 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
1005 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
1010 #endif /* CONFIG_P2P */
1014 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1015 * @wpa_s: Pointer to wpa_supplicant data
1016 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1017 * Returns: Pointer to the BSS entry or %NULL if not found
1019 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1021 struct wpa_bss *bss;
1022 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1031 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1032 * @wpa_s: Pointer to wpa_supplicant data
1033 * @idf: Smallest allowed identifier assigned for the entry
1034 * @idf: Largest allowed identifier assigned for the entry
1035 * Returns: Pointer to the BSS entry or %NULL if not found
1037 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1038 * smallest id value to be fetched within the specified range without the
1039 * caller having to know the exact id.
1041 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1042 unsigned int idf, unsigned int idl)
1044 struct wpa_bss *bss;
1045 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1046 if (bss->id >= idf && bss->id <= idl)
1054 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1055 * @bss: BSS table entry
1056 * @ie: Information element identitifier (WLAN_EID_*)
1057 * Returns: Pointer to the information element (id field) or %NULL if not found
1059 * This function returns the first matching information element in the BSS
1062 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1064 return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
1069 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1070 * @bss: BSS table entry
1071 * @vendor_type: Vendor type (four octets starting the IE payload)
1072 * Returns: Pointer to the information element (id field) or %NULL if not found
1074 * This function returns the first matching information element in the BSS
1077 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1079 const u8 *end, *pos;
1081 pos = (const u8 *) (bss + 1);
1082 end = pos + bss->ie_len;
1084 while (end - pos > 1) {
1085 if (2 + pos[1] > end - pos)
1087 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1088 vendor_type == WPA_GET_BE32(&pos[2]))
1098 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1099 * @bss: BSS table entry
1100 * @vendor_type: Vendor type (four octets starting the IE payload)
1101 * Returns: Pointer to the information element (id field) or %NULL if not found
1103 * This function returns the first matching information element in the BSS
1106 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1107 * from Beacon frames instead of either Beacon or Probe Response frames.
1109 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1112 const u8 *end, *pos;
1114 if (bss->beacon_ie_len == 0)
1117 pos = (const u8 *) (bss + 1);
1119 end = pos + bss->beacon_ie_len;
1121 while (end - pos > 1) {
1122 if (2 + pos[1] > end - pos)
1124 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1125 vendor_type == WPA_GET_BE32(&pos[2]))
1135 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1136 * @bss: BSS table entry
1137 * @vendor_type: Vendor type (four octets starting the IE payload)
1138 * Returns: Pointer to the information element payload or %NULL if not found
1140 * This function returns concatenated payload of possibly fragmented vendor
1141 * specific information elements in the BSS entry. The caller is responsible for
1142 * freeing the returned buffer.
1144 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1148 const u8 *end, *pos;
1150 buf = wpabuf_alloc(bss->ie_len);
1154 pos = (const u8 *) (bss + 1);
1155 end = pos + bss->ie_len;
1157 while (end - pos > 1) {
1158 if (2 + pos[1] > end - pos)
1160 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1161 vendor_type == WPA_GET_BE32(&pos[2]))
1162 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1166 if (wpabuf_len(buf) == 0) {
1176 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1177 * @bss: BSS table entry
1178 * @vendor_type: Vendor type (four octets starting the IE payload)
1179 * Returns: Pointer to the information element payload or %NULL if not found
1181 * This function returns concatenated payload of possibly fragmented vendor
1182 * specific information elements in the BSS entry. The caller is responsible for
1183 * freeing the returned buffer.
1185 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1186 * from Beacon frames instead of either Beacon or Probe Response frames.
1188 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1192 const u8 *end, *pos;
1194 buf = wpabuf_alloc(bss->beacon_ie_len);
1198 pos = (const u8 *) (bss + 1);
1200 end = pos + bss->beacon_ie_len;
1202 while (end - pos > 1) {
1203 if (2 + pos[1] > end - pos)
1205 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1206 vendor_type == WPA_GET_BE32(&pos[2]))
1207 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1211 if (wpabuf_len(buf) == 0) {
1221 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1222 * @bss: BSS table entry
1223 * Returns: Maximum legacy rate in units of 500 kbps
1225 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1231 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1232 for (i = 0; ie && i < ie[1]; i++) {
1233 if ((ie[i + 2] & 0x7f) > rate)
1234 rate = ie[i + 2] & 0x7f;
1237 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1238 for (i = 0; ie && i < ie[1]; i++) {
1239 if ((ie[i + 2] & 0x7f) > rate)
1240 rate = ie[i + 2] & 0x7f;
1248 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1249 * @bss: BSS table entry
1250 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1251 * Returns: number of legacy TX rates or -1 on failure
1253 * The caller is responsible for freeing the returned buffer with os_free() in
1256 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1263 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1264 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1266 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1272 for (i = 0; ie && i < ie[1]; i++)
1273 r[i] = ie[i + 2] & 0x7f;
1275 for (j = 0; ie2 && j < ie2[1]; j++)
1276 r[i + j] = ie2[j + 2] & 0x7f;