3 * Copyright (c) 2009-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "drivers/driver.h"
15 #include "wpa_supplicant_i.h"
23 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
25 #define WPA_BSS_EXPIRATION_PERIOD 10
27 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
28 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
29 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
30 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
31 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
32 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
33 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
34 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
35 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
38 static void wpa_bss_set_hessid(struct wpa_bss
*bss
)
40 #ifdef CONFIG_INTERWORKING
41 const u8
*ie
= wpa_bss_get_ie(bss
, WLAN_EID_INTERWORKING
);
42 if (ie
== NULL
|| (ie
[1] != 7 && ie
[1] != 9)) {
43 os_memset(bss
->hessid
, 0, ETH_ALEN
);
47 os_memcpy(bss
->hessid
, ie
+ 3, ETH_ALEN
);
49 os_memcpy(bss
->hessid
, ie
+ 5, ETH_ALEN
);
50 #endif /* CONFIG_INTERWORKING */
55 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
56 * Returns: Allocated ANQP data structure or %NULL on failure
58 * The allocated ANQP data structure has its users count set to 1. It may be
59 * shared by multiple BSS entries and each shared entry is freed with
60 * wpa_bss_anqp_free().
62 struct wpa_bss_anqp
* wpa_bss_anqp_alloc(void)
64 struct wpa_bss_anqp
*anqp
;
65 anqp
= os_zalloc(sizeof(*anqp
));
74 * wpa_bss_anqp_clone - Clone an ANQP data structure
75 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
76 * Returns: Cloned ANQP data structure or %NULL on failure
78 static struct wpa_bss_anqp
* wpa_bss_anqp_clone(struct wpa_bss_anqp
*anqp
)
80 struct wpa_bss_anqp
*n
;
82 n
= os_zalloc(sizeof(*n
));
86 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
87 #ifdef CONFIG_INTERWORKING
89 ANQP_DUP(network_auth_type
);
90 ANQP_DUP(roaming_consortium
);
91 ANQP_DUP(ip_addr_type_availability
);
94 ANQP_DUP(domain_name
);
95 #endif /* CONFIG_INTERWORKING */
97 ANQP_DUP(hs20_operator_friendly_name
);
98 ANQP_DUP(hs20_wan_metrics
);
99 ANQP_DUP(hs20_connection_capability
);
100 ANQP_DUP(hs20_operating_class
);
101 ANQP_DUP(hs20_osu_providers_list
);
102 #endif /* CONFIG_HS20 */
110 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
112 * Returns: 0 on success, -1 on failure
114 * This function ensures the specific BSS entry has an ANQP data structure that
115 * is not shared with any other BSS entry.
117 int wpa_bss_anqp_unshare_alloc(struct wpa_bss
*bss
)
119 struct wpa_bss_anqp
*anqp
;
121 if (bss
->anqp
&& bss
->anqp
->users
> 1) {
122 /* allocated, but shared - clone an unshared copy */
123 anqp
= wpa_bss_anqp_clone(bss
->anqp
);
133 return 0; /* already allocated and not shared */
135 /* not allocated - allocate a new storage area */
136 bss
->anqp
= wpa_bss_anqp_alloc();
137 return bss
->anqp
? 0 : -1;
142 * wpa_bss_anqp_free - Free an ANQP data structure
143 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
145 static void wpa_bss_anqp_free(struct wpa_bss_anqp
*anqp
)
151 if (anqp
->users
> 0) {
152 /* Another BSS entry holds a pointer to this ANQP info */
156 #ifdef CONFIG_INTERWORKING
157 wpabuf_free(anqp
->venue_name
);
158 wpabuf_free(anqp
->network_auth_type
);
159 wpabuf_free(anqp
->roaming_consortium
);
160 wpabuf_free(anqp
->ip_addr_type_availability
);
161 wpabuf_free(anqp
->nai_realm
);
162 wpabuf_free(anqp
->anqp_3gpp
);
163 wpabuf_free(anqp
->domain_name
);
164 #endif /* CONFIG_INTERWORKING */
166 wpabuf_free(anqp
->hs20_operator_friendly_name
);
167 wpabuf_free(anqp
->hs20_wan_metrics
);
168 wpabuf_free(anqp
->hs20_connection_capability
);
169 wpabuf_free(anqp
->hs20_operating_class
);
170 wpabuf_free(anqp
->hs20_osu_providers_list
);
171 #endif /* CONFIG_HS20 */
177 static void wpa_bss_update_pending_connect(struct wpa_supplicant
*wpa_s
,
178 struct wpa_bss
*old_bss
,
179 struct wpa_bss
*new_bss
)
181 struct wpa_radio_work
*work
;
182 struct wpa_connect_work
*cwork
;
184 work
= radio_work_pending(wpa_s
, "sme-connect");
186 work
= radio_work_pending(wpa_s
, "connect");
191 if (cwork
->bss
!= old_bss
)
194 wpa_printf(MSG_DEBUG
,
195 "Update BSS pointer for the pending connect radio work");
196 cwork
->bss
= new_bss
;
198 cwork
->bss_removed
= 1;
202 static void wpa_bss_remove(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
205 if (wpa_s
->last_scan_res
) {
207 for (i
= 0; i
< wpa_s
->last_scan_res_used
; i
++) {
208 if (wpa_s
->last_scan_res
[i
] == bss
) {
209 os_memmove(&wpa_s
->last_scan_res
[i
],
210 &wpa_s
->last_scan_res
[i
+ 1],
211 (wpa_s
->last_scan_res_used
- i
- 1)
212 * sizeof(struct wpa_bss
*));
213 wpa_s
->last_scan_res_used
--;
218 wpa_bss_update_pending_connect(wpa_s
, bss
, NULL
);
219 dl_list_del(&bss
->list
);
220 dl_list_del(&bss
->list_id
);
222 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Remove id %u BSSID " MACSTR
223 " SSID '%s' due to %s", bss
->id
, MAC2STR(bss
->bssid
),
224 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
), reason
);
225 wpas_notify_bss_removed(wpa_s
, bss
->bssid
, bss
->id
);
226 wpa_bss_anqp_free(bss
->anqp
);
232 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
233 * @wpa_s: Pointer to wpa_supplicant data
236 * @ssid_len: Length of @ssid
237 * Returns: Pointer to the BSS entry or %NULL if not found
239 struct wpa_bss
* wpa_bss_get(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
240 const u8
*ssid
, size_t ssid_len
)
243 if (!wpa_supplicant_filter_bssid_match(wpa_s
, bssid
))
245 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
246 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0 &&
247 bss
->ssid_len
== ssid_len
&&
248 os_memcmp(bss
->ssid
, ssid
, ssid_len
) == 0)
255 static void calculate_update_time(const struct os_reltime
*fetch_time
,
257 struct os_reltime
*update_time
)
261 update_time
->sec
= fetch_time
->sec
;
262 update_time
->usec
= fetch_time
->usec
;
263 update_time
->sec
-= age_ms
/ 1000;
264 usec
= (age_ms
% 1000) * 1000;
265 if (update_time
->usec
< usec
) {
267 update_time
->usec
+= 1000000;
269 update_time
->usec
-= usec
;
273 static void wpa_bss_copy_res(struct wpa_bss
*dst
, struct wpa_scan_res
*src
,
274 struct os_reltime
*fetch_time
)
276 dst
->flags
= src
->flags
;
277 os_memcpy(dst
->bssid
, src
->bssid
, ETH_ALEN
);
278 dst
->freq
= src
->freq
;
279 dst
->beacon_int
= src
->beacon_int
;
280 dst
->caps
= src
->caps
;
281 dst
->qual
= src
->qual
;
282 dst
->noise
= src
->noise
;
283 dst
->level
= src
->level
;
286 calculate_update_time(fetch_time
, src
->age
, &dst
->last_update
);
290 static int wpa_bss_known(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
292 struct wpa_ssid
*ssid
;
294 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
295 if (ssid
->ssid
== NULL
|| ssid
->ssid_len
== 0)
297 if (ssid
->ssid_len
== bss
->ssid_len
&&
298 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) == 0)
306 static int wpa_bss_in_use(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
308 return bss
== wpa_s
->current_bss
||
309 (!is_zero_ether_addr(bss
->bssid
) &&
310 (os_memcmp(bss
->bssid
, wpa_s
->bssid
, ETH_ALEN
) == 0 ||
311 os_memcmp(bss
->bssid
, wpa_s
->pending_bssid
, ETH_ALEN
) == 0));
315 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant
*wpa_s
)
319 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
320 if (!wpa_bss_known(wpa_s
, bss
)) {
321 wpa_bss_remove(wpa_s
, bss
, __func__
);
330 static int wpa_bss_remove_oldest(struct wpa_supplicant
*wpa_s
)
335 * Remove the oldest entry that does not match with any configured
338 if (wpa_bss_remove_oldest_unknown(wpa_s
) == 0)
342 * Remove the oldest entry that isn't currently in use.
344 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
345 if (!wpa_bss_in_use(wpa_s
, bss
)) {
346 wpa_bss_remove(wpa_s
, bss
, __func__
);
355 static struct wpa_bss
* wpa_bss_add(struct wpa_supplicant
*wpa_s
,
356 const u8
*ssid
, size_t ssid_len
,
357 struct wpa_scan_res
*res
,
358 struct os_reltime
*fetch_time
)
362 bss
= os_zalloc(sizeof(*bss
) + res
->ie_len
+ res
->beacon_ie_len
);
365 bss
->id
= wpa_s
->bss_next_id
++;
366 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
367 wpa_bss_copy_res(bss
, res
, fetch_time
);
368 os_memcpy(bss
->ssid
, ssid
, ssid_len
);
369 bss
->ssid_len
= ssid_len
;
370 bss
->ie_len
= res
->ie_len
;
371 bss
->beacon_ie_len
= res
->beacon_ie_len
;
372 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
373 wpa_bss_set_hessid(bss
);
375 if (wpa_s
->num_bss
+ 1 > wpa_s
->conf
->bss_max_count
&&
376 wpa_bss_remove_oldest(wpa_s
) != 0) {
377 wpa_printf(MSG_ERROR
, "Increasing the MAX BSS count to %d "
378 "because all BSSes are in use. We should normally "
379 "not get here!", (int) wpa_s
->num_bss
+ 1);
380 wpa_s
->conf
->bss_max_count
= wpa_s
->num_bss
+ 1;
383 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
384 dl_list_add_tail(&wpa_s
->bss_id
, &bss
->list_id
);
386 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Add new id %u BSSID " MACSTR
388 bss
->id
, MAC2STR(bss
->bssid
), wpa_ssid_txt(ssid
, ssid_len
));
389 wpas_notify_bss_added(wpa_s
, bss
->bssid
, bss
->id
);
394 static int are_ies_equal(const struct wpa_bss
*old
,
395 const struct wpa_scan_res
*new, u32 ie
)
397 const u8
*old_ie
, *new_ie
;
398 struct wpabuf
*old_ie_buff
= NULL
;
399 struct wpabuf
*new_ie_buff
= NULL
;
400 int new_ie_len
, old_ie_len
, ret
, is_multi
;
403 case WPA_IE_VENDOR_TYPE
:
404 old_ie
= wpa_bss_get_vendor_ie(old
, ie
);
405 new_ie
= wpa_scan_get_vendor_ie(new, ie
);
408 case WPS_IE_VENDOR_TYPE
:
409 old_ie_buff
= wpa_bss_get_vendor_ie_multi(old
, ie
);
410 new_ie_buff
= wpa_scan_get_vendor_ie_multi(new, ie
);
414 case WLAN_EID_SUPP_RATES
:
415 case WLAN_EID_EXT_SUPP_RATES
:
416 old_ie
= wpa_bss_get_ie(old
, ie
);
417 new_ie
= wpa_scan_get_ie(new, ie
);
421 wpa_printf(MSG_DEBUG
, "bss: %s: cannot compare IEs", __func__
);
426 /* in case of multiple IEs stored in buffer */
427 old_ie
= old_ie_buff
? wpabuf_head_u8(old_ie_buff
) : NULL
;
428 new_ie
= new_ie_buff
? wpabuf_head_u8(new_ie_buff
) : NULL
;
429 old_ie_len
= old_ie_buff
? wpabuf_len(old_ie_buff
) : 0;
430 new_ie_len
= new_ie_buff
? wpabuf_len(new_ie_buff
) : 0;
432 /* in case of single IE */
433 old_ie_len
= old_ie
? old_ie
[1] + 2 : 0;
434 new_ie_len
= new_ie
? new_ie
[1] + 2 : 0;
437 if (!old_ie
|| !new_ie
)
438 ret
= !old_ie
&& !new_ie
;
440 ret
= (old_ie_len
== new_ie_len
&&
441 os_memcmp(old_ie
, new_ie
, old_ie_len
) == 0);
443 wpabuf_free(old_ie_buff
);
444 wpabuf_free(new_ie_buff
);
450 static u32
wpa_bss_compare_res(const struct wpa_bss
*old
,
451 const struct wpa_scan_res
*new)
454 int caps_diff
= old
->caps
^ new->caps
;
456 if (old
->freq
!= new->freq
)
457 changes
|= WPA_BSS_FREQ_CHANGED_FLAG
;
459 if (old
->level
!= new->level
)
460 changes
|= WPA_BSS_SIGNAL_CHANGED_FLAG
;
462 if (caps_diff
& IEEE80211_CAP_PRIVACY
)
463 changes
|= WPA_BSS_PRIVACY_CHANGED_FLAG
;
465 if (caps_diff
& IEEE80211_CAP_IBSS
)
466 changes
|= WPA_BSS_MODE_CHANGED_FLAG
;
468 if (old
->ie_len
== new->ie_len
&&
469 os_memcmp(old
+ 1, new + 1, old
->ie_len
) == 0)
471 changes
|= WPA_BSS_IES_CHANGED_FLAG
;
473 if (!are_ies_equal(old
, new, WPA_IE_VENDOR_TYPE
))
474 changes
|= WPA_BSS_WPAIE_CHANGED_FLAG
;
476 if (!are_ies_equal(old
, new, WLAN_EID_RSN
))
477 changes
|= WPA_BSS_RSNIE_CHANGED_FLAG
;
479 if (!are_ies_equal(old
, new, WPS_IE_VENDOR_TYPE
))
480 changes
|= WPA_BSS_WPS_CHANGED_FLAG
;
482 if (!are_ies_equal(old
, new, WLAN_EID_SUPP_RATES
) ||
483 !are_ies_equal(old
, new, WLAN_EID_EXT_SUPP_RATES
))
484 changes
|= WPA_BSS_RATES_CHANGED_FLAG
;
490 static void notify_bss_changes(struct wpa_supplicant
*wpa_s
, u32 changes
,
491 const struct wpa_bss
*bss
)
493 if (changes
& WPA_BSS_FREQ_CHANGED_FLAG
)
494 wpas_notify_bss_freq_changed(wpa_s
, bss
->id
);
496 if (changes
& WPA_BSS_SIGNAL_CHANGED_FLAG
)
497 wpas_notify_bss_signal_changed(wpa_s
, bss
->id
);
499 if (changes
& WPA_BSS_PRIVACY_CHANGED_FLAG
)
500 wpas_notify_bss_privacy_changed(wpa_s
, bss
->id
);
502 if (changes
& WPA_BSS_MODE_CHANGED_FLAG
)
503 wpas_notify_bss_mode_changed(wpa_s
, bss
->id
);
505 if (changes
& WPA_BSS_WPAIE_CHANGED_FLAG
)
506 wpas_notify_bss_wpaie_changed(wpa_s
, bss
->id
);
508 if (changes
& WPA_BSS_RSNIE_CHANGED_FLAG
)
509 wpas_notify_bss_rsnie_changed(wpa_s
, bss
->id
);
511 if (changes
& WPA_BSS_WPS_CHANGED_FLAG
)
512 wpas_notify_bss_wps_changed(wpa_s
, bss
->id
);
514 if (changes
& WPA_BSS_IES_CHANGED_FLAG
)
515 wpas_notify_bss_ies_changed(wpa_s
, bss
->id
);
517 if (changes
& WPA_BSS_RATES_CHANGED_FLAG
)
518 wpas_notify_bss_rates_changed(wpa_s
, bss
->id
);
520 wpas_notify_bss_seen(wpa_s
, bss
->id
);
524 static struct wpa_bss
*
525 wpa_bss_update(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
526 struct wpa_scan_res
*res
, struct os_reltime
*fetch_time
)
530 changes
= wpa_bss_compare_res(bss
, res
);
531 bss
->scan_miss_count
= 0;
532 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
533 wpa_bss_copy_res(bss
, res
, fetch_time
);
534 /* Move the entry to the end of the list */
535 dl_list_del(&bss
->list
);
537 if (wpa_bss_get_vendor_ie(bss
, P2P_IE_VENDOR_TYPE
) &&
538 !wpa_scan_get_vendor_ie(res
, P2P_IE_VENDOR_TYPE
)) {
540 * This can happen when non-P2P station interface runs a scan
541 * without P2P IE in the Probe Request frame. P2P GO would reply
542 * to that with a Probe Response that does not include P2P IE.
543 * Do not update the IEs in this BSS entry to avoid such loss of
544 * information that may be needed for P2P operations to
545 * determine group information.
547 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Do not update scan IEs for "
548 MACSTR
" since that would remove P2P IE information",
549 MAC2STR(bss
->bssid
));
551 #endif /* CONFIG_P2P */
552 if (bss
->ie_len
+ bss
->beacon_ie_len
>=
553 res
->ie_len
+ res
->beacon_ie_len
) {
554 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
555 bss
->ie_len
= res
->ie_len
;
556 bss
->beacon_ie_len
= res
->beacon_ie_len
;
558 struct wpa_bss
*nbss
;
559 struct dl_list
*prev
= bss
->list_id
.prev
;
560 dl_list_del(&bss
->list_id
);
561 nbss
= os_realloc(bss
, sizeof(*bss
) + res
->ie_len
+
565 for (i
= 0; i
< wpa_s
->last_scan_res_used
; i
++) {
566 if (wpa_s
->last_scan_res
[i
] == bss
) {
567 wpa_s
->last_scan_res
[i
] = nbss
;
571 if (wpa_s
->current_bss
== bss
)
572 wpa_s
->current_bss
= nbss
;
573 wpa_bss_update_pending_connect(wpa_s
, bss
, nbss
);
575 os_memcpy(bss
+ 1, res
+ 1,
576 res
->ie_len
+ res
->beacon_ie_len
);
577 bss
->ie_len
= res
->ie_len
;
578 bss
->beacon_ie_len
= res
->beacon_ie_len
;
580 dl_list_add(prev
, &bss
->list_id
);
582 if (changes
& WPA_BSS_IES_CHANGED_FLAG
)
583 wpa_bss_set_hessid(bss
);
584 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
586 notify_bss_changes(wpa_s
, changes
, bss
);
593 * wpa_bss_update_start - Start a BSS table update from scan results
594 * @wpa_s: Pointer to wpa_supplicant data
596 * This function is called at the start of each BSS table update round for new
597 * scan results. The actual scan result entries are indicated with calls to
598 * wpa_bss_update_scan_res() and the update round is finished with a call to
599 * wpa_bss_update_end().
601 void wpa_bss_update_start(struct wpa_supplicant
*wpa_s
)
603 wpa_s
->bss_update_idx
++;
604 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Start scan result update %u",
605 wpa_s
->bss_update_idx
);
606 wpa_s
->last_scan_res_used
= 0;
611 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
612 * @wpa_s: Pointer to wpa_supplicant data
614 * @fetch_time: Time when the result was fetched from the driver
616 * This function updates a BSS table entry (or adds one) based on a scan result.
617 * This is called separately for each scan result between the calls to
618 * wpa_bss_update_start() and wpa_bss_update_end().
620 void wpa_bss_update_scan_res(struct wpa_supplicant
*wpa_s
,
621 struct wpa_scan_res
*res
,
622 struct os_reltime
*fetch_time
)
624 const u8
*ssid
, *p2p
, *mesh
;
627 if (wpa_s
->conf
->ignore_old_scan_res
) {
628 struct os_reltime update
;
629 calculate_update_time(fetch_time
, res
->age
, &update
);
630 if (os_reltime_before(&update
, &wpa_s
->scan_trigger_time
)) {
631 struct os_reltime age
;
632 os_reltime_sub(&wpa_s
->scan_trigger_time
, &update
,
634 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Ignore driver BSS "
635 "table entry that is %u.%06u seconds older "
636 "than our scan trigger",
637 (unsigned int) age
.sec
,
638 (unsigned int) age
.usec
);
643 ssid
= wpa_scan_get_ie(res
, WLAN_EID_SSID
);
645 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: No SSID IE included for "
646 MACSTR
, MAC2STR(res
->bssid
));
650 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Too long SSID IE included for "
651 MACSTR
, MAC2STR(res
->bssid
));
655 p2p
= wpa_scan_get_vendor_ie(res
, P2P_IE_VENDOR_TYPE
);
658 wpa_s
->p2p_group_interface
!= NOT_P2P_GROUP_INTERFACE
) {
660 * If it's a P2P specific interface, then don't update
661 * the scan result without a P2P IE.
663 wpa_printf(MSG_DEBUG
, "BSS: No P2P IE - skipping BSS " MACSTR
664 " update for P2P interface", MAC2STR(res
->bssid
));
667 #endif /* CONFIG_P2P */
668 if (p2p
&& ssid
[1] == P2P_WILDCARD_SSID_LEN
&&
669 os_memcmp(ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
) == 0)
670 return; /* Skip P2P listen discovery results here */
672 /* TODO: add option for ignoring BSSes we are not interested in
673 * (to save memory) */
675 mesh
= wpa_scan_get_ie(res
, WLAN_EID_MESH_ID
);
676 if (mesh
&& mesh
[1] <= 32)
679 bss
= wpa_bss_get(wpa_s
, res
->bssid
, ssid
+ 2, ssid
[1]);
681 bss
= wpa_bss_add(wpa_s
, ssid
+ 2, ssid
[1], res
, fetch_time
);
683 bss
= wpa_bss_update(wpa_s
, bss
, res
, fetch_time
);
684 if (wpa_s
->last_scan_res
) {
686 for (i
= 0; i
< wpa_s
->last_scan_res_used
; i
++) {
687 if (bss
== wpa_s
->last_scan_res
[i
]) {
688 /* Already in the list */
697 if (wpa_s
->last_scan_res_used
>= wpa_s
->last_scan_res_size
) {
700 if (wpa_s
->last_scan_res_size
== 0)
703 siz
= wpa_s
->last_scan_res_size
* 2;
704 n
= os_realloc_array(wpa_s
->last_scan_res
, siz
,
705 sizeof(struct wpa_bss
*));
708 wpa_s
->last_scan_res
= n
;
709 wpa_s
->last_scan_res_size
= siz
;
712 if (wpa_s
->last_scan_res
)
713 wpa_s
->last_scan_res
[wpa_s
->last_scan_res_used
++] = bss
;
717 static int wpa_bss_included_in_scan(const struct wpa_bss
*bss
,
718 const struct scan_info
*info
)
726 if (info
->num_freqs
) {
728 for (i
= 0; i
< info
->num_freqs
; i
++) {
729 if (bss
->freq
== info
->freqs
[i
]) {
738 if (info
->num_ssids
) {
740 for (i
= 0; i
< info
->num_ssids
; i
++) {
741 const struct wpa_driver_scan_ssid
*s
= &info
->ssids
[i
];
742 if ((s
->ssid
== NULL
|| s
->ssid_len
== 0) ||
743 (s
->ssid_len
== bss
->ssid_len
&&
744 os_memcmp(s
->ssid
, bss
->ssid
, bss
->ssid_len
) ==
759 * wpa_bss_update_end - End a BSS table update from scan results
760 * @wpa_s: Pointer to wpa_supplicant data
761 * @info: Information about scan parameters
762 * @new_scan: Whether this update round was based on a new scan
764 * This function is called at the end of each BSS table update round for new
765 * scan results. The start of the update was indicated with a call to
766 * wpa_bss_update_start().
768 void wpa_bss_update_end(struct wpa_supplicant
*wpa_s
, struct scan_info
*info
,
771 struct wpa_bss
*bss
, *n
;
773 os_get_reltime(&wpa_s
->last_scan
);
775 return; /* do not expire entries without new scan */
777 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
778 if (wpa_bss_in_use(wpa_s
, bss
))
780 if (!wpa_bss_included_in_scan(bss
, info
))
781 continue; /* expire only BSSes that were scanned */
782 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
)
783 bss
->scan_miss_count
++;
784 if (bss
->scan_miss_count
>=
785 wpa_s
->conf
->bss_expiration_scan_count
) {
786 wpa_bss_remove(wpa_s
, bss
, "no match in scan");
790 wpa_printf(MSG_DEBUG
, "BSS: last_scan_res_used=%u/%u",
791 wpa_s
->last_scan_res_used
, wpa_s
->last_scan_res_size
);
796 * wpa_bss_flush_by_age - Flush old BSS entries
797 * @wpa_s: Pointer to wpa_supplicant data
798 * @age: Maximum entry age in seconds
800 * Remove BSS entries that have not been updated during the last @age seconds.
802 void wpa_bss_flush_by_age(struct wpa_supplicant
*wpa_s
, int age
)
804 struct wpa_bss
*bss
, *n
;
807 if (dl_list_empty(&wpa_s
->bss
))
813 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
814 if (wpa_bss_in_use(wpa_s
, bss
))
817 if (os_reltime_before(&bss
->last_update
, &t
)) {
818 wpa_bss_remove(wpa_s
, bss
, __func__
);
825 static void wpa_bss_timeout(void *eloop_ctx
, void *timeout_ctx
)
827 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
829 wpa_bss_flush_by_age(wpa_s
, wpa_s
->conf
->bss_expiration_age
);
830 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
831 wpa_bss_timeout
, wpa_s
, NULL
);
836 * wpa_bss_init - Initialize BSS table
837 * @wpa_s: Pointer to wpa_supplicant data
838 * Returns: 0 on success, -1 on failure
840 * This prepares BSS table lists and timer for periodic updates. The BSS table
841 * is deinitialized with wpa_bss_deinit() once not needed anymore.
843 int wpa_bss_init(struct wpa_supplicant
*wpa_s
)
845 dl_list_init(&wpa_s
->bss
);
846 dl_list_init(&wpa_s
->bss_id
);
847 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
848 wpa_bss_timeout
, wpa_s
, NULL
);
854 * wpa_bss_flush - Flush all unused BSS entries
855 * @wpa_s: Pointer to wpa_supplicant data
857 void wpa_bss_flush(struct wpa_supplicant
*wpa_s
)
859 struct wpa_bss
*bss
, *n
;
861 wpa_s
->clear_driver_scan_cache
= 1;
863 if (wpa_s
->bss
.next
== NULL
)
864 return; /* BSS table not yet initialized */
866 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
867 if (wpa_bss_in_use(wpa_s
, bss
))
869 wpa_bss_remove(wpa_s
, bss
, __func__
);
875 * wpa_bss_deinit - Deinitialize BSS table
876 * @wpa_s: Pointer to wpa_supplicant data
878 void wpa_bss_deinit(struct wpa_supplicant
*wpa_s
)
880 eloop_cancel_timeout(wpa_bss_timeout
, wpa_s
, NULL
);
881 wpa_bss_flush(wpa_s
);
886 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
887 * @wpa_s: Pointer to wpa_supplicant data
889 * Returns: Pointer to the BSS entry or %NULL if not found
891 struct wpa_bss
* wpa_bss_get_bssid(struct wpa_supplicant
*wpa_s
,
895 if (!wpa_supplicant_filter_bssid_match(wpa_s
, bssid
))
897 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
898 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0)
906 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
907 * @wpa_s: Pointer to wpa_supplicant data
909 * Returns: Pointer to the BSS entry or %NULL if not found
911 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
912 * find the entry that has the most recent update. This can help in finding the
913 * correct entry in cases where the SSID of the AP may have changed recently
914 * (e.g., in WPS reconfiguration cases).
916 struct wpa_bss
* wpa_bss_get_bssid_latest(struct wpa_supplicant
*wpa_s
,
919 struct wpa_bss
*bss
, *found
= NULL
;
920 if (!wpa_supplicant_filter_bssid_match(wpa_s
, bssid
))
922 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
923 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) != 0)
926 os_reltime_before(&found
->last_update
, &bss
->last_update
))
935 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
936 * @wpa_s: Pointer to wpa_supplicant data
937 * @dev_addr: P2P Device Address of the GO
938 * Returns: Pointer to the BSS entry or %NULL if not found
940 struct wpa_bss
* wpa_bss_get_p2p_dev_addr(struct wpa_supplicant
*wpa_s
,
944 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
946 if (p2p_parse_dev_addr((const u8
*) (bss
+ 1), bss
->ie_len
,
948 os_memcmp(addr
, dev_addr
, ETH_ALEN
) == 0)
953 #endif /* CONFIG_P2P */
957 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
958 * @wpa_s: Pointer to wpa_supplicant data
959 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
960 * Returns: Pointer to the BSS entry or %NULL if not found
962 struct wpa_bss
* wpa_bss_get_id(struct wpa_supplicant
*wpa_s
, unsigned int id
)
965 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
974 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
975 * @wpa_s: Pointer to wpa_supplicant data
976 * @idf: Smallest allowed identifier assigned for the entry
977 * @idf: Largest allowed identifier assigned for the entry
978 * Returns: Pointer to the BSS entry or %NULL if not found
980 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
981 * smallest id value to be fetched within the specified range without the
982 * caller having to know the exact id.
984 struct wpa_bss
* wpa_bss_get_id_range(struct wpa_supplicant
*wpa_s
,
985 unsigned int idf
, unsigned int idl
)
988 dl_list_for_each(bss
, &wpa_s
->bss_id
, struct wpa_bss
, list_id
) {
989 if (bss
->id
>= idf
&& bss
->id
<= idl
)
997 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
998 * @bss: BSS table entry
999 * @ie: Information element identitifier (WLAN_EID_*)
1000 * Returns: Pointer to the information element (id field) or %NULL if not found
1002 * This function returns the first matching information element in the BSS
1005 const u8
* wpa_bss_get_ie(const struct wpa_bss
*bss
, u8 ie
)
1007 const u8
*end
, *pos
;
1009 pos
= (const u8
*) (bss
+ 1);
1010 end
= pos
+ bss
->ie_len
;
1012 while (pos
+ 1 < end
) {
1013 if (pos
+ 2 + pos
[1] > end
)
1025 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1026 * @bss: BSS table entry
1027 * @vendor_type: Vendor type (four octets starting the IE payload)
1028 * Returns: Pointer to the information element (id field) or %NULL if not found
1030 * This function returns the first matching information element in the BSS
1033 const u8
* wpa_bss_get_vendor_ie(const struct wpa_bss
*bss
, u32 vendor_type
)
1035 const u8
*end
, *pos
;
1037 pos
= (const u8
*) (bss
+ 1);
1038 end
= pos
+ bss
->ie_len
;
1040 while (pos
+ 1 < end
) {
1041 if (pos
+ 2 + pos
[1] > end
)
1043 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1044 vendor_type
== WPA_GET_BE32(&pos
[2]))
1054 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1055 * @bss: BSS table entry
1056 * @vendor_type: Vendor type (four octets starting the IE payload)
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 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1063 * from Beacon frames instead of either Beacon or Probe Response frames.
1065 const u8
* wpa_bss_get_vendor_ie_beacon(const struct wpa_bss
*bss
,
1068 const u8
*end
, *pos
;
1070 if (bss
->beacon_ie_len
== 0)
1073 pos
= (const u8
*) (bss
+ 1);
1075 end
= pos
+ bss
->beacon_ie_len
;
1077 while (pos
+ 1 < end
) {
1078 if (pos
+ 2 + pos
[1] > end
)
1080 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1081 vendor_type
== WPA_GET_BE32(&pos
[2]))
1091 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1092 * @bss: BSS table entry
1093 * @vendor_type: Vendor type (four octets starting the IE payload)
1094 * Returns: Pointer to the information element payload or %NULL if not found
1096 * This function returns concatenated payload of possibly fragmented vendor
1097 * specific information elements in the BSS entry. The caller is responsible for
1098 * freeing the returned buffer.
1100 struct wpabuf
* wpa_bss_get_vendor_ie_multi(const struct wpa_bss
*bss
,
1104 const u8
*end
, *pos
;
1106 buf
= wpabuf_alloc(bss
->ie_len
);
1110 pos
= (const u8
*) (bss
+ 1);
1111 end
= pos
+ bss
->ie_len
;
1113 while (pos
+ 1 < end
) {
1114 if (pos
+ 2 + pos
[1] > end
)
1116 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1117 vendor_type
== WPA_GET_BE32(&pos
[2]))
1118 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1122 if (wpabuf_len(buf
) == 0) {
1132 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1133 * @bss: BSS table entry
1134 * @vendor_type: Vendor type (four octets starting the IE payload)
1135 * Returns: Pointer to the information element payload or %NULL if not found
1137 * This function returns concatenated payload of possibly fragmented vendor
1138 * specific information elements in the BSS entry. The caller is responsible for
1139 * freeing the returned buffer.
1141 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1142 * from Beacon frames instead of either Beacon or Probe Response frames.
1144 struct wpabuf
* wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss
*bss
,
1148 const u8
*end
, *pos
;
1150 buf
= wpabuf_alloc(bss
->beacon_ie_len
);
1154 pos
= (const u8
*) (bss
+ 1);
1156 end
= pos
+ bss
->beacon_ie_len
;
1158 while (pos
+ 1 < end
) {
1159 if (pos
+ 2 + pos
[1] > end
)
1161 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
1162 vendor_type
== WPA_GET_BE32(&pos
[2]))
1163 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
1167 if (wpabuf_len(buf
) == 0) {
1177 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1178 * @bss: BSS table entry
1179 * Returns: Maximum legacy rate in units of 500 kbps
1181 int wpa_bss_get_max_rate(const struct wpa_bss
*bss
)
1187 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
1188 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1189 if ((ie
[i
+ 2] & 0x7f) > rate
)
1190 rate
= ie
[i
+ 2] & 0x7f;
1193 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
1194 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
1195 if ((ie
[i
+ 2] & 0x7f) > rate
)
1196 rate
= ie
[i
+ 2] & 0x7f;
1204 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1205 * @bss: BSS table entry
1206 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1207 * Returns: number of legacy TX rates or -1 on failure
1209 * The caller is responsible for freeing the returned buffer with os_free() in
1212 int wpa_bss_get_bit_rates(const struct wpa_bss
*bss
, u8
**rates
)
1219 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
1220 ie2
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
1222 len
= (ie
? ie
[1] : 0) + (ie2
? ie2
[1] : 0);
1228 for (i
= 0; ie
&& i
< ie
[1]; i
++)
1229 r
[i
] = ie
[i
+ 2] & 0x7f;
1231 for (j
= 0; ie2
&& j
< ie2
[1]; j
++)
1232 r
[i
+ j
] = ie2
[j
+ 2] & 0x7f;