blob: 813456275ca746e55e6d8a4955b9e3acbc89596a [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"
15#include "wpa_supplicant_i.h"
16#include "config.h"
17#include "notify.h"
18#include "scan.h"
19#include "bss.h"
20
21
22/**
23 * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
24 */
25#define WPA_BSS_EXPIRATION_PERIOD 10
26
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)
36
37
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070038static void wpa_bss_set_hessid(struct wpa_bss *bss)
39{
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);
44 return;
45 }
46 if (ie[1] == 7)
47 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
48 else
49 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
50#endif /* CONFIG_INTERWORKING */
51}
52
53
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080054/**
55 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
56 * Returns: Allocated ANQP data structure or %NULL on failure
57 *
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().
61 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070062struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
63{
64 struct wpa_bss_anqp *anqp;
65 anqp = os_zalloc(sizeof(*anqp));
66 if (anqp == NULL)
67 return NULL;
68 anqp->users = 1;
69 return anqp;
70}
71
72
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080073/**
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
77 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080078static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
79{
80 struct wpa_bss_anqp *n;
81
82 n = os_zalloc(sizeof(*n));
83 if (n == NULL)
84 return NULL;
85
86#define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
87#ifdef CONFIG_INTERWORKING
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{
149 if (anqp == NULL)
150 return;
151
152 anqp->users--;
153 if (anqp->users > 0) {
154 /* Another BSS entry holds a pointer to this ANQP info */
155 return;
156 }
157
158#ifdef CONFIG_INTERWORKING
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800159 wpabuf_free(anqp->capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700160 wpabuf_free(anqp->venue_name);
161 wpabuf_free(anqp->network_auth_type);
162 wpabuf_free(anqp->roaming_consortium);
163 wpabuf_free(anqp->ip_addr_type_availability);
164 wpabuf_free(anqp->nai_realm);
165 wpabuf_free(anqp->anqp_3gpp);
166 wpabuf_free(anqp->domain_name);
167#endif /* CONFIG_INTERWORKING */
168#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800169 wpabuf_free(anqp->hs20_capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700170 wpabuf_free(anqp->hs20_operator_friendly_name);
171 wpabuf_free(anqp->hs20_wan_metrics);
172 wpabuf_free(anqp->hs20_connection_capability);
173 wpabuf_free(anqp->hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800174 wpabuf_free(anqp->hs20_osu_providers_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700175#endif /* CONFIG_HS20 */
176
177 os_free(anqp);
178}
179
180
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800181static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
182 struct wpa_bss *old_bss,
183 struct wpa_bss *new_bss)
184{
185 struct wpa_radio_work *work;
186 struct wpa_connect_work *cwork;
187
188 work = radio_work_pending(wpa_s, "sme-connect");
189 if (!work)
190 work = radio_work_pending(wpa_s, "connect");
191 if (!work)
192 return;
193
194 cwork = work->ctx;
195 if (cwork->bss != old_bss)
196 return;
197
198 wpa_printf(MSG_DEBUG,
199 "Update BSS pointer for the pending connect radio work");
200 cwork->bss = new_bss;
201 if (!new_bss)
202 cwork->bss_removed = 1;
203}
204
205
Dmitry Shmidt04949592012-07-19 12:16:46 -0700206static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
207 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700208{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700209 if (wpa_s->last_scan_res) {
210 unsigned int i;
211 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
212 if (wpa_s->last_scan_res[i] == bss) {
213 os_memmove(&wpa_s->last_scan_res[i],
214 &wpa_s->last_scan_res[i + 1],
215 (wpa_s->last_scan_res_used - i - 1)
216 * sizeof(struct wpa_bss *));
217 wpa_s->last_scan_res_used--;
218 break;
219 }
220 }
221 }
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800222 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700223 dl_list_del(&bss->list);
224 dl_list_del(&bss->list_id);
225 wpa_s->num_bss--;
226 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700227 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
228 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700230 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 os_free(bss);
232}
233
234
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800235/**
236 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
237 * @wpa_s: Pointer to wpa_supplicant data
238 * @bssid: BSSID
239 * @ssid: SSID
240 * @ssid_len: Length of @ssid
241 * Returns: Pointer to the BSS entry or %NULL if not found
242 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
244 const u8 *ssid, size_t ssid_len)
245{
246 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700247 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
248 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
250 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
251 bss->ssid_len == ssid_len &&
252 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
253 return bss;
254 }
255 return NULL;
256}
257
258
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800259static void calculate_update_time(const struct os_reltime *fetch_time,
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700260 unsigned int age_ms,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800261 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262{
263 os_time_t usec;
264
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700265 update_time->sec = fetch_time->sec;
266 update_time->usec = fetch_time->usec;
267 update_time->sec -= age_ms / 1000;
268 usec = (age_ms % 1000) * 1000;
269 if (update_time->usec < usec) {
270 update_time->sec--;
271 update_time->usec += 1000000;
272 }
273 update_time->usec -= usec;
274}
275
276
277static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800278 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700279{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 dst->flags = src->flags;
281 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
282 dst->freq = src->freq;
283 dst->beacon_int = src->beacon_int;
284 dst->caps = src->caps;
285 dst->qual = src->qual;
286 dst->noise = src->noise;
287 dst->level = src->level;
288 dst->tsf = src->tsf;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800289 dst->est_throughput = src->est_throughput;
290 dst->snr = src->snr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700291
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700292 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293}
294
295
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
297{
298 struct wpa_ssid *ssid;
299
300 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
301 if (ssid->ssid == NULL || ssid->ssid_len == 0)
302 continue;
303 if (ssid->ssid_len == bss->ssid_len &&
304 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
305 return 1;
306 }
307
308 return 0;
309}
310
311
Dmitry Shmidt04949592012-07-19 12:16:46 -0700312static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
313{
314 return bss == wpa_s->current_bss ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800315 (!is_zero_ether_addr(bss->bssid) &&
316 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
317 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700318}
319
320
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
322{
323 struct wpa_bss *bss;
324
325 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
326 if (!wpa_bss_known(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700327 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800328 return 0;
329 }
330 }
331
332 return -1;
333}
334
335
Dmitry Shmidt04949592012-07-19 12:16:46 -0700336static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800337{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700338 struct wpa_bss *bss;
339
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800340 /*
341 * Remove the oldest entry that does not match with any configured
342 * network.
343 */
344 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700345 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346
347 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800349 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700350 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
351 if (!wpa_bss_in_use(wpa_s, bss)) {
352 wpa_bss_remove(wpa_s, bss, __func__);
353 return 0;
354 }
355 }
356
357 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358}
359
360
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700361static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
362 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800363 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800364 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365{
366 struct wpa_bss *bss;
367
368 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
369 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700370 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 bss->id = wpa_s->bss_next_id++;
372 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800373 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 os_memcpy(bss->ssid, ssid, ssid_len);
375 bss->ssid_len = ssid_len;
376 bss->ie_len = res->ie_len;
377 bss->beacon_ie_len = res->beacon_ie_len;
378 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700379 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300381 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
382 wpa_bss_remove_oldest(wpa_s) != 0) {
383 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
384 "because all BSSes are in use. We should normally "
385 "not get here!", (int) wpa_s->num_bss + 1);
386 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
387 }
388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 dl_list_add_tail(&wpa_s->bss, &bss->list);
390 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
391 wpa_s->num_bss++;
392 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
393 " SSID '%s'",
394 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
395 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700396 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397}
398
399
400static int are_ies_equal(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700401 const struct wpa_scan_res *new_res, u32 ie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402{
403 const u8 *old_ie, *new_ie;
404 struct wpabuf *old_ie_buff = NULL;
405 struct wpabuf *new_ie_buff = NULL;
406 int new_ie_len, old_ie_len, ret, is_multi;
407
408 switch (ie) {
409 case WPA_IE_VENDOR_TYPE:
410 old_ie = wpa_bss_get_vendor_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700411 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 is_multi = 0;
413 break;
414 case WPS_IE_VENDOR_TYPE:
415 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700416 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 is_multi = 1;
418 break;
419 case WLAN_EID_RSN:
420 case WLAN_EID_SUPP_RATES:
421 case WLAN_EID_EXT_SUPP_RATES:
422 old_ie = wpa_bss_get_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700423 new_ie = wpa_scan_get_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 is_multi = 0;
425 break;
426 default:
427 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
428 return 0;
429 }
430
431 if (is_multi) {
432 /* in case of multiple IEs stored in buffer */
433 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
434 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
435 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
436 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
437 } else {
438 /* in case of single IE */
439 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
440 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
441 }
442
443 if (!old_ie || !new_ie)
444 ret = !old_ie && !new_ie;
445 else
446 ret = (old_ie_len == new_ie_len &&
447 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
448
449 wpabuf_free(old_ie_buff);
450 wpabuf_free(new_ie_buff);
451
452 return ret;
453}
454
455
456static u32 wpa_bss_compare_res(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700457 const struct wpa_scan_res *new_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458{
459 u32 changes = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700460 int caps_diff = old->caps ^ new_res->caps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700461
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700462 if (old->freq != new_res->freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
464
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700465 if (old->level != new_res->level)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
467
468 if (caps_diff & IEEE80211_CAP_PRIVACY)
469 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
470
471 if (caps_diff & IEEE80211_CAP_IBSS)
472 changes |= WPA_BSS_MODE_CHANGED_FLAG;
473
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700474 if (old->ie_len == new_res->ie_len &&
475 os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 return changes;
477 changes |= WPA_BSS_IES_CHANGED_FLAG;
478
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700479 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
481
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700482 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
484
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700485 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 changes |= WPA_BSS_WPS_CHANGED_FLAG;
487
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700488 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
489 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 changes |= WPA_BSS_RATES_CHANGED_FLAG;
491
492 return changes;
493}
494
495
496static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
497 const struct wpa_bss *bss)
498{
499 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
500 wpas_notify_bss_freq_changed(wpa_s, bss->id);
501
502 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
503 wpas_notify_bss_signal_changed(wpa_s, bss->id);
504
505 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
506 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
507
508 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
509 wpas_notify_bss_mode_changed(wpa_s, bss->id);
510
511 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
512 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
513
514 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
515 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
516
517 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
518 wpas_notify_bss_wps_changed(wpa_s, bss->id);
519
520 if (changes & WPA_BSS_IES_CHANGED_FLAG)
521 wpas_notify_bss_ies_changed(wpa_s, bss->id);
522
523 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
524 wpas_notify_bss_rates_changed(wpa_s, bss->id);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700525
526 wpas_notify_bss_seen(wpa_s, bss->id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527}
528
529
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700530static struct wpa_bss *
531wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800532 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533{
534 u32 changes;
535
536 changes = wpa_bss_compare_res(bss, res);
537 bss->scan_miss_count = 0;
538 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800539 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 /* Move the entry to the end of the list */
541 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700542#ifdef CONFIG_P2P
543 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
544 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
545 /*
546 * This can happen when non-P2P station interface runs a scan
547 * without P2P IE in the Probe Request frame. P2P GO would reply
548 * to that with a Probe Response that does not include P2P IE.
549 * Do not update the IEs in this BSS entry to avoid such loss of
550 * information that may be needed for P2P operations to
551 * determine group information.
552 */
553 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
554 MACSTR " since that would remove P2P IE information",
555 MAC2STR(bss->bssid));
556 } else
557#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558 if (bss->ie_len + bss->beacon_ie_len >=
559 res->ie_len + res->beacon_ie_len) {
560 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
561 bss->ie_len = res->ie_len;
562 bss->beacon_ie_len = res->beacon_ie_len;
563 } else {
564 struct wpa_bss *nbss;
565 struct dl_list *prev = bss->list_id.prev;
566 dl_list_del(&bss->list_id);
567 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
568 res->beacon_ie_len);
569 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700570 unsigned int i;
571 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
572 if (wpa_s->last_scan_res[i] == bss) {
573 wpa_s->last_scan_res[i] = nbss;
574 break;
575 }
576 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700577 if (wpa_s->current_bss == bss)
578 wpa_s->current_bss = nbss;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800579 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 bss = nbss;
581 os_memcpy(bss + 1, res + 1,
582 res->ie_len + res->beacon_ie_len);
583 bss->ie_len = res->ie_len;
584 bss->beacon_ie_len = res->beacon_ie_len;
585 }
586 dl_list_add(prev, &bss->list_id);
587 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700588 if (changes & WPA_BSS_IES_CHANGED_FLAG)
589 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 dl_list_add_tail(&wpa_s->bss, &bss->list);
591
592 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700593
594 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595}
596
597
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800598/**
599 * wpa_bss_update_start - Start a BSS table update from scan results
600 * @wpa_s: Pointer to wpa_supplicant data
601 *
602 * This function is called at the start of each BSS table update round for new
603 * scan results. The actual scan result entries are indicated with calls to
604 * wpa_bss_update_scan_res() and the update round is finished with a call to
605 * wpa_bss_update_end().
606 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
608{
609 wpa_s->bss_update_idx++;
610 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
611 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700612 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613}
614
615
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800616/**
617 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
618 * @wpa_s: Pointer to wpa_supplicant data
619 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800620 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800621 *
622 * This function updates a BSS table entry (or adds one) based on a scan result.
623 * This is called separately for each scan result between the calls to
624 * wpa_bss_update_start() and wpa_bss_update_end().
625 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800627 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800628 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800630 const u8 *ssid, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631 struct wpa_bss *bss;
632
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700633 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800634 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700635 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800636 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
637 struct os_reltime age;
638 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
639 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700640 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
641 "table entry that is %u.%06u seconds older "
642 "than our scan trigger",
643 (unsigned int) age.sec,
644 (unsigned int) age.usec);
645 return;
646 }
647 }
648
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
650 if (ssid == NULL) {
651 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
652 MACSTR, MAC2STR(res->bssid));
653 return;
654 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700655 if (ssid[1] > SSID_MAX_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
657 MACSTR, MAC2STR(res->bssid));
658 return;
659 }
660
661 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700662#ifdef CONFIG_P2P
663 if (p2p == NULL &&
664 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
665 /*
666 * If it's a P2P specific interface, then don't update
667 * the scan result without a P2P IE.
668 */
669 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
670 " update for P2P interface", MAC2STR(res->bssid));
671 return;
672 }
673#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700674 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
675 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
676 return; /* Skip P2P listen discovery results here */
677
678 /* TODO: add option for ignoring BSSes we are not interested in
679 * (to save memory) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800680
681 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700682 if (mesh && mesh[1] <= SSID_MAX_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800683 ssid = mesh;
684
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
686 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800687 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700688 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800689 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700690 if (wpa_s->last_scan_res) {
691 unsigned int i;
692 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
693 if (bss == wpa_s->last_scan_res[i]) {
694 /* Already in the list */
695 return;
696 }
697 }
698 }
699 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700700
701 if (bss == NULL)
702 return;
703 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
704 struct wpa_bss **n;
705 unsigned int siz;
706 if (wpa_s->last_scan_res_size == 0)
707 siz = 32;
708 else
709 siz = wpa_s->last_scan_res_size * 2;
710 n = os_realloc_array(wpa_s->last_scan_res, siz,
711 sizeof(struct wpa_bss *));
712 if (n == NULL)
713 return;
714 wpa_s->last_scan_res = n;
715 wpa_s->last_scan_res_size = siz;
716 }
717
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700718 if (wpa_s->last_scan_res)
719 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720}
721
722
723static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
724 const struct scan_info *info)
725{
726 int found;
727 size_t i;
728
729 if (info == NULL)
730 return 1;
731
732 if (info->num_freqs) {
733 found = 0;
734 for (i = 0; i < info->num_freqs; i++) {
735 if (bss->freq == info->freqs[i]) {
736 found = 1;
737 break;
738 }
739 }
740 if (!found)
741 return 0;
742 }
743
744 if (info->num_ssids) {
745 found = 0;
746 for (i = 0; i < info->num_ssids; i++) {
747 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
748 if ((s->ssid == NULL || s->ssid_len == 0) ||
749 (s->ssid_len == bss->ssid_len &&
750 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
751 0)) {
752 found = 1;
753 break;
754 }
755 }
756 if (!found)
757 return 0;
758 }
759
760 return 1;
761}
762
763
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800764/**
765 * wpa_bss_update_end - End a BSS table update from scan results
766 * @wpa_s: Pointer to wpa_supplicant data
767 * @info: Information about scan parameters
768 * @new_scan: Whether this update round was based on a new scan
769 *
770 * This function is called at the end of each BSS table update round for new
771 * scan results. The start of the update was indicated with a call to
772 * wpa_bss_update_start().
773 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
775 int new_scan)
776{
777 struct wpa_bss *bss, *n;
778
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800779 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 if (!new_scan)
781 return; /* do not expire entries without new scan */
782
783 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
784 if (wpa_bss_in_use(wpa_s, bss))
785 continue;
786 if (!wpa_bss_included_in_scan(bss, info))
787 continue; /* expire only BSSes that were scanned */
788 if (bss->last_update_idx < wpa_s->bss_update_idx)
789 bss->scan_miss_count++;
790 if (bss->scan_miss_count >=
791 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700792 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 }
794 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700795
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800796 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
797 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798}
799
800
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800801/**
802 * wpa_bss_flush_by_age - Flush old BSS entries
803 * @wpa_s: Pointer to wpa_supplicant data
804 * @age: Maximum entry age in seconds
805 *
806 * Remove BSS entries that have not been updated during the last @age seconds.
807 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
809{
810 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800811 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812
813 if (dl_list_empty(&wpa_s->bss))
814 return;
815
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800816 os_get_reltime(&t);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 t.sec -= age;
818
819 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
820 if (wpa_bss_in_use(wpa_s, bss))
821 continue;
822
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800823 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700824 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700825 } else
826 break;
827 }
828}
829
830
831static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
832{
833 struct wpa_supplicant *wpa_s = eloop_ctx;
834
835 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
836 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
837 wpa_bss_timeout, wpa_s, NULL);
838}
839
840
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800841/**
842 * wpa_bss_init - Initialize BSS table
843 * @wpa_s: Pointer to wpa_supplicant data
844 * Returns: 0 on success, -1 on failure
845 *
846 * This prepares BSS table lists and timer for periodic updates. The BSS table
847 * is deinitialized with wpa_bss_deinit() once not needed anymore.
848 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849int wpa_bss_init(struct wpa_supplicant *wpa_s)
850{
851 dl_list_init(&wpa_s->bss);
852 dl_list_init(&wpa_s->bss_id);
853 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
854 wpa_bss_timeout, wpa_s, NULL);
855 return 0;
856}
857
858
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800859/**
860 * wpa_bss_flush - Flush all unused BSS entries
861 * @wpa_s: Pointer to wpa_supplicant data
862 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863void wpa_bss_flush(struct wpa_supplicant *wpa_s)
864{
865 struct wpa_bss *bss, *n;
866
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800867 wpa_s->clear_driver_scan_cache = 1;
868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 if (wpa_s->bss.next == NULL)
870 return; /* BSS table not yet initialized */
871
872 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
873 if (wpa_bss_in_use(wpa_s, bss))
874 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700875 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 }
877}
878
879
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800880/**
881 * wpa_bss_deinit - Deinitialize BSS table
882 * @wpa_s: Pointer to wpa_supplicant data
883 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
885{
886 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
887 wpa_bss_flush(wpa_s);
888}
889
890
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800891/**
892 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
893 * @wpa_s: Pointer to wpa_supplicant data
894 * @bssid: BSSID
895 * Returns: Pointer to the BSS entry or %NULL if not found
896 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
898 const u8 *bssid)
899{
900 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700901 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
902 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
904 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
905 return bss;
906 }
907 return NULL;
908}
909
910
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700911/**
912 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
913 * @wpa_s: Pointer to wpa_supplicant data
914 * @bssid: BSSID
915 * Returns: Pointer to the BSS entry or %NULL if not found
916 *
917 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
918 * find the entry that has the most recent update. This can help in finding the
919 * correct entry in cases where the SSID of the AP may have changed recently
920 * (e.g., in WPS reconfiguration cases).
921 */
922struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
923 const u8 *bssid)
924{
925 struct wpa_bss *bss, *found = NULL;
926 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
927 return NULL;
928 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
929 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
930 continue;
931 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800932 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700933 found = bss;
934 }
935 return found;
936}
937
938
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800939#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800940/**
941 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
942 * @wpa_s: Pointer to wpa_supplicant data
943 * @dev_addr: P2P Device Address of the GO
944 * Returns: Pointer to the BSS entry or %NULL if not found
945 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800946struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
947 const u8 *dev_addr)
948{
949 struct wpa_bss *bss;
950 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
951 u8 addr[ETH_ALEN];
952 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
953 addr) == 0 &&
954 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
955 return bss;
956 }
957 return NULL;
958}
959#endif /* CONFIG_P2P */
960
961
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800962/**
963 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
964 * @wpa_s: Pointer to wpa_supplicant data
965 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
966 * Returns: Pointer to the BSS entry or %NULL if not found
967 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
969{
970 struct wpa_bss *bss;
971 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
972 if (bss->id == id)
973 return bss;
974 }
975 return NULL;
976}
977
978
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800979/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800980 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
981 * @wpa_s: Pointer to wpa_supplicant data
982 * @idf: Smallest allowed identifier assigned for the entry
983 * @idf: Largest allowed identifier assigned for the entry
984 * Returns: Pointer to the BSS entry or %NULL if not found
985 *
986 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
987 * smallest id value to be fetched within the specified range without the
988 * caller having to know the exact id.
989 */
990struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
991 unsigned int idf, unsigned int idl)
992{
993 struct wpa_bss *bss;
994 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
995 if (bss->id >= idf && bss->id <= idl)
996 return bss;
997 }
998 return NULL;
999}
1000
1001
1002/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001003 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1004 * @bss: BSS table entry
1005 * @ie: Information element identitifier (WLAN_EID_*)
1006 * Returns: Pointer to the information element (id field) or %NULL if not found
1007 *
1008 * This function returns the first matching information element in the BSS
1009 * entry.
1010 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1012{
1013 const u8 *end, *pos;
1014
1015 pos = (const u8 *) (bss + 1);
1016 end = pos + bss->ie_len;
1017
1018 while (pos + 1 < end) {
1019 if (pos + 2 + pos[1] > end)
1020 break;
1021 if (pos[0] == ie)
1022 return pos;
1023 pos += 2 + pos[1];
1024 }
1025
1026 return NULL;
1027}
1028
1029
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001030/**
1031 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1032 * @bss: BSS table entry
1033 * @vendor_type: Vendor type (four octets starting the IE payload)
1034 * Returns: Pointer to the information element (id field) or %NULL if not found
1035 *
1036 * This function returns the first matching information element in the BSS
1037 * entry.
1038 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001039const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1040{
1041 const u8 *end, *pos;
1042
1043 pos = (const u8 *) (bss + 1);
1044 end = pos + bss->ie_len;
1045
1046 while (pos + 1 < end) {
1047 if (pos + 2 + pos[1] > end)
1048 break;
1049 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1050 vendor_type == WPA_GET_BE32(&pos[2]))
1051 return pos;
1052 pos += 2 + pos[1];
1053 }
1054
1055 return NULL;
1056}
1057
1058
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001059/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001060 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1061 * @bss: BSS table entry
1062 * @vendor_type: Vendor type (four octets starting the IE payload)
1063 * Returns: Pointer to the information element (id field) or %NULL if not found
1064 *
1065 * This function returns the first matching information element in the BSS
1066 * entry.
1067 *
1068 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1069 * from Beacon frames instead of either Beacon or Probe Response frames.
1070 */
1071const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1072 u32 vendor_type)
1073{
1074 const u8 *end, *pos;
1075
1076 if (bss->beacon_ie_len == 0)
1077 return NULL;
1078
1079 pos = (const u8 *) (bss + 1);
1080 pos += bss->ie_len;
1081 end = pos + bss->beacon_ie_len;
1082
1083 while (pos + 1 < end) {
1084 if (pos + 2 + pos[1] > end)
1085 break;
1086 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1087 vendor_type == WPA_GET_BE32(&pos[2]))
1088 return pos;
1089 pos += 2 + pos[1];
1090 }
1091
1092 return NULL;
1093}
1094
1095
1096/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001097 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1098 * @bss: BSS table entry
1099 * @vendor_type: Vendor type (four octets starting the IE payload)
1100 * Returns: Pointer to the information element payload or %NULL if not found
1101 *
1102 * This function returns concatenated payload of possibly fragmented vendor
1103 * specific information elements in the BSS entry. The caller is responsible for
1104 * freeing the returned buffer.
1105 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1107 u32 vendor_type)
1108{
1109 struct wpabuf *buf;
1110 const u8 *end, *pos;
1111
1112 buf = wpabuf_alloc(bss->ie_len);
1113 if (buf == NULL)
1114 return NULL;
1115
1116 pos = (const u8 *) (bss + 1);
1117 end = pos + bss->ie_len;
1118
1119 while (pos + 1 < end) {
1120 if (pos + 2 + pos[1] > end)
1121 break;
1122 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1123 vendor_type == WPA_GET_BE32(&pos[2]))
1124 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1125 pos += 2 + pos[1];
1126 }
1127
1128 if (wpabuf_len(buf) == 0) {
1129 wpabuf_free(buf);
1130 buf = NULL;
1131 }
1132
1133 return buf;
1134}
1135
1136
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001137/**
1138 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1139 * @bss: BSS table entry
1140 * @vendor_type: Vendor type (four octets starting the IE payload)
1141 * Returns: Pointer to the information element payload or %NULL if not found
1142 *
1143 * This function returns concatenated payload of possibly fragmented vendor
1144 * specific information elements in the BSS entry. The caller is responsible for
1145 * freeing the returned buffer.
1146 *
1147 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1148 * from Beacon frames instead of either Beacon or Probe Response frames.
1149 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001150struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1151 u32 vendor_type)
1152{
1153 struct wpabuf *buf;
1154 const u8 *end, *pos;
1155
1156 buf = wpabuf_alloc(bss->beacon_ie_len);
1157 if (buf == NULL)
1158 return NULL;
1159
1160 pos = (const u8 *) (bss + 1);
1161 pos += bss->ie_len;
1162 end = pos + bss->beacon_ie_len;
1163
1164 while (pos + 1 < end) {
1165 if (pos + 2 + pos[1] > end)
1166 break;
1167 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1168 vendor_type == WPA_GET_BE32(&pos[2]))
1169 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1170 pos += 2 + pos[1];
1171 }
1172
1173 if (wpabuf_len(buf) == 0) {
1174 wpabuf_free(buf);
1175 buf = NULL;
1176 }
1177
1178 return buf;
1179}
1180
1181
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001182/**
1183 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1184 * @bss: BSS table entry
1185 * Returns: Maximum legacy rate in units of 500 kbps
1186 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001187int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1188{
1189 int rate = 0;
1190 const u8 *ie;
1191 int i;
1192
1193 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1194 for (i = 0; ie && i < ie[1]; i++) {
1195 if ((ie[i + 2] & 0x7f) > rate)
1196 rate = ie[i + 2] & 0x7f;
1197 }
1198
1199 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1200 for (i = 0; ie && i < ie[1]; i++) {
1201 if ((ie[i + 2] & 0x7f) > rate)
1202 rate = ie[i + 2] & 0x7f;
1203 }
1204
1205 return rate;
1206}
1207
1208
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001209/**
1210 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1211 * @bss: BSS table entry
1212 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1213 * Returns: number of legacy TX rates or -1 on failure
1214 *
1215 * The caller is responsible for freeing the returned buffer with os_free() in
1216 * case of success.
1217 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1219{
1220 const u8 *ie, *ie2;
1221 int i, j;
1222 unsigned int len;
1223 u8 *r;
1224
1225 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1226 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1227
1228 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1229
1230 r = os_malloc(len);
1231 if (!r)
1232 return -1;
1233
1234 for (i = 0; ie && i < ie[1]; i++)
1235 r[i] = ie[i + 2] & 0x7f;
1236
1237 for (j = 0; ie2 && j < ie2[1]; j++)
1238 r[i + j] = ie2[j + 2] & 0x7f;
1239
1240 *rates = r;
1241 return len;
1242}