blob: 44a9a7b2c4a95731e716cb082dba37e375b28325 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * BSS table
Dmitry Shmidt7f656022015-02-25 14:36:37 -08003 * Copyright (c) 2009-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "drivers/driver.h"
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -070015#include "eap_peer/eap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "wpa_supplicant_i.h"
17#include "config.h"
18#include "notify.h"
19#include "scan.h"
20#include "bss.h"
21
22
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023#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)
32
33
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070034static void wpa_bss_set_hessid(struct wpa_bss *bss)
35{
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);
40 return;
41 }
42 if (ie[1] == 7)
43 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
44 else
45 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
46#endif /* CONFIG_INTERWORKING */
47}
48
49
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080050/**
51 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
52 * Returns: Allocated ANQP data structure or %NULL on failure
53 *
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().
57 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070058struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
59{
60 struct wpa_bss_anqp *anqp;
61 anqp = os_zalloc(sizeof(*anqp));
62 if (anqp == NULL)
63 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080064#ifdef CONFIG_INTERWORKING
65 dl_list_init(&anqp->anqp_elems);
66#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070067 anqp->users = 1;
68 return anqp;
69}
70
71
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080072/**
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
76 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080077static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
78{
79 struct wpa_bss_anqp *n;
80
81 n = os_zalloc(sizeof(*n));
82 if (n == NULL)
83 return NULL;
84
85#define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
86#ifdef CONFIG_INTERWORKING
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080087 dl_list_init(&n->anqp_elems);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080088 ANQP_DUP(capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080089 ANQP_DUP(venue_name);
90 ANQP_DUP(network_auth_type);
91 ANQP_DUP(roaming_consortium);
92 ANQP_DUP(ip_addr_type_availability);
93 ANQP_DUP(nai_realm);
94 ANQP_DUP(anqp_3gpp);
95 ANQP_DUP(domain_name);
96#endif /* CONFIG_INTERWORKING */
97#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -080098 ANQP_DUP(hs20_capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080099 ANQP_DUP(hs20_operator_friendly_name);
100 ANQP_DUP(hs20_wan_metrics);
101 ANQP_DUP(hs20_connection_capability);
102 ANQP_DUP(hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800103 ANQP_DUP(hs20_osu_providers_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800104#endif /* CONFIG_HS20 */
105#undef ANQP_DUP
106
107 return n;
108}
109
110
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800111/**
112 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
113 * @bss: BSS entry
114 * Returns: 0 on success, -1 on failure
115 *
116 * This function ensures the specific BSS entry has an ANQP data structure that
117 * is not shared with any other BSS entry.
118 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800119int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
120{
121 struct wpa_bss_anqp *anqp;
122
123 if (bss->anqp && bss->anqp->users > 1) {
124 /* allocated, but shared - clone an unshared copy */
125 anqp = wpa_bss_anqp_clone(bss->anqp);
126 if (anqp == NULL)
127 return -1;
128 anqp->users = 1;
129 bss->anqp->users--;
130 bss->anqp = anqp;
131 return 0;
132 }
133
134 if (bss->anqp)
135 return 0; /* already allocated and not shared */
136
137 /* not allocated - allocate a new storage area */
138 bss->anqp = wpa_bss_anqp_alloc();
139 return bss->anqp ? 0 : -1;
140}
141
142
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800143/**
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()
146 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700147static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
148{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800149#ifdef CONFIG_INTERWORKING
150 struct wpa_bss_anqp_elem *elem;
151#endif /* CONFIG_INTERWORKING */
152
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700153 if (anqp == NULL)
154 return;
155
156 anqp->users--;
157 if (anqp->users > 0) {
158 /* Another BSS entry holds a pointer to this ANQP info */
159 return;
160 }
161
162#ifdef CONFIG_INTERWORKING
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800163 wpabuf_free(anqp->capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700164 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);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800171
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);
176 os_free(elem);
177 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700178#endif /* CONFIG_INTERWORKING */
179#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800180 wpabuf_free(anqp->hs20_capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700181 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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800185 wpabuf_free(anqp->hs20_osu_providers_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700186#endif /* CONFIG_HS20 */
187
188 os_free(anqp);
189}
190
191
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800192static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
193 struct wpa_bss *old_bss,
194 struct wpa_bss *new_bss)
195{
196 struct wpa_radio_work *work;
197 struct wpa_connect_work *cwork;
198
199 work = radio_work_pending(wpa_s, "sme-connect");
200 if (!work)
201 work = radio_work_pending(wpa_s, "connect");
202 if (!work)
203 return;
204
205 cwork = work->ctx;
206 if (cwork->bss != old_bss)
207 return;
208
209 wpa_printf(MSG_DEBUG,
210 "Update BSS pointer for the pending connect radio work");
211 cwork->bss = new_bss;
212 if (!new_bss)
213 cwork->bss_removed = 1;
214}
215
216
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800217void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
218 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700220 if (wpa_s->last_scan_res) {
221 unsigned int i;
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--;
229 break;
230 }
231 }
232 }
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800233 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 dl_list_del(&bss->list);
235 dl_list_del(&bss->list_id);
236 wpa_s->num_bss--;
237 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700238 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
239 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700241 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 os_free(bss);
243}
244
245
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800246/**
247 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
248 * @wpa_s: Pointer to wpa_supplicant data
249 * @bssid: BSSID
250 * @ssid: SSID
251 * @ssid_len: Length of @ssid
252 * Returns: Pointer to the BSS entry or %NULL if not found
253 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
255 const u8 *ssid, size_t ssid_len)
256{
257 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700258 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
259 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260 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)
264 return bss;
265 }
266 return NULL;
267}
268
269
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800270static void calculate_update_time(const struct os_reltime *fetch_time,
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700271 unsigned int age_ms,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800272 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273{
274 os_time_t usec;
275
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700276 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) {
281 update_time->sec--;
282 update_time->usec += 1000000;
283 }
284 update_time->usec -= usec;
285}
286
287
288static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800289 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700290{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291 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;
299 dst->tsf = src->tsf;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800300 dst->est_throughput = src->est_throughput;
301 dst->snr = src->snr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700303 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304}
305
306
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700307static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
308 struct wpa_bss *bss)
309{
310#ifdef CONFIG_WPS
311 struct wpa_ssid *ssid;
312 struct wpabuf *wps_ie;
313 int pbc = 0, ret;
314
315 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
316 if (!wps_ie)
317 return 0;
318
319 if (wps_is_selected_pbc_registrar(wps_ie)) {
320 pbc = 1;
321 } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
322 wpabuf_free(wps_ie);
323 return 0;
324 }
325
326 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
327 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
328 continue;
329 if (ssid->ssid_len &&
330 (ssid->ssid_len != bss->ssid_len ||
331 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
332 continue;
333
334 if (pbc)
335 ret = eap_is_wps_pbc_enrollee(&ssid->eap);
336 else
337 ret = eap_is_wps_pin_enrollee(&ssid->eap);
338 wpabuf_free(wps_ie);
339 return ret;
340 }
341 wpabuf_free(wps_ie);
342#endif /* CONFIG_WPS */
343
344 return 0;
345}
346
347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800348static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
349{
350 struct wpa_ssid *ssid;
351
352 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
353 if (ssid->ssid == NULL || ssid->ssid_len == 0)
354 continue;
355 if (ssid->ssid_len == bss->ssid_len &&
356 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
357 return 1;
358 }
359
360 return 0;
361}
362
363
Dmitry Shmidt04949592012-07-19 12:16:46 -0700364static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
365{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800366 if (bss == wpa_s->current_bss)
367 return 1;
368
369 if (wpa_s->current_bss &&
370 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
371 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
372 bss->ssid_len) != 0))
373 return 0; /* SSID has changed */
374
375 return !is_zero_ether_addr(bss->bssid) &&
376 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
377 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700378}
379
380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
382{
383 struct wpa_bss *bss;
384
385 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700386 if (!wpa_bss_known(wpa_s, bss) &&
387 !wpa_bss_is_wps_candidate(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700388 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389 return 0;
390 }
391 }
392
393 return -1;
394}
395
396
Dmitry Shmidt04949592012-07-19 12:16:46 -0700397static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800398{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700399 struct wpa_bss *bss;
400
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800401 /*
402 * Remove the oldest entry that does not match with any configured
403 * network.
404 */
405 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700406 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800407
408 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700409 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800410 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700411 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
412 if (!wpa_bss_in_use(wpa_s, bss)) {
413 wpa_bss_remove(wpa_s, bss, __func__);
414 return 0;
415 }
416 }
417
418 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800419}
420
421
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700422static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
423 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800424 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800425 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426{
427 struct wpa_bss *bss;
428
429 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
430 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700431 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 bss->id = wpa_s->bss_next_id++;
433 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800434 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 os_memcpy(bss->ssid, ssid, ssid_len);
436 bss->ssid_len = ssid_len;
437 bss->ie_len = res->ie_len;
438 bss->beacon_ie_len = res->beacon_ie_len;
439 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700440 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300442 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
443 wpa_bss_remove_oldest(wpa_s) != 0) {
444 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
445 "because all BSSes are in use. We should normally "
446 "not get here!", (int) wpa_s->num_bss + 1);
447 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
448 }
449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 dl_list_add_tail(&wpa_s->bss, &bss->list);
451 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
452 wpa_s->num_bss++;
453 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800454 " SSID '%s' freq %d",
455 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
456 bss->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700458 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459}
460
461
462static int are_ies_equal(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700463 const struct wpa_scan_res *new_res, u32 ie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464{
465 const u8 *old_ie, *new_ie;
466 struct wpabuf *old_ie_buff = NULL;
467 struct wpabuf *new_ie_buff = NULL;
468 int new_ie_len, old_ie_len, ret, is_multi;
469
470 switch (ie) {
471 case WPA_IE_VENDOR_TYPE:
472 old_ie = wpa_bss_get_vendor_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700473 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 is_multi = 0;
475 break;
476 case WPS_IE_VENDOR_TYPE:
477 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700478 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479 is_multi = 1;
480 break;
481 case WLAN_EID_RSN:
482 case WLAN_EID_SUPP_RATES:
483 case WLAN_EID_EXT_SUPP_RATES:
484 old_ie = wpa_bss_get_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700485 new_ie = wpa_scan_get_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 is_multi = 0;
487 break;
488 default:
489 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
490 return 0;
491 }
492
493 if (is_multi) {
494 /* in case of multiple IEs stored in buffer */
495 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
496 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
497 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
498 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
499 } else {
500 /* in case of single IE */
501 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
502 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
503 }
504
505 if (!old_ie || !new_ie)
506 ret = !old_ie && !new_ie;
507 else
508 ret = (old_ie_len == new_ie_len &&
509 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
510
511 wpabuf_free(old_ie_buff);
512 wpabuf_free(new_ie_buff);
513
514 return ret;
515}
516
517
518static u32 wpa_bss_compare_res(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700519 const struct wpa_scan_res *new_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520{
521 u32 changes = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700522 int caps_diff = old->caps ^ new_res->caps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700524 if (old->freq != new_res->freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
526
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700527 if (old->level != new_res->level)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
529
530 if (caps_diff & IEEE80211_CAP_PRIVACY)
531 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
532
533 if (caps_diff & IEEE80211_CAP_IBSS)
534 changes |= WPA_BSS_MODE_CHANGED_FLAG;
535
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700536 if (old->ie_len == new_res->ie_len &&
537 os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 return changes;
539 changes |= WPA_BSS_IES_CHANGED_FLAG;
540
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700541 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
543
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700544 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
546
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700547 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 changes |= WPA_BSS_WPS_CHANGED_FLAG;
549
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700550 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
551 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 changes |= WPA_BSS_RATES_CHANGED_FLAG;
553
554 return changes;
555}
556
557
558static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
559 const struct wpa_bss *bss)
560{
561 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
562 wpas_notify_bss_freq_changed(wpa_s, bss->id);
563
564 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
565 wpas_notify_bss_signal_changed(wpa_s, bss->id);
566
567 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
568 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
569
570 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
571 wpas_notify_bss_mode_changed(wpa_s, bss->id);
572
573 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
574 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
575
576 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
577 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
578
579 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
580 wpas_notify_bss_wps_changed(wpa_s, bss->id);
581
582 if (changes & WPA_BSS_IES_CHANGED_FLAG)
583 wpas_notify_bss_ies_changed(wpa_s, bss->id);
584
585 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
586 wpas_notify_bss_rates_changed(wpa_s, bss->id);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700587
588 wpas_notify_bss_seen(wpa_s, bss->id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589}
590
591
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700592static struct wpa_bss *
593wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800594 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595{
596 u32 changes;
597
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800598 if (bss->last_update_idx == wpa_s->bss_update_idx) {
599 struct os_reltime update_time;
600
601 /*
602 * Some drivers (e.g., cfg80211) include multiple BSS entries
603 * for the same BSS if that BSS's channel changes. The BSS list
604 * implementation in wpa_supplicant does not do that and we need
605 * to filter out the obsolete results here to make sure only the
606 * most current BSS information remains in the table.
607 */
608 wpa_printf(MSG_DEBUG, "BSS: " MACSTR
609 " has multiple entries in the scan results - select the most current one",
610 MAC2STR(bss->bssid));
611 calculate_update_time(fetch_time, res->age, &update_time);
612 wpa_printf(MSG_DEBUG,
613 "Previous last_update: %u.%06u (freq %d%s)",
614 (unsigned int) bss->last_update.sec,
615 (unsigned int) bss->last_update.usec,
616 bss->freq,
617 (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
618 wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
619 (unsigned int) update_time.sec,
620 (unsigned int) update_time.usec,
621 res->freq,
622 (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
623 if ((bss->flags & WPA_BSS_ASSOCIATED) ||
624 (!(res->flags & WPA_SCAN_ASSOCIATED) &&
625 !os_reltime_before(&bss->last_update, &update_time))) {
626 wpa_printf(MSG_DEBUG,
627 "Ignore this BSS entry since the previous update looks more current");
628 return bss;
629 }
630 wpa_printf(MSG_DEBUG,
631 "Accept this BSS entry since it looks more current than the previous update");
632 }
633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 changes = wpa_bss_compare_res(bss, res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800635 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
636 wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
637 MAC2STR(bss->bssid), bss->freq, res->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 bss->scan_miss_count = 0;
639 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800640 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 /* Move the entry to the end of the list */
642 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700643#ifdef CONFIG_P2P
644 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
645 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
646 /*
647 * This can happen when non-P2P station interface runs a scan
648 * without P2P IE in the Probe Request frame. P2P GO would reply
649 * to that with a Probe Response that does not include P2P IE.
650 * Do not update the IEs in this BSS entry to avoid such loss of
651 * information that may be needed for P2P operations to
652 * determine group information.
653 */
654 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
655 MACSTR " since that would remove P2P IE information",
656 MAC2STR(bss->bssid));
657 } else
658#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659 if (bss->ie_len + bss->beacon_ie_len >=
660 res->ie_len + res->beacon_ie_len) {
661 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
662 bss->ie_len = res->ie_len;
663 bss->beacon_ie_len = res->beacon_ie_len;
664 } else {
665 struct wpa_bss *nbss;
666 struct dl_list *prev = bss->list_id.prev;
667 dl_list_del(&bss->list_id);
668 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
669 res->beacon_ie_len);
670 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700671 unsigned int i;
672 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
673 if (wpa_s->last_scan_res[i] == bss) {
674 wpa_s->last_scan_res[i] = nbss;
675 break;
676 }
677 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700678 if (wpa_s->current_bss == bss)
679 wpa_s->current_bss = nbss;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800680 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 bss = nbss;
682 os_memcpy(bss + 1, res + 1,
683 res->ie_len + res->beacon_ie_len);
684 bss->ie_len = res->ie_len;
685 bss->beacon_ie_len = res->beacon_ie_len;
686 }
687 dl_list_add(prev, &bss->list_id);
688 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700689 if (changes & WPA_BSS_IES_CHANGED_FLAG)
690 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 dl_list_add_tail(&wpa_s->bss, &bss->list);
692
693 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700694
695 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696}
697
698
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800699/**
700 * wpa_bss_update_start - Start a BSS table update from scan results
701 * @wpa_s: Pointer to wpa_supplicant data
702 *
703 * This function is called at the start of each BSS table update round for new
704 * scan results. The actual scan result entries are indicated with calls to
705 * wpa_bss_update_scan_res() and the update round is finished with a call to
706 * wpa_bss_update_end().
707 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
709{
710 wpa_s->bss_update_idx++;
711 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
712 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700713 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714}
715
716
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800717/**
718 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
719 * @wpa_s: Pointer to wpa_supplicant data
720 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800721 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800722 *
723 * This function updates a BSS table entry (or adds one) based on a scan result.
724 * This is called separately for each scan result between the calls to
725 * wpa_bss_update_start() and wpa_bss_update_end().
726 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800728 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800729 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800731 const u8 *ssid, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732 struct wpa_bss *bss;
733
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700734 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800735 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700736 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800737 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
738 struct os_reltime age;
739 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
740 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700741 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
742 "table entry that is %u.%06u seconds older "
743 "than our scan trigger",
744 (unsigned int) age.sec,
745 (unsigned int) age.usec);
746 return;
747 }
748 }
749
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
751 if (ssid == NULL) {
752 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
753 MACSTR, MAC2STR(res->bssid));
754 return;
755 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700756 if (ssid[1] > SSID_MAX_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
758 MACSTR, MAC2STR(res->bssid));
759 return;
760 }
761
762 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700763#ifdef CONFIG_P2P
764 if (p2p == NULL &&
765 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
766 /*
767 * If it's a P2P specific interface, then don't update
768 * the scan result without a P2P IE.
769 */
770 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
771 " update for P2P interface", MAC2STR(res->bssid));
772 return;
773 }
774#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
776 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
777 return; /* Skip P2P listen discovery results here */
778
779 /* TODO: add option for ignoring BSSes we are not interested in
780 * (to save memory) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800781
782 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700783 if (mesh && mesh[1] <= SSID_MAX_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800784 ssid = mesh;
785
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
787 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800788 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700789 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800790 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700791 if (wpa_s->last_scan_res) {
792 unsigned int i;
793 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
794 if (bss == wpa_s->last_scan_res[i]) {
795 /* Already in the list */
796 return;
797 }
798 }
799 }
800 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700801
802 if (bss == NULL)
803 return;
804 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
805 struct wpa_bss **n;
806 unsigned int siz;
807 if (wpa_s->last_scan_res_size == 0)
808 siz = 32;
809 else
810 siz = wpa_s->last_scan_res_size * 2;
811 n = os_realloc_array(wpa_s->last_scan_res, siz,
812 sizeof(struct wpa_bss *));
813 if (n == NULL)
814 return;
815 wpa_s->last_scan_res = n;
816 wpa_s->last_scan_res_size = siz;
817 }
818
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700819 if (wpa_s->last_scan_res)
820 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821}
822
823
824static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
825 const struct scan_info *info)
826{
827 int found;
828 size_t i;
829
830 if (info == NULL)
831 return 1;
832
833 if (info->num_freqs) {
834 found = 0;
835 for (i = 0; i < info->num_freqs; i++) {
836 if (bss->freq == info->freqs[i]) {
837 found = 1;
838 break;
839 }
840 }
841 if (!found)
842 return 0;
843 }
844
845 if (info->num_ssids) {
846 found = 0;
847 for (i = 0; i < info->num_ssids; i++) {
848 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
849 if ((s->ssid == NULL || s->ssid_len == 0) ||
850 (s->ssid_len == bss->ssid_len &&
851 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
852 0)) {
853 found = 1;
854 break;
855 }
856 }
857 if (!found)
858 return 0;
859 }
860
861 return 1;
862}
863
864
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800865/**
866 * wpa_bss_update_end - End a BSS table update from scan results
867 * @wpa_s: Pointer to wpa_supplicant data
868 * @info: Information about scan parameters
869 * @new_scan: Whether this update round was based on a new scan
870 *
871 * This function is called at the end of each BSS table update round for new
872 * scan results. The start of the update was indicated with a call to
873 * wpa_bss_update_start().
874 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
876 int new_scan)
877{
878 struct wpa_bss *bss, *n;
879
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800880 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800881 if ((info && info->aborted) || !new_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 return; /* do not expire entries without new scan */
883
884 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
885 if (wpa_bss_in_use(wpa_s, bss))
886 continue;
887 if (!wpa_bss_included_in_scan(bss, info))
888 continue; /* expire only BSSes that were scanned */
889 if (bss->last_update_idx < wpa_s->bss_update_idx)
890 bss->scan_miss_count++;
891 if (bss->scan_miss_count >=
892 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700893 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 }
895 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700896
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800897 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
898 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899}
900
901
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800902/**
903 * wpa_bss_flush_by_age - Flush old BSS entries
904 * @wpa_s: Pointer to wpa_supplicant data
905 * @age: Maximum entry age in seconds
906 *
907 * Remove BSS entries that have not been updated during the last @age seconds.
908 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700909void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
910{
911 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800912 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700913
914 if (dl_list_empty(&wpa_s->bss))
915 return;
916
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800917 os_get_reltime(&t);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 t.sec -= age;
919
920 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
921 if (wpa_bss_in_use(wpa_s, bss))
922 continue;
923
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800924 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700925 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 } else
927 break;
928 }
929}
930
931
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800932/**
933 * wpa_bss_init - Initialize BSS table
934 * @wpa_s: Pointer to wpa_supplicant data
935 * Returns: 0 on success, -1 on failure
936 *
937 * This prepares BSS table lists and timer for periodic updates. The BSS table
938 * is deinitialized with wpa_bss_deinit() once not needed anymore.
939 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940int wpa_bss_init(struct wpa_supplicant *wpa_s)
941{
942 dl_list_init(&wpa_s->bss);
943 dl_list_init(&wpa_s->bss_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700944 return 0;
945}
946
947
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800948/**
949 * wpa_bss_flush - Flush all unused BSS entries
950 * @wpa_s: Pointer to wpa_supplicant data
951 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952void wpa_bss_flush(struct wpa_supplicant *wpa_s)
953{
954 struct wpa_bss *bss, *n;
955
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800956 wpa_s->clear_driver_scan_cache = 1;
957
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958 if (wpa_s->bss.next == NULL)
959 return; /* BSS table not yet initialized */
960
961 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
962 if (wpa_bss_in_use(wpa_s, bss))
963 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700964 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 }
966}
967
968
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800969/**
970 * wpa_bss_deinit - Deinitialize BSS table
971 * @wpa_s: Pointer to wpa_supplicant data
972 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
974{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975 wpa_bss_flush(wpa_s);
976}
977
978
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800979/**
980 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
981 * @wpa_s: Pointer to wpa_supplicant data
982 * @bssid: BSSID
983 * Returns: Pointer to the BSS entry or %NULL if not found
984 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
986 const u8 *bssid)
987{
988 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700989 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
990 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
992 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
993 return bss;
994 }
995 return NULL;
996}
997
998
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700999/**
1000 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1001 * @wpa_s: Pointer to wpa_supplicant data
1002 * @bssid: BSSID
1003 * Returns: Pointer to the BSS entry or %NULL if not found
1004 *
1005 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1006 * find the entry that has the most recent update. This can help in finding the
1007 * correct entry in cases where the SSID of the AP may have changed recently
1008 * (e.g., in WPS reconfiguration cases).
1009 */
1010struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1011 const u8 *bssid)
1012{
1013 struct wpa_bss *bss, *found = NULL;
1014 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1015 return NULL;
1016 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1017 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
1018 continue;
1019 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001020 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001021 found = bss;
1022 }
1023 return found;
1024}
1025
1026
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001027#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001028/**
1029 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
1030 * @wpa_s: Pointer to wpa_supplicant data
1031 * @dev_addr: P2P Device Address of the GO
1032 * Returns: Pointer to the BSS entry or %NULL if not found
1033 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001034struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1035 const u8 *dev_addr)
1036{
1037 struct wpa_bss *bss;
1038 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1039 u8 addr[ETH_ALEN];
1040 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
1041 addr) == 0 &&
1042 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
1043 return bss;
1044 }
1045 return NULL;
1046}
1047#endif /* CONFIG_P2P */
1048
1049
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001050/**
1051 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1052 * @wpa_s: Pointer to wpa_supplicant data
1053 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1054 * Returns: Pointer to the BSS entry or %NULL if not found
1055 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1057{
1058 struct wpa_bss *bss;
1059 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1060 if (bss->id == id)
1061 return bss;
1062 }
1063 return NULL;
1064}
1065
1066
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001067/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001068 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1069 * @wpa_s: Pointer to wpa_supplicant data
1070 * @idf: Smallest allowed identifier assigned for the entry
1071 * @idf: Largest allowed identifier assigned for the entry
1072 * Returns: Pointer to the BSS entry or %NULL if not found
1073 *
1074 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1075 * smallest id value to be fetched within the specified range without the
1076 * caller having to know the exact id.
1077 */
1078struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1079 unsigned int idf, unsigned int idl)
1080{
1081 struct wpa_bss *bss;
1082 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1083 if (bss->id >= idf && bss->id <= idl)
1084 return bss;
1085 }
1086 return NULL;
1087}
1088
1089
1090/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001091 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1092 * @bss: BSS table entry
1093 * @ie: Information element identitifier (WLAN_EID_*)
1094 * Returns: Pointer to the information element (id field) or %NULL if not found
1095 *
1096 * This function returns the first matching information element in the BSS
1097 * entry.
1098 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1100{
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001101 return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102}
1103
1104
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001105/**
1106 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1107 * @bss: BSS table entry
1108 * @vendor_type: Vendor type (four octets starting the IE payload)
1109 * Returns: Pointer to the information element (id field) or %NULL if not found
1110 *
1111 * This function returns the first matching information element in the BSS
1112 * entry.
1113 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1115{
1116 const u8 *end, *pos;
1117
1118 pos = (const u8 *) (bss + 1);
1119 end = pos + bss->ie_len;
1120
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001121 while (end - pos > 1) {
1122 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 break;
1124 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1125 vendor_type == WPA_GET_BE32(&pos[2]))
1126 return pos;
1127 pos += 2 + pos[1];
1128 }
1129
1130 return NULL;
1131}
1132
1133
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001134/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001135 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information 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 (id field) or %NULL if not found
1139 *
1140 * This function returns the first matching information element in the BSS
1141 * entry.
1142 *
1143 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1144 * from Beacon frames instead of either Beacon or Probe Response frames.
1145 */
1146const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1147 u32 vendor_type)
1148{
1149 const u8 *end, *pos;
1150
1151 if (bss->beacon_ie_len == 0)
1152 return NULL;
1153
1154 pos = (const u8 *) (bss + 1);
1155 pos += bss->ie_len;
1156 end = pos + bss->beacon_ie_len;
1157
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001158 while (end - pos > 1) {
1159 if (2 + pos[1] > end - pos)
Dmitry Shmidt96571392013-10-14 12:54:46 -07001160 break;
1161 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1162 vendor_type == WPA_GET_BE32(&pos[2]))
1163 return pos;
1164 pos += 2 + pos[1];
1165 }
1166
1167 return NULL;
1168}
1169
1170
1171/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001172 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1173 * @bss: BSS table entry
1174 * @vendor_type: Vendor type (four octets starting the IE payload)
1175 * Returns: Pointer to the information element payload or %NULL if not found
1176 *
1177 * This function returns concatenated payload of possibly fragmented vendor
1178 * specific information elements in the BSS entry. The caller is responsible for
1179 * freeing the returned buffer.
1180 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1182 u32 vendor_type)
1183{
1184 struct wpabuf *buf;
1185 const u8 *end, *pos;
1186
1187 buf = wpabuf_alloc(bss->ie_len);
1188 if (buf == NULL)
1189 return NULL;
1190
1191 pos = (const u8 *) (bss + 1);
1192 end = pos + bss->ie_len;
1193
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001194 while (end - pos > 1) {
1195 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001196 break;
1197 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1198 vendor_type == WPA_GET_BE32(&pos[2]))
1199 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1200 pos += 2 + pos[1];
1201 }
1202
1203 if (wpabuf_len(buf) == 0) {
1204 wpabuf_free(buf);
1205 buf = NULL;
1206 }
1207
1208 return buf;
1209}
1210
1211
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001212/**
1213 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1214 * @bss: BSS table entry
1215 * @vendor_type: Vendor type (four octets starting the IE payload)
1216 * Returns: Pointer to the information element payload or %NULL if not found
1217 *
1218 * This function returns concatenated payload of possibly fragmented vendor
1219 * specific information elements in the BSS entry. The caller is responsible for
1220 * freeing the returned buffer.
1221 *
1222 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1223 * from Beacon frames instead of either Beacon or Probe Response frames.
1224 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001225struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1226 u32 vendor_type)
1227{
1228 struct wpabuf *buf;
1229 const u8 *end, *pos;
1230
1231 buf = wpabuf_alloc(bss->beacon_ie_len);
1232 if (buf == NULL)
1233 return NULL;
1234
1235 pos = (const u8 *) (bss + 1);
1236 pos += bss->ie_len;
1237 end = pos + bss->beacon_ie_len;
1238
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001239 while (end - pos > 1) {
1240 if (2 + pos[1] > end - pos)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001241 break;
1242 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1243 vendor_type == WPA_GET_BE32(&pos[2]))
1244 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1245 pos += 2 + pos[1];
1246 }
1247
1248 if (wpabuf_len(buf) == 0) {
1249 wpabuf_free(buf);
1250 buf = NULL;
1251 }
1252
1253 return buf;
1254}
1255
1256
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001257/**
1258 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1259 * @bss: BSS table entry
1260 * Returns: Maximum legacy rate in units of 500 kbps
1261 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001262int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1263{
1264 int rate = 0;
1265 const u8 *ie;
1266 int i;
1267
1268 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1269 for (i = 0; ie && i < ie[1]; i++) {
1270 if ((ie[i + 2] & 0x7f) > rate)
1271 rate = ie[i + 2] & 0x7f;
1272 }
1273
1274 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1275 for (i = 0; ie && i < ie[1]; i++) {
1276 if ((ie[i + 2] & 0x7f) > rate)
1277 rate = ie[i + 2] & 0x7f;
1278 }
1279
1280 return rate;
1281}
1282
1283
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001284/**
1285 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1286 * @bss: BSS table entry
1287 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1288 * Returns: number of legacy TX rates or -1 on failure
1289 *
1290 * The caller is responsible for freeing the returned buffer with os_free() in
1291 * case of success.
1292 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1294{
1295 const u8 *ie, *ie2;
1296 int i, j;
1297 unsigned int len;
1298 u8 *r;
1299
1300 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1301 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1302
1303 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1304
1305 r = os_malloc(len);
1306 if (!r)
1307 return -1;
1308
1309 for (i = 0; ie && i < ie[1]; i++)
1310 r[i] = ie[i + 2] & 0x7f;
1311
1312 for (j = 0; ie2 && j < ie2[1]; j++)
1313 r[i + j] = ie2[j + 2] & 0x7f;
1314
1315 *rates = r;
1316 return len;
1317}