blob: cf94d4be5c0fa9fd6fd69ac5c003b0138ebd7971 [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"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000016#include "rsn_supp/wpa.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "wpa_supplicant_i.h"
18#include "config.h"
19#include "notify.h"
20#include "scan.h"
Sunil Ravib0ac25f2024-07-12 01:42:03 +000021#include "bssid_ignore.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "bss.h"
23
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070024static void wpa_bss_set_hessid(struct wpa_bss *bss)
25{
26#ifdef CONFIG_INTERWORKING
27 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
28 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
29 os_memset(bss->hessid, 0, ETH_ALEN);
30 return;
31 }
32 if (ie[1] == 7)
33 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
34 else
35 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
36#endif /* CONFIG_INTERWORKING */
37}
38
39
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080040/**
41 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
42 * Returns: Allocated ANQP data structure or %NULL on failure
43 *
44 * The allocated ANQP data structure has its users count set to 1. It may be
45 * shared by multiple BSS entries and each shared entry is freed with
46 * wpa_bss_anqp_free().
47 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070048struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
49{
50 struct wpa_bss_anqp *anqp;
51 anqp = os_zalloc(sizeof(*anqp));
52 if (anqp == NULL)
53 return NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054#ifdef CONFIG_INTERWORKING
55 dl_list_init(&anqp->anqp_elems);
56#endif /* CONFIG_INTERWORKING */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070057 anqp->users = 1;
58 return anqp;
59}
60
61
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080062/**
63 * wpa_bss_anqp_clone - Clone an ANQP data structure
64 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
65 * Returns: Cloned ANQP data structure or %NULL on failure
66 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080067static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
68{
69 struct wpa_bss_anqp *n;
70
71 n = os_zalloc(sizeof(*n));
72 if (n == NULL)
73 return NULL;
74
75#define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
76#ifdef CONFIG_INTERWORKING
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080077 dl_list_init(&n->anqp_elems);
Dmitry Shmidt7f656022015-02-25 14:36:37 -080078 ANQP_DUP(capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080079 ANQP_DUP(venue_name);
80 ANQP_DUP(network_auth_type);
81 ANQP_DUP(roaming_consortium);
82 ANQP_DUP(ip_addr_type_availability);
83 ANQP_DUP(nai_realm);
84 ANQP_DUP(anqp_3gpp);
85 ANQP_DUP(domain_name);
Dmitry Shmidt29333592017-01-09 12:27:11 -080086 ANQP_DUP(fils_realm_info);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080087#endif /* CONFIG_INTERWORKING */
88#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -080089 ANQP_DUP(hs20_capability_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080090 ANQP_DUP(hs20_operator_friendly_name);
91 ANQP_DUP(hs20_wan_metrics);
92 ANQP_DUP(hs20_connection_capability);
93 ANQP_DUP(hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080094 ANQP_DUP(hs20_osu_providers_list);
Roshan Pius3a1667e2018-07-03 15:17:14 -070095 ANQP_DUP(hs20_operator_icon_metadata);
Hai Shalom39ba6fc2019-01-22 12:40:38 -080096 ANQP_DUP(hs20_osu_providers_nai_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080097#endif /* CONFIG_HS20 */
98#undef ANQP_DUP
99
100 return n;
101}
102
103
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800104/**
105 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
106 * @bss: BSS entry
107 * Returns: 0 on success, -1 on failure
108 *
109 * This function ensures the specific BSS entry has an ANQP data structure that
110 * is not shared with any other BSS entry.
111 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800112int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
113{
114 struct wpa_bss_anqp *anqp;
115
116 if (bss->anqp && bss->anqp->users > 1) {
117 /* allocated, but shared - clone an unshared copy */
118 anqp = wpa_bss_anqp_clone(bss->anqp);
119 if (anqp == NULL)
120 return -1;
121 anqp->users = 1;
122 bss->anqp->users--;
123 bss->anqp = anqp;
124 return 0;
125 }
126
127 if (bss->anqp)
128 return 0; /* already allocated and not shared */
129
130 /* not allocated - allocate a new storage area */
131 bss->anqp = wpa_bss_anqp_alloc();
132 return bss->anqp ? 0 : -1;
133}
134
135
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800136/**
137 * wpa_bss_anqp_free - Free an ANQP data structure
138 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
139 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700140static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
141{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800142#ifdef CONFIG_INTERWORKING
143 struct wpa_bss_anqp_elem *elem;
144#endif /* CONFIG_INTERWORKING */
145
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700146 if (anqp == NULL)
147 return;
148
149 anqp->users--;
150 if (anqp->users > 0) {
151 /* Another BSS entry holds a pointer to this ANQP info */
152 return;
153 }
154
155#ifdef CONFIG_INTERWORKING
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800156 wpabuf_free(anqp->capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700157 wpabuf_free(anqp->venue_name);
158 wpabuf_free(anqp->network_auth_type);
159 wpabuf_free(anqp->roaming_consortium);
160 wpabuf_free(anqp->ip_addr_type_availability);
161 wpabuf_free(anqp->nai_realm);
162 wpabuf_free(anqp->anqp_3gpp);
163 wpabuf_free(anqp->domain_name);
Dmitry Shmidt29333592017-01-09 12:27:11 -0800164 wpabuf_free(anqp->fils_realm_info);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800165
166 while ((elem = dl_list_first(&anqp->anqp_elems,
167 struct wpa_bss_anqp_elem, list))) {
168 dl_list_del(&elem->list);
169 wpabuf_free(elem->payload);
170 os_free(elem);
171 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700172#endif /* CONFIG_INTERWORKING */
173#ifdef CONFIG_HS20
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800174 wpabuf_free(anqp->hs20_capability_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700175 wpabuf_free(anqp->hs20_operator_friendly_name);
176 wpabuf_free(anqp->hs20_wan_metrics);
177 wpabuf_free(anqp->hs20_connection_capability);
178 wpabuf_free(anqp->hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800179 wpabuf_free(anqp->hs20_osu_providers_list);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700180 wpabuf_free(anqp->hs20_operator_icon_metadata);
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800181 wpabuf_free(anqp->hs20_osu_providers_nai_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700182#endif /* CONFIG_HS20 */
183
184 os_free(anqp);
185}
186
187
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000188static struct wpa_connect_work *
189wpa_bss_check_pending_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800190{
191 struct wpa_radio_work *work;
192 struct wpa_connect_work *cwork;
193
194 work = radio_work_pending(wpa_s, "sme-connect");
195 if (!work)
196 work = radio_work_pending(wpa_s, "connect");
197 if (!work)
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000198 return NULL;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800199
200 cwork = work->ctx;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000201 if (cwork->bss != bss)
202 return NULL;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800203
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000204 return cwork;
205}
206
207
208static void wpa_bss_update_pending_connect(struct wpa_connect_work *cwork,
209 struct wpa_bss *new_bss)
210{
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800211 wpa_printf(MSG_DEBUG,
212 "Update BSS pointer for the pending connect radio work");
213 cwork->bss = new_bss;
214 if (!new_bss)
215 cwork->bss_removed = 1;
216}
217
218
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800219void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
220 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221{
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000222 struct wpa_connect_work *cwork;
223
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700224 if (wpa_s->last_scan_res) {
225 unsigned int i;
226 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
227 if (wpa_s->last_scan_res[i] == bss) {
228 os_memmove(&wpa_s->last_scan_res[i],
229 &wpa_s->last_scan_res[i + 1],
230 (wpa_s->last_scan_res_used - i - 1)
231 * sizeof(struct wpa_bss *));
232 wpa_s->last_scan_res_used--;
233 break;
234 }
235 }
236 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000237 cwork = wpa_bss_check_pending_connect(wpa_s, bss);
238 if (cwork)
239 wpa_bss_update_pending_connect(cwork, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240 dl_list_del(&bss->list);
241 dl_list_del(&bss->list_id);
242 wpa_s->num_bss--;
243 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700244 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
245 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700247 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 os_free(bss);
249}
250
251
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800252/**
253 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
254 * @wpa_s: Pointer to wpa_supplicant data
Matthew Wangdcf19452022-11-07 20:42:52 -0800255 * @bssid: BSSID, or %NULL to match any BSSID
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800256 * @ssid: SSID
257 * @ssid_len: Length of @ssid
258 * Returns: Pointer to the BSS entry or %NULL if not found
259 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
261 const u8 *ssid, size_t ssid_len)
262{
263 struct wpa_bss *bss;
Matthew Wangafc981e2023-03-17 21:30:12 +0000264
265 if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700266 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000268 if ((!bssid || ether_addr_equal(bss->bssid, bssid)) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700269 bss->ssid_len == ssid_len &&
270 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
271 return bss;
272 }
273 return NULL;
274}
275
276
Dmitry Shmidt29333592017-01-09 12:27:11 -0800277void calculate_update_time(const struct os_reltime *fetch_time,
278 unsigned int age_ms,
279 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280{
281 os_time_t usec;
282
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700283 update_time->sec = fetch_time->sec;
284 update_time->usec = fetch_time->usec;
285 update_time->sec -= age_ms / 1000;
286 usec = (age_ms % 1000) * 1000;
287 if (update_time->usec < usec) {
288 update_time->sec--;
289 update_time->usec += 1000000;
290 }
291 update_time->usec -= usec;
292}
293
294
295static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800296 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700297{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 dst->flags = src->flags;
299 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
300 dst->freq = src->freq;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000301 dst->max_cw = src->max_cw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 dst->beacon_int = src->beacon_int;
303 dst->caps = src->caps;
304 dst->qual = src->qual;
305 dst->noise = src->noise;
306 dst->level = src->level;
307 dst->tsf = src->tsf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700308 dst->beacon_newer = src->beacon_newer;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800309 dst->est_throughput = src->est_throughput;
310 dst->snr = src->snr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700312 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700313}
314
315
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700316static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
317 struct wpa_bss *bss)
318{
319#ifdef CONFIG_WPS
320 struct wpa_ssid *ssid;
321 struct wpabuf *wps_ie;
322 int pbc = 0, ret;
323
324 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
325 if (!wps_ie)
326 return 0;
327
328 if (wps_is_selected_pbc_registrar(wps_ie)) {
329 pbc = 1;
330 } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
331 wpabuf_free(wps_ie);
332 return 0;
333 }
334
335 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
336 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
337 continue;
338 if (ssid->ssid_len &&
339 (ssid->ssid_len != bss->ssid_len ||
340 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
341 continue;
342
343 if (pbc)
344 ret = eap_is_wps_pbc_enrollee(&ssid->eap);
345 else
346 ret = eap_is_wps_pin_enrollee(&ssid->eap);
347 wpabuf_free(wps_ie);
348 return ret;
349 }
350 wpabuf_free(wps_ie);
351#endif /* CONFIG_WPS */
352
353 return 0;
354}
355
356
Hai Shalom60840252021-02-19 19:02:11 -0800357static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s,
358 struct wpa_bss *bss)
359{
360#ifdef CONFIG_P2P
361 u8 addr[ETH_ALEN];
362
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000363 if (ether_addr_equal(bss->bssid, wpa_s->pending_join_iface_addr))
Hai Shalom60840252021-02-19 19:02:11 -0800364 return true;
365 if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) &&
366 p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000367 ether_addr_equal(addr, wpa_s->pending_join_dev_addr))
Hai Shalom60840252021-02-19 19:02:11 -0800368 return true;
369#endif /* CONFIG_P2P */
370 return false;
371}
372
373
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800374static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
375{
376 struct wpa_ssid *ssid;
377
Hai Shalom60840252021-02-19 19:02:11 -0800378 if (is_p2p_pending_bss(wpa_s, bss))
379 return 1;
380
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
382 if (ssid->ssid == NULL || ssid->ssid_len == 0)
383 continue;
384 if (ssid->ssid_len == bss->ssid_len &&
385 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
386 return 1;
387 }
388
389 return 0;
390}
391
392
Dmitry Shmidt04949592012-07-19 12:16:46 -0700393static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
394{
Sunil Ravi89eba102022-09-13 21:04:37 -0700395 int i;
396
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800397 if (bss == wpa_s->current_bss)
398 return 1;
399
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000400 if (bss == wpa_s->ml_connect_probe_bss)
401 return 1;
402
Sunil Ravi99c035e2024-07-12 01:42:03 +0000403#ifdef CONFIG_WNM
404 if (bss == wpa_s->wnm_target_bss)
405 return 1;
406#endif /* CONFIG_WNM */
407
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800408 if (wpa_s->current_bss &&
409 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
410 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
411 bss->ssid_len) != 0))
412 return 0; /* SSID has changed */
413
Sunil Ravi89eba102022-09-13 21:04:37 -0700414 if (!is_zero_ether_addr(bss->bssid) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000415 (ether_addr_equal(bss->bssid, wpa_s->bssid) ||
416 ether_addr_equal(bss->bssid, wpa_s->pending_bssid)))
Sunil Ravi89eba102022-09-13 21:04:37 -0700417 return 1;
418
419 if (!wpa_s->valid_links)
420 return 0;
421
Sunil Ravi99c035e2024-07-12 01:42:03 +0000422 for_each_link(wpa_s->valid_links, i) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000423 if (ether_addr_equal(bss->bssid, wpa_s->links[i].bssid))
Sunil Ravi89eba102022-09-13 21:04:37 -0700424 return 1;
425 }
426
427 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700428}
429
430
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
432{
433 struct wpa_bss *bss;
434
435 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700436 if (!wpa_bss_known(wpa_s, bss) &&
437 !wpa_bss_is_wps_candidate(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800439 return 0;
440 }
441 }
442
443 return -1;
444}
445
446
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800448{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700449 struct wpa_bss *bss;
450
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800451 /*
452 * Remove the oldest entry that does not match with any configured
453 * network.
454 */
455 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700456 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457
458 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700459 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800460 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
462 if (!wpa_bss_in_use(wpa_s, bss)) {
463 wpa_bss_remove(wpa_s, bss, __func__);
464 return 0;
465 }
466 }
467
468 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800469}
470
471
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700472static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
473 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800474 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800475 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476{
477 struct wpa_bss *bss;
Sunil Ravi89eba102022-09-13 21:04:37 -0700478 char extra[100];
479 const u8 *ml_ie;
480 char *pos, *end;
481 int ret = 0;
482 const u8 *mld_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483
484 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
485 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700486 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 bss->id = wpa_s->bss_next_id++;
488 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800489 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 os_memcpy(bss->ssid, ssid, ssid_len);
491 bss->ssid_len = ssid_len;
492 bss->ie_len = res->ie_len;
493 bss->beacon_ie_len = res->beacon_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -0800494 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700495 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496
Sunil Ravi89eba102022-09-13 21:04:37 -0700497 os_memset(bss->mld_addr, 0, ETH_ALEN);
498 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
499 if (ml_ie) {
500 mld_addr = get_basic_mle_mld_addr(&ml_ie[3], ml_ie[1] - 1);
501 if (mld_addr)
502 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
503 }
504
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300505 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
506 wpa_bss_remove_oldest(wpa_s) != 0) {
507 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
508 "because all BSSes are in use. We should normally "
509 "not get here!", (int) wpa_s->num_bss + 1);
510 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
511 }
512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 dl_list_add_tail(&wpa_s->bss, &bss->list);
514 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
515 wpa_s->num_bss++;
Sunil Ravi89eba102022-09-13 21:04:37 -0700516
517 extra[0] = '\0';
518 pos = extra;
519 end = pos + sizeof(extra);
Hai Shalom81f62d82019-07-22 12:10:00 -0700520 if (!is_zero_ether_addr(bss->hessid))
Sunil Ravi89eba102022-09-13 21:04:37 -0700521 ret = os_snprintf(pos, end - pos, " HESSID " MACSTR,
522 MAC2STR(bss->hessid));
523
524 if (!is_zero_ether_addr(bss->mld_addr) &&
525 !os_snprintf_error(end - pos, ret)) {
526 pos += ret;
527 ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR,
528 MAC2STR(bss->mld_addr));
529 }
530
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
Hai Shalom81f62d82019-07-22 12:10:00 -0700532 " SSID '%s' freq %d%s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800533 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
Hai Shalom81f62d82019-07-22 12:10:00 -0700534 bss->freq, extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700536 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537}
538
539
540static int are_ies_equal(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700541 const struct wpa_scan_res *new_res, u32 ie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542{
543 const u8 *old_ie, *new_ie;
544 struct wpabuf *old_ie_buff = NULL;
545 struct wpabuf *new_ie_buff = NULL;
546 int new_ie_len, old_ie_len, ret, is_multi;
547
548 switch (ie) {
549 case WPA_IE_VENDOR_TYPE:
550 old_ie = wpa_bss_get_vendor_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700551 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 is_multi = 0;
553 break;
554 case WPS_IE_VENDOR_TYPE:
555 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700556 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 is_multi = 1;
558 break;
559 case WLAN_EID_RSN:
560 case WLAN_EID_SUPP_RATES:
561 case WLAN_EID_EXT_SUPP_RATES:
562 old_ie = wpa_bss_get_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700563 new_ie = wpa_scan_get_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564 is_multi = 0;
565 break;
566 default:
567 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
568 return 0;
569 }
570
571 if (is_multi) {
572 /* in case of multiple IEs stored in buffer */
573 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
574 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
575 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
576 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
577 } else {
578 /* in case of single IE */
579 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
580 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
581 }
582
583 if (!old_ie || !new_ie)
584 ret = !old_ie && !new_ie;
585 else
586 ret = (old_ie_len == new_ie_len &&
587 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
588
589 wpabuf_free(old_ie_buff);
590 wpabuf_free(new_ie_buff);
591
592 return ret;
593}
594
595
596static u32 wpa_bss_compare_res(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700597 const struct wpa_scan_res *new_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598{
599 u32 changes = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700600 int caps_diff = old->caps ^ new_res->caps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700601
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700602 if (old->freq != new_res->freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
604
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700605 if (old->level != new_res->level)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
607
608 if (caps_diff & IEEE80211_CAP_PRIVACY)
609 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
610
611 if (caps_diff & IEEE80211_CAP_IBSS)
612 changes |= WPA_BSS_MODE_CHANGED_FLAG;
613
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700614 if (old->ie_len == new_res->ie_len &&
Hai Shalom60840252021-02-19 19:02:11 -0800615 os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 return changes;
617 changes |= WPA_BSS_IES_CHANGED_FLAG;
618
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700619 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
621
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700622 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
624
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700625 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 changes |= WPA_BSS_WPS_CHANGED_FLAG;
627
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700628 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
629 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 changes |= WPA_BSS_RATES_CHANGED_FLAG;
631
632 return changes;
633}
634
635
Hai Shalom60840252021-02-19 19:02:11 -0800636void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
637 const struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638{
639 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
640 wpas_notify_bss_freq_changed(wpa_s, bss->id);
641
642 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
643 wpas_notify_bss_signal_changed(wpa_s, bss->id);
644
645 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
646 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
647
648 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
649 wpas_notify_bss_mode_changed(wpa_s, bss->id);
650
651 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
652 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
653
654 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
655 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
656
657 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
658 wpas_notify_bss_wps_changed(wpa_s, bss->id);
659
660 if (changes & WPA_BSS_IES_CHANGED_FLAG)
661 wpas_notify_bss_ies_changed(wpa_s, bss->id);
662
663 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
664 wpas_notify_bss_rates_changed(wpa_s, bss->id);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700665
666 wpas_notify_bss_seen(wpa_s, bss->id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667}
668
669
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700670static struct wpa_bss *
671wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800672 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700673{
674 u32 changes;
675
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800676 if (bss->last_update_idx == wpa_s->bss_update_idx) {
677 struct os_reltime update_time;
678
679 /*
680 * Some drivers (e.g., cfg80211) include multiple BSS entries
681 * for the same BSS if that BSS's channel changes. The BSS list
682 * implementation in wpa_supplicant does not do that and we need
683 * to filter out the obsolete results here to make sure only the
684 * most current BSS information remains in the table.
685 */
686 wpa_printf(MSG_DEBUG, "BSS: " MACSTR
687 " has multiple entries in the scan results - select the most current one",
688 MAC2STR(bss->bssid));
689 calculate_update_time(fetch_time, res->age, &update_time);
690 wpa_printf(MSG_DEBUG,
691 "Previous last_update: %u.%06u (freq %d%s)",
692 (unsigned int) bss->last_update.sec,
693 (unsigned int) bss->last_update.usec,
694 bss->freq,
695 (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
696 wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
697 (unsigned int) update_time.sec,
698 (unsigned int) update_time.usec,
699 res->freq,
700 (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
701 if ((bss->flags & WPA_BSS_ASSOCIATED) ||
702 (!(res->flags & WPA_SCAN_ASSOCIATED) &&
703 !os_reltime_before(&bss->last_update, &update_time))) {
704 wpa_printf(MSG_DEBUG,
705 "Ignore this BSS entry since the previous update looks more current");
706 return bss;
707 }
708 wpa_printf(MSG_DEBUG,
709 "Accept this BSS entry since it looks more current than the previous update");
710 }
711
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712 changes = wpa_bss_compare_res(bss, res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800713 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
714 wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
715 MAC2STR(bss->bssid), bss->freq, res->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 bss->scan_miss_count = 0;
717 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800718 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 /* Move the entry to the end of the list */
720 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700721#ifdef CONFIG_P2P
722 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000723 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE) &&
724 !(changes & WPA_BSS_FREQ_CHANGED_FLAG)) {
Dmitry Shmidt96571392013-10-14 12:54:46 -0700725 /*
726 * This can happen when non-P2P station interface runs a scan
727 * without P2P IE in the Probe Request frame. P2P GO would reply
728 * to that with a Probe Response that does not include P2P IE.
729 * Do not update the IEs in this BSS entry to avoid such loss of
730 * information that may be needed for P2P operations to
731 * determine group information.
732 */
733 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
734 MACSTR " since that would remove P2P IE information",
735 MAC2STR(bss->bssid));
736 } else
737#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 if (bss->ie_len + bss->beacon_ie_len >=
739 res->ie_len + res->beacon_ie_len) {
Hai Shalom60840252021-02-19 19:02:11 -0800740 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741 bss->ie_len = res->ie_len;
742 bss->beacon_ie_len = res->beacon_ie_len;
743 } else {
744 struct wpa_bss *nbss;
745 struct dl_list *prev = bss->list_id.prev;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000746 struct wpa_connect_work *cwork;
747 unsigned int i;
748 bool update_current_bss = wpa_s->current_bss == bss;
749 bool update_ml_probe_bss = wpa_s->ml_connect_probe_bss == bss;
750
751 cwork = wpa_bss_check_pending_connect(wpa_s, bss);
752
753 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
754 if (wpa_s->last_scan_res[i] == bss)
755 break;
756 }
757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758 dl_list_del(&bss->list_id);
759 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
760 res->beacon_ie_len);
761 if (nbss) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000762 if (i != wpa_s->last_scan_res_used)
763 wpa_s->last_scan_res[i] = nbss;
764
765 if (update_current_bss)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700766 wpa_s->current_bss = nbss;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000767
768 if (update_ml_probe_bss)
769 wpa_s->ml_connect_probe_bss = nbss;
770
771 if (cwork)
772 wpa_bss_update_pending_connect(cwork, nbss);
773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 bss = nbss;
Hai Shalom60840252021-02-19 19:02:11 -0800775 os_memcpy(bss->ies, res + 1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776 res->ie_len + res->beacon_ie_len);
777 bss->ie_len = res->ie_len;
778 bss->beacon_ie_len = res->beacon_ie_len;
779 }
780 dl_list_add(prev, &bss->list_id);
781 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700782 if (changes & WPA_BSS_IES_CHANGED_FLAG) {
783 const u8 *ml_ie, *mld_addr;
784
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700785 wpa_bss_set_hessid(bss);
Sunil Ravi89eba102022-09-13 21:04:37 -0700786 os_memset(bss->mld_addr, 0, ETH_ALEN);
787 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
788 if (ml_ie) {
789 mld_addr = get_basic_mle_mld_addr(&ml_ie[3],
790 ml_ie[1] - 1);
791 if (mld_addr)
792 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
793 }
794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 dl_list_add_tail(&wpa_s->bss, &bss->list);
796
797 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700798
799 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800}
801
802
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800803/**
804 * wpa_bss_update_start - Start a BSS table update from scan results
805 * @wpa_s: Pointer to wpa_supplicant data
806 *
807 * This function is called at the start of each BSS table update round for new
808 * scan results. The actual scan result entries are indicated with calls to
809 * wpa_bss_update_scan_res() and the update round is finished with a call to
810 * wpa_bss_update_end().
811 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
813{
814 wpa_s->bss_update_idx++;
815 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
816 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700817 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818}
819
820
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800821/**
822 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
823 * @wpa_s: Pointer to wpa_supplicant data
824 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800825 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800826 *
827 * This function updates a BSS table entry (or adds one) based on a scan result.
828 * This is called separately for each scan result between the calls to
829 * wpa_bss_update_start() and wpa_bss_update_end().
830 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800832 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800833 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800835 const u8 *ssid, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 struct wpa_bss *bss;
837
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700838 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800839 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700840 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800841 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
842 struct os_reltime age;
843 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
844 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700845 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
846 "table entry that is %u.%06u seconds older "
847 "than our scan trigger",
848 (unsigned int) age.sec,
849 (unsigned int) age.usec);
850 return;
851 }
852 }
853
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
855 if (ssid == NULL) {
856 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
857 MACSTR, MAC2STR(res->bssid));
858 return;
859 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700860 if (ssid[1] > SSID_MAX_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
862 MACSTR, MAC2STR(res->bssid));
863 return;
864 }
865
866 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700867#ifdef CONFIG_P2P
868 if (p2p == NULL &&
869 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
870 /*
871 * If it's a P2P specific interface, then don't update
872 * the scan result without a P2P IE.
873 */
874 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
875 " update for P2P interface", MAC2STR(res->bssid));
876 return;
877 }
878#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
880 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
881 return; /* Skip P2P listen discovery results here */
882
883 /* TODO: add option for ignoring BSSes we are not interested in
884 * (to save memory) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885
886 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700887 if (mesh && mesh[1] <= SSID_MAX_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 ssid = mesh;
889
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
891 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800892 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700893 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800894 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700895 if (wpa_s->last_scan_res) {
896 unsigned int i;
897 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
898 if (bss == wpa_s->last_scan_res[i]) {
899 /* Already in the list */
900 return;
901 }
902 }
903 }
904 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700905
906 if (bss == NULL)
907 return;
908 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
909 struct wpa_bss **n;
910 unsigned int siz;
911 if (wpa_s->last_scan_res_size == 0)
912 siz = 32;
913 else
914 siz = wpa_s->last_scan_res_size * 2;
915 n = os_realloc_array(wpa_s->last_scan_res, siz,
916 sizeof(struct wpa_bss *));
917 if (n == NULL)
918 return;
919 wpa_s->last_scan_res = n;
920 wpa_s->last_scan_res_size = siz;
921 }
922
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700923 if (wpa_s->last_scan_res)
924 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925}
926
927
928static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
929 const struct scan_info *info)
930{
931 int found;
932 size_t i;
933
934 if (info == NULL)
935 return 1;
936
937 if (info->num_freqs) {
938 found = 0;
939 for (i = 0; i < info->num_freqs; i++) {
940 if (bss->freq == info->freqs[i]) {
941 found = 1;
942 break;
943 }
944 }
945 if (!found)
946 return 0;
947 }
948
949 if (info->num_ssids) {
950 found = 0;
951 for (i = 0; i < info->num_ssids; i++) {
952 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
953 if ((s->ssid == NULL || s->ssid_len == 0) ||
954 (s->ssid_len == bss->ssid_len &&
955 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
956 0)) {
957 found = 1;
958 break;
959 }
960 }
961 if (!found)
962 return 0;
963 }
964
965 return 1;
966}
967
968
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800969/**
970 * wpa_bss_update_end - End a BSS table update from scan results
971 * @wpa_s: Pointer to wpa_supplicant data
972 * @info: Information about scan parameters
973 * @new_scan: Whether this update round was based on a new scan
974 *
975 * This function is called at the end of each BSS table update round for new
976 * scan results. The start of the update was indicated with a call to
977 * wpa_bss_update_start().
978 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
980 int new_scan)
981{
982 struct wpa_bss *bss, *n;
983
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800984 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800985 if ((info && info->aborted) || !new_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986 return; /* do not expire entries without new scan */
987
988 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
989 if (wpa_bss_in_use(wpa_s, bss))
990 continue;
991 if (!wpa_bss_included_in_scan(bss, info))
992 continue; /* expire only BSSes that were scanned */
993 if (bss->last_update_idx < wpa_s->bss_update_idx)
994 bss->scan_miss_count++;
995 if (bss->scan_miss_count >=
996 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700997 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 }
999 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001000
Hai Shalomfdcde762020-04-02 11:19:20 -07001001 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001002 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003}
1004
1005
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001006/**
1007 * wpa_bss_flush_by_age - Flush old BSS entries
1008 * @wpa_s: Pointer to wpa_supplicant data
1009 * @age: Maximum entry age in seconds
1010 *
1011 * Remove BSS entries that have not been updated during the last @age seconds.
1012 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
1014{
1015 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001016 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017
1018 if (dl_list_empty(&wpa_s->bss))
1019 return;
1020
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001021 os_get_reltime(&t);
Sunil Raviaf8751c2023-03-29 11:35:17 -07001022
1023 if (t.sec < age)
1024 return; /* avoid underflow; there can be no older entries */
1025
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001026 t.sec -= age;
1027
1028 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1029 if (wpa_bss_in_use(wpa_s, bss))
1030 continue;
1031
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001032 if (wpa_s->reassoc_same_ess &&
1033 wpa_s->wpa_state != WPA_COMPLETED &&
1034 wpa_s->last_ssid &&
1035 bss->ssid_len == wpa_s->last_ssid->ssid_len &&
1036 os_memcmp(bss->ssid, wpa_s->last_ssid->ssid,
1037 bss->ssid_len) == 0)
1038 continue;
1039
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001040 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001041 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001042 } else
1043 break;
1044 }
1045}
1046
1047
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001048/**
1049 * wpa_bss_init - Initialize BSS table
1050 * @wpa_s: Pointer to wpa_supplicant data
1051 * Returns: 0 on success, -1 on failure
1052 *
1053 * This prepares BSS table lists and timer for periodic updates. The BSS table
1054 * is deinitialized with wpa_bss_deinit() once not needed anymore.
1055 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056int wpa_bss_init(struct wpa_supplicant *wpa_s)
1057{
1058 dl_list_init(&wpa_s->bss);
1059 dl_list_init(&wpa_s->bss_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001060 return 0;
1061}
1062
1063
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001064/**
1065 * wpa_bss_flush - Flush all unused BSS entries
1066 * @wpa_s: Pointer to wpa_supplicant data
1067 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068void wpa_bss_flush(struct wpa_supplicant *wpa_s)
1069{
1070 struct wpa_bss *bss, *n;
1071
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001072 wpa_s->clear_driver_scan_cache = 1;
1073
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074 if (wpa_s->bss.next == NULL)
1075 return; /* BSS table not yet initialized */
1076
1077 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1078 if (wpa_bss_in_use(wpa_s, bss))
1079 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001080 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 }
1082}
1083
1084
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001085/**
1086 * wpa_bss_deinit - Deinitialize BSS table
1087 * @wpa_s: Pointer to wpa_supplicant data
1088 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
1090{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 wpa_bss_flush(wpa_s);
1092}
1093
1094
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001095/**
1096 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
1097 * @wpa_s: Pointer to wpa_supplicant data
1098 * @bssid: BSSID
1099 * Returns: Pointer to the BSS entry or %NULL if not found
1100 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
1102 const u8 *bssid)
1103{
1104 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001105 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1106 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001108 if (ether_addr_equal(bss->bssid, bssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 return bss;
1110 }
1111 return NULL;
1112}
1113
1114
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001115/**
1116 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1117 * @wpa_s: Pointer to wpa_supplicant data
1118 * @bssid: BSSID
1119 * Returns: Pointer to the BSS entry or %NULL if not found
1120 *
1121 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1122 * find the entry that has the most recent update. This can help in finding the
1123 * correct entry in cases where the SSID of the AP may have changed recently
1124 * (e.g., in WPS reconfiguration cases).
1125 */
1126struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1127 const u8 *bssid)
1128{
1129 struct wpa_bss *bss, *found = NULL;
1130 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1131 return NULL;
1132 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001133 if (!ether_addr_equal(bss->bssid, bssid))
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001134 continue;
1135 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001136 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001137 found = bss;
1138 }
1139 return found;
1140}
1141
1142
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001143#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001144/**
Hai Shalomc3565922019-10-28 11:58:20 -07001145 * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001146 * @wpa_s: Pointer to wpa_supplicant data
1147 * @dev_addr: P2P Device Address of the GO
1148 * Returns: Pointer to the BSS entry or %NULL if not found
Hai Shalomc3565922019-10-28 11:58:20 -07001149 *
1150 * This function tries to find the entry that has the most recent update. This
1151 * can help in finding the correct entry in cases where the SSID of the P2P
1152 * Device may have changed recently.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001153 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001154struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1155 const u8 *dev_addr)
1156{
Hai Shalomc3565922019-10-28 11:58:20 -07001157 struct wpa_bss *bss, *found = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001158 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1159 u8 addr[ETH_ALEN];
Hai Shalom60840252021-02-19 19:02:11 -08001160 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07001161 addr) != 0 ||
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001162 !ether_addr_equal(addr, dev_addr))
Hai Shalomc3565922019-10-28 11:58:20 -07001163 continue;
1164 if (!found ||
1165 os_reltime_before(&found->last_update, &bss->last_update))
1166 found = bss;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001167 }
Hai Shalomc3565922019-10-28 11:58:20 -07001168 return found;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001169}
1170#endif /* CONFIG_P2P */
1171
1172
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001173/**
1174 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1175 * @wpa_s: Pointer to wpa_supplicant data
1176 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1177 * Returns: Pointer to the BSS entry or %NULL if not found
1178 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1180{
1181 struct wpa_bss *bss;
1182 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1183 if (bss->id == id)
1184 return bss;
1185 }
1186 return NULL;
1187}
1188
1189
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001190/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001191 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1192 * @wpa_s: Pointer to wpa_supplicant data
1193 * @idf: Smallest allowed identifier assigned for the entry
1194 * @idf: Largest allowed identifier assigned for the entry
1195 * Returns: Pointer to the BSS entry or %NULL if not found
1196 *
1197 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1198 * smallest id value to be fetched within the specified range without the
1199 * caller having to know the exact id.
1200 */
1201struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1202 unsigned int idf, unsigned int idl)
1203{
1204 struct wpa_bss *bss;
1205 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1206 if (bss->id >= idf && bss->id <= idl)
1207 return bss;
1208 }
1209 return NULL;
1210}
1211
1212
1213/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001214 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1215 * @bss: BSS table entry
1216 * @ie: Information element identitifier (WLAN_EID_*)
1217 * Returns: Pointer to the information element (id field) or %NULL if not found
1218 *
1219 * This function returns the first matching information element in the BSS
1220 * entry.
1221 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1223{
Hai Shalom60840252021-02-19 19:02:11 -08001224 return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie);
1225}
1226
1227
1228/**
Sunil Ravi7f769292024-07-23 22:21:32 +00001229 * wpa_bss_get_ie_beacon - Fetch a specified information element from a BSS entry
1230 * @bss: BSS table entry
1231 * @ie: Information element identitifier (WLAN_EID_*)
1232 * Returns: Pointer to the information element (id field) or %NULL if not found
1233 *
1234 * This function returns the first matching information element in the BSS
1235 * entry.
1236 *
1237 * This function is like wpa_bss_get_ie(), but uses IE buffer only from Beacon
1238 * frames instead of either Beacon or Probe Response frames.
1239 */
1240const u8 * wpa_bss_get_ie_beacon(const struct wpa_bss *bss, u8 ie)
1241{
1242 const u8 *ies;
1243
1244 if (bss->beacon_ie_len == 0)
1245 return NULL;
1246
1247 ies = wpa_bss_ie_ptr(bss);
1248 ies += bss->ie_len;
1249 return get_ie(ies, bss->beacon_ie_len, ie);
1250}
1251
1252
1253/**
Hai Shalom60840252021-02-19 19:02:11 -08001254 * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry
1255 * @bss: BSS table entry
1256 * @ext: Information element extension identifier (WLAN_EID_EXT_*)
1257 * Returns: Pointer to the information element (id field) or %NULL if not found
1258 *
1259 * This function returns the first matching information element in the BSS
1260 * entry.
1261 */
1262const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext)
1263{
1264 return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265}
1266
1267
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001268/**
1269 * wpa_bss_get_vendor_ie - Fetch a vendor information element 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 (id field) or %NULL if not found
1273 *
1274 * This function returns the first matching information element in the BSS
1275 * entry.
1276 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1278{
Hai Shalom60840252021-02-19 19:02:11 -08001279 const u8 *ies;
1280 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281
Hai Shalom60840252021-02-19 19:02:11 -08001282 ies = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283
Hai Shalom60840252021-02-19 19:02:11 -08001284 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) {
1285 if (elem->datalen >= 4 &&
1286 vendor_type == WPA_GET_BE32(elem->data))
1287 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288 }
1289
1290 return NULL;
1291}
1292
1293
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001294/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001295 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1296 * @bss: BSS table entry
1297 * @vendor_type: Vendor type (four octets starting the IE payload)
1298 * Returns: Pointer to the information element (id field) or %NULL if not found
1299 *
1300 * This function returns the first matching information element in the BSS
1301 * entry.
1302 *
1303 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1304 * from Beacon frames instead of either Beacon or Probe Response frames.
1305 */
1306const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1307 u32 vendor_type)
1308{
Hai Shalom60840252021-02-19 19:02:11 -08001309 const u8 *ies;
1310 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001311
1312 if (bss->beacon_ie_len == 0)
1313 return NULL;
1314
Hai Shalom60840252021-02-19 19:02:11 -08001315 ies = wpa_bss_ie_ptr(bss);
1316 ies += bss->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001317
Hai Shalom60840252021-02-19 19:02:11 -08001318 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1319 bss->beacon_ie_len) {
1320 if (elem->datalen >= 4 &&
1321 vendor_type == WPA_GET_BE32(elem->data))
1322 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001323 }
1324
1325 return NULL;
1326}
1327
1328
1329/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001330 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1331 * @bss: BSS table entry
1332 * @vendor_type: Vendor type (four octets starting the IE payload)
1333 * Returns: Pointer to the information element payload or %NULL if not found
1334 *
1335 * This function returns concatenated payload of possibly fragmented vendor
1336 * specific information elements in the BSS entry. The caller is responsible for
1337 * freeing the returned buffer.
1338 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1340 u32 vendor_type)
1341{
1342 struct wpabuf *buf;
1343 const u8 *end, *pos;
1344
1345 buf = wpabuf_alloc(bss->ie_len);
1346 if (buf == NULL)
1347 return NULL;
1348
Hai Shalom60840252021-02-19 19:02:11 -08001349 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 end = pos + bss->ie_len;
1351
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001352 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08001353 u8 ie, len;
1354
1355 ie = pos[0];
1356 len = pos[1];
1357 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 break;
Hai Shalom60840252021-02-19 19:02:11 -08001359 pos += 2;
1360 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1361 vendor_type == WPA_GET_BE32(pos))
1362 wpabuf_put_data(buf, pos + 4, len - 4);
1363 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364 }
1365
1366 if (wpabuf_len(buf) == 0) {
1367 wpabuf_free(buf);
1368 buf = NULL;
1369 }
1370
1371 return buf;
1372}
1373
1374
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001375/**
1376 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1377 * @bss: BSS table entry
1378 * @vendor_type: Vendor type (four octets starting the IE payload)
1379 * Returns: Pointer to the information element payload or %NULL if not found
1380 *
1381 * This function returns concatenated payload of possibly fragmented vendor
1382 * specific information elements in the BSS entry. The caller is responsible for
1383 * freeing the returned buffer.
1384 *
1385 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1386 * from Beacon frames instead of either Beacon or Probe Response frames.
1387 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001388struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1389 u32 vendor_type)
1390{
1391 struct wpabuf *buf;
1392 const u8 *end, *pos;
1393
1394 buf = wpabuf_alloc(bss->beacon_ie_len);
1395 if (buf == NULL)
1396 return NULL;
1397
Hai Shalom60840252021-02-19 19:02:11 -08001398 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001399 pos += bss->ie_len;
1400 end = pos + bss->beacon_ie_len;
1401
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001402 while (end - pos > 1) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001403 u8 id, len;
1404
1405 id = *pos++;
1406 len = *pos++;
1407 if (len > end - pos)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001408 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001409 if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1410 vendor_type == WPA_GET_BE32(pos))
1411 wpabuf_put_data(buf, pos + 4, len - 4);
1412 pos += len;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001413 }
1414
1415 if (wpabuf_len(buf) == 0) {
1416 wpabuf_free(buf);
1417 buf = NULL;
1418 }
1419
1420 return buf;
1421}
1422
1423
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001424/**
1425 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1426 * @bss: BSS table entry
1427 * Returns: Maximum legacy rate in units of 500 kbps
1428 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1430{
1431 int rate = 0;
1432 const u8 *ie;
1433 int i;
1434
1435 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1436 for (i = 0; ie && i < ie[1]; i++) {
1437 if ((ie[i + 2] & 0x7f) > rate)
1438 rate = ie[i + 2] & 0x7f;
1439 }
1440
1441 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1442 for (i = 0; ie && i < ie[1]; i++) {
1443 if ((ie[i + 2] & 0x7f) > rate)
1444 rate = ie[i + 2] & 0x7f;
1445 }
1446
1447 return rate;
1448}
1449
1450
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001451/**
1452 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1453 * @bss: BSS table entry
1454 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1455 * Returns: number of legacy TX rates or -1 on failure
1456 *
1457 * The caller is responsible for freeing the returned buffer with os_free() in
1458 * case of success.
1459 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1461{
1462 const u8 *ie, *ie2;
1463 int i, j;
1464 unsigned int len;
1465 u8 *r;
1466
1467 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1468 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1469
1470 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1471
1472 r = os_malloc(len);
1473 if (!r)
1474 return -1;
1475
1476 for (i = 0; ie && i < ie[1]; i++)
1477 r[i] = ie[i + 2] & 0x7f;
1478
1479 for (j = 0; ie2 && j < ie2[1]; j++)
1480 r[i + j] = ie2[j + 2] & 0x7f;
1481
1482 *rates = r;
1483 return len;
1484}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001485
1486
1487#ifdef CONFIG_FILS
Hai Shalom60840252021-02-19 19:02:11 -08001488const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001489{
1490 const u8 *ie;
1491
1492 if (bss) {
1493 ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
1494 if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7))
1495 return ie + 4;
1496 }
1497
1498 return NULL;
1499}
1500#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001501
1502
1503int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab)
1504{
Hai Shalom60840252021-02-19 19:02:11 -08001505 if (!bss)
1506 return 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08001507 return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB),
1508 capab);
1509}
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001510
1511
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001512static void
1513wpa_bss_parse_ml_rnr_ap_info(struct wpa_supplicant *wpa_s,
1514 struct wpa_bss *bss, u8 mbssid_idx,
1515 const struct ieee80211_neighbor_ap_info *ap_info,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001516 size_t len, u16 *seen, u16 *missing,
1517 struct wpa_ssid *ssid)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001518{
1519 const u8 *pos, *end;
1520 const u8 *mld_params;
1521 u8 count, mld_params_offset;
1522 u8 i, type, link_id;
1523
1524 count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
1525 type = ap_info->tbtt_info_hdr & RNR_TBTT_INFO_HDR_TYPE_MSK;
1526
1527 /* MLD information is at offset 13 or at start */
1528 if (type == 0 && ap_info->tbtt_info_len >= RNR_TBTT_INFO_MLD_LEN) {
1529 /* MLD info is appended */
1530 mld_params_offset = RNR_TBTT_INFO_LEN;
1531 } else {
1532 /* TODO: Support NSTR AP */
1533 return;
1534 }
1535
1536 pos = (const u8 *) ap_info;
1537 end = pos + len;
1538 pos += sizeof(*ap_info);
1539
1540 for (i = 0; i < count; i++) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001541 u8 bss_params;
1542
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001543 if (end - pos < ap_info->tbtt_info_len)
1544 break;
1545
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001546 bss_params = pos[1 + ETH_ALEN + 4];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001547 mld_params = pos + mld_params_offset;
1548
1549 link_id = *(mld_params + 1) & EHT_ML_LINK_ID_MSK;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001550 if (link_id >= MAX_NUM_MLD_LINKS)
1551 return;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001552
1553 if (*mld_params != mbssid_idx) {
1554 wpa_printf(MSG_DEBUG,
1555 "MLD: Reported link not part of MLD");
1556 } else if (!(BIT(link_id) & *seen)) {
Sunil Ravi7f769292024-07-23 22:21:32 +00001557 struct wpa_bss *neigh_bss;
1558
1559 if (ssid && ssid->ssid_len)
1560 neigh_bss = wpa_bss_get(wpa_s, pos + 1,
1561 ssid->ssid,
1562 ssid->ssid_len);
1563 else
1564 neigh_bss = wpa_bss_get_bssid(wpa_s, pos + 1);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001565
1566 *seen |= BIT(link_id);
1567 wpa_printf(MSG_DEBUG, "MLD: mld ID=%u, link ID=%u",
1568 *mld_params, link_id);
1569
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001570 if (!neigh_bss) {
1571 *missing |= BIT(link_id);
1572 } else if ((!ssid ||
1573 (bss_params & (RNR_BSS_PARAM_SAME_SSID |
1574 RNR_BSS_PARAM_CO_LOCATED)) ||
1575 wpa_scan_res_match(wpa_s, 0, neigh_bss,
1576 ssid, 1, 0)) &&
1577 !wpa_bssid_ignore_is_listed(
1578 wpa_s, neigh_bss->bssid)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001579 struct mld_link *l;
1580
Sunil Ravi99c035e2024-07-12 01:42:03 +00001581 bss->valid_links |= BIT(link_id);
1582 l = &bss->mld_links[link_id];
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001583 os_memcpy(l->bssid, pos + 1, ETH_ALEN);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001584 l->freq = neigh_bss->freq;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001585 l->disabled = mld_params[2] &
1586 RNR_TBTT_INFO_MLD_PARAM2_LINK_DISABLED;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001587 }
1588 }
1589
1590 pos += ap_info->tbtt_info_len;
1591 }
1592}
1593
1594
1595/**
1596 * wpa_bss_parse_basic_ml_element - Parse the Basic Multi-Link element
1597 * @wpa_s: Pointer to wpa_supplicant data
1598 * @bss: BSS table entry
1599 * @mld_addr: AP MLD address (or %NULL)
1600 * @link_info: Array to store link information (or %NULL),
1601 * should be initialized and #MAX_NUM_MLD_LINKS elements long
1602 * @missing_links: Result bitmask of links that were not discovered (or %NULL)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001603 * @ssid: Target SSID (or %NULL)
1604 * @ap_mld_id: On return would hold the corresponding AP MLD ID (or %NULL)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001605 * Returns: 0 on success or -1 for non-MLD or parsing failures
1606 *
1607 * Parses the Basic Multi-Link element of the BSS into @link_info using the scan
1608 * information stored in the wpa_supplicant data to fill in information for
1609 * links where possible. The @missing_links out parameter will contain any links
1610 * for which no corresponding BSS was found.
1611 */
1612int wpa_bss_parse_basic_ml_element(struct wpa_supplicant *wpa_s,
1613 struct wpa_bss *bss,
1614 u8 *ap_mld_addr,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001615 u16 *missing_links,
1616 struct wpa_ssid *ssid,
1617 u8 *ap_mld_id)
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001618{
1619 struct ieee802_11_elems elems;
1620 struct wpabuf *mlbuf;
1621 const struct element *elem;
1622 u8 mbssid_idx = 0;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001623 size_t ml_ie_len;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001624 const struct ieee80211_eht_ml *eht_ml;
1625 const struct eht_ml_basic_common_info *ml_basic_common_info;
1626 u8 i, link_id;
1627 const u16 control_mask =
1628 MULTI_LINK_CONTROL_TYPE_MASK |
1629 BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1630 BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1631 BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
1632 const u16 control =
1633 MULTI_LINK_CONTROL_TYPE_BASIC |
1634 BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1635 BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1636 BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
1637 u16 missing = 0;
1638 u16 seen;
1639 const u8 *ies_pos = wpa_bss_ie_ptr(bss);
1640 size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
1641 int ret = -1;
1642 struct mld_link *l;
1643
1644 if (ieee802_11_parse_elems(ies_pos, ies_len, &elems, 1) ==
1645 ParseFailed) {
1646 wpa_dbg(wpa_s, MSG_DEBUG, "MLD: Failed to parse elements");
1647 return ret;
1648 }
1649
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001650 mlbuf = ieee802_11_defrag(elems.basic_mle, elems.basic_mle_len, true);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001651 if (!mlbuf) {
1652 wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No Multi-Link element");
1653 return ret;
1654 }
1655
1656 ml_ie_len = wpabuf_len(mlbuf);
1657
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001658 if (ssid) {
1659 struct wpa_ie_data ie;
Sunil Ravi7f769292024-07-23 22:21:32 +00001660 const u8 *rsne;
1661 size_t rsne_len;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001662
Sunil Ravi7f769292024-07-23 22:21:32 +00001663 if (elems.rsne_override_2 && wpas_rsn_overriding(wpa_s)) {
1664 rsne = elems.rsne_override_2;
1665 rsne_len = elems.rsne_override_2_len;
1666 } else if (elems.rsne_override &&
1667 wpas_rsn_overriding(wpa_s)) {
1668 rsne = elems.rsne_override;
1669 rsne_len = elems.rsne_override_len;
1670 } else {
1671 rsne = elems.rsn_ie;
1672 rsne_len = elems.rsn_ie_len;
1673 }
1674 if (!rsne ||
1675 wpa_parse_wpa_ie(rsne - 2, 2 + rsne_len, &ie)) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001676 wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No RSN element");
1677 goto out;
1678 }
1679
1680 if (!(ie.capabilities & WPA_CAPABILITY_MFPC) ||
1681 wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION) {
1682 wpa_dbg(wpa_s, MSG_DEBUG,
1683 "MLD: No management frame protection");
1684 goto out;
1685 }
1686
1687 ie.key_mgmt &= ~(WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
1688 WPA_KEY_MGMT_PSK_SHA256);
1689 if (!(ie.key_mgmt & ssid->key_mgmt)) {
1690 wpa_dbg(wpa_s, MSG_DEBUG,
1691 "MLD: No valid key management");
1692 goto out;
1693 }
1694 }
1695
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001696 /*
1697 * for ext ID + 2 control + common info len + MLD address +
1698 * link info
1699 */
1700 if (ml_ie_len < 2UL + 1UL + ETH_ALEN + 1UL)
1701 goto out;
1702
1703 eht_ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
1704 if ((le_to_host16(eht_ml->ml_control) & control_mask) != control) {
1705 wpa_printf(MSG_DEBUG,
1706 "MLD: Unexpected Multi-Link element control=0x%x (mask 0x%x expected 0x%x)",
1707 le_to_host16(eht_ml->ml_control), control_mask,
1708 control);
1709 goto out;
1710 }
1711
1712 ml_basic_common_info =
1713 (const struct eht_ml_basic_common_info *) eht_ml->variable;
1714
1715 /* Common info length should be valid */
1716 if (ml_basic_common_info->len < ETH_ALEN + 1UL)
1717 goto out;
1718
1719 /* Get the MLD address and MLD link ID */
1720 if (ap_mld_addr)
1721 os_memcpy(ap_mld_addr, ml_basic_common_info->mld_addr,
1722 ETH_ALEN);
1723
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001724 link_id = ml_basic_common_info->variable[0] & EHT_ML_LINK_ID_MSK;
Sunil Ravi99c035e2024-07-12 01:42:03 +00001725
1726 bss->mld_link_id = link_id;
1727 seen = bss->valid_links = BIT(link_id);
1728
1729 l = &bss->mld_links[link_id];
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001730 os_memcpy(l->bssid, bss->bssid, ETH_ALEN);
1731 l->freq = bss->freq;
1732
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001733
1734 /*
1735 * The AP MLD ID in the RNR corresponds to the MBSSID index, see
1736 * IEEE P802.11be/D4.0, 9.4.2.169.2 (Neighbor AP Information field).
1737 *
1738 * For the transmitting BSSID it is clear that both the MBSSID index
1739 * and the AP MLD ID in the RNR are zero.
1740 *
1741 * For nontransmitted BSSIDs we will have a BSS generated from the
1742 * MBSSID element(s) using inheritance rules. Included in the elements
1743 * is the MBSSID Index Element. The RNR is copied from the Beacon/Probe
1744 * Response frame that was send by the transmitting BSSID. As such, the
1745 * reported AP MLD ID in the RNR will match the value in the MBSSID
1746 * Index Element.
1747 */
1748 elem = (const struct element *)
1749 wpa_bss_get_ie(bss, WLAN_EID_MULTIPLE_BSSID_INDEX);
1750 if (elem && elem->datalen >= 1)
1751 mbssid_idx = elem->data[0];
1752
1753 for_each_element_id(elem, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
1754 wpa_bss_ie_ptr(bss),
1755 bss->ie_len ? bss->ie_len : bss->beacon_ie_len) {
1756 const struct ieee80211_neighbor_ap_info *ap_info;
1757 const u8 *pos = elem->data;
1758 size_t len = elem->datalen;
1759
1760 /* RNR IE may contain more than one Neighbor AP Info */
1761 while (sizeof(*ap_info) <= len) {
1762 size_t ap_info_len = sizeof(*ap_info);
1763 u8 count;
1764
1765 ap_info = (const struct ieee80211_neighbor_ap_info *)
1766 pos;
1767 count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
1768 ap_info_len += count * ap_info->tbtt_info_len;
1769
1770 if (ap_info_len > len)
1771 goto out;
1772
1773 wpa_bss_parse_ml_rnr_ap_info(wpa_s, bss, mbssid_idx,
1774 ap_info, len, &seen,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001775 &missing, ssid);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001776
1777 pos += ap_info_len;
1778 len -= ap_info_len;
1779 }
1780 }
1781
Sunil Ravi99c035e2024-07-12 01:42:03 +00001782 wpa_printf(MSG_DEBUG, "MLD: valid_links=%04hx (unresolved: 0x%04hx)",
1783 bss->valid_links, missing);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001784
Sunil Ravi99c035e2024-07-12 01:42:03 +00001785 for_each_link(bss->valid_links, i) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001786 wpa_printf(MSG_DEBUG, "MLD: link=%u, bssid=" MACSTR,
Sunil Ravi99c035e2024-07-12 01:42:03 +00001787 i, MAC2STR(bss->mld_links[i].bssid));
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001788 }
1789
1790 if (missing_links)
1791 *missing_links = missing;
1792
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001793 if (ap_mld_id)
1794 *ap_mld_id = mbssid_idx;
1795
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001796 ret = 0;
1797out:
1798 wpabuf_free(mlbuf);
1799 return ret;
1800}
1801
1802
1803/*
1804 * wpa_bss_parse_reconf_ml_element - Parse the Reconfiguration ML element
1805 * @wpa_s: Pointer to wpa_supplicant data
1806 * @bss: BSS table entry
1807 * Returns: The bitmap of links that are going to be removed
1808 */
1809u16 wpa_bss_parse_reconf_ml_element(struct wpa_supplicant *wpa_s,
1810 struct wpa_bss *bss)
1811{
1812 struct ieee802_11_elems elems;
1813 struct wpabuf *mlbuf;
1814 const u8 *pos = wpa_bss_ie_ptr(bss);
1815 size_t len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
1816 const struct ieee80211_eht_ml *ml;
1817 u16 removed_links = 0;
1818 u8 ml_common_len;
1819
1820 if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
1821 return 0;
1822
1823 if (!elems.reconf_mle || !elems.reconf_mle_len)
1824 return 0;
1825
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001826 mlbuf = ieee802_11_defrag(elems.reconf_mle, elems.reconf_mle_len, true);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001827 if (!mlbuf)
1828 return 0;
1829
1830 ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
1831 len = wpabuf_len(mlbuf);
1832
1833 if (len < sizeof(*ml))
1834 goto out;
1835
1836 ml_common_len = 1;
1837 if (ml->ml_control & RECONF_MULTI_LINK_CTRL_PRES_MLD_MAC_ADDR)
1838 ml_common_len += ETH_ALEN;
1839
1840 if (len < sizeof(*ml) + ml_common_len) {
1841 wpa_printf(MSG_DEBUG,
1842 "MLD: Unexpected Reconfiguration ML element length: (%zu < %zu)",
1843 len, sizeof(*ml) + ml_common_len);
1844 goto out;
1845 }
1846
1847 pos = ml->variable + ml_common_len;
1848 len -= sizeof(*ml) + ml_common_len;
1849
1850 while (len >= 2 + sizeof(struct ieee80211_eht_per_sta_profile)) {
1851 size_t sub_elem_len = *(pos + 1);
1852
1853 if (2 + sub_elem_len > len) {
1854 wpa_printf(MSG_DEBUG,
1855 "MLD: Invalid link info len: %zu %zu",
1856 2 + sub_elem_len, len);
1857 goto out;
1858 }
1859
1860 if (*pos == EHT_ML_SUB_ELEM_PER_STA_PROFILE) {
1861 const struct ieee80211_eht_per_sta_profile *sta_prof =
1862 (const struct ieee80211_eht_per_sta_profile *)
1863 (pos + 2);
1864 u16 control = le_to_host16(sta_prof->sta_control);
1865 u8 link_id;
1866
1867 link_id = control & EHT_PER_STA_RECONF_CTRL_LINK_ID_MSK;
1868 removed_links |= BIT(link_id);
1869 }
1870
1871 pos += 2 + sub_elem_len;
1872 len -= 2 + sub_elem_len;
1873 }
1874
1875 wpa_printf(MSG_DEBUG, "MLD: Reconfiguration: removed_links=0x%x",
1876 removed_links);
1877out:
1878 wpabuf_free(mlbuf);
1879 return removed_links;
1880}
Sunil Ravi7f769292024-07-23 22:21:32 +00001881
1882
1883static bool wpa_bss_supported_cipher(struct wpa_supplicant *wpa_s,
1884 int pairwise_cipher)
1885{
1886 if (!wpa_s->drv_enc)
1887 return true;
1888
1889 if ((pairwise_cipher & WPA_CIPHER_CCMP) &&
1890 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP))
1891 return true;
1892
1893 if ((pairwise_cipher & WPA_CIPHER_GCMP) &&
1894 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP))
1895 return true;
1896
1897 if ((pairwise_cipher & WPA_CIPHER_CCMP_256) &&
1898 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256))
1899 return true;
1900
1901 if ((pairwise_cipher & WPA_CIPHER_GCMP_256) &&
1902 (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256))
1903 return true;
1904
1905 return false;
1906}
1907
1908
1909static bool wpa_bss_supported_key_mgmt(struct wpa_supplicant *wpa_s,
1910 int key_mgmt)
1911{
1912 if (!wpa_s->drv_key_mgmt)
1913 return true;
1914
1915 if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X) &&
1916 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2))
1917 return true;
1918 if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) &&
1919 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256))
1920 return true;
1921 if ((key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) &&
1922 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT))
1923 return true;
1924 if ((key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
1925 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384))
1926 return true;
1927 if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) &&
1928 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B))
1929 return true;
1930 if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) &&
1931 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192))
1932 return true;
1933 if ((key_mgmt & WPA_KEY_MGMT_PSK) &&
1934 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK))
1935 return true;
1936 if ((key_mgmt & WPA_KEY_MGMT_FT_PSK) &&
1937 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK))
1938 return true;
1939 if ((key_mgmt & WPA_KEY_MGMT_PSK_SHA256) &&
1940 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256))
1941 return true;
1942 if ((key_mgmt & WPA_KEY_MGMT_SAE) &&
1943 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE))
1944 return true;
1945 if ((key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) &&
1946 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY))
1947 return true;
1948 if ((key_mgmt & WPA_KEY_MGMT_FT_SAE) &&
1949 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE))
1950 return true;
1951 if ((key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
1952 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY))
1953 return true;
1954 if ((key_mgmt & WPA_KEY_MGMT_OWE) &&
1955 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE))
1956 return true;
1957 if ((key_mgmt & WPA_KEY_MGMT_DPP) &&
1958 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP))
1959 return true;
1960 if ((key_mgmt & WPA_KEY_MGMT_FILS_SHA256) &&
1961 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256))
1962 return true;
1963 if ((key_mgmt & WPA_KEY_MGMT_FILS_SHA384) &&
1964 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384))
1965 return true;
1966 if ((key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) &&
1967 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256))
1968 return true;
1969 if ((key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) &&
1970 (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384))
1971 return true;
1972
1973 return false;
1974}
1975
1976
1977static bool wpa_bss_supported_rsne(struct wpa_supplicant *wpa_s,
1978 struct wpa_ssid *ssid, const u8 *ie)
1979{
1980 struct wpa_ie_data data;
1981
1982 if (wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) < 0)
1983 return false;
1984
1985 /* Check that there is a supported AKM and pairwise cipher based on
1986 * overall capabilities */
1987 if (!data.pairwise_cipher || !data.key_mgmt)
1988 return false;
1989
1990 if (wpa_s->drv_capa_known) {
1991 if (!wpa_bss_supported_cipher(wpa_s, data.pairwise_cipher) ||
1992 !wpa_bss_supported_key_mgmt(wpa_s, data.key_mgmt))
1993 return false;
1994 }
1995
1996 if (ssid) {
1997 /* Check that there is a supported AKM and pairwise cipher
1998 * based on the specific network profile. */
1999 if ((ssid->pairwise_cipher & data.pairwise_cipher) == 0)
2000 return false;
2001 if ((ssid->key_mgmt & data.key_mgmt) == 0)
2002 return false;
2003 }
2004
2005 return true;
2006}
2007
2008
2009const u8 * wpa_bss_get_rsne(struct wpa_supplicant *wpa_s,
2010 const struct wpa_bss *bss, struct wpa_ssid *ssid,
2011 bool mlo)
2012{
2013 const u8 *ie;
2014
2015 if (wpas_rsn_overriding(wpa_s)) {
2016 if (!ssid)
2017 ssid = wpa_s->current_ssid;
2018
2019 /* MLO cases for RSN overriding are required to use RSNE
2020 * Override 2 element and RSNXE Override element together. */
2021 ie = wpa_bss_get_vendor_ie(bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
2022 if (mlo && ie &&
2023 !wpa_bss_get_vendor_ie(bss,
2024 RSNXE_OVERRIDE_IE_VENDOR_TYPE)) {
2025 wpa_printf(MSG_DEBUG, "BSS " MACSTR
2026 " advertises RSNE Override 2 element without RSNXE Override element - ignore RSNE Override 2 element for MLO",
2027 MAC2STR(bss->bssid));
2028 } else if (ie && wpa_bss_supported_rsne(wpa_s, ssid, ie)) {
2029 return ie;
2030 }
2031
2032 if (!mlo) {
2033 ie = wpa_bss_get_vendor_ie(
2034 bss, RSNE_OVERRIDE_IE_VENDOR_TYPE);
2035 if (ie && wpa_bss_supported_rsne(wpa_s, ssid, ie))
2036 return ie;
2037 }
2038 }
2039
2040 return wpa_bss_get_ie(bss, WLAN_EID_RSN);
2041}
2042
2043
2044const u8 * wpa_bss_get_rsnxe(struct wpa_supplicant *wpa_s,
2045 const struct wpa_bss *bss, struct wpa_ssid *ssid,
2046 bool mlo)
2047{
2048 const u8 *ie;
2049
2050 if (wpas_rsn_overriding(wpa_s)) {
2051 ie = wpa_bss_get_vendor_ie(bss, RSNXE_OVERRIDE_IE_VENDOR_TYPE);
2052 if (ie) {
2053 const u8 *tmp;
2054
2055 tmp = wpa_bss_get_rsne(wpa_s, bss, ssid, mlo);
2056 if (!tmp || tmp[0] == WLAN_EID_RSN) {
2057 /* An acceptable RSNE override element was not
2058 * found, so need to ignore RSNXE overriding. */
2059 goto out;
2060 }
2061
2062 return ie;
2063 }
2064
2065 /* MLO cases for RSN overriding are required to use RSNE
2066 * Override 2 element and RSNXE Override element together. */
2067 if (mlo && wpa_bss_get_vendor_ie(
2068 bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE)) {
2069 wpa_printf(MSG_DEBUG, "BSS " MACSTR
2070 " advertises RSNXE Override element without RSNE Override 2 element - ignore RSNXE Override element for MLO",
2071 MAC2STR(bss->bssid));
2072 goto out;
2073 }
2074 }
2075
2076out:
2077 return wpa_bss_get_ie(bss, WLAN_EID_RSNX);
2078}