3 * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "drivers/driver.h"
21 #include "wpa_supplicant_i.h"
29 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
31 #define WPA_BSS_EXPIRATION_PERIOD 10
33 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
34 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
35 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
36 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
37 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
38 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
39 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
40 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
41 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
44 static void wpa_bss_remove(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
46 dl_list_del(&bss
->list
);
47 dl_list_del(&bss
->list_id
);
49 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Remove id %u BSSID " MACSTR
50 " SSID '%s'", bss
->id
, MAC2STR(bss
->bssid
),
51 wpa_ssid_txt(bss
->ssid
, bss
->ssid_len
));
52 wpas_notify_bss_removed(wpa_s
, bss
->bssid
, bss
->id
);
57 struct wpa_bss
* wpa_bss_get(struct wpa_supplicant
*wpa_s
, const u8
*bssid
,
58 const u8
*ssid
, size_t ssid_len
)
61 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
62 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0 &&
63 bss
->ssid_len
== ssid_len
&&
64 os_memcmp(bss
->ssid
, ssid
, ssid_len
) == 0)
71 static void wpa_bss_copy_res(struct wpa_bss
*dst
, struct wpa_scan_res
*src
)
75 dst
->flags
= src
->flags
;
76 os_memcpy(dst
->bssid
, src
->bssid
, ETH_ALEN
);
77 dst
->freq
= src
->freq
;
78 dst
->beacon_int
= src
->beacon_int
;
79 dst
->caps
= src
->caps
;
80 dst
->qual
= src
->qual
;
81 dst
->noise
= src
->noise
;
82 dst
->level
= src
->level
;
85 os_get_time(&dst
->last_update
);
86 dst
->last_update
.sec
-= src
->age
/ 1000;
87 usec
= (src
->age
% 1000) * 1000;
88 if (dst
->last_update
.usec
< usec
) {
89 dst
->last_update
.sec
--;
90 dst
->last_update
.usec
+= 1000000;
92 dst
->last_update
.usec
-= usec
;
96 static void wpa_bss_add(struct wpa_supplicant
*wpa_s
,
97 const u8
*ssid
, size_t ssid_len
,
98 struct wpa_scan_res
*res
)
102 bss
= os_zalloc(sizeof(*bss
) + res
->ie_len
+ res
->beacon_ie_len
);
105 bss
->id
= wpa_s
->bss_next_id
++;
106 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
107 wpa_bss_copy_res(bss
, res
);
108 os_memcpy(bss
->ssid
, ssid
, ssid_len
);
109 bss
->ssid_len
= ssid_len
;
110 bss
->ie_len
= res
->ie_len
;
111 bss
->beacon_ie_len
= res
->beacon_ie_len
;
112 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
114 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
115 dl_list_add_tail(&wpa_s
->bss_id
, &bss
->list_id
);
117 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Add new id %u BSSID " MACSTR
119 bss
->id
, MAC2STR(bss
->bssid
), wpa_ssid_txt(ssid
, ssid_len
));
120 wpas_notify_bss_added(wpa_s
, bss
->bssid
, bss
->id
);
121 if (wpa_s
->num_bss
> wpa_s
->conf
->bss_max_count
) {
122 /* Remove the oldest entry */
123 wpa_bss_remove(wpa_s
, dl_list_first(&wpa_s
->bss
,
124 struct wpa_bss
, list
));
129 static int are_ies_equal(const struct wpa_bss
*old
,
130 const struct wpa_scan_res
*new, u32 ie
)
132 const u8
*old_ie
, *new_ie
;
133 struct wpabuf
*old_ie_buff
= NULL
;
134 struct wpabuf
*new_ie_buff
= NULL
;
135 int new_ie_len
, old_ie_len
, ret
, is_multi
;
138 case WPA_IE_VENDOR_TYPE
:
139 old_ie
= wpa_bss_get_vendor_ie(old
, ie
);
140 new_ie
= wpa_scan_get_vendor_ie(new, ie
);
143 case WPS_IE_VENDOR_TYPE
:
144 old_ie_buff
= wpa_bss_get_vendor_ie_multi(old
, ie
);
145 new_ie_buff
= wpa_scan_get_vendor_ie_multi(new, ie
);
149 case WLAN_EID_SUPP_RATES
:
150 case WLAN_EID_EXT_SUPP_RATES
:
151 old_ie
= wpa_bss_get_ie(old
, ie
);
152 new_ie
= wpa_scan_get_ie(new, ie
);
156 wpa_printf(MSG_DEBUG
, "bss: %s: cannot compare IEs", __func__
);
161 /* in case of multiple IEs stored in buffer */
162 old_ie
= old_ie_buff
? wpabuf_head_u8(old_ie_buff
) : NULL
;
163 new_ie
= new_ie_buff
? wpabuf_head_u8(new_ie_buff
) : NULL
;
164 old_ie_len
= old_ie_buff
? wpabuf_len(old_ie_buff
) : 0;
165 new_ie_len
= new_ie_buff
? wpabuf_len(new_ie_buff
) : 0;
167 /* in case of single IE */
168 old_ie_len
= old_ie
? old_ie
[1] + 2 : 0;
169 new_ie_len
= new_ie
? new_ie
[1] + 2 : 0;
172 ret
= (old_ie_len
== new_ie_len
&&
173 os_memcmp(old_ie
, new_ie
, old_ie_len
) == 0);
175 wpabuf_free(old_ie_buff
);
176 wpabuf_free(new_ie_buff
);
182 static u32
wpa_bss_compare_res(const struct wpa_bss
*old
,
183 const struct wpa_scan_res
*new)
186 int caps_diff
= old
->caps
^ new->caps
;
188 if (old
->freq
!= new->freq
)
189 changes
|= WPA_BSS_FREQ_CHANGED_FLAG
;
191 if (old
->level
!= new->level
)
192 changes
|= WPA_BSS_SIGNAL_CHANGED_FLAG
;
194 if (caps_diff
& IEEE80211_CAP_PRIVACY
)
195 changes
|= WPA_BSS_PRIVACY_CHANGED_FLAG
;
197 if (caps_diff
& IEEE80211_CAP_IBSS
)
198 changes
|= WPA_BSS_MODE_CHANGED_FLAG
;
200 if (old
->ie_len
== new->ie_len
&&
201 os_memcmp(old
+ 1, new + 1, old
->ie_len
) == 0)
203 changes
|= WPA_BSS_IES_CHANGED_FLAG
;
205 if (!are_ies_equal(old
, new, WPA_IE_VENDOR_TYPE
))
206 changes
|= WPA_BSS_WPAIE_CHANGED_FLAG
;
208 if (!are_ies_equal(old
, new, WLAN_EID_RSN
))
209 changes
|= WPA_BSS_RSNIE_CHANGED_FLAG
;
211 if (!are_ies_equal(old
, new, WPS_IE_VENDOR_TYPE
))
212 changes
|= WPA_BSS_WPS_CHANGED_FLAG
;
214 if (!are_ies_equal(old
, new, WLAN_EID_SUPP_RATES
) ||
215 !are_ies_equal(old
, new, WLAN_EID_EXT_SUPP_RATES
))
216 changes
|= WPA_BSS_RATES_CHANGED_FLAG
;
222 static void notify_bss_changes(struct wpa_supplicant
*wpa_s
, u32 changes
,
223 const struct wpa_bss
*bss
)
225 if (changes
& WPA_BSS_FREQ_CHANGED_FLAG
)
226 wpas_notify_bss_freq_changed(wpa_s
, bss
->id
);
228 if (changes
& WPA_BSS_SIGNAL_CHANGED_FLAG
)
229 wpas_notify_bss_signal_changed(wpa_s
, bss
->id
);
231 if (changes
& WPA_BSS_PRIVACY_CHANGED_FLAG
)
232 wpas_notify_bss_privacy_changed(wpa_s
, bss
->id
);
234 if (changes
& WPA_BSS_MODE_CHANGED_FLAG
)
235 wpas_notify_bss_mode_changed(wpa_s
, bss
->id
);
237 if (changes
& WPA_BSS_WPAIE_CHANGED_FLAG
)
238 wpas_notify_bss_wpaie_changed(wpa_s
, bss
->id
);
240 if (changes
& WPA_BSS_RSNIE_CHANGED_FLAG
)
241 wpas_notify_bss_rsnie_changed(wpa_s
, bss
->id
);
243 if (changes
& WPA_BSS_WPS_CHANGED_FLAG
)
244 wpas_notify_bss_wps_changed(wpa_s
, bss
->id
);
246 if (changes
& WPA_BSS_IES_CHANGED_FLAG
)
247 wpas_notify_bss_ies_changed(wpa_s
, bss
->id
);
249 if (changes
& WPA_BSS_RATES_CHANGED_FLAG
)
250 wpas_notify_bss_rates_changed(wpa_s
, bss
->id
);
254 static void wpa_bss_update(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
,
255 struct wpa_scan_res
*res
)
259 changes
= wpa_bss_compare_res(bss
, res
);
260 bss
->scan_miss_count
= 0;
261 bss
->last_update_idx
= wpa_s
->bss_update_idx
;
262 wpa_bss_copy_res(bss
, res
);
263 /* Move the entry to the end of the list */
264 dl_list_del(&bss
->list
);
265 if (bss
->ie_len
+ bss
->beacon_ie_len
>=
266 res
->ie_len
+ res
->beacon_ie_len
) {
267 os_memcpy(bss
+ 1, res
+ 1, res
->ie_len
+ res
->beacon_ie_len
);
268 bss
->ie_len
= res
->ie_len
;
269 bss
->beacon_ie_len
= res
->beacon_ie_len
;
271 struct wpa_bss
*nbss
;
272 struct dl_list
*prev
= bss
->list_id
.prev
;
273 dl_list_del(&bss
->list_id
);
274 nbss
= os_realloc(bss
, sizeof(*bss
) + res
->ie_len
+
278 os_memcpy(bss
+ 1, res
+ 1,
279 res
->ie_len
+ res
->beacon_ie_len
);
280 bss
->ie_len
= res
->ie_len
;
281 bss
->beacon_ie_len
= res
->beacon_ie_len
;
283 dl_list_add(prev
, &bss
->list_id
);
285 dl_list_add_tail(&wpa_s
->bss
, &bss
->list
);
287 notify_bss_changes(wpa_s
, changes
, bss
);
291 static int wpa_bss_in_use(struct wpa_supplicant
*wpa_s
, struct wpa_bss
*bss
)
293 return bss
== wpa_s
->current_bss
||
294 os_memcmp(bss
->bssid
, wpa_s
->bssid
, ETH_ALEN
) == 0 ||
295 os_memcmp(bss
->bssid
, wpa_s
->pending_bssid
, ETH_ALEN
) == 0;
299 void wpa_bss_update_start(struct wpa_supplicant
*wpa_s
)
301 wpa_s
->bss_update_idx
++;
302 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Start scan result update %u",
303 wpa_s
->bss_update_idx
);
307 void wpa_bss_update_scan_res(struct wpa_supplicant
*wpa_s
,
308 struct wpa_scan_res
*res
)
310 const u8
*ssid
, *p2p
;
313 ssid
= wpa_scan_get_ie(res
, WLAN_EID_SSID
);
315 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: No SSID IE included for "
316 MACSTR
, MAC2STR(res
->bssid
));
320 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Too long SSID IE included for "
321 MACSTR
, MAC2STR(res
->bssid
));
325 p2p
= wpa_scan_get_vendor_ie(res
, P2P_IE_VENDOR_TYPE
);
326 if (p2p
&& ssid
[1] == P2P_WILDCARD_SSID_LEN
&&
327 os_memcmp(ssid
+ 2, P2P_WILDCARD_SSID
, P2P_WILDCARD_SSID_LEN
) == 0)
328 return; /* Skip P2P listen discovery results here */
330 /* TODO: add option for ignoring BSSes we are not interested in
331 * (to save memory) */
332 bss
= wpa_bss_get(wpa_s
, res
->bssid
, ssid
+ 2, ssid
[1]);
334 wpa_bss_add(wpa_s
, ssid
+ 2, ssid
[1], res
);
336 wpa_bss_update(wpa_s
, bss
, res
);
340 static int wpa_bss_included_in_scan(const struct wpa_bss
*bss
,
341 const struct scan_info
*info
)
349 if (info
->num_freqs
) {
351 for (i
= 0; i
< info
->num_freqs
; i
++) {
352 if (bss
->freq
== info
->freqs
[i
]) {
361 if (info
->num_ssids
) {
363 for (i
= 0; i
< info
->num_ssids
; i
++) {
364 const struct wpa_driver_scan_ssid
*s
= &info
->ssids
[i
];
365 if ((s
->ssid
== NULL
|| s
->ssid_len
== 0) ||
366 (s
->ssid_len
== bss
->ssid_len
&&
367 os_memcmp(s
->ssid
, bss
->ssid
, bss
->ssid_len
) ==
381 void wpa_bss_update_end(struct wpa_supplicant
*wpa_s
, struct scan_info
*info
,
384 struct wpa_bss
*bss
, *n
;
387 return; /* do not expire entries without new scan */
389 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
390 if (wpa_bss_in_use(wpa_s
, bss
))
392 if (!wpa_bss_included_in_scan(bss
, info
))
393 continue; /* expire only BSSes that were scanned */
394 if (bss
->last_update_idx
< wpa_s
->bss_update_idx
)
395 bss
->scan_miss_count
++;
396 if (bss
->scan_miss_count
>=
397 wpa_s
->conf
->bss_expiration_scan_count
) {
398 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Expire BSS %u due to "
399 "no match in scan", bss
->id
);
400 wpa_bss_remove(wpa_s
, bss
);
406 void wpa_bss_flush_by_age(struct wpa_supplicant
*wpa_s
, int age
)
408 struct wpa_bss
*bss
, *n
;
411 if (dl_list_empty(&wpa_s
->bss
))
417 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
418 if (wpa_bss_in_use(wpa_s
, bss
))
421 if (os_time_before(&bss
->last_update
, &t
)) {
422 wpa_dbg(wpa_s
, MSG_DEBUG
, "BSS: Expire BSS %u due to "
424 wpa_bss_remove(wpa_s
, bss
);
431 static void wpa_bss_timeout(void *eloop_ctx
, void *timeout_ctx
)
433 struct wpa_supplicant
*wpa_s
= eloop_ctx
;
435 wpa_bss_flush_by_age(wpa_s
, wpa_s
->conf
->bss_expiration_age
);
436 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
437 wpa_bss_timeout
, wpa_s
, NULL
);
441 int wpa_bss_init(struct wpa_supplicant
*wpa_s
)
443 dl_list_init(&wpa_s
->bss
);
444 dl_list_init(&wpa_s
->bss_id
);
445 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD
, 0,
446 wpa_bss_timeout
, wpa_s
, NULL
);
451 void wpa_bss_flush(struct wpa_supplicant
*wpa_s
)
453 struct wpa_bss
*bss
, *n
;
455 if (wpa_s
->bss
.next
== NULL
)
456 return; /* BSS table not yet initialized */
458 dl_list_for_each_safe(bss
, n
, &wpa_s
->bss
, struct wpa_bss
, list
) {
459 if (wpa_bss_in_use(wpa_s
, bss
))
461 wpa_bss_remove(wpa_s
, bss
);
466 void wpa_bss_deinit(struct wpa_supplicant
*wpa_s
)
468 eloop_cancel_timeout(wpa_bss_timeout
, wpa_s
, NULL
);
469 wpa_bss_flush(wpa_s
);
473 struct wpa_bss
* wpa_bss_get_bssid(struct wpa_supplicant
*wpa_s
,
477 dl_list_for_each_reverse(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
478 if (os_memcmp(bss
->bssid
, bssid
, ETH_ALEN
) == 0)
485 struct wpa_bss
* wpa_bss_get_id(struct wpa_supplicant
*wpa_s
, unsigned int id
)
488 dl_list_for_each(bss
, &wpa_s
->bss
, struct wpa_bss
, list
) {
496 const u8
* wpa_bss_get_ie(const struct wpa_bss
*bss
, u8 ie
)
500 pos
= (const u8
*) (bss
+ 1);
501 end
= pos
+ bss
->ie_len
;
503 while (pos
+ 1 < end
) {
504 if (pos
+ 2 + pos
[1] > end
)
515 const u8
* wpa_bss_get_vendor_ie(const struct wpa_bss
*bss
, u32 vendor_type
)
519 pos
= (const u8
*) (bss
+ 1);
520 end
= pos
+ bss
->ie_len
;
522 while (pos
+ 1 < end
) {
523 if (pos
+ 2 + pos
[1] > end
)
525 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
526 vendor_type
== WPA_GET_BE32(&pos
[2]))
535 struct wpabuf
* wpa_bss_get_vendor_ie_multi(const struct wpa_bss
*bss
,
541 buf
= wpabuf_alloc(bss
->ie_len
);
545 pos
= (const u8
*) (bss
+ 1);
546 end
= pos
+ bss
->ie_len
;
548 while (pos
+ 1 < end
) {
549 if (pos
+ 2 + pos
[1] > end
)
551 if (pos
[0] == WLAN_EID_VENDOR_SPECIFIC
&& pos
[1] >= 4 &&
552 vendor_type
== WPA_GET_BE32(&pos
[2]))
553 wpabuf_put_data(buf
, pos
+ 2 + 4, pos
[1] - 4);
557 if (wpabuf_len(buf
) == 0) {
566 int wpa_bss_get_max_rate(const struct wpa_bss
*bss
)
572 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
573 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
574 if ((ie
[i
+ 2] & 0x7f) > rate
)
575 rate
= ie
[i
+ 2] & 0x7f;
578 ie
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
579 for (i
= 0; ie
&& i
< ie
[1]; i
++) {
580 if ((ie
[i
+ 2] & 0x7f) > rate
)
581 rate
= ie
[i
+ 2] & 0x7f;
588 int wpa_bss_get_bit_rates(const struct wpa_bss
*bss
, u8
**rates
)
595 ie
= wpa_bss_get_ie(bss
, WLAN_EID_SUPP_RATES
);
596 ie2
= wpa_bss_get_ie(bss
, WLAN_EID_EXT_SUPP_RATES
);
598 len
= (ie
? ie
[1] : 0) + (ie2
? ie2
[1] : 0);
604 for (i
= 0; ie
&& i
< ie
[1]; i
++)
605 r
[i
] = ie
[i
+ 2] & 0x7f;
607 for (j
= 0; ie2
&& j
< ie2
[1]; j
++)
608 r
[i
+ j
] = ie2
[j
+ 2] & 0x7f;