blob: 0f986bbcad8121a0e91eb577393d4471503b77d1 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * BSS table
Hai Shalom74f70d42019-02-11 14:42:39 -08003 * Copyright (c) 2009-2019, 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
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070022static void wpa_bss_set_hessid(struct wpa_bss *bss)
23{
24#ifdef CONFIG_INTERWORKING
25 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
26 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
27 os_memset(bss->hessid, 0, ETH_ALEN);
28 return;
29 }
30 if (ie[1] == 7)
31 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
32 else
33 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
34#endif /* CONFIG_INTERWORKING */
35}
36
37
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080038/**
39 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
40 * Returns: Allocated ANQP data structure or %NULL on failure
41 *
42 * The allocated ANQP data structure has its users count set to 1. It may be
43 * shared by multiple BSS entries and each shared entry is freed with
44 * wpa_bss_anqp_free().
45 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070046struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
47{
48 struct wpa_bss_anqp *anqp;
49 anqp = os_zalloc(sizeof(*anqp));
50 if (anqp == NULL)
51 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052#ifdef CONFIG_INTERWORKING
53 dl_list_init(&anqp->anqp_elems);
54#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070055 anqp->users = 1;
56 return anqp;
57}
58
59
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080060/**
61 * wpa_bss_anqp_clone - Clone an ANQP data structure
62 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
63 * Returns: Cloned ANQP data structure or %NULL on failure
64 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080065static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
66{
67 struct wpa_bss_anqp *n;
68
69 n = os_zalloc(sizeof(*n));
70 if (n == NULL)
71 return NULL;
72
73#define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
74#ifdef CONFIG_INTERWORKING
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080075 dl_list_init(&n->anqp_elems);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080076 ANQP_DUP(capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080077 ANQP_DUP(venue_name);
78 ANQP_DUP(network_auth_type);
79 ANQP_DUP(roaming_consortium);
80 ANQP_DUP(ip_addr_type_availability);
81 ANQP_DUP(nai_realm);
82 ANQP_DUP(anqp_3gpp);
83 ANQP_DUP(domain_name);
Dmitry Shmidt29333592017-01-09 12:27:11 -080084 ANQP_DUP(fils_realm_info);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080085#endif /* CONFIG_INTERWORKING */
86#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -080087 ANQP_DUP(hs20_capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080088 ANQP_DUP(hs20_operator_friendly_name);
89 ANQP_DUP(hs20_wan_metrics);
90 ANQP_DUP(hs20_connection_capability);
91 ANQP_DUP(hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080092 ANQP_DUP(hs20_osu_providers_list);
Roshan Pius3a1667e2018-07-03 15:17:14 -070093 ANQP_DUP(hs20_operator_icon_metadata);
Hai Shalom39ba6fc2019-01-22 12:40:38 -080094 ANQP_DUP(hs20_osu_providers_nai_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080095#endif /* CONFIG_HS20 */
96#undef ANQP_DUP
97
98 return n;
99}
100
101
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800102/**
103 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
104 * @bss: BSS entry
105 * Returns: 0 on success, -1 on failure
106 *
107 * This function ensures the specific BSS entry has an ANQP data structure that
108 * is not shared with any other BSS entry.
109 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800110int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
111{
112 struct wpa_bss_anqp *anqp;
113
114 if (bss->anqp && bss->anqp->users > 1) {
115 /* allocated, but shared - clone an unshared copy */
116 anqp = wpa_bss_anqp_clone(bss->anqp);
117 if (anqp == NULL)
118 return -1;
119 anqp->users = 1;
120 bss->anqp->users--;
121 bss->anqp = anqp;
122 return 0;
123 }
124
125 if (bss->anqp)
126 return 0; /* already allocated and not shared */
127
128 /* not allocated - allocate a new storage area */
129 bss->anqp = wpa_bss_anqp_alloc();
130 return bss->anqp ? 0 : -1;
131}
132
133
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800134/**
135 * wpa_bss_anqp_free - Free an ANQP data structure
136 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
137 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700138static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
139{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800140#ifdef CONFIG_INTERWORKING
141 struct wpa_bss_anqp_elem *elem;
142#endif /* CONFIG_INTERWORKING */
143
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700144 if (anqp == NULL)
145 return;
146
147 anqp->users--;
148 if (anqp->users > 0) {
149 /* Another BSS entry holds a pointer to this ANQP info */
150 return;
151 }
152
153#ifdef CONFIG_INTERWORKING
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800154 wpabuf_free(anqp->capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700155 wpabuf_free(anqp->venue_name);
156 wpabuf_free(anqp->network_auth_type);
157 wpabuf_free(anqp->roaming_consortium);
158 wpabuf_free(anqp->ip_addr_type_availability);
159 wpabuf_free(anqp->nai_realm);
160 wpabuf_free(anqp->anqp_3gpp);
161 wpabuf_free(anqp->domain_name);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800162 wpabuf_free(anqp->fils_realm_info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800163
164 while ((elem = dl_list_first(&anqp->anqp_elems,
165 struct wpa_bss_anqp_elem, list))) {
166 dl_list_del(&elem->list);
167 wpabuf_free(elem->payload);
168 os_free(elem);
169 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700170#endif /* CONFIG_INTERWORKING */
171#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800172 wpabuf_free(anqp->hs20_capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700173 wpabuf_free(anqp->hs20_operator_friendly_name);
174 wpabuf_free(anqp->hs20_wan_metrics);
175 wpabuf_free(anqp->hs20_connection_capability);
176 wpabuf_free(anqp->hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800177 wpabuf_free(anqp->hs20_osu_providers_list);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700178 wpabuf_free(anqp->hs20_operator_icon_metadata);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800179 wpabuf_free(anqp->hs20_osu_providers_nai_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700180#endif /* CONFIG_HS20 */
181
182 os_free(anqp);
183}
184
185
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800186static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
187 struct wpa_bss *old_bss,
188 struct wpa_bss *new_bss)
189{
190 struct wpa_radio_work *work;
191 struct wpa_connect_work *cwork;
192
193 work = radio_work_pending(wpa_s, "sme-connect");
194 if (!work)
195 work = radio_work_pending(wpa_s, "connect");
196 if (!work)
197 return;
198
199 cwork = work->ctx;
200 if (cwork->bss != old_bss)
201 return;
202
203 wpa_printf(MSG_DEBUG,
204 "Update BSS pointer for the pending connect radio work");
205 cwork->bss = new_bss;
206 if (!new_bss)
207 cwork->bss_removed = 1;
208}
209
210
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800211void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
212 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700214 if (wpa_s->last_scan_res) {
215 unsigned int i;
216 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
217 if (wpa_s->last_scan_res[i] == bss) {
218 os_memmove(&wpa_s->last_scan_res[i],
219 &wpa_s->last_scan_res[i + 1],
220 (wpa_s->last_scan_res_used - i - 1)
221 * sizeof(struct wpa_bss *));
222 wpa_s->last_scan_res_used--;
223 break;
224 }
225 }
226 }
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800227 wpa_bss_update_pending_connect(wpa_s, bss, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 dl_list_del(&bss->list);
229 dl_list_del(&bss->list_id);
230 wpa_s->num_bss--;
231 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700232 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
233 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700235 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 os_free(bss);
237}
238
239
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800240/**
241 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
242 * @wpa_s: Pointer to wpa_supplicant data
Matthew Wangdcf19452022-11-07 20:42:52 -0800243 * @bssid: BSSID, or %NULL to match any BSSID
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800244 * @ssid: SSID
245 * @ssid_len: Length of @ssid
246 * Returns: Pointer to the BSS entry or %NULL if not found
247 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
249 const u8 *ssid, size_t ssid_len)
250{
251 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700252 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
253 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Matthew Wangdcf19452022-11-07 20:42:52 -0800255 if ((!bssid ||
256 os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 bss->ssid_len == ssid_len &&
258 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
259 return bss;
260 }
261 return NULL;
262}
263
264
Dmitry Shmidt29333592017-01-09 12:27:11 -0800265void calculate_update_time(const struct os_reltime *fetch_time,
266 unsigned int age_ms,
267 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268{
269 os_time_t usec;
270
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700271 update_time->sec = fetch_time->sec;
272 update_time->usec = fetch_time->usec;
273 update_time->sec -= age_ms / 1000;
274 usec = (age_ms % 1000) * 1000;
275 if (update_time->usec < usec) {
276 update_time->sec--;
277 update_time->usec += 1000000;
278 }
279 update_time->usec -= usec;
280}
281
282
283static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800284 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700285{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 dst->flags = src->flags;
287 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
288 dst->freq = src->freq;
289 dst->beacon_int = src->beacon_int;
290 dst->caps = src->caps;
291 dst->qual = src->qual;
292 dst->noise = src->noise;
293 dst->level = src->level;
294 dst->tsf = src->tsf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700295 dst->beacon_newer = src->beacon_newer;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800296 dst->est_throughput = src->est_throughput;
297 dst->snr = src->snr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700299 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300}
301
302
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700303static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
304 struct wpa_bss *bss)
305{
306#ifdef CONFIG_WPS
307 struct wpa_ssid *ssid;
308 struct wpabuf *wps_ie;
309 int pbc = 0, ret;
310
311 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
312 if (!wps_ie)
313 return 0;
314
315 if (wps_is_selected_pbc_registrar(wps_ie)) {
316 pbc = 1;
317 } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
318 wpabuf_free(wps_ie);
319 return 0;
320 }
321
322 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
323 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
324 continue;
325 if (ssid->ssid_len &&
326 (ssid->ssid_len != bss->ssid_len ||
327 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
328 continue;
329
330 if (pbc)
331 ret = eap_is_wps_pbc_enrollee(&ssid->eap);
332 else
333 ret = eap_is_wps_pin_enrollee(&ssid->eap);
334 wpabuf_free(wps_ie);
335 return ret;
336 }
337 wpabuf_free(wps_ie);
338#endif /* CONFIG_WPS */
339
340 return 0;
341}
342
343
Hai Shalom60840252021-02-19 19:02:11 -0800344static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s,
345 struct wpa_bss *bss)
346{
347#ifdef CONFIG_P2P
348 u8 addr[ETH_ALEN];
349
350 if (os_memcmp(bss->bssid, wpa_s->pending_join_iface_addr,
351 ETH_ALEN) == 0)
352 return true;
353 if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) &&
354 p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 &&
355 os_memcmp(addr, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0)
356 return true;
357#endif /* CONFIG_P2P */
358 return false;
359}
360
361
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800362static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
363{
364 struct wpa_ssid *ssid;
365
Hai Shalom60840252021-02-19 19:02:11 -0800366 if (is_p2p_pending_bss(wpa_s, bss))
367 return 1;
368
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800369 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
370 if (ssid->ssid == NULL || ssid->ssid_len == 0)
371 continue;
372 if (ssid->ssid_len == bss->ssid_len &&
373 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
374 return 1;
375 }
376
377 return 0;
378}
379
380
Dmitry Shmidt04949592012-07-19 12:16:46 -0700381static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
382{
Sunil Ravi89eba102022-09-13 21:04:37 -0700383 int i;
384
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800385 if (bss == wpa_s->current_bss)
386 return 1;
387
388 if (wpa_s->current_bss &&
389 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
390 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
391 bss->ssid_len) != 0))
392 return 0; /* SSID has changed */
393
Sunil Ravi89eba102022-09-13 21:04:37 -0700394 if (!is_zero_ether_addr(bss->bssid) &&
395 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
396 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0))
397 return 1;
398
399 if (!wpa_s->valid_links)
400 return 0;
401
402 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
403 if (!(wpa_s->valid_links & BIT(i)))
404 continue;
405
406 if (os_memcmp(bss->bssid, wpa_s->links[i].bssid, ETH_ALEN) == 0)
407 return 1;
408 }
409
410 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700411}
412
413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
415{
416 struct wpa_bss *bss;
417
418 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700419 if (!wpa_bss_known(wpa_s, bss) &&
420 !wpa_bss_is_wps_candidate(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700421 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800422 return 0;
423 }
424 }
425
426 return -1;
427}
428
429
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700432 struct wpa_bss *bss;
433
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800434 /*
435 * Remove the oldest entry that does not match with any configured
436 * network.
437 */
438 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700439 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800440
441 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700442 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800443 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700444 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
445 if (!wpa_bss_in_use(wpa_s, bss)) {
446 wpa_bss_remove(wpa_s, bss, __func__);
447 return 0;
448 }
449 }
450
451 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800452}
453
454
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700455static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
456 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800457 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800458 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459{
460 struct wpa_bss *bss;
Sunil Ravi89eba102022-09-13 21:04:37 -0700461 char extra[100];
462 const u8 *ml_ie;
463 char *pos, *end;
464 int ret = 0;
465 const u8 *mld_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466
467 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
468 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700469 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 bss->id = wpa_s->bss_next_id++;
471 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800472 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 os_memcpy(bss->ssid, ssid, ssid_len);
474 bss->ssid_len = ssid_len;
475 bss->ie_len = res->ie_len;
476 bss->beacon_ie_len = res->beacon_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -0800477 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700478 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479
Sunil Ravi89eba102022-09-13 21:04:37 -0700480 os_memset(bss->mld_addr, 0, ETH_ALEN);
481 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
482 if (ml_ie) {
483 mld_addr = get_basic_mle_mld_addr(&ml_ie[3], ml_ie[1] - 1);
484 if (mld_addr)
485 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
486 }
487
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300488 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
489 wpa_bss_remove_oldest(wpa_s) != 0) {
490 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
491 "because all BSSes are in use. We should normally "
492 "not get here!", (int) wpa_s->num_bss + 1);
493 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
494 }
495
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 dl_list_add_tail(&wpa_s->bss, &bss->list);
497 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
498 wpa_s->num_bss++;
Sunil Ravi89eba102022-09-13 21:04:37 -0700499
500 extra[0] = '\0';
501 pos = extra;
502 end = pos + sizeof(extra);
Hai Shalom81f62d82019-07-22 12:10:00 -0700503 if (!is_zero_ether_addr(bss->hessid))
Sunil Ravi89eba102022-09-13 21:04:37 -0700504 ret = os_snprintf(pos, end - pos, " HESSID " MACSTR,
505 MAC2STR(bss->hessid));
506
507 if (!is_zero_ether_addr(bss->mld_addr) &&
508 !os_snprintf_error(end - pos, ret)) {
509 pos += ret;
510 ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR,
511 MAC2STR(bss->mld_addr));
512 }
513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
Hai Shalom81f62d82019-07-22 12:10:00 -0700515 " SSID '%s' freq %d%s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800516 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
Hai Shalom81f62d82019-07-22 12:10:00 -0700517 bss->freq, extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700519 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700520}
521
522
523static int are_ies_equal(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700524 const struct wpa_scan_res *new_res, u32 ie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525{
526 const u8 *old_ie, *new_ie;
527 struct wpabuf *old_ie_buff = NULL;
528 struct wpabuf *new_ie_buff = NULL;
529 int new_ie_len, old_ie_len, ret, is_multi;
530
531 switch (ie) {
532 case WPA_IE_VENDOR_TYPE:
533 old_ie = wpa_bss_get_vendor_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700534 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 is_multi = 0;
536 break;
537 case WPS_IE_VENDOR_TYPE:
538 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700539 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 is_multi = 1;
541 break;
542 case WLAN_EID_RSN:
543 case WLAN_EID_SUPP_RATES:
544 case WLAN_EID_EXT_SUPP_RATES:
545 old_ie = wpa_bss_get_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700546 new_ie = wpa_scan_get_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 is_multi = 0;
548 break;
549 default:
550 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
551 return 0;
552 }
553
554 if (is_multi) {
555 /* in case of multiple IEs stored in buffer */
556 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
557 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
558 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
559 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
560 } else {
561 /* in case of single IE */
562 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
563 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
564 }
565
566 if (!old_ie || !new_ie)
567 ret = !old_ie && !new_ie;
568 else
569 ret = (old_ie_len == new_ie_len &&
570 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
571
572 wpabuf_free(old_ie_buff);
573 wpabuf_free(new_ie_buff);
574
575 return ret;
576}
577
578
579static u32 wpa_bss_compare_res(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700580 const struct wpa_scan_res *new_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581{
582 u32 changes = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700583 int caps_diff = old->caps ^ new_res->caps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700585 if (old->freq != new_res->freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
587
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700588 if (old->level != new_res->level)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
590
591 if (caps_diff & IEEE80211_CAP_PRIVACY)
592 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
593
594 if (caps_diff & IEEE80211_CAP_IBSS)
595 changes |= WPA_BSS_MODE_CHANGED_FLAG;
596
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700597 if (old->ie_len == new_res->ie_len &&
Hai Shalom60840252021-02-19 19:02:11 -0800598 os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 return changes;
600 changes |= WPA_BSS_IES_CHANGED_FLAG;
601
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700602 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
604
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700605 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
607
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700608 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 changes |= WPA_BSS_WPS_CHANGED_FLAG;
610
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700611 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
612 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 changes |= WPA_BSS_RATES_CHANGED_FLAG;
614
615 return changes;
616}
617
618
Hai Shalom60840252021-02-19 19:02:11 -0800619void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
620 const struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621{
622 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
623 wpas_notify_bss_freq_changed(wpa_s, bss->id);
624
625 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
626 wpas_notify_bss_signal_changed(wpa_s, bss->id);
627
628 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
629 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
630
631 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
632 wpas_notify_bss_mode_changed(wpa_s, bss->id);
633
634 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
635 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
636
637 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
638 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
639
640 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
641 wpas_notify_bss_wps_changed(wpa_s, bss->id);
642
643 if (changes & WPA_BSS_IES_CHANGED_FLAG)
644 wpas_notify_bss_ies_changed(wpa_s, bss->id);
645
646 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
647 wpas_notify_bss_rates_changed(wpa_s, bss->id);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700648
649 wpas_notify_bss_seen(wpa_s, bss->id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650}
651
652
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700653static struct wpa_bss *
654wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800655 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700656{
657 u32 changes;
658
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800659 if (bss->last_update_idx == wpa_s->bss_update_idx) {
660 struct os_reltime update_time;
661
662 /*
663 * Some drivers (e.g., cfg80211) include multiple BSS entries
664 * for the same BSS if that BSS's channel changes. The BSS list
665 * implementation in wpa_supplicant does not do that and we need
666 * to filter out the obsolete results here to make sure only the
667 * most current BSS information remains in the table.
668 */
669 wpa_printf(MSG_DEBUG, "BSS: " MACSTR
670 " has multiple entries in the scan results - select the most current one",
671 MAC2STR(bss->bssid));
672 calculate_update_time(fetch_time, res->age, &update_time);
673 wpa_printf(MSG_DEBUG,
674 "Previous last_update: %u.%06u (freq %d%s)",
675 (unsigned int) bss->last_update.sec,
676 (unsigned int) bss->last_update.usec,
677 bss->freq,
678 (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
679 wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
680 (unsigned int) update_time.sec,
681 (unsigned int) update_time.usec,
682 res->freq,
683 (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
684 if ((bss->flags & WPA_BSS_ASSOCIATED) ||
685 (!(res->flags & WPA_SCAN_ASSOCIATED) &&
686 !os_reltime_before(&bss->last_update, &update_time))) {
687 wpa_printf(MSG_DEBUG,
688 "Ignore this BSS entry since the previous update looks more current");
689 return bss;
690 }
691 wpa_printf(MSG_DEBUG,
692 "Accept this BSS entry since it looks more current than the previous update");
693 }
694
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 changes = wpa_bss_compare_res(bss, res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800696 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
697 wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
698 MAC2STR(bss->bssid), bss->freq, res->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 bss->scan_miss_count = 0;
700 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800701 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702 /* Move the entry to the end of the list */
703 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700704#ifdef CONFIG_P2P
705 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
706 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
707 /*
708 * This can happen when non-P2P station interface runs a scan
709 * without P2P IE in the Probe Request frame. P2P GO would reply
710 * to that with a Probe Response that does not include P2P IE.
711 * Do not update the IEs in this BSS entry to avoid such loss of
712 * information that may be needed for P2P operations to
713 * determine group information.
714 */
715 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
716 MACSTR " since that would remove P2P IE information",
717 MAC2STR(bss->bssid));
718 } else
719#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 if (bss->ie_len + bss->beacon_ie_len >=
721 res->ie_len + res->beacon_ie_len) {
Hai Shalom60840252021-02-19 19:02:11 -0800722 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 bss->ie_len = res->ie_len;
724 bss->beacon_ie_len = res->beacon_ie_len;
725 } else {
726 struct wpa_bss *nbss;
727 struct dl_list *prev = bss->list_id.prev;
728 dl_list_del(&bss->list_id);
729 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
730 res->beacon_ie_len);
731 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700732 unsigned int i;
733 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
734 if (wpa_s->last_scan_res[i] == bss) {
735 wpa_s->last_scan_res[i] = nbss;
736 break;
737 }
738 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700739 if (wpa_s->current_bss == bss)
740 wpa_s->current_bss = nbss;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800741 wpa_bss_update_pending_connect(wpa_s, bss, nbss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 bss = nbss;
Hai Shalom60840252021-02-19 19:02:11 -0800743 os_memcpy(bss->ies, res + 1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 res->ie_len + res->beacon_ie_len);
745 bss->ie_len = res->ie_len;
746 bss->beacon_ie_len = res->beacon_ie_len;
747 }
748 dl_list_add(prev, &bss->list_id);
749 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700750 if (changes & WPA_BSS_IES_CHANGED_FLAG) {
751 const u8 *ml_ie, *mld_addr;
752
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700753 wpa_bss_set_hessid(bss);
Sunil Ravi89eba102022-09-13 21:04:37 -0700754 os_memset(bss->mld_addr, 0, ETH_ALEN);
755 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
756 if (ml_ie) {
757 mld_addr = get_basic_mle_mld_addr(&ml_ie[3],
758 ml_ie[1] - 1);
759 if (mld_addr)
760 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
761 }
762 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 dl_list_add_tail(&wpa_s->bss, &bss->list);
764
765 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700766
767 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768}
769
770
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800771/**
772 * wpa_bss_update_start - Start a BSS table update from scan results
773 * @wpa_s: Pointer to wpa_supplicant data
774 *
775 * This function is called at the start of each BSS table update round for new
776 * scan results. The actual scan result entries are indicated with calls to
777 * wpa_bss_update_scan_res() and the update round is finished with a call to
778 * wpa_bss_update_end().
779 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
781{
782 wpa_s->bss_update_idx++;
783 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
784 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700785 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786}
787
788
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800789/**
790 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
791 * @wpa_s: Pointer to wpa_supplicant data
792 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800793 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800794 *
795 * This function updates a BSS table entry (or adds one) based on a scan result.
796 * This is called separately for each scan result between the calls to
797 * wpa_bss_update_start() and wpa_bss_update_end().
798 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800800 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800801 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800803 const u8 *ssid, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 struct wpa_bss *bss;
805
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700806 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800807 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700808 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800809 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
810 struct os_reltime age;
811 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
812 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700813 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
814 "table entry that is %u.%06u seconds older "
815 "than our scan trigger",
816 (unsigned int) age.sec,
817 (unsigned int) age.usec);
818 return;
819 }
820 }
821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
823 if (ssid == NULL) {
824 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
825 MACSTR, MAC2STR(res->bssid));
826 return;
827 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700828 if (ssid[1] > SSID_MAX_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700829 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
830 MACSTR, MAC2STR(res->bssid));
831 return;
832 }
833
834 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700835#ifdef CONFIG_P2P
836 if (p2p == NULL &&
837 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
838 /*
839 * If it's a P2P specific interface, then don't update
840 * the scan result without a P2P IE.
841 */
842 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
843 " update for P2P interface", MAC2STR(res->bssid));
844 return;
845 }
846#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
848 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
849 return; /* Skip P2P listen discovery results here */
850
851 /* TODO: add option for ignoring BSSes we are not interested in
852 * (to save memory) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853
854 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700855 if (mesh && mesh[1] <= SSID_MAX_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800856 ssid = mesh;
857
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
859 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800860 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700861 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800862 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700863 if (wpa_s->last_scan_res) {
864 unsigned int i;
865 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
866 if (bss == wpa_s->last_scan_res[i]) {
867 /* Already in the list */
868 return;
869 }
870 }
871 }
872 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700873
874 if (bss == NULL)
875 return;
876 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
877 struct wpa_bss **n;
878 unsigned int siz;
879 if (wpa_s->last_scan_res_size == 0)
880 siz = 32;
881 else
882 siz = wpa_s->last_scan_res_size * 2;
883 n = os_realloc_array(wpa_s->last_scan_res, siz,
884 sizeof(struct wpa_bss *));
885 if (n == NULL)
886 return;
887 wpa_s->last_scan_res = n;
888 wpa_s->last_scan_res_size = siz;
889 }
890
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700891 if (wpa_s->last_scan_res)
892 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893}
894
895
896static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
897 const struct scan_info *info)
898{
899 int found;
900 size_t i;
901
902 if (info == NULL)
903 return 1;
904
905 if (info->num_freqs) {
906 found = 0;
907 for (i = 0; i < info->num_freqs; i++) {
908 if (bss->freq == info->freqs[i]) {
909 found = 1;
910 break;
911 }
912 }
913 if (!found)
914 return 0;
915 }
916
917 if (info->num_ssids) {
918 found = 0;
919 for (i = 0; i < info->num_ssids; i++) {
920 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
921 if ((s->ssid == NULL || s->ssid_len == 0) ||
922 (s->ssid_len == bss->ssid_len &&
923 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
924 0)) {
925 found = 1;
926 break;
927 }
928 }
929 if (!found)
930 return 0;
931 }
932
933 return 1;
934}
935
936
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800937/**
938 * wpa_bss_update_end - End a BSS table update from scan results
939 * @wpa_s: Pointer to wpa_supplicant data
940 * @info: Information about scan parameters
941 * @new_scan: Whether this update round was based on a new scan
942 *
943 * This function is called at the end of each BSS table update round for new
944 * scan results. The start of the update was indicated with a call to
945 * wpa_bss_update_start().
946 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
948 int new_scan)
949{
950 struct wpa_bss *bss, *n;
951
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800952 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800953 if ((info && info->aborted) || !new_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 return; /* do not expire entries without new scan */
955
956 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
957 if (wpa_bss_in_use(wpa_s, bss))
958 continue;
959 if (!wpa_bss_included_in_scan(bss, info))
960 continue; /* expire only BSSes that were scanned */
961 if (bss->last_update_idx < wpa_s->bss_update_idx)
962 bss->scan_miss_count++;
963 if (bss->scan_miss_count >=
964 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966 }
967 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700968
Hai Shalomfdcde762020-04-02 11:19:20 -0700969 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800970 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971}
972
973
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800974/**
975 * wpa_bss_flush_by_age - Flush old BSS entries
976 * @wpa_s: Pointer to wpa_supplicant data
977 * @age: Maximum entry age in seconds
978 *
979 * Remove BSS entries that have not been updated during the last @age seconds.
980 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
982{
983 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985
986 if (dl_list_empty(&wpa_s->bss))
987 return;
988
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800989 os_get_reltime(&t);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 t.sec -= age;
991
992 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
993 if (wpa_bss_in_use(wpa_s, bss))
994 continue;
995
Sunil Ravi38ad1ed2023-01-17 23:58:31 +0000996 if (wpa_s->reassoc_same_ess &&
997 wpa_s->wpa_state != WPA_COMPLETED &&
998 wpa_s->last_ssid &&
999 bss->ssid_len == wpa_s->last_ssid->ssid_len &&
1000 os_memcmp(bss->ssid, wpa_s->last_ssid->ssid,
1001 bss->ssid_len) == 0)
1002 continue;
1003
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001004 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006 } else
1007 break;
1008 }
1009}
1010
1011
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001012/**
1013 * wpa_bss_init - Initialize BSS table
1014 * @wpa_s: Pointer to wpa_supplicant data
1015 * Returns: 0 on success, -1 on failure
1016 *
1017 * This prepares BSS table lists and timer for periodic updates. The BSS table
1018 * is deinitialized with wpa_bss_deinit() once not needed anymore.
1019 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020int wpa_bss_init(struct wpa_supplicant *wpa_s)
1021{
1022 dl_list_init(&wpa_s->bss);
1023 dl_list_init(&wpa_s->bss_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024 return 0;
1025}
1026
1027
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001028/**
1029 * wpa_bss_flush - Flush all unused BSS entries
1030 * @wpa_s: Pointer to wpa_supplicant data
1031 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032void wpa_bss_flush(struct wpa_supplicant *wpa_s)
1033{
1034 struct wpa_bss *bss, *n;
1035
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001036 wpa_s->clear_driver_scan_cache = 1;
1037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 if (wpa_s->bss.next == NULL)
1039 return; /* BSS table not yet initialized */
1040
1041 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1042 if (wpa_bss_in_use(wpa_s, bss))
1043 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001044 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 }
1046}
1047
1048
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001049/**
1050 * wpa_bss_deinit - Deinitialize BSS table
1051 * @wpa_s: Pointer to wpa_supplicant data
1052 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
1054{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 wpa_bss_flush(wpa_s);
1056}
1057
1058
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001059/**
1060 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
1061 * @wpa_s: Pointer to wpa_supplicant data
1062 * @bssid: BSSID
1063 * Returns: Pointer to the BSS entry or %NULL if not found
1064 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
1066 const u8 *bssid)
1067{
1068 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001069 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1070 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1072 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1073 return bss;
1074 }
1075 return NULL;
1076}
1077
1078
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001079/**
1080 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1081 * @wpa_s: Pointer to wpa_supplicant data
1082 * @bssid: BSSID
1083 * Returns: Pointer to the BSS entry or %NULL if not found
1084 *
1085 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1086 * find the entry that has the most recent update. This can help in finding the
1087 * correct entry in cases where the SSID of the AP may have changed recently
1088 * (e.g., in WPS reconfiguration cases).
1089 */
1090struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1091 const u8 *bssid)
1092{
1093 struct wpa_bss *bss, *found = NULL;
1094 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1095 return NULL;
1096 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1097 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
1098 continue;
1099 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001100 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001101 found = bss;
1102 }
1103 return found;
1104}
1105
1106
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001107#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001108/**
Hai Shalomc3565922019-10-28 11:58:20 -07001109 * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001110 * @wpa_s: Pointer to wpa_supplicant data
1111 * @dev_addr: P2P Device Address of the GO
1112 * Returns: Pointer to the BSS entry or %NULL if not found
Hai Shalomc3565922019-10-28 11:58:20 -07001113 *
1114 * This function tries to find the entry that has the most recent update. This
1115 * can help in finding the correct entry in cases where the SSID of the P2P
1116 * Device may have changed recently.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001117 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001118struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1119 const u8 *dev_addr)
1120{
Hai Shalomc3565922019-10-28 11:58:20 -07001121 struct wpa_bss *bss, *found = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001122 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1123 u8 addr[ETH_ALEN];
Hai Shalom60840252021-02-19 19:02:11 -08001124 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07001125 addr) != 0 ||
1126 os_memcmp(addr, dev_addr, ETH_ALEN) != 0)
1127 continue;
1128 if (!found ||
1129 os_reltime_before(&found->last_update, &bss->last_update))
1130 found = bss;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001131 }
Hai Shalomc3565922019-10-28 11:58:20 -07001132 return found;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001133}
1134#endif /* CONFIG_P2P */
1135
1136
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001137/**
1138 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1139 * @wpa_s: Pointer to wpa_supplicant data
1140 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1141 * Returns: Pointer to the BSS entry or %NULL if not found
1142 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1144{
1145 struct wpa_bss *bss;
1146 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1147 if (bss->id == id)
1148 return bss;
1149 }
1150 return NULL;
1151}
1152
1153
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001154/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001155 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1156 * @wpa_s: Pointer to wpa_supplicant data
1157 * @idf: Smallest allowed identifier assigned for the entry
1158 * @idf: Largest allowed identifier assigned for the entry
1159 * Returns: Pointer to the BSS entry or %NULL if not found
1160 *
1161 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1162 * smallest id value to be fetched within the specified range without the
1163 * caller having to know the exact id.
1164 */
1165struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1166 unsigned int idf, unsigned int idl)
1167{
1168 struct wpa_bss *bss;
1169 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1170 if (bss->id >= idf && bss->id <= idl)
1171 return bss;
1172 }
1173 return NULL;
1174}
1175
1176
1177/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001178 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1179 * @bss: BSS table entry
1180 * @ie: Information element identitifier (WLAN_EID_*)
1181 * Returns: Pointer to the information element (id field) or %NULL if not found
1182 *
1183 * This function returns the first matching information element in the BSS
1184 * entry.
1185 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1187{
Hai Shalom60840252021-02-19 19:02:11 -08001188 return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie);
1189}
1190
1191
1192/**
1193 * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry
1194 * @bss: BSS table entry
1195 * @ext: Information element extension identifier (WLAN_EID_EXT_*)
1196 * Returns: Pointer to the information element (id field) or %NULL if not found
1197 *
1198 * This function returns the first matching information element in the BSS
1199 * entry.
1200 */
1201const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext)
1202{
1203 return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204}
1205
1206
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001207/**
1208 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1209 * @bss: BSS table entry
1210 * @vendor_type: Vendor type (four octets starting the IE payload)
1211 * Returns: Pointer to the information element (id field) or %NULL if not found
1212 *
1213 * This function returns the first matching information element in the BSS
1214 * entry.
1215 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1217{
Hai Shalom60840252021-02-19 19:02:11 -08001218 const u8 *ies;
1219 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220
Hai Shalom60840252021-02-19 19:02:11 -08001221 ies = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222
Hai Shalom60840252021-02-19 19:02:11 -08001223 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) {
1224 if (elem->datalen >= 4 &&
1225 vendor_type == WPA_GET_BE32(elem->data))
1226 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227 }
1228
1229 return NULL;
1230}
1231
1232
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001233/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001234 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1235 * @bss: BSS table entry
1236 * @vendor_type: Vendor type (four octets starting the IE payload)
1237 * Returns: Pointer to the information element (id field) or %NULL if not found
1238 *
1239 * This function returns the first matching information element in the BSS
1240 * entry.
1241 *
1242 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1243 * from Beacon frames instead of either Beacon or Probe Response frames.
1244 */
1245const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1246 u32 vendor_type)
1247{
Hai Shalom60840252021-02-19 19:02:11 -08001248 const u8 *ies;
1249 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001250
1251 if (bss->beacon_ie_len == 0)
1252 return NULL;
1253
Hai Shalom60840252021-02-19 19:02:11 -08001254 ies = wpa_bss_ie_ptr(bss);
1255 ies += bss->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001256
Hai Shalom60840252021-02-19 19:02:11 -08001257 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1258 bss->beacon_ie_len) {
1259 if (elem->datalen >= 4 &&
1260 vendor_type == WPA_GET_BE32(elem->data))
1261 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001262 }
1263
1264 return NULL;
1265}
1266
1267
1268/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001269 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1270 * @bss: BSS table entry
1271 * @vendor_type: Vendor type (four octets starting the IE payload)
1272 * Returns: Pointer to the information element payload or %NULL if not found
1273 *
1274 * This function returns concatenated payload of possibly fragmented vendor
1275 * specific information elements in the BSS entry. The caller is responsible for
1276 * freeing the returned buffer.
1277 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1279 u32 vendor_type)
1280{
1281 struct wpabuf *buf;
1282 const u8 *end, *pos;
1283
1284 buf = wpabuf_alloc(bss->ie_len);
1285 if (buf == NULL)
1286 return NULL;
1287
Hai Shalom60840252021-02-19 19:02:11 -08001288 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001289 end = pos + bss->ie_len;
1290
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001291 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08001292 u8 ie, len;
1293
1294 ie = pos[0];
1295 len = pos[1];
1296 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 break;
Hai Shalom60840252021-02-19 19:02:11 -08001298 pos += 2;
1299 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1300 vendor_type == WPA_GET_BE32(pos))
1301 wpabuf_put_data(buf, pos + 4, len - 4);
1302 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001303 }
1304
1305 if (wpabuf_len(buf) == 0) {
1306 wpabuf_free(buf);
1307 buf = NULL;
1308 }
1309
1310 return buf;
1311}
1312
1313
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001314/**
1315 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1316 * @bss: BSS table entry
1317 * @vendor_type: Vendor type (four octets starting the IE payload)
1318 * Returns: Pointer to the information element payload or %NULL if not found
1319 *
1320 * This function returns concatenated payload of possibly fragmented vendor
1321 * specific information elements in the BSS entry. The caller is responsible for
1322 * freeing the returned buffer.
1323 *
1324 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1325 * from Beacon frames instead of either Beacon or Probe Response frames.
1326 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001327struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1328 u32 vendor_type)
1329{
1330 struct wpabuf *buf;
1331 const u8 *end, *pos;
1332
1333 buf = wpabuf_alloc(bss->beacon_ie_len);
1334 if (buf == NULL)
1335 return NULL;
1336
Hai Shalom60840252021-02-19 19:02:11 -08001337 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001338 pos += bss->ie_len;
1339 end = pos + bss->beacon_ie_len;
1340
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001341 while (end - pos > 1) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001342 u8 id, len;
1343
1344 id = *pos++;
1345 len = *pos++;
1346 if (len > end - pos)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001347 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001348 if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1349 vendor_type == WPA_GET_BE32(pos))
1350 wpabuf_put_data(buf, pos + 4, len - 4);
1351 pos += len;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001352 }
1353
1354 if (wpabuf_len(buf) == 0) {
1355 wpabuf_free(buf);
1356 buf = NULL;
1357 }
1358
1359 return buf;
1360}
1361
1362
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001363/**
1364 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1365 * @bss: BSS table entry
1366 * Returns: Maximum legacy rate in units of 500 kbps
1367 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001368int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1369{
1370 int rate = 0;
1371 const u8 *ie;
1372 int i;
1373
1374 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1375 for (i = 0; ie && i < ie[1]; i++) {
1376 if ((ie[i + 2] & 0x7f) > rate)
1377 rate = ie[i + 2] & 0x7f;
1378 }
1379
1380 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1381 for (i = 0; ie && i < ie[1]; i++) {
1382 if ((ie[i + 2] & 0x7f) > rate)
1383 rate = ie[i + 2] & 0x7f;
1384 }
1385
1386 return rate;
1387}
1388
1389
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001390/**
1391 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1392 * @bss: BSS table entry
1393 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1394 * Returns: number of legacy TX rates or -1 on failure
1395 *
1396 * The caller is responsible for freeing the returned buffer with os_free() in
1397 * case of success.
1398 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1400{
1401 const u8 *ie, *ie2;
1402 int i, j;
1403 unsigned int len;
1404 u8 *r;
1405
1406 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1407 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1408
1409 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1410
1411 r = os_malloc(len);
1412 if (!r)
1413 return -1;
1414
1415 for (i = 0; ie && i < ie[1]; i++)
1416 r[i] = ie[i + 2] & 0x7f;
1417
1418 for (j = 0; ie2 && j < ie2[1]; j++)
1419 r[i + j] = ie2[j + 2] & 0x7f;
1420
1421 *rates = r;
1422 return len;
1423}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001424
1425
1426#ifdef CONFIG_FILS
Hai Shalom60840252021-02-19 19:02:11 -08001427const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001428{
1429 const u8 *ie;
1430
1431 if (bss) {
1432 ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
1433 if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7))
1434 return ie + 4;
1435 }
1436
1437 return NULL;
1438}
1439#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001440
1441
1442int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab)
1443{
Hai Shalom60840252021-02-19 19:02:11 -08001444 if (!bss)
1445 return 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08001446 return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB),
1447 capab);
1448}
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001449
1450
1451/**
1452 * wpa_bss_defrag_mle - Get a buffer holding a de-fragmented ML element
1453 * @bss: BSS table entry
1454 * @type: ML control type
1455 */
1456struct wpabuf * wpa_bss_defrag_mle(const struct wpa_bss *bss, u8 type)
1457{
1458 struct ieee802_11_elems elems;
1459 const u8 *pos = wpa_bss_ie_ptr(bss);
1460 size_t len = bss->ie_len;
1461
1462 if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
1463 return NULL;
1464
1465 return ieee802_11_defrag_mle(&elems, type);
1466}