blob: c1be660415b1f4724611aa8df7d55207d9638356 [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
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000186static struct wpa_connect_work *
187wpa_bss_check_pending_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800188{
189 struct wpa_radio_work *work;
190 struct wpa_connect_work *cwork;
191
192 work = radio_work_pending(wpa_s, "sme-connect");
193 if (!work)
194 work = radio_work_pending(wpa_s, "connect");
195 if (!work)
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000196 return NULL;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800197
198 cwork = work->ctx;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000199 if (cwork->bss != bss)
200 return NULL;
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800201
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000202 return cwork;
203}
204
205
206static void wpa_bss_update_pending_connect(struct wpa_connect_work *cwork,
207 struct wpa_bss *new_bss)
208{
Dmitry Shmidt2e425d62014-11-10 11:18:27 -0800209 wpa_printf(MSG_DEBUG,
210 "Update BSS pointer for the pending connect radio work");
211 cwork->bss = new_bss;
212 if (!new_bss)
213 cwork->bss_removed = 1;
214}
215
216
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800217void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
218 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219{
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000220 struct wpa_connect_work *cwork;
221
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700222 if (wpa_s->last_scan_res) {
223 unsigned int i;
224 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
225 if (wpa_s->last_scan_res[i] == bss) {
226 os_memmove(&wpa_s->last_scan_res[i],
227 &wpa_s->last_scan_res[i + 1],
228 (wpa_s->last_scan_res_used - i - 1)
229 * sizeof(struct wpa_bss *));
230 wpa_s->last_scan_res_used--;
231 break;
232 }
233 }
234 }
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000235 cwork = wpa_bss_check_pending_connect(wpa_s, bss);
236 if (cwork)
237 wpa_bss_update_pending_connect(cwork, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238 dl_list_del(&bss->list);
239 dl_list_del(&bss->list_id);
240 wpa_s->num_bss--;
241 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700242 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
243 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700245 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246 os_free(bss);
247}
248
249
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800250/**
251 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
252 * @wpa_s: Pointer to wpa_supplicant data
Matthew Wangdcf19452022-11-07 20:42:52 -0800253 * @bssid: BSSID, or %NULL to match any BSSID
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800254 * @ssid: SSID
255 * @ssid_len: Length of @ssid
256 * Returns: Pointer to the BSS entry or %NULL if not found
257 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
259 const u8 *ssid, size_t ssid_len)
260{
261 struct wpa_bss *bss;
Matthew Wangafc981e2023-03-17 21:30:12 +0000262
263 if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid))
Dmitry Shmidt04949592012-07-19 12:16:46 -0700264 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Matthew Wangafc981e2023-03-17 21:30:12 +0000266 if ((!bssid || os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 bss->ssid_len == ssid_len &&
268 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
269 return bss;
270 }
271 return NULL;
272}
273
274
Dmitry Shmidt29333592017-01-09 12:27:11 -0800275void calculate_update_time(const struct os_reltime *fetch_time,
276 unsigned int age_ms,
277 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700278{
279 os_time_t usec;
280
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700281 update_time->sec = fetch_time->sec;
282 update_time->usec = fetch_time->usec;
283 update_time->sec -= age_ms / 1000;
284 usec = (age_ms % 1000) * 1000;
285 if (update_time->usec < usec) {
286 update_time->sec--;
287 update_time->usec += 1000000;
288 }
289 update_time->usec -= usec;
290}
291
292
293static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800294 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700295{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 dst->flags = src->flags;
297 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
298 dst->freq = src->freq;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000299 dst->max_cw = src->max_cw;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 dst->beacon_int = src->beacon_int;
301 dst->caps = src->caps;
302 dst->qual = src->qual;
303 dst->noise = src->noise;
304 dst->level = src->level;
305 dst->tsf = src->tsf;
Sunil Ravia04bd252022-05-02 22:54:18 -0700306 dst->beacon_newer = src->beacon_newer;
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800307 dst->est_throughput = src->est_throughput;
308 dst->snr = src->snr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700310 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311}
312
313
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700314static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
315 struct wpa_bss *bss)
316{
317#ifdef CONFIG_WPS
318 struct wpa_ssid *ssid;
319 struct wpabuf *wps_ie;
320 int pbc = 0, ret;
321
322 wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
323 if (!wps_ie)
324 return 0;
325
326 if (wps_is_selected_pbc_registrar(wps_ie)) {
327 pbc = 1;
328 } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
329 wpabuf_free(wps_ie);
330 return 0;
331 }
332
333 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
334 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
335 continue;
336 if (ssid->ssid_len &&
337 (ssid->ssid_len != bss->ssid_len ||
338 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
339 continue;
340
341 if (pbc)
342 ret = eap_is_wps_pbc_enrollee(&ssid->eap);
343 else
344 ret = eap_is_wps_pin_enrollee(&ssid->eap);
345 wpabuf_free(wps_ie);
346 return ret;
347 }
348 wpabuf_free(wps_ie);
349#endif /* CONFIG_WPS */
350
351 return 0;
352}
353
354
Hai Shalom60840252021-02-19 19:02:11 -0800355static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s,
356 struct wpa_bss *bss)
357{
358#ifdef CONFIG_P2P
359 u8 addr[ETH_ALEN];
360
361 if (os_memcmp(bss->bssid, wpa_s->pending_join_iface_addr,
362 ETH_ALEN) == 0)
363 return true;
364 if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) &&
365 p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 &&
366 os_memcmp(addr, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0)
367 return true;
368#endif /* CONFIG_P2P */
369 return false;
370}
371
372
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
374{
375 struct wpa_ssid *ssid;
376
Hai Shalom60840252021-02-19 19:02:11 -0800377 if (is_p2p_pending_bss(wpa_s, bss))
378 return 1;
379
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
381 if (ssid->ssid == NULL || ssid->ssid_len == 0)
382 continue;
383 if (ssid->ssid_len == bss->ssid_len &&
384 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
385 return 1;
386 }
387
388 return 0;
389}
390
391
Dmitry Shmidt04949592012-07-19 12:16:46 -0700392static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
393{
Sunil Ravi89eba102022-09-13 21:04:37 -0700394 int i;
395
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800396 if (bss == wpa_s->current_bss)
397 return 1;
398
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000399 if (bss == wpa_s->ml_connect_probe_bss)
400 return 1;
401
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800402 if (wpa_s->current_bss &&
403 (bss->ssid_len != wpa_s->current_bss->ssid_len ||
404 os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
405 bss->ssid_len) != 0))
406 return 0; /* SSID has changed */
407
Sunil Ravi89eba102022-09-13 21:04:37 -0700408 if (!is_zero_ether_addr(bss->bssid) &&
409 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
410 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0))
411 return 1;
412
413 if (!wpa_s->valid_links)
414 return 0;
415
416 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
417 if (!(wpa_s->valid_links & BIT(i)))
418 continue;
419
420 if (os_memcmp(bss->bssid, wpa_s->links[i].bssid, ETH_ALEN) == 0)
421 return 1;
422 }
423
424 return 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700425}
426
427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
429{
430 struct wpa_bss *bss;
431
432 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700433 if (!wpa_bss_known(wpa_s, bss) &&
434 !wpa_bss_is_wps_candidate(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700435 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800436 return 0;
437 }
438 }
439
440 return -1;
441}
442
443
Dmitry Shmidt04949592012-07-19 12:16:46 -0700444static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800445{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700446 struct wpa_bss *bss;
447
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800448 /*
449 * Remove the oldest entry that does not match with any configured
450 * network.
451 */
452 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700453 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800454
455 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700456 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800457 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700458 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
459 if (!wpa_bss_in_use(wpa_s, bss)) {
460 wpa_bss_remove(wpa_s, bss, __func__);
461 return 0;
462 }
463 }
464
465 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800466}
467
468
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700469static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
470 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800471 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800472 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473{
474 struct wpa_bss *bss;
Sunil Ravi89eba102022-09-13 21:04:37 -0700475 char extra[100];
476 const u8 *ml_ie;
477 char *pos, *end;
478 int ret = 0;
479 const u8 *mld_addr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
482 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700483 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 bss->id = wpa_s->bss_next_id++;
485 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800486 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 os_memcpy(bss->ssid, ssid, ssid_len);
488 bss->ssid_len = ssid_len;
489 bss->ie_len = res->ie_len;
490 bss->beacon_ie_len = res->beacon_ie_len;
Hai Shalom60840252021-02-19 19:02:11 -0800491 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700492 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493
Sunil Ravi89eba102022-09-13 21:04:37 -0700494 os_memset(bss->mld_addr, 0, ETH_ALEN);
495 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
496 if (ml_ie) {
497 mld_addr = get_basic_mle_mld_addr(&ml_ie[3], ml_ie[1] - 1);
498 if (mld_addr)
499 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
500 }
501
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300502 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
503 wpa_bss_remove_oldest(wpa_s) != 0) {
504 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
505 "because all BSSes are in use. We should normally "
506 "not get here!", (int) wpa_s->num_bss + 1);
507 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
508 }
509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 dl_list_add_tail(&wpa_s->bss, &bss->list);
511 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
512 wpa_s->num_bss++;
Sunil Ravi89eba102022-09-13 21:04:37 -0700513
514 extra[0] = '\0';
515 pos = extra;
516 end = pos + sizeof(extra);
Hai Shalom81f62d82019-07-22 12:10:00 -0700517 if (!is_zero_ether_addr(bss->hessid))
Sunil Ravi89eba102022-09-13 21:04:37 -0700518 ret = os_snprintf(pos, end - pos, " HESSID " MACSTR,
519 MAC2STR(bss->hessid));
520
521 if (!is_zero_ether_addr(bss->mld_addr) &&
522 !os_snprintf_error(end - pos, ret)) {
523 pos += ret;
524 ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR,
525 MAC2STR(bss->mld_addr));
526 }
527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
Hai Shalom81f62d82019-07-22 12:10:00 -0700529 " SSID '%s' freq %d%s",
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800530 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
Hai Shalom81f62d82019-07-22 12:10:00 -0700531 bss->freq, extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700533 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534}
535
536
537static int are_ies_equal(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700538 const struct wpa_scan_res *new_res, u32 ie)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539{
540 const u8 *old_ie, *new_ie;
541 struct wpabuf *old_ie_buff = NULL;
542 struct wpabuf *new_ie_buff = NULL;
543 int new_ie_len, old_ie_len, ret, is_multi;
544
545 switch (ie) {
546 case WPA_IE_VENDOR_TYPE:
547 old_ie = wpa_bss_get_vendor_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700548 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 is_multi = 0;
550 break;
551 case WPS_IE_VENDOR_TYPE:
552 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700553 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 is_multi = 1;
555 break;
556 case WLAN_EID_RSN:
557 case WLAN_EID_SUPP_RATES:
558 case WLAN_EID_EXT_SUPP_RATES:
559 old_ie = wpa_bss_get_ie(old, ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700560 new_ie = wpa_scan_get_ie(new_res, ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 is_multi = 0;
562 break;
563 default:
564 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
565 return 0;
566 }
567
568 if (is_multi) {
569 /* in case of multiple IEs stored in buffer */
570 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
571 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
572 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
573 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
574 } else {
575 /* in case of single IE */
576 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
577 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
578 }
579
580 if (!old_ie || !new_ie)
581 ret = !old_ie && !new_ie;
582 else
583 ret = (old_ie_len == new_ie_len &&
584 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
585
586 wpabuf_free(old_ie_buff);
587 wpabuf_free(new_ie_buff);
588
589 return ret;
590}
591
592
593static u32 wpa_bss_compare_res(const struct wpa_bss *old,
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700594 const struct wpa_scan_res *new_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595{
596 u32 changes = 0;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700597 int caps_diff = old->caps ^ new_res->caps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700599 if (old->freq != new_res->freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
601
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700602 if (old->level != new_res->level)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700603 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
604
605 if (caps_diff & IEEE80211_CAP_PRIVACY)
606 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
607
608 if (caps_diff & IEEE80211_CAP_IBSS)
609 changes |= WPA_BSS_MODE_CHANGED_FLAG;
610
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700611 if (old->ie_len == new_res->ie_len &&
Hai Shalom60840252021-02-19 19:02:11 -0800612 os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 return changes;
614 changes |= WPA_BSS_IES_CHANGED_FLAG;
615
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700616 if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
618
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700619 if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
621
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700622 if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623 changes |= WPA_BSS_WPS_CHANGED_FLAG;
624
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700625 if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
626 !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 changes |= WPA_BSS_RATES_CHANGED_FLAG;
628
629 return changes;
630}
631
632
Hai Shalom60840252021-02-19 19:02:11 -0800633void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
634 const struct wpa_bss *bss)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635{
636 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
637 wpas_notify_bss_freq_changed(wpa_s, bss->id);
638
639 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
640 wpas_notify_bss_signal_changed(wpa_s, bss->id);
641
642 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
643 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
644
645 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
646 wpas_notify_bss_mode_changed(wpa_s, bss->id);
647
648 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
649 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
650
651 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
652 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
653
654 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
655 wpas_notify_bss_wps_changed(wpa_s, bss->id);
656
657 if (changes & WPA_BSS_IES_CHANGED_FLAG)
658 wpas_notify_bss_ies_changed(wpa_s, bss->id);
659
660 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
661 wpas_notify_bss_rates_changed(wpa_s, bss->id);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700662
663 wpas_notify_bss_seen(wpa_s, bss->id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664}
665
666
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700667static struct wpa_bss *
668wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800669 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670{
671 u32 changes;
672
Dmitry Shmidtabb90a32016-12-05 15:34:39 -0800673 if (bss->last_update_idx == wpa_s->bss_update_idx) {
674 struct os_reltime update_time;
675
676 /*
677 * Some drivers (e.g., cfg80211) include multiple BSS entries
678 * for the same BSS if that BSS's channel changes. The BSS list
679 * implementation in wpa_supplicant does not do that and we need
680 * to filter out the obsolete results here to make sure only the
681 * most current BSS information remains in the table.
682 */
683 wpa_printf(MSG_DEBUG, "BSS: " MACSTR
684 " has multiple entries in the scan results - select the most current one",
685 MAC2STR(bss->bssid));
686 calculate_update_time(fetch_time, res->age, &update_time);
687 wpa_printf(MSG_DEBUG,
688 "Previous last_update: %u.%06u (freq %d%s)",
689 (unsigned int) bss->last_update.sec,
690 (unsigned int) bss->last_update.usec,
691 bss->freq,
692 (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
693 wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
694 (unsigned int) update_time.sec,
695 (unsigned int) update_time.usec,
696 res->freq,
697 (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
698 if ((bss->flags & WPA_BSS_ASSOCIATED) ||
699 (!(res->flags & WPA_SCAN_ASSOCIATED) &&
700 !os_reltime_before(&bss->last_update, &update_time))) {
701 wpa_printf(MSG_DEBUG,
702 "Ignore this BSS entry since the previous update looks more current");
703 return bss;
704 }
705 wpa_printf(MSG_DEBUG,
706 "Accept this BSS entry since it looks more current than the previous update");
707 }
708
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 changes = wpa_bss_compare_res(bss, res);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800710 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
711 wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
712 MAC2STR(bss->bssid), bss->freq, res->freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713 bss->scan_miss_count = 0;
714 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800715 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 /* Move the entry to the end of the list */
717 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700718#ifdef CONFIG_P2P
719 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
720 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
721 /*
722 * This can happen when non-P2P station interface runs a scan
723 * without P2P IE in the Probe Request frame. P2P GO would reply
724 * to that with a Probe Response that does not include P2P IE.
725 * Do not update the IEs in this BSS entry to avoid such loss of
726 * information that may be needed for P2P operations to
727 * determine group information.
728 */
729 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
730 MACSTR " since that would remove P2P IE information",
731 MAC2STR(bss->bssid));
732 } else
733#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 if (bss->ie_len + bss->beacon_ie_len >=
735 res->ie_len + res->beacon_ie_len) {
Hai Shalom60840252021-02-19 19:02:11 -0800736 os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 bss->ie_len = res->ie_len;
738 bss->beacon_ie_len = res->beacon_ie_len;
739 } else {
740 struct wpa_bss *nbss;
741 struct dl_list *prev = bss->list_id.prev;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000742 struct wpa_connect_work *cwork;
743 unsigned int i;
744 bool update_current_bss = wpa_s->current_bss == bss;
745 bool update_ml_probe_bss = wpa_s->ml_connect_probe_bss == bss;
746
747 cwork = wpa_bss_check_pending_connect(wpa_s, bss);
748
749 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
750 if (wpa_s->last_scan_res[i] == bss)
751 break;
752 }
753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 dl_list_del(&bss->list_id);
755 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
756 res->beacon_ie_len);
757 if (nbss) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000758 if (i != wpa_s->last_scan_res_used)
759 wpa_s->last_scan_res[i] = nbss;
760
761 if (update_current_bss)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700762 wpa_s->current_bss = nbss;
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000763
764 if (update_ml_probe_bss)
765 wpa_s->ml_connect_probe_bss = nbss;
766
767 if (cwork)
768 wpa_bss_update_pending_connect(cwork, nbss);
769
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 bss = nbss;
Hai Shalom60840252021-02-19 19:02:11 -0800771 os_memcpy(bss->ies, res + 1,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772 res->ie_len + res->beacon_ie_len);
773 bss->ie_len = res->ie_len;
774 bss->beacon_ie_len = res->beacon_ie_len;
775 }
776 dl_list_add(prev, &bss->list_id);
777 }
Sunil Ravi89eba102022-09-13 21:04:37 -0700778 if (changes & WPA_BSS_IES_CHANGED_FLAG) {
779 const u8 *ml_ie, *mld_addr;
780
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700781 wpa_bss_set_hessid(bss);
Sunil Ravi89eba102022-09-13 21:04:37 -0700782 os_memset(bss->mld_addr, 0, ETH_ALEN);
783 ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
784 if (ml_ie) {
785 mld_addr = get_basic_mle_mld_addr(&ml_ie[3],
786 ml_ie[1] - 1);
787 if (mld_addr)
788 os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
789 }
790 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700791 dl_list_add_tail(&wpa_s->bss, &bss->list);
792
793 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700794
795 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796}
797
798
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800799/**
800 * wpa_bss_update_start - Start a BSS table update from scan results
801 * @wpa_s: Pointer to wpa_supplicant data
802 *
803 * This function is called at the start of each BSS table update round for new
804 * scan results. The actual scan result entries are indicated with calls to
805 * wpa_bss_update_scan_res() and the update round is finished with a call to
806 * wpa_bss_update_end().
807 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
809{
810 wpa_s->bss_update_idx++;
811 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
812 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700813 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814}
815
816
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800817/**
818 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
819 * @wpa_s: Pointer to wpa_supplicant data
820 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800821 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800822 *
823 * This function updates a BSS table entry (or adds one) based on a scan result.
824 * This is called separately for each scan result between the calls to
825 * wpa_bss_update_start() and wpa_bss_update_end().
826 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800828 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800829 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800831 const u8 *ssid, *p2p, *mesh;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832 struct wpa_bss *bss;
833
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700834 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800835 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700836 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800837 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
838 struct os_reltime age;
839 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
840 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700841 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
842 "table entry that is %u.%06u seconds older "
843 "than our scan trigger",
844 (unsigned int) age.sec,
845 (unsigned int) age.usec);
846 return;
847 }
848 }
849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
851 if (ssid == NULL) {
852 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
853 MACSTR, MAC2STR(res->bssid));
854 return;
855 }
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700856 if (ssid[1] > SSID_MAX_LEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
858 MACSTR, MAC2STR(res->bssid));
859 return;
860 }
861
862 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700863#ifdef CONFIG_P2P
864 if (p2p == NULL &&
865 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
866 /*
867 * If it's a P2P specific interface, then don't update
868 * the scan result without a P2P IE.
869 */
870 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
871 " update for P2P interface", MAC2STR(res->bssid));
872 return;
873 }
874#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
876 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
877 return; /* Skip P2P listen discovery results here */
878
879 /* TODO: add option for ignoring BSSes we are not interested in
880 * (to save memory) */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800881
882 mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700883 if (mesh && mesh[1] <= SSID_MAX_LEN)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800884 ssid = mesh;
885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
887 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800888 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700889 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800890 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700891 if (wpa_s->last_scan_res) {
892 unsigned int i;
893 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
894 if (bss == wpa_s->last_scan_res[i]) {
895 /* Already in the list */
896 return;
897 }
898 }
899 }
900 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700901
902 if (bss == NULL)
903 return;
904 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
905 struct wpa_bss **n;
906 unsigned int siz;
907 if (wpa_s->last_scan_res_size == 0)
908 siz = 32;
909 else
910 siz = wpa_s->last_scan_res_size * 2;
911 n = os_realloc_array(wpa_s->last_scan_res, siz,
912 sizeof(struct wpa_bss *));
913 if (n == NULL)
914 return;
915 wpa_s->last_scan_res = n;
916 wpa_s->last_scan_res_size = siz;
917 }
918
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700919 if (wpa_s->last_scan_res)
920 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921}
922
923
924static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
925 const struct scan_info *info)
926{
927 int found;
928 size_t i;
929
930 if (info == NULL)
931 return 1;
932
933 if (info->num_freqs) {
934 found = 0;
935 for (i = 0; i < info->num_freqs; i++) {
936 if (bss->freq == info->freqs[i]) {
937 found = 1;
938 break;
939 }
940 }
941 if (!found)
942 return 0;
943 }
944
945 if (info->num_ssids) {
946 found = 0;
947 for (i = 0; i < info->num_ssids; i++) {
948 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
949 if ((s->ssid == NULL || s->ssid_len == 0) ||
950 (s->ssid_len == bss->ssid_len &&
951 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
952 0)) {
953 found = 1;
954 break;
955 }
956 }
957 if (!found)
958 return 0;
959 }
960
961 return 1;
962}
963
964
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800965/**
966 * wpa_bss_update_end - End a BSS table update from scan results
967 * @wpa_s: Pointer to wpa_supplicant data
968 * @info: Information about scan parameters
969 * @new_scan: Whether this update round was based on a new scan
970 *
971 * This function is called at the end of each BSS table update round for new
972 * scan results. The start of the update was indicated with a call to
973 * wpa_bss_update_start().
974 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
976 int new_scan)
977{
978 struct wpa_bss *bss, *n;
979
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800980 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800981 if ((info && info->aborted) || !new_scan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982 return; /* do not expire entries without new scan */
983
984 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
985 if (wpa_bss_in_use(wpa_s, bss))
986 continue;
987 if (!wpa_bss_included_in_scan(bss, info))
988 continue; /* expire only BSSes that were scanned */
989 if (bss->last_update_idx < wpa_s->bss_update_idx)
990 bss->scan_miss_count++;
991 if (bss->scan_miss_count >=
992 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700993 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 }
995 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700996
Hai Shalomfdcde762020-04-02 11:19:20 -0700997 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800998 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700999}
1000
1001
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001002/**
1003 * wpa_bss_flush_by_age - Flush old BSS entries
1004 * @wpa_s: Pointer to wpa_supplicant data
1005 * @age: Maximum entry age in seconds
1006 *
1007 * Remove BSS entries that have not been updated during the last @age seconds.
1008 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
1010{
1011 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001012 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013
1014 if (dl_list_empty(&wpa_s->bss))
1015 return;
1016
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001017 os_get_reltime(&t);
Sunil Raviaf8751c2023-03-29 11:35:17 -07001018
1019 if (t.sec < age)
1020 return; /* avoid underflow; there can be no older entries */
1021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 t.sec -= age;
1023
1024 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1025 if (wpa_bss_in_use(wpa_s, bss))
1026 continue;
1027
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001028 if (wpa_s->reassoc_same_ess &&
1029 wpa_s->wpa_state != WPA_COMPLETED &&
1030 wpa_s->last_ssid &&
1031 bss->ssid_len == wpa_s->last_ssid->ssid_len &&
1032 os_memcmp(bss->ssid, wpa_s->last_ssid->ssid,
1033 bss->ssid_len) == 0)
1034 continue;
1035
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001036 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001037 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 } else
1039 break;
1040 }
1041}
1042
1043
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001044/**
1045 * wpa_bss_init - Initialize BSS table
1046 * @wpa_s: Pointer to wpa_supplicant data
1047 * Returns: 0 on success, -1 on failure
1048 *
1049 * This prepares BSS table lists and timer for periodic updates. The BSS table
1050 * is deinitialized with wpa_bss_deinit() once not needed anymore.
1051 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052int wpa_bss_init(struct wpa_supplicant *wpa_s)
1053{
1054 dl_list_init(&wpa_s->bss);
1055 dl_list_init(&wpa_s->bss_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 return 0;
1057}
1058
1059
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001060/**
1061 * wpa_bss_flush - Flush all unused BSS entries
1062 * @wpa_s: Pointer to wpa_supplicant data
1063 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064void wpa_bss_flush(struct wpa_supplicant *wpa_s)
1065{
1066 struct wpa_bss *bss, *n;
1067
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001068 wpa_s->clear_driver_scan_cache = 1;
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 if (wpa_s->bss.next == NULL)
1071 return; /* BSS table not yet initialized */
1072
1073 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1074 if (wpa_bss_in_use(wpa_s, bss))
1075 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001076 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 }
1078}
1079
1080
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001081/**
1082 * wpa_bss_deinit - Deinitialize BSS table
1083 * @wpa_s: Pointer to wpa_supplicant data
1084 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
1086{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 wpa_bss_flush(wpa_s);
1088}
1089
1090
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001091/**
1092 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
1093 * @wpa_s: Pointer to wpa_supplicant data
1094 * @bssid: BSSID
1095 * Returns: Pointer to the BSS entry or %NULL if not found
1096 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
1098 const u8 *bssid)
1099{
1100 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001101 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1102 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1104 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1105 return bss;
1106 }
1107 return NULL;
1108}
1109
1110
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001111/**
1112 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1113 * @wpa_s: Pointer to wpa_supplicant data
1114 * @bssid: BSSID
1115 * Returns: Pointer to the BSS entry or %NULL if not found
1116 *
1117 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1118 * find the entry that has the most recent update. This can help in finding the
1119 * correct entry in cases where the SSID of the AP may have changed recently
1120 * (e.g., in WPS reconfiguration cases).
1121 */
1122struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1123 const u8 *bssid)
1124{
1125 struct wpa_bss *bss, *found = NULL;
1126 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1127 return NULL;
1128 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1129 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
1130 continue;
1131 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001132 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001133 found = bss;
1134 }
1135 return found;
1136}
1137
1138
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001139#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001140/**
Hai Shalomc3565922019-10-28 11:58:20 -07001141 * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001142 * @wpa_s: Pointer to wpa_supplicant data
1143 * @dev_addr: P2P Device Address of the GO
1144 * Returns: Pointer to the BSS entry or %NULL if not found
Hai Shalomc3565922019-10-28 11:58:20 -07001145 *
1146 * This function tries to find the entry that has the most recent update. This
1147 * can help in finding the correct entry in cases where the SSID of the P2P
1148 * Device may have changed recently.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001149 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001150struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1151 const u8 *dev_addr)
1152{
Hai Shalomc3565922019-10-28 11:58:20 -07001153 struct wpa_bss *bss, *found = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001154 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1155 u8 addr[ETH_ALEN];
Hai Shalom60840252021-02-19 19:02:11 -08001156 if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
Hai Shalomc3565922019-10-28 11:58:20 -07001157 addr) != 0 ||
1158 os_memcmp(addr, dev_addr, ETH_ALEN) != 0)
1159 continue;
1160 if (!found ||
1161 os_reltime_before(&found->last_update, &bss->last_update))
1162 found = bss;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001163 }
Hai Shalomc3565922019-10-28 11:58:20 -07001164 return found;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001165}
1166#endif /* CONFIG_P2P */
1167
1168
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001169/**
1170 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1171 * @wpa_s: Pointer to wpa_supplicant data
1172 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1173 * Returns: Pointer to the BSS entry or %NULL if not found
1174 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1176{
1177 struct wpa_bss *bss;
1178 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1179 if (bss->id == id)
1180 return bss;
1181 }
1182 return NULL;
1183}
1184
1185
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001186/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001187 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1188 * @wpa_s: Pointer to wpa_supplicant data
1189 * @idf: Smallest allowed identifier assigned for the entry
1190 * @idf: Largest allowed identifier assigned for the entry
1191 * Returns: Pointer to the BSS entry or %NULL if not found
1192 *
1193 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1194 * smallest id value to be fetched within the specified range without the
1195 * caller having to know the exact id.
1196 */
1197struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1198 unsigned int idf, unsigned int idl)
1199{
1200 struct wpa_bss *bss;
1201 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1202 if (bss->id >= idf && bss->id <= idl)
1203 return bss;
1204 }
1205 return NULL;
1206}
1207
1208
1209/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001210 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1211 * @bss: BSS table entry
1212 * @ie: Information element identitifier (WLAN_EID_*)
1213 * Returns: Pointer to the information element (id field) or %NULL if not found
1214 *
1215 * This function returns the first matching information element in the BSS
1216 * entry.
1217 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1219{
Hai Shalom60840252021-02-19 19:02:11 -08001220 return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie);
1221}
1222
1223
1224/**
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001225 * wpa_bss_get_ie_nth - Fetch a specified information element from a BSS entry
1226 * @bss: BSS table entry
1227 * @ie: Information element identitifier (WLAN_EID_*)
1228 * @nth: Return the nth element of the requested type (2 returns the second)
1229 * Returns: Pointer to the information element (id field) or %NULL if not found
1230 *
1231 * This function returns the nth matching information element in the BSS
1232 * entry.
1233 */
1234const u8 * wpa_bss_get_ie_nth(const struct wpa_bss *bss, u8 ie, int nth)
1235{
1236 return get_ie_nth(wpa_bss_ie_ptr(bss), bss->ie_len, ie, nth);
1237}
1238
1239
1240/**
Hai Shalom60840252021-02-19 19:02:11 -08001241 * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry
1242 * @bss: BSS table entry
1243 * @ext: Information element extension identifier (WLAN_EID_EXT_*)
1244 * Returns: Pointer to the information element (id field) or %NULL if not found
1245 *
1246 * This function returns the first matching information element in the BSS
1247 * entry.
1248 */
1249const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext)
1250{
1251 return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252}
1253
1254
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001255/**
1256 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1257 * @bss: BSS table entry
1258 * @vendor_type: Vendor type (four octets starting the IE payload)
1259 * Returns: Pointer to the information element (id field) or %NULL if not found
1260 *
1261 * This function returns the first matching information element in the BSS
1262 * entry.
1263 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1265{
Hai Shalom60840252021-02-19 19:02:11 -08001266 const u8 *ies;
1267 const struct element *elem;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001268
Hai Shalom60840252021-02-19 19:02:11 -08001269 ies = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270
Hai Shalom60840252021-02-19 19:02:11 -08001271 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) {
1272 if (elem->datalen >= 4 &&
1273 vendor_type == WPA_GET_BE32(elem->data))
1274 return &elem->id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275 }
1276
1277 return NULL;
1278}
1279
1280
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001281/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001282 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1283 * @bss: BSS table entry
1284 * @vendor_type: Vendor type (four octets starting the IE payload)
1285 * Returns: Pointer to the information element (id field) or %NULL if not found
1286 *
1287 * This function returns the first matching information element in the BSS
1288 * entry.
1289 *
1290 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1291 * from Beacon frames instead of either Beacon or Probe Response frames.
1292 */
1293const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1294 u32 vendor_type)
1295{
Hai Shalom60840252021-02-19 19:02:11 -08001296 const u8 *ies;
1297 const struct element *elem;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001298
1299 if (bss->beacon_ie_len == 0)
1300 return NULL;
1301
Hai Shalom60840252021-02-19 19:02:11 -08001302 ies = wpa_bss_ie_ptr(bss);
1303 ies += bss->ie_len;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001304
Hai Shalom60840252021-02-19 19:02:11 -08001305 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1306 bss->beacon_ie_len) {
1307 if (elem->datalen >= 4 &&
1308 vendor_type == WPA_GET_BE32(elem->data))
1309 return &elem->id;
Dmitry Shmidt96571392013-10-14 12:54:46 -07001310 }
1311
1312 return NULL;
1313}
1314
1315
1316/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001317 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1318 * @bss: BSS table entry
1319 * @vendor_type: Vendor type (four octets starting the IE payload)
1320 * Returns: Pointer to the information element payload or %NULL if not found
1321 *
1322 * This function returns concatenated payload of possibly fragmented vendor
1323 * specific information elements in the BSS entry. The caller is responsible for
1324 * freeing the returned buffer.
1325 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1327 u32 vendor_type)
1328{
1329 struct wpabuf *buf;
1330 const u8 *end, *pos;
1331
1332 buf = wpabuf_alloc(bss->ie_len);
1333 if (buf == NULL)
1334 return NULL;
1335
Hai Shalom60840252021-02-19 19:02:11 -08001336 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 end = pos + bss->ie_len;
1338
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001339 while (end - pos > 1) {
Hai Shalom60840252021-02-19 19:02:11 -08001340 u8 ie, len;
1341
1342 ie = pos[0];
1343 len = pos[1];
1344 if (len > end - pos - 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 break;
Hai Shalom60840252021-02-19 19:02:11 -08001346 pos += 2;
1347 if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1348 vendor_type == WPA_GET_BE32(pos))
1349 wpabuf_put_data(buf, pos + 4, len - 4);
1350 pos += len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 }
1352
1353 if (wpabuf_len(buf) == 0) {
1354 wpabuf_free(buf);
1355 buf = NULL;
1356 }
1357
1358 return buf;
1359}
1360
1361
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001362/**
1363 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1364 * @bss: BSS table entry
1365 * @vendor_type: Vendor type (four octets starting the IE payload)
1366 * Returns: Pointer to the information element payload or %NULL if not found
1367 *
1368 * This function returns concatenated payload of possibly fragmented vendor
1369 * specific information elements in the BSS entry. The caller is responsible for
1370 * freeing the returned buffer.
1371 *
1372 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1373 * from Beacon frames instead of either Beacon or Probe Response frames.
1374 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001375struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1376 u32 vendor_type)
1377{
1378 struct wpabuf *buf;
1379 const u8 *end, *pos;
1380
1381 buf = wpabuf_alloc(bss->beacon_ie_len);
1382 if (buf == NULL)
1383 return NULL;
1384
Hai Shalom60840252021-02-19 19:02:11 -08001385 pos = wpa_bss_ie_ptr(bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001386 pos += bss->ie_len;
1387 end = pos + bss->beacon_ie_len;
1388
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001389 while (end - pos > 1) {
Sunil8cd6f4d2022-06-28 18:40:46 +00001390 u8 id, len;
1391
1392 id = *pos++;
1393 len = *pos++;
1394 if (len > end - pos)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001395 break;
Sunil8cd6f4d2022-06-28 18:40:46 +00001396 if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1397 vendor_type == WPA_GET_BE32(pos))
1398 wpabuf_put_data(buf, pos + 4, len - 4);
1399 pos += len;
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001400 }
1401
1402 if (wpabuf_len(buf) == 0) {
1403 wpabuf_free(buf);
1404 buf = NULL;
1405 }
1406
1407 return buf;
1408}
1409
1410
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001411/**
1412 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1413 * @bss: BSS table entry
1414 * Returns: Maximum legacy rate in units of 500 kbps
1415 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1417{
1418 int rate = 0;
1419 const u8 *ie;
1420 int i;
1421
1422 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1423 for (i = 0; ie && i < ie[1]; i++) {
1424 if ((ie[i + 2] & 0x7f) > rate)
1425 rate = ie[i + 2] & 0x7f;
1426 }
1427
1428 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1429 for (i = 0; ie && i < ie[1]; i++) {
1430 if ((ie[i + 2] & 0x7f) > rate)
1431 rate = ie[i + 2] & 0x7f;
1432 }
1433
1434 return rate;
1435}
1436
1437
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001438/**
1439 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1440 * @bss: BSS table entry
1441 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1442 * Returns: number of legacy TX rates or -1 on failure
1443 *
1444 * The caller is responsible for freeing the returned buffer with os_free() in
1445 * case of success.
1446 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1448{
1449 const u8 *ie, *ie2;
1450 int i, j;
1451 unsigned int len;
1452 u8 *r;
1453
1454 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1455 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1456
1457 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1458
1459 r = os_malloc(len);
1460 if (!r)
1461 return -1;
1462
1463 for (i = 0; ie && i < ie[1]; i++)
1464 r[i] = ie[i + 2] & 0x7f;
1465
1466 for (j = 0; ie2 && j < ie2[1]; j++)
1467 r[i + j] = ie2[j + 2] & 0x7f;
1468
1469 *rates = r;
1470 return len;
1471}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001472
1473
1474#ifdef CONFIG_FILS
Hai Shalom60840252021-02-19 19:02:11 -08001475const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001476{
1477 const u8 *ie;
1478
1479 if (bss) {
1480 ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
1481 if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7))
1482 return ie + 4;
1483 }
1484
1485 return NULL;
1486}
1487#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001488
1489
1490int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab)
1491{
Hai Shalom60840252021-02-19 19:02:11 -08001492 if (!bss)
1493 return 0;
Hai Shalom74f70d42019-02-11 14:42:39 -08001494 return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB),
1495 capab);
1496}
Sunil Ravi38ad1ed2023-01-17 23:58:31 +00001497
1498
1499/**
1500 * wpa_bss_defrag_mle - Get a buffer holding a de-fragmented ML element
1501 * @bss: BSS table entry
1502 * @type: ML control type
1503 */
1504struct wpabuf * wpa_bss_defrag_mle(const struct wpa_bss *bss, u8 type)
1505{
1506 struct ieee802_11_elems elems;
1507 const u8 *pos = wpa_bss_ie_ptr(bss);
1508 size_t len = bss->ie_len;
1509
1510 if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
1511 return NULL;
1512
1513 return ieee802_11_defrag_mle(&elems, type);
1514}
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001515
1516
1517static void
1518wpa_bss_parse_ml_rnr_ap_info(struct wpa_supplicant *wpa_s,
1519 struct wpa_bss *bss, u8 mbssid_idx,
1520 const struct ieee80211_neighbor_ap_info *ap_info,
1521 size_t len, u16 *seen, u16 *missing)
1522{
1523 const u8 *pos, *end;
1524 const u8 *mld_params;
1525 u8 count, mld_params_offset;
1526 u8 i, type, link_id;
1527
1528 count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
1529 type = ap_info->tbtt_info_hdr & RNR_TBTT_INFO_HDR_TYPE_MSK;
1530
1531 /* MLD information is at offset 13 or at start */
1532 if (type == 0 && ap_info->tbtt_info_len >= RNR_TBTT_INFO_MLD_LEN) {
1533 /* MLD info is appended */
1534 mld_params_offset = RNR_TBTT_INFO_LEN;
1535 } else {
1536 /* TODO: Support NSTR AP */
1537 return;
1538 }
1539
1540 pos = (const u8 *) ap_info;
1541 end = pos + len;
1542 pos += sizeof(*ap_info);
1543
1544 for (i = 0; i < count; i++) {
1545 if (bss->n_mld_links >= MAX_NUM_MLD_LINKS)
1546 return;
1547
1548 if (end - pos < ap_info->tbtt_info_len)
1549 break;
1550
1551 mld_params = pos + mld_params_offset;
1552
1553 link_id = *(mld_params + 1) & EHT_ML_LINK_ID_MSK;
1554
1555 if (*mld_params != mbssid_idx) {
1556 wpa_printf(MSG_DEBUG,
1557 "MLD: Reported link not part of MLD");
1558 } else if (!(BIT(link_id) & *seen)) {
1559 struct wpa_bss *neigh_bss =
1560 wpa_bss_get_bssid(wpa_s, ap_info->data + 1);
1561
1562 *seen |= BIT(link_id);
1563 wpa_printf(MSG_DEBUG, "MLD: mld ID=%u, link ID=%u",
1564 *mld_params, link_id);
1565
1566 if (neigh_bss) {
1567 struct mld_link *l;
1568
1569 l = &bss->mld_links[bss->n_mld_links];
1570 l->link_id = link_id;
1571 os_memcpy(l->bssid, ap_info->data + 1,
1572 ETH_ALEN);
1573 l->freq = neigh_bss->freq;
1574 bss->n_mld_links++;
1575 } else {
1576 *missing |= BIT(link_id);
1577 }
1578 }
1579
1580 pos += ap_info->tbtt_info_len;
1581 }
1582}
1583
1584
1585/**
1586 * wpa_bss_parse_basic_ml_element - Parse the Basic Multi-Link element
1587 * @wpa_s: Pointer to wpa_supplicant data
1588 * @bss: BSS table entry
1589 * @mld_addr: AP MLD address (or %NULL)
1590 * @link_info: Array to store link information (or %NULL),
1591 * should be initialized and #MAX_NUM_MLD_LINKS elements long
1592 * @missing_links: Result bitmask of links that were not discovered (or %NULL)
1593 * Returns: 0 on success or -1 for non-MLD or parsing failures
1594 *
1595 * Parses the Basic Multi-Link element of the BSS into @link_info using the scan
1596 * information stored in the wpa_supplicant data to fill in information for
1597 * links where possible. The @missing_links out parameter will contain any links
1598 * for which no corresponding BSS was found.
1599 */
1600int wpa_bss_parse_basic_ml_element(struct wpa_supplicant *wpa_s,
1601 struct wpa_bss *bss,
1602 u8 *ap_mld_addr,
1603 u16 *missing_links)
1604{
1605 struct ieee802_11_elems elems;
1606 struct wpabuf *mlbuf;
1607 const struct element *elem;
1608 u8 mbssid_idx = 0;
1609 u8 ml_ie_len;
1610 const struct ieee80211_eht_ml *eht_ml;
1611 const struct eht_ml_basic_common_info *ml_basic_common_info;
1612 u8 i, link_id;
1613 const u16 control_mask =
1614 MULTI_LINK_CONTROL_TYPE_MASK |
1615 BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1616 BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1617 BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
1618 const u16 control =
1619 MULTI_LINK_CONTROL_TYPE_BASIC |
1620 BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1621 BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1622 BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
1623 u16 missing = 0;
1624 u16 seen;
1625 const u8 *ies_pos = wpa_bss_ie_ptr(bss);
1626 size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
1627 int ret = -1;
1628 struct mld_link *l;
1629
1630 if (ieee802_11_parse_elems(ies_pos, ies_len, &elems, 1) ==
1631 ParseFailed) {
1632 wpa_dbg(wpa_s, MSG_DEBUG, "MLD: Failed to parse elements");
1633 return ret;
1634 }
1635
1636 mlbuf = ieee802_11_defrag_mle(&elems, MULTI_LINK_CONTROL_TYPE_BASIC);
1637 if (!mlbuf) {
1638 wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No Multi-Link element");
1639 return ret;
1640 }
1641
1642 ml_ie_len = wpabuf_len(mlbuf);
1643
1644 /*
1645 * for ext ID + 2 control + common info len + MLD address +
1646 * link info
1647 */
1648 if (ml_ie_len < 2UL + 1UL + ETH_ALEN + 1UL)
1649 goto out;
1650
1651 eht_ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
1652 if ((le_to_host16(eht_ml->ml_control) & control_mask) != control) {
1653 wpa_printf(MSG_DEBUG,
1654 "MLD: Unexpected Multi-Link element control=0x%x (mask 0x%x expected 0x%x)",
1655 le_to_host16(eht_ml->ml_control), control_mask,
1656 control);
1657 goto out;
1658 }
1659
1660 ml_basic_common_info =
1661 (const struct eht_ml_basic_common_info *) eht_ml->variable;
1662
1663 /* Common info length should be valid */
1664 if (ml_basic_common_info->len < ETH_ALEN + 1UL)
1665 goto out;
1666
1667 /* Get the MLD address and MLD link ID */
1668 if (ap_mld_addr)
1669 os_memcpy(ap_mld_addr, ml_basic_common_info->mld_addr,
1670 ETH_ALEN);
1671
1672 bss->n_mld_links = 0;
1673 l = &bss->mld_links[bss->n_mld_links];
1674 link_id = ml_basic_common_info->variable[0] & EHT_ML_LINK_ID_MSK;
1675 l->link_id = link_id;
1676 os_memcpy(l->bssid, bss->bssid, ETH_ALEN);
1677 l->freq = bss->freq;
1678
1679 seen = BIT(link_id);
1680 bss->n_mld_links++;
1681
1682 /*
1683 * The AP MLD ID in the RNR corresponds to the MBSSID index, see
1684 * IEEE P802.11be/D4.0, 9.4.2.169.2 (Neighbor AP Information field).
1685 *
1686 * For the transmitting BSSID it is clear that both the MBSSID index
1687 * and the AP MLD ID in the RNR are zero.
1688 *
1689 * For nontransmitted BSSIDs we will have a BSS generated from the
1690 * MBSSID element(s) using inheritance rules. Included in the elements
1691 * is the MBSSID Index Element. The RNR is copied from the Beacon/Probe
1692 * Response frame that was send by the transmitting BSSID. As such, the
1693 * reported AP MLD ID in the RNR will match the value in the MBSSID
1694 * Index Element.
1695 */
1696 elem = (const struct element *)
1697 wpa_bss_get_ie(bss, WLAN_EID_MULTIPLE_BSSID_INDEX);
1698 if (elem && elem->datalen >= 1)
1699 mbssid_idx = elem->data[0];
1700
1701 for_each_element_id(elem, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
1702 wpa_bss_ie_ptr(bss),
1703 bss->ie_len ? bss->ie_len : bss->beacon_ie_len) {
1704 const struct ieee80211_neighbor_ap_info *ap_info;
1705 const u8 *pos = elem->data;
1706 size_t len = elem->datalen;
1707
1708 /* RNR IE may contain more than one Neighbor AP Info */
1709 while (sizeof(*ap_info) <= len) {
1710 size_t ap_info_len = sizeof(*ap_info);
1711 u8 count;
1712
1713 ap_info = (const struct ieee80211_neighbor_ap_info *)
1714 pos;
1715 count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
1716 ap_info_len += count * ap_info->tbtt_info_len;
1717
1718 if (ap_info_len > len)
1719 goto out;
1720
1721 wpa_bss_parse_ml_rnr_ap_info(wpa_s, bss, mbssid_idx,
1722 ap_info, len, &seen,
1723 &missing);
1724
1725 pos += ap_info_len;
1726 len -= ap_info_len;
1727 }
1728 }
1729
1730 wpa_printf(MSG_DEBUG, "MLD: n_mld_links=%u (unresolved: 0x%04hx)",
1731 bss->n_mld_links, missing);
1732
1733 for (i = 0; i < bss->n_mld_links; i++) {
1734 wpa_printf(MSG_DEBUG, "MLD: link=%u, bssid=" MACSTR,
1735 bss->mld_links[i].link_id,
1736 MAC2STR(bss->mld_links[i].bssid));
1737 }
1738
1739 if (missing_links)
1740 *missing_links = missing;
1741
1742 ret = 0;
1743out:
1744 wpabuf_free(mlbuf);
1745 return ret;
1746}
1747
1748
1749/*
1750 * wpa_bss_parse_reconf_ml_element - Parse the Reconfiguration ML element
1751 * @wpa_s: Pointer to wpa_supplicant data
1752 * @bss: BSS table entry
1753 * Returns: The bitmap of links that are going to be removed
1754 */
1755u16 wpa_bss_parse_reconf_ml_element(struct wpa_supplicant *wpa_s,
1756 struct wpa_bss *bss)
1757{
1758 struct ieee802_11_elems elems;
1759 struct wpabuf *mlbuf;
1760 const u8 *pos = wpa_bss_ie_ptr(bss);
1761 size_t len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
1762 const struct ieee80211_eht_ml *ml;
1763 u16 removed_links = 0;
1764 u8 ml_common_len;
1765
1766 if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
1767 return 0;
1768
1769 if (!elems.reconf_mle || !elems.reconf_mle_len)
1770 return 0;
1771
1772 mlbuf = ieee802_11_defrag_mle(&elems, MULTI_LINK_CONTROL_TYPE_RECONF);
1773 if (!mlbuf)
1774 return 0;
1775
1776 ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
1777 len = wpabuf_len(mlbuf);
1778
1779 if (len < sizeof(*ml))
1780 goto out;
1781
1782 ml_common_len = 1;
1783 if (ml->ml_control & RECONF_MULTI_LINK_CTRL_PRES_MLD_MAC_ADDR)
1784 ml_common_len += ETH_ALEN;
1785
1786 if (len < sizeof(*ml) + ml_common_len) {
1787 wpa_printf(MSG_DEBUG,
1788 "MLD: Unexpected Reconfiguration ML element length: (%zu < %zu)",
1789 len, sizeof(*ml) + ml_common_len);
1790 goto out;
1791 }
1792
1793 pos = ml->variable + ml_common_len;
1794 len -= sizeof(*ml) + ml_common_len;
1795
1796 while (len >= 2 + sizeof(struct ieee80211_eht_per_sta_profile)) {
1797 size_t sub_elem_len = *(pos + 1);
1798
1799 if (2 + sub_elem_len > len) {
1800 wpa_printf(MSG_DEBUG,
1801 "MLD: Invalid link info len: %zu %zu",
1802 2 + sub_elem_len, len);
1803 goto out;
1804 }
1805
1806 if (*pos == EHT_ML_SUB_ELEM_PER_STA_PROFILE) {
1807 const struct ieee80211_eht_per_sta_profile *sta_prof =
1808 (const struct ieee80211_eht_per_sta_profile *)
1809 (pos + 2);
1810 u16 control = le_to_host16(sta_prof->sta_control);
1811 u8 link_id;
1812
1813 link_id = control & EHT_PER_STA_RECONF_CTRL_LINK_ID_MSK;
1814 removed_links |= BIT(link_id);
1815 }
1816
1817 pos += 2 + sub_elem_len;
1818 len -= 2 + sub_elem_len;
1819 }
1820
1821 wpa_printf(MSG_DEBUG, "MLD: Reconfiguration: removed_links=0x%x",
1822 removed_links);
1823out:
1824 wpabuf_free(mlbuf);
1825 return removed_links;
1826}