3 * Copyright (c) 2009-2010, 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_remove(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
40 dl_list_del(&bss
->list
);
41 dl_list_del(&bss
->list_id
);
43 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Remove id %u BSSID " MACSTR
44 " SSID '%s'", bss
->id
, MAC2STR(bss
->bssid
),
45 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
46 wpas_notify_bss_removed(wpa_s
, bss
->bssid
, bss
->id
);
47 #ifdef CONFIG_INTERWORKING
48 wpabuf_free(bss
->anqp_venue_name
);
49 wpabuf_free(bss
->anqp_network_auth_type
);
50 wpabuf_free(bss
->anqp_roaming_consortium
);
51 wpabuf_free(bss
->anqp_ip_addr_type_availability
);
52 wpabuf_free(bss
->anqp_nai_realm
);
53 wpabuf_free(bss
->anqp_3gpp
);
54 wpabuf_free(bss
->anqp_domain_name
);
55 #endif /* CONFIG_INTERWORKING */
60 struct wpa_bss
* wpa_bss_get(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
61 const u8
*ssid
, size_t ssid_len
)
64 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
65 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0 &&
66 bss
->ssid_len
== ssid_len
&&
67 os_memcmp(bss
->ssid
, ssid
, ssid_len
) == 0)
74 static void wpa_bss_copy_res(struct wpa_bss
*dst
, struct wpa_scan_res
*src
)
78 dst
->flags
= src
->flags
;
79 os_memcpy(dst
->bssid
, src
->bssid
, ETH_ALEN
);
80 dst
->freq
= src
->freq
;
81 dst
->beacon_int
= src
->beacon_int
;
82 dst
->caps
= src
->caps
;
83 dst
->qual
= src
->qual
;
84 dst
->noise
= src
->noise
;
85 dst
->level
= src
->level
;
88 os_get_time(&dst
->last_update
);
89 dst
->last_update
.sec
-= src
->age
/ 1000;
90 usec
= (src
->age
% 1000) * 1000;
91 if (dst
->last_update
.usec
< usec
) {
92 dst
->last_update
.sec
--;
93 dst
->last_update
.usec
+= 1000000;
95 dst
->last_update
.usec
-= usec
;
99 static int wpa_bss_known(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
101 struct wpa_ssid
*ssid
;
103 for (ssid
= wpa_s
->conf
->ssid
; ssid
; ssid
= ssid
->next
) {
104 if (ssid
->ssid
== NULL
|| ssid
->ssid_len
== 0)
106 if (ssid
->ssid_len
== bss
->ssid_len
&&
107 os_memcmp(ssid
->ssid
, bss
->ssid
, ssid
->ssid_len
) == 0)
115 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant
*wpa_s
)
119 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
120 if (!wpa_bss_known(wpa_s
, bss
)) {
121 wpa_bss_remove(wpa_s
, bss
);
130 static void wpa_bss_remove_oldest(struct wpa_supplicant
*wpa_s
)
133 * Remove the oldest entry that does not match with any configured
136 if (wpa_bss_remove_oldest_unknown(wpa_s
) == 0)
140 * Remove the oldest entry since no better candidate for removal was
143 wpa_bss_remove(wpa_s
, dl_list_first(&wpa_s
->bss
,
144 struct wpa_bss
, list
));
148 static void wpa_bss_add(struct wpa_supplicant
*wpa_s
,
149 const u8
*ssid
, size_t ssid_len
,
150 struct wpa_scan_res
*res
)
154 bss
= os_zalloc(sizeof(*bss
) + res
->ie_len
+ res
->beacon_ie_len
);
157 bss
->id
= wpa_s
->bss_next_id
++;
158 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
159 wpa_bss_copy_res(bss
, res
);
160 os_memcpy(bss
->ssid
, ssid
, ssid_len
);
161 bss
->ssid_len
= ssid_len
;
162 bss
->ie_len
= res
->ie_len
;
163 bss
->beacon_ie_len
= res
->beacon_ie_len
;
164 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
166 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
167 dl_list_add_tail(&wpa_s
->bss_id
, &bss
->list_id
);
169 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Add new id %u BSSID " MACSTR
171 bss
->id
, MAC2STR(bss
->bssid
), wpa_ssid_txt(ssid
, ssid_len
));
172 wpas_notify_bss_added(wpa_s
, bss
->bssid
, bss
->id
);
173 if (wpa_s
->num_bss
> wpa_s
->conf
->bss_max_count
)
174 wpa_bss_remove_oldest(wpa_s
);
178 static int are_ies_equal(const struct wpa_bss
*old
,
179 const struct wpa_scan_res
*new, u32 ie
)
181 const u8
*old_ie
, *new_ie
;
182 struct wpabuf
*old_ie_buff
= NULL
;
183 struct wpabuf
*new_ie_buff
= NULL
;
184 int new_ie_len
, old_ie_len
, ret
, is_multi
;
187 case WPA_IE_VENDOR_TYPE
:
188 old_ie
= wpa_bss_get_vendor_ie(old
, ie
);
189 new_ie
= wpa_scan_get_vendor_ie(new, ie
);
192 case WPS_IE_VENDOR_TYPE
:
193 old_ie_buff
= wpa_bss_get_vendor_ie_multi(old
, ie
);
194 new_ie_buff
= wpa_scan_get_vendor_ie_multi(new, ie
);
198 case WLAN_EID_SUPP_RATES
:
199 case WLAN_EID_EXT_SUPP_RATES
:
200 old_ie
= wpa_bss_get_ie(old
, ie
);
201 new_ie
= wpa_scan_get_ie(new, ie
);
205 wpa_printf(MSG_DEBUG
, "bss: %s: cannot compare IEs", __func__
);
210 /* in case of multiple IEs stored in buffer */
211 old_ie
= old_ie_buff
? wpabuf_head_u8(old_ie_buff
) : NULL
;
212 new_ie
= new_ie_buff
? wpabuf_head_u8(new_ie_buff
) : NULL
;
213 old_ie_len
= old_ie_buff
? wpabuf_len(old_ie_buff
) : 0;
214 new_ie_len
= new_ie_buff
? wpabuf_len(new_ie_buff
) : 0;
216 /* in case of single IE */
217 old_ie_len
= old_ie
? old_ie
[1] + 2 : 0;
218 new_ie_len
= new_ie
? new_ie
[1] + 2 : 0;
221 if (!old_ie
|| !new_ie
)
222 ret
= !old_ie
&& !new_ie
;
224 ret
= (old_ie_len
== new_ie_len
&&
225 os_memcmp(old_ie
, new_ie
, old_ie_len
) == 0);
227 wpabuf_free(old_ie_buff
);
228 wpabuf_free(new_ie_buff
);
234 static u32
wpa_bss_compare_res(const struct wpa_bss
*old
,
235 const struct wpa_scan_res
*new)
238 int caps_diff
= old
->caps
^ new->caps
;
240 if (old
->freq
!= new->freq
)
241 changes
|= WPA_BSS_FREQ_CHANGED_FLAG
;
243 if (old
->level
!= new->level
)
244 changes
|= WPA_BSS_SIGNAL_CHANGED_FLAG
;
246 if (caps_diff
& IEEE80211_CAP_PRIVACY
)
247 changes
|= WPA_BSS_PRIVACY_CHANGED_FLAG
;
249 if (caps_diff
& IEEE80211_CAP_IBSS
)
250 changes
|= WPA_BSS_MODE_CHANGED_FLAG
;
252 if (old
->ie_len
== new->ie_len
&&
253 os_memcmp(old
+ 1, new + 1, old
->ie_len
) == 0)
255 changes
|= WPA_BSS_IES_CHANGED_FLAG
;
257 if (!are_ies_equal(old
, new, WPA_IE_VENDOR_TYPE
))
258 changes
|= WPA_BSS_WPAIE_CHANGED_FLAG
;
260 if (!are_ies_equal(old
, new, WLAN_EID_RSN
))
261 changes
|= WPA_BSS_RSNIE_CHANGED_FLAG
;
263 if (!are_ies_equal(old
, new, WPS_IE_VENDOR_TYPE
))
264 changes
|= WPA_BSS_WPS_CHANGED_FLAG
;
266 if (!are_ies_equal(old
, new, WLAN_EID_SUPP_RATES
) ||
267 !are_ies_equal(old
, new, WLAN_EID_EXT_SUPP_RATES
))
268 changes
|= WPA_BSS_RATES_CHANGED_FLAG
;
274 static void notify_bss_changes(struct wpa_supplicant
*wpa_s
, u32 changes
,
275 const struct wpa_bss
*bss
)
277 if (changes
& WPA_BSS_FREQ_CHANGED_FLAG
)
278 wpas_notify_bss_freq_changed(wpa_s
, bss
->id
);
280 if (changes
& WPA_BSS_SIGNAL_CHANGED_FLAG
)
281 wpas_notify_bss_signal_changed(wpa_s
, bss
->id
);
283 if (changes
& WPA_BSS_PRIVACY_CHANGED_FLAG
)
284 wpas_notify_bss_privacy_changed(wpa_s
, bss
->id
);
286 if (changes
& WPA_BSS_MODE_CHANGED_FLAG
)
287 wpas_notify_bss_mode_changed(wpa_s
, bss
->id
);
289 if (changes
& WPA_BSS_WPAIE_CHANGED_FLAG
)
290 wpas_notify_bss_wpaie_changed(wpa_s
, bss
->id
);
292 if (changes
& WPA_BSS_RSNIE_CHANGED_FLAG
)
293 wpas_notify_bss_rsnie_changed(wpa_s
, bss
->id
);
295 if (changes
& WPA_BSS_WPS_CHANGED_FLAG
)
296 wpas_notify_bss_wps_changed(wpa_s
, bss
->id
);
298 if (changes
& WPA_BSS_IES_CHANGED_FLAG
)
299 wpas_notify_bss_ies_changed(wpa_s
, bss
->id
);
301 if (changes
& WPA_BSS_RATES_CHANGED_FLAG
)
302 wpas_notify_bss_rates_changed(wpa_s
, bss
->id
);
306 static void wpa_bss_update(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
307 struct wpa_scan_res
*res
)
311 changes
= wpa_bss_compare_res(bss
, res
);
312 bss
->scan_miss_count
= 0;
313 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
314 wpa_bss_copy_res(bss
, res
);
315 /* Move the entry to the end of the list */
316 dl_list_del(&bss
->list
);
317 if (bss
->ie_len
+ bss
->beacon_ie_len
>=
318 res
->ie_len
+ res
->beacon_ie_len
) {
319 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
320 bss
->ie_len
= res
->ie_len
;
321 bss
->beacon_ie_len
= res
->beacon_ie_len
;
323 struct wpa_bss
*nbss
;
324 struct dl_list
*prev
= bss
->list_id
.prev
;
325 dl_list_del(&bss
->list_id
);
326 nbss
= os_realloc(bss
, sizeof(*bss
) + res
->ie_len
+
330 os_memcpy(bss
+ 1, res
+ 1,
331 res
->ie_len
+ res
->beacon_ie_len
);
332 bss
->ie_len
= res
->ie_len
;
333 bss
->beacon_ie_len
= res
->beacon_ie_len
;
335 dl_list_add(prev
, &bss
->list_id
);
337 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
339 notify_bss_changes(wpa_s
, changes
, bss
);
343 static int wpa_bss_in_use(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
345 return bss
== wpa_s
->current_bss
||
346 os_memcmp(bss
->bssid
, wpa_s
->bssid
, ETH_ALEN
) == 0 ||
347 os_memcmp(bss
->bssid
, wpa_s
->pending_bssid
, ETH_ALEN
) == 0;
351 void wpa_bss_update_start(struct wpa_supplicant
*wpa_s
)
353 wpa_s
->bss_update_idx
++;
354 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Start scan result update %u",
355 wpa_s
->bss_update_idx
);
359 void wpa_bss_update_scan_res(struct wpa_supplicant
*wpa_s
,
360 struct wpa_scan_res
*res
)
362 const u8
*ssid
, *p2p
;
365 ssid
= wpa_scan_get_ie(res
, WLAN_EID_SSID
);
367 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: No SSID IE included for "
368 MACSTR
, MAC2STR(res
->bssid
));
372 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Too long SSID IE included for "
373 MACSTR
, MAC2STR(res
->bssid
));
377 p2p
= wpa_scan_get_vendor_ie(res
, P2P_IE_VENDOR_TYPE
);
378 if (p2p
&& ssid
[1] == P2P_WILDCARD_SSID_LEN
&&
379 os_memcmp(ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
) == 0)
380 return; /* Skip P2P listen discovery results here */
382 /* TODO: add option for ignoring BSSes we are not interested in
383 * (to save memory) */
384 bss
= wpa_bss_get(wpa_s
, res
->bssid
, ssid
+ 2, ssid
[1]);
386 wpa_bss_add(wpa_s
, ssid
+ 2, ssid
[1], res
);
388 wpa_bss_update(wpa_s
, bss
, res
);
392 static int wpa_bss_included_in_scan(const struct wpa_bss
*bss
,
393 const struct scan_info
*info
)
401 if (info
->num_freqs
) {
403 for (i
= 0; i
< info
->num_freqs
; i
++) {
404 if (bss
->freq
== info
->freqs
[i
]) {
413 if (info
->num_ssids
) {
415 for (i
= 0; i
< info
->num_ssids
; i
++) {
416 const struct wpa_driver_scan_ssid
*s
= &info
->ssids
[i
];
417 if ((s
->ssid
== NULL
|| s
->ssid_len
== 0) ||
418 (s
->ssid_len
== bss
->ssid_len
&&
419 os_memcmp(s
->ssid
, bss
->ssid
, bss
->ssid_len
) ==
433 void wpa_bss_update_end(struct wpa_supplicant
*wpa_s
, struct scan_info
*info
,
436 struct wpa_bss
*bss
, *n
;
439 return; /* do not expire entries without new scan */
441 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
442 if (wpa_bss_in_use(wpa_s
, bss
))
444 if (!wpa_bss_included_in_scan(bss
, info
))
445 continue; /* expire only BSSes that were scanned */
446 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
)
447 bss
->scan_miss_count
++;
448 if (bss
->scan_miss_count
>=
449 wpa_s
->conf
->bss_expiration_scan_count
) {
450 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Expire BSS %u due to "
451 "no match in scan", bss
->id
);
452 wpa_bss_remove(wpa_s
, bss
);
458 void wpa_bss_flush_by_age(struct wpa_supplicant
*wpa_s
, int age
)
460 struct wpa_bss
*bss
, *n
;
463 if (dl_list_empty(&wpa_s
->bss
))
469 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
470 if (wpa_bss_in_use(wpa_s
, bss
))
473 if (os_time_before(&bss
->last_update
, &t
)) {
474 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Expire BSS %u due to "
476 wpa_bss_remove(wpa_s
, bss
);
483 static void wpa_bss_timeout(void *eloop_ctx
, void *timeout_ctx
)
485 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
487 wpa_bss_flush_by_age(wpa_s
, wpa_s
->conf
->bss_expiration_age
);
488 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
489 wpa_bss_timeout
, wpa_s
, NULL
);
493 int wpa_bss_init(struct wpa_supplicant
*wpa_s
)
495 dl_list_init(&wpa_s
->bss
);
496 dl_list_init(&wpa_s
->bss_id
);
497 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
498 wpa_bss_timeout
, wpa_s
, NULL
);
503 void wpa_bss_flush(struct wpa_supplicant
*wpa_s
)
505 struct wpa_bss
*bss
, *n
;
507 if (wpa_s
->bss
.next
== NULL
)
508 return; /* BSS table not yet initialized */
510 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
511 if (wpa_bss_in_use(wpa_s
, bss
))
513 wpa_bss_remove(wpa_s
, bss
);
518 void wpa_bss_deinit(struct wpa_supplicant
*wpa_s
)
520 eloop_cancel_timeout(wpa_bss_timeout
, wpa_s
, NULL
);
521 wpa_bss_flush(wpa_s
);
525 struct wpa_bss
* wpa_bss_get_bssid(struct wpa_supplicant
*wpa_s
,
529 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
530 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0)
538 struct wpa_bss
* wpa_bss_get_p2p_dev_addr(struct wpa_supplicant
*wpa_s
,
542 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
544 if (p2p_parse_dev_addr((const u8
*) (bss
+ 1), bss
->ie_len
,
546 os_memcmp(addr
, dev_addr
, ETH_ALEN
) == 0)
551 #endif /* CONFIG_P2P */
554 struct wpa_bss
* wpa_bss_get_id(struct wpa_supplicant
*wpa_s
, unsigned int id
)
557 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
565 const u8
* wpa_bss_get_ie(const struct wpa_bss
*bss
, u8 ie
)
569 pos
= (const u8
*) (bss
+ 1);
570 end
= pos
+ bss
->ie_len
;
572 while (pos
+ 1 < end
) {
573 if (pos
+ 2 + pos
[1] > end
)
584 const u8
* wpa_bss_get_vendor_ie(const struct wpa_bss
*bss
, u32 vendor_type
)
588 pos
= (const u8
*) (bss
+ 1);
589 end
= pos
+ bss
->ie_len
;
591 while (pos
+ 1 < end
) {
592 if (pos
+ 2 + pos
[1] > end
)
594 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
595 vendor_type
== WPA_GET_BE32(&pos
[2]))
604 struct wpabuf
* wpa_bss_get_vendor_ie_multi(const struct wpa_bss
*bss
,
610 buf
= wpabuf_alloc(bss
->ie_len
);
614 pos
= (const u8
*) (bss
+ 1);
615 end
= pos
+ bss
->ie_len
;
617 while (pos
+ 1 < end
) {
618 if (pos
+ 2 + pos
[1] > end
)
620 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
621 vendor_type
== WPA_GET_BE32(&pos
[2]))
622 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
626 if (wpabuf_len(buf
) == 0) {
635 int wpa_bss_get_max_rate(const struct wpa_bss
*bss
)
641 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
642 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
643 if ((ie
[i
+ 2] & 0x7f) > rate
)
644 rate
= ie
[i
+ 2] & 0x7f;
647 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
648 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
649 if ((ie
[i
+ 2] & 0x7f) > rate
)
650 rate
= ie
[i
+ 2] & 0x7f;
657 int wpa_bss_get_bit_rates(const struct wpa_bss
*bss
, u8
**rates
)
664 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
665 ie2
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
667 len
= (ie
? ie
[1] : 0) + (ie2
? ie2
[1] : 0);
673 for (i
= 0; ie
&& i
< ie
[1]; i
++)
674 r
[i
] = ie
[i
+ 2] & 0x7f;
676 for (j
= 0; ie2
&& j
< ie2
[1]; j
++)
677 r
[i
+ j
] = ie2
[j
+ 2] & 0x7f;