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"
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
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)
153 if (anqp->users > 0) {
154 /* Another BSS entry holds a pointer to this ANQP info */
158 #ifdef CONFIG_INTERWORKING
159 wpabuf_free(anqp->capability_list);
160 wpabuf_free(anqp->venue_name);
161 wpabuf_free(anqp->network_auth_type);
162 wpabuf_free(anqp->roaming_consortium);
163 wpabuf_free(anqp->ip_addr_type_availability);
164 wpabuf_free(anqp->nai_realm);
165 wpabuf_free(anqp->anqp_3gpp);
166 wpabuf_free(anqp->domain_name);
167 #endif /* CONFIG_INTERWORKING */
169 wpabuf_free(anqp->hs20_capability_list);
170 wpabuf_free(anqp->hs20_operator_friendly_name);
171 wpabuf_free(anqp->hs20_wan_metrics);
172 wpabuf_free(anqp->hs20_connection_capability);
173 wpabuf_free(anqp->hs20_operating_class);
174 wpabuf_free(anqp->hs20_osu_providers_list);
175 #endif /* CONFIG_HS20 */
181 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
182 struct wpa_bss *old_bss,
183 struct wpa_bss *new_bss)
185 struct wpa_radio_work *work;
186 struct wpa_connect_work *cwork;
188 work = radio_work_pending(wpa_s, "sme-connect");
190 work = radio_work_pending(wpa_s, "connect");
195 if (cwork->bss != old_bss)
198 wpa_printf(MSG_DEBUG,
199 "Update BSS pointer for the pending connect radio work");
200 cwork->bss = new_bss;
202 cwork->bss_removed = 1;
206 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
209 if (wpa_s->last_scan_res) {
211 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
212 if (wpa_s->last_scan_res[i] == bss) {
213 os_memmove(&wpa_s->last_scan_res[i],
214 &wpa_s->last_scan_res[i + 1],
215 (wpa_s->last_scan_res_used - i - 1)
216 * sizeof(struct wpa_bss *));
217 wpa_s->last_scan_res_used--;
222 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
223 dl_list_del(&bss->list);
224 dl_list_del(&bss->list_id);
226 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
227 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
228 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
229 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
230 wpa_bss_anqp_free(bss->anqp);
236 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
237 * @wpa_s: Pointer to wpa_supplicant data
240 * @ssid_len: Length of @ssid
241 * Returns: Pointer to the BSS entry or %NULL if not found
243 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
244 const u8 *ssid, size_t ssid_len)
247 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
249 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
250 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
251 bss->ssid_len == ssid_len &&
252 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
259 static void calculate_update_time(const struct os_reltime *fetch_time,
261 struct os_reltime *update_time)
265 update_time->sec = fetch_time->sec;
266 update_time->usec = fetch_time->usec;
267 update_time->sec -= age_ms / 1000;
268 usec = (age_ms % 1000) * 1000;
269 if (update_time->usec < usec) {
271 update_time->usec += 1000000;
273 update_time->usec -= usec;
277 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
278 struct os_reltime *fetch_time)
280 dst->flags = src->flags;
281 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
282 dst->freq = src->freq;
283 dst->beacon_int = src->beacon_int;
284 dst->caps = src->caps;
285 dst->qual = src->qual;
286 dst->noise = src->noise;
287 dst->level = src->level;
289 dst->est_throughput = src->est_throughput;
292 calculate_update_time(fetch_time, src->age, &dst->last_update);
296 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
298 struct wpa_ssid *ssid;
300 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
301 if (ssid->ssid == NULL || ssid->ssid_len == 0)
303 if (ssid->ssid_len == bss->ssid_len &&
304 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
312 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
314 return bss == wpa_s->current_bss ||
315 (!is_zero_ether_addr(bss->bssid) &&
316 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
317 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0));
321 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
325 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
326 if (!wpa_bss_known(wpa_s, bss)) {
327 wpa_bss_remove(wpa_s, bss, __func__);
336 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
341 * Remove the oldest entry that does not match with any configured
344 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
348 * Remove the oldest entry that isn't currently in use.
350 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
351 if (!wpa_bss_in_use(wpa_s, bss)) {
352 wpa_bss_remove(wpa_s, bss, __func__);
361 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
362 const u8 *ssid, size_t ssid_len,
363 struct wpa_scan_res *res,
364 struct os_reltime *fetch_time)
368 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
371 bss->id = wpa_s->bss_next_id++;
372 bss->last_update_idx = wpa_s->bss_update_idx;
373 wpa_bss_copy_res(bss, res, fetch_time);
374 os_memcpy(bss->ssid, ssid, ssid_len);
375 bss->ssid_len = ssid_len;
376 bss->ie_len = res->ie_len;
377 bss->beacon_ie_len = res->beacon_ie_len;
378 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
379 wpa_bss_set_hessid(bss);
381 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
382 wpa_bss_remove_oldest(wpa_s) != 0) {
383 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
384 "because all BSSes are in use. We should normally "
385 "not get here!", (int) wpa_s->num_bss + 1);
386 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
389 dl_list_add_tail(&wpa_s->bss, &bss->list);
390 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
392 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
394 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
395 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
400 static int are_ies_equal(const struct wpa_bss *old,
401 const struct wpa_scan_res *new, u32 ie)
403 const u8 *old_ie, *new_ie;
404 struct wpabuf *old_ie_buff = NULL;
405 struct wpabuf *new_ie_buff = NULL;
406 int new_ie_len, old_ie_len, ret, is_multi;
409 case WPA_IE_VENDOR_TYPE:
410 old_ie = wpa_bss_get_vendor_ie(old, ie);
411 new_ie = wpa_scan_get_vendor_ie(new, ie);
414 case WPS_IE_VENDOR_TYPE:
415 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
416 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
420 case WLAN_EID_SUPP_RATES:
421 case WLAN_EID_EXT_SUPP_RATES:
422 old_ie = wpa_bss_get_ie(old, ie);
423 new_ie = wpa_scan_get_ie(new, ie);
427 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
432 /* in case of multiple IEs stored in buffer */
433 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
434 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
435 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
436 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
438 /* in case of single IE */
439 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
440 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
443 if (!old_ie || !new_ie)
444 ret = !old_ie && !new_ie;
446 ret = (old_ie_len == new_ie_len &&
447 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
449 wpabuf_free(old_ie_buff);
450 wpabuf_free(new_ie_buff);
456 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
457 const struct wpa_scan_res *new)
460 int caps_diff = old->caps ^ new->caps;
462 if (old->freq != new->freq)
463 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
465 if (old->level != new->level)
466 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
468 if (caps_diff & IEEE80211_CAP_PRIVACY)
469 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
471 if (caps_diff & IEEE80211_CAP_IBSS)
472 changes |= WPA_BSS_MODE_CHANGED_FLAG;
474 if (old->ie_len == new->ie_len &&
475 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
477 changes |= WPA_BSS_IES_CHANGED_FLAG;
479 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
480 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
482 if (!are_ies_equal(old, new, WLAN_EID_RSN))
483 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
485 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
486 changes |= WPA_BSS_WPS_CHANGED_FLAG;
488 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
489 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
490 changes |= WPA_BSS_RATES_CHANGED_FLAG;
496 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
497 const struct wpa_bss *bss)
499 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
500 wpas_notify_bss_freq_changed(wpa_s, bss->id);
502 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
503 wpas_notify_bss_signal_changed(wpa_s, bss->id);
505 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
506 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
508 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
509 wpas_notify_bss_mode_changed(wpa_s, bss->id);
511 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
512 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
514 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
515 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
517 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
518 wpas_notify_bss_wps_changed(wpa_s, bss->id);
520 if (changes & WPA_BSS_IES_CHANGED_FLAG)
521 wpas_notify_bss_ies_changed(wpa_s, bss->id);
523 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
524 wpas_notify_bss_rates_changed(wpa_s, bss->id);
526 wpas_notify_bss_seen(wpa_s, bss->id);
530 static struct wpa_bss *
531 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
532 struct wpa_scan_res *res, struct os_reltime *fetch_time)
536 changes = wpa_bss_compare_res(bss, res);
537 bss->scan_miss_count = 0;
538 bss->last_update_idx = wpa_s->bss_update_idx;
539 wpa_bss_copy_res(bss, res, fetch_time);
540 /* Move the entry to the end of the list */
541 dl_list_del(&bss->list);
543 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
544 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
546 * This can happen when non-P2P station interface runs a scan
547 * without P2P IE in the Probe Request frame. P2P GO would reply
548 * to that with a Probe Response that does not include P2P IE.
549 * Do not update the IEs in this BSS entry to avoid such loss of
550 * information that may be needed for P2P operations to
551 * determine group information.
553 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
554 MACSTR " since that would remove P2P IE information",
555 MAC2STR(bss->bssid));
557 #endif /* CONFIG_P2P */
558 if (bss->ie_len + bss->beacon_ie_len >=
559 res->ie_len + res->beacon_ie_len) {
560 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
561 bss->ie_len = res->ie_len;
562 bss->beacon_ie_len = res->beacon_ie_len;
564 struct wpa_bss *nbss;
565 struct dl_list *prev = bss->list_id.prev;
566 dl_list_del(&bss->list_id);
567 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
571 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
572 if (wpa_s->last_scan_res[i] == bss) {
573 wpa_s->last_scan_res[i] = nbss;
577 if (wpa_s->current_bss == bss)
578 wpa_s->current_bss = nbss;
579 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
581 os_memcpy(bss + 1, res + 1,
582 res->ie_len + res->beacon_ie_len);
583 bss->ie_len = res->ie_len;
584 bss->beacon_ie_len = res->beacon_ie_len;
586 dl_list_add(prev, &bss->list_id);
588 if (changes & WPA_BSS_IES_CHANGED_FLAG)
589 wpa_bss_set_hessid(bss);
590 dl_list_add_tail(&wpa_s->bss, &bss->list);
592 notify_bss_changes(wpa_s, changes, bss);
599 * wpa_bss_update_start - Start a BSS table update from scan results
600 * @wpa_s: Pointer to wpa_supplicant data
602 * This function is called at the start of each BSS table update round for new
603 * scan results. The actual scan result entries are indicated with calls to
604 * wpa_bss_update_scan_res() and the update round is finished with a call to
605 * wpa_bss_update_end().
607 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
609 wpa_s->bss_update_idx++;
610 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
611 wpa_s->bss_update_idx);
612 wpa_s->last_scan_res_used = 0;
617 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
618 * @wpa_s: Pointer to wpa_supplicant data
620 * @fetch_time: Time when the result was fetched from the driver
622 * This function updates a BSS table entry (or adds one) based on a scan result.
623 * This is called separately for each scan result between the calls to
624 * wpa_bss_update_start() and wpa_bss_update_end().
626 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
627 struct wpa_scan_res *res,
628 struct os_reltime *fetch_time)
630 const u8 *ssid, *p2p, *mesh;
633 if (wpa_s->conf->ignore_old_scan_res) {
634 struct os_reltime update;
635 calculate_update_time(fetch_time, res->age, &update);
636 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
637 struct os_reltime age;
638 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
640 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
641 "table entry that is %u.%06u seconds older "
642 "than our scan trigger",
643 (unsigned int) age.sec,
644 (unsigned int) age.usec);
649 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
651 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
652 MACSTR, MAC2STR(res->bssid));
656 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
657 MACSTR, MAC2STR(res->bssid));
661 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
664 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
666 * If it's a P2P specific interface, then don't update
667 * the scan result without a P2P IE.
669 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
670 " update for P2P interface", MAC2STR(res->bssid));
673 #endif /* CONFIG_P2P */
674 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
675 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
676 return; /* Skip P2P listen discovery results here */
678 /* TODO: add option for ignoring BSSes we are not interested in
679 * (to save memory) */
681 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
682 if (mesh && mesh[1] <= 32)
685 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
687 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
689 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
690 if (wpa_s->last_scan_res) {
692 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
693 if (bss == wpa_s->last_scan_res[i]) {
694 /* Already in the list */
703 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
706 if (wpa_s->last_scan_res_size == 0)
709 siz = wpa_s->last_scan_res_size * 2;
710 n = os_realloc_array(wpa_s->last_scan_res, siz,
711 sizeof(struct wpa_bss *));
714 wpa_s->last_scan_res = n;
715 wpa_s->last_scan_res_size = siz;
718 if (wpa_s->last_scan_res)
719 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
723 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
724 const struct scan_info *info)
732 if (info->num_freqs) {
734 for (i = 0; i < info->num_freqs; i++) {
735 if (bss->freq == info->freqs[i]) {
744 if (info->num_ssids) {
746 for (i = 0; i < info->num_ssids; i++) {
747 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
748 if ((s->ssid == NULL || s->ssid_len == 0) ||
749 (s->ssid_len == bss->ssid_len &&
750 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
765 * wpa_bss_update_end - End a BSS table update from scan results
766 * @wpa_s: Pointer to wpa_supplicant data
767 * @info: Information about scan parameters
768 * @new_scan: Whether this update round was based on a new scan
770 * This function is called at the end of each BSS table update round for new
771 * scan results. The start of the update was indicated with a call to
772 * wpa_bss_update_start().
774 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
777 struct wpa_bss *bss, *n;
779 os_get_reltime(&wpa_s->last_scan);
781 return; /* do not expire entries without new scan */
783 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
784 if (wpa_bss_in_use(wpa_s, bss))
786 if (!wpa_bss_included_in_scan(bss, info))
787 continue; /* expire only BSSes that were scanned */
788 if (bss->last_update_idx < wpa_s->bss_update_idx)
789 bss->scan_miss_count++;
790 if (bss->scan_miss_count >=
791 wpa_s->conf->bss_expiration_scan_count) {
792 wpa_bss_remove(wpa_s, bss, "no match in scan");
796 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
797 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
802 * wpa_bss_flush_by_age - Flush old BSS entries
803 * @wpa_s: Pointer to wpa_supplicant data
804 * @age: Maximum entry age in seconds
806 * Remove BSS entries that have not been updated during the last @age seconds.
808 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
810 struct wpa_bss *bss, *n;
813 if (dl_list_empty(&wpa_s->bss))
819 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
820 if (wpa_bss_in_use(wpa_s, bss))
823 if (os_reltime_before(&bss->last_update, &t)) {
824 wpa_bss_remove(wpa_s, bss, __func__);
831 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
833 struct wpa_supplicant *wpa_s = eloop_ctx;
835 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
836 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
837 wpa_bss_timeout, wpa_s, NULL);
842 * wpa_bss_init - Initialize BSS table
843 * @wpa_s: Pointer to wpa_supplicant data
844 * Returns: 0 on success, -1 on failure
846 * This prepares BSS table lists and timer for periodic updates. The BSS table
847 * is deinitialized with wpa_bss_deinit() once not needed anymore.
849 int wpa_bss_init(struct wpa_supplicant *wpa_s)
851 dl_list_init(&wpa_s->bss);
852 dl_list_init(&wpa_s->bss_id);
853 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
854 wpa_bss_timeout, wpa_s, NULL);
860 * wpa_bss_flush - Flush all unused BSS entries
861 * @wpa_s: Pointer to wpa_supplicant data
863 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
865 struct wpa_bss *bss, *n;
867 wpa_s->clear_driver_scan_cache = 1;
869 if (wpa_s->bss.next == NULL)
870 return; /* BSS table not yet initialized */
872 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
873 if (wpa_bss_in_use(wpa_s, bss))
875 wpa_bss_remove(wpa_s, bss, __func__);
881 * wpa_bss_deinit - Deinitialize BSS table
882 * @wpa_s: Pointer to wpa_supplicant data
884 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
886 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
887 wpa_bss_flush(wpa_s);
892 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
893 * @wpa_s: Pointer to wpa_supplicant data
895 * Returns: Pointer to the BSS entry or %NULL if not found
897 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
901 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
903 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
904 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
912 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
913 * @wpa_s: Pointer to wpa_supplicant data
915 * Returns: Pointer to the BSS entry or %NULL if not found
917 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
918 * find the entry that has the most recent update. This can help in finding the
919 * correct entry in cases where the SSID of the AP may have changed recently
920 * (e.g., in WPS reconfiguration cases).
922 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
925 struct wpa_bss *bss, *found = NULL;
926 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
928 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
929 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
932 os_reltime_before(&found->last_update, &bss->last_update))
941 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
942 * @wpa_s: Pointer to wpa_supplicant data
943 * @dev_addr: P2P Device Address of the GO
944 * Returns: Pointer to the BSS entry or %NULL if not found
946 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
950 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
952 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
954 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
959 #endif /* CONFIG_P2P */
963 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
964 * @wpa_s: Pointer to wpa_supplicant data
965 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
966 * Returns: Pointer to the BSS entry or %NULL if not found
968 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
971 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
980 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
981 * @wpa_s: Pointer to wpa_supplicant data
982 * @idf: Smallest allowed identifier assigned for the entry
983 * @idf: Largest allowed identifier assigned for the entry
984 * Returns: Pointer to the BSS entry or %NULL if not found
986 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
987 * smallest id value to be fetched within the specified range without the
988 * caller having to know the exact id.
990 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
991 unsigned int idf, unsigned int idl)
994 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
995 if (bss->id >= idf && bss->id <= idl)
1003 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1004 * @bss: BSS table entry
1005 * @ie: Information element identitifier (WLAN_EID_*)
1006 * Returns: Pointer to the information element (id field) or %NULL if not found
1008 * This function returns the first matching information element in the BSS
1011 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1013 const u8 *end, *pos;
1015 pos = (const u8 *) (bss + 1);
1016 end = pos + bss->ie_len;
1018 while (pos + 1 < end) {
1019 if (pos + 2 + pos[1] > end)
1031 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1032 * @bss: BSS table entry
1033 * @vendor_type: Vendor type (four octets starting the IE payload)
1034 * Returns: Pointer to the information element (id field) or %NULL if not found
1036 * This function returns the first matching information element in the BSS
1039 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1041 const u8 *end, *pos;
1043 pos = (const u8 *) (bss + 1);
1044 end = pos + bss->ie_len;
1046 while (pos + 1 < end) {
1047 if (pos + 2 + pos[1] > end)
1049 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1050 vendor_type == WPA_GET_BE32(&pos[2]))
1060 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1061 * @bss: BSS table entry
1062 * @vendor_type: Vendor type (four octets starting the IE payload)
1063 * Returns: Pointer to the information element (id field) or %NULL if not found
1065 * This function returns the first matching information element in the BSS
1068 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1069 * from Beacon frames instead of either Beacon or Probe Response frames.
1071 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1074 const u8 *end, *pos;
1076 if (bss->beacon_ie_len == 0)
1079 pos = (const u8 *) (bss + 1);
1081 end = pos + bss->beacon_ie_len;
1083 while (pos + 1 < end) {
1084 if (pos + 2 + pos[1] > end)
1086 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1087 vendor_type == WPA_GET_BE32(&pos[2]))
1097 * wpa_bss_get_vendor_ie_multi - 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 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1110 const u8 *end, *pos;
1112 buf = wpabuf_alloc(bss->ie_len);
1116 pos = (const u8 *) (bss + 1);
1117 end = pos + bss->ie_len;
1119 while (pos + 1 < end) {
1120 if (pos + 2 + pos[1] > end)
1122 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1123 vendor_type == WPA_GET_BE32(&pos[2]))
1124 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1128 if (wpabuf_len(buf) == 0) {
1138 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1139 * @bss: BSS table entry
1140 * @vendor_type: Vendor type (four octets starting the IE payload)
1141 * Returns: Pointer to the information element payload or %NULL if not found
1143 * This function returns concatenated payload of possibly fragmented vendor
1144 * specific information elements in the BSS entry. The caller is responsible for
1145 * freeing the returned buffer.
1147 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1148 * from Beacon frames instead of either Beacon or Probe Response frames.
1150 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1154 const u8 *end, *pos;
1156 buf = wpabuf_alloc(bss->beacon_ie_len);
1160 pos = (const u8 *) (bss + 1);
1162 end = pos + bss->beacon_ie_len;
1164 while (pos + 1 < end) {
1165 if (pos + 2 + pos[1] > end)
1167 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1168 vendor_type == WPA_GET_BE32(&pos[2]))
1169 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1173 if (wpabuf_len(buf) == 0) {
1183 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1184 * @bss: BSS table entry
1185 * Returns: Maximum legacy rate in units of 500 kbps
1187 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1193 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1194 for (i = 0; ie && i < ie[1]; i++) {
1195 if ((ie[i + 2] & 0x7f) > rate)
1196 rate = ie[i + 2] & 0x7f;
1199 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1200 for (i = 0; ie && i < ie[1]; i++) {
1201 if ((ie[i + 2] & 0x7f) > rate)
1202 rate = ie[i + 2] & 0x7f;
1210 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1211 * @bss: BSS table entry
1212 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1213 * Returns: number of legacy TX rates or -1 on failure
1215 * The caller is responsible for freeing the returned buffer with os_free() in
1218 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1225 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1226 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1228 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1234 for (i = 0; ie && i < ie[1]; i++)
1235 r[i] = ie[i + 2] & 0x7f;
1237 for (j = 0; ie2 && j < ie2[1]; j++)
1238 r[i + j] = ie2[j + 2] & 0x7f;