xref: /freebsd/contrib/wpa/wpa_supplicant/bss.c (revision 71e72c9e91c4b8007a4292e09669e8b549c29e97)
1 /*
2  * BSS table
3  * Copyright (c) 2009-2019, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "drivers/driver.h"
15 #include "eap_peer/eap.h"
16 #include "rsn_supp/wpa.h"
17 #include "wpa_supplicant_i.h"
18 #include "config.h"
19 #include "notify.h"
20 #include "scan.h"
21 #include "bssid_ignore.h"
22 #include "bss.h"
23 
wpa_bss_set_hessid(struct wpa_bss * bss)24 static 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 
40 /**
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  */
wpa_bss_anqp_alloc(void)48 struct 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;
54 #ifdef CONFIG_INTERWORKING
55 	dl_list_init(&anqp->anqp_elems);
56 #endif /* CONFIG_INTERWORKING */
57 	anqp->users = 1;
58 	return anqp;
59 }
60 
61 
62 /**
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  */
wpa_bss_anqp_clone(struct wpa_bss_anqp * anqp)67 static 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
77 	dl_list_init(&n->anqp_elems);
78 	ANQP_DUP(capability_list);
79 	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);
86 	ANQP_DUP(fils_realm_info);
87 #endif /* CONFIG_INTERWORKING */
88 #ifdef CONFIG_HS20
89 	ANQP_DUP(hs20_capability_list);
90 	ANQP_DUP(hs20_operator_friendly_name);
91 	ANQP_DUP(hs20_wan_metrics);
92 	ANQP_DUP(hs20_connection_capability);
93 	ANQP_DUP(hs20_operating_class);
94 #endif /* CONFIG_HS20 */
95 #undef ANQP_DUP
96 
97 	return n;
98 }
99 
100 
101 /**
102  * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
103  * @bss: BSS entry
104  * Returns: 0 on success, -1 on failure
105  *
106  * This function ensures the specific BSS entry has an ANQP data structure that
107  * is not shared with any other BSS entry.
108  */
wpa_bss_anqp_unshare_alloc(struct wpa_bss * bss)109 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
110 {
111 	struct wpa_bss_anqp *anqp;
112 
113 	if (bss->anqp && bss->anqp->users > 1) {
114 		/* allocated, but shared - clone an unshared copy */
115 		anqp = wpa_bss_anqp_clone(bss->anqp);
116 		if (anqp == NULL)
117 			return -1;
118 		anqp->users = 1;
119 		bss->anqp->users--;
120 		bss->anqp = anqp;
121 		return 0;
122 	}
123 
124 	if (bss->anqp)
125 		return 0; /* already allocated and not shared */
126 
127 	/* not allocated - allocate a new storage area */
128 	bss->anqp = wpa_bss_anqp_alloc();
129 	return bss->anqp ? 0 : -1;
130 }
131 
132 
133 /**
134  * wpa_bss_anqp_free - Free an ANQP data structure
135  * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
136  */
wpa_bss_anqp_free(struct wpa_bss_anqp * anqp)137 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
138 {
139 #ifdef CONFIG_INTERWORKING
140 	struct wpa_bss_anqp_elem *elem;
141 #endif /* CONFIG_INTERWORKING */
142 
143 	if (anqp == NULL)
144 		return;
145 
146 	anqp->users--;
147 	if (anqp->users > 0) {
148 		/* Another BSS entry holds a pointer to this ANQP info */
149 		return;
150 	}
151 
152 #ifdef CONFIG_INTERWORKING
153 	wpabuf_free(anqp->capability_list);
154 	wpabuf_free(anqp->venue_name);
155 	wpabuf_free(anqp->network_auth_type);
156 	wpabuf_free(anqp->roaming_consortium);
157 	wpabuf_free(anqp->ip_addr_type_availability);
158 	wpabuf_free(anqp->nai_realm);
159 	wpabuf_free(anqp->anqp_3gpp);
160 	wpabuf_free(anqp->domain_name);
161 	wpabuf_free(anqp->fils_realm_info);
162 
163 	while ((elem = dl_list_first(&anqp->anqp_elems,
164 				     struct wpa_bss_anqp_elem, list))) {
165 		dl_list_del(&elem->list);
166 		wpabuf_free(elem->payload);
167 		os_free(elem);
168 	}
169 #endif /* CONFIG_INTERWORKING */
170 #ifdef CONFIG_HS20
171 	wpabuf_free(anqp->hs20_capability_list);
172 	wpabuf_free(anqp->hs20_operator_friendly_name);
173 	wpabuf_free(anqp->hs20_wan_metrics);
174 	wpabuf_free(anqp->hs20_connection_capability);
175 	wpabuf_free(anqp->hs20_operating_class);
176 #endif /* CONFIG_HS20 */
177 
178 	os_free(anqp);
179 }
180 
181 
182 static struct wpa_connect_work *
wpa_bss_check_pending_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)183 wpa_bss_check_pending_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
184 {
185 	struct wpa_radio_work *work;
186 	struct wpa_connect_work *cwork;
187 
188 	work = radio_work_pending(wpa_s, "sme-connect");
189 	if (!work)
190 		work = radio_work_pending(wpa_s, "connect");
191 	if (!work)
192 		return NULL;
193 
194 	cwork = work->ctx;
195 	if (cwork->bss != bss)
196 		return NULL;
197 
198 	return cwork;
199 }
200 
201 
wpa_bss_update_pending_connect(struct wpa_connect_work * cwork,struct wpa_bss * new_bss)202 static void wpa_bss_update_pending_connect(struct wpa_connect_work *cwork,
203 					   struct wpa_bss *new_bss)
204 {
205 	wpa_printf(MSG_DEBUG,
206 		   "Update BSS pointer for the pending connect radio work");
207 	cwork->bss = new_bss;
208 	if (!new_bss)
209 		cwork->bss_removed = 1;
210 }
211 
212 
wpa_bss_remove(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const char * reason)213 void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
214 		    const char *reason)
215 {
216 	struct wpa_connect_work *cwork;
217 	unsigned int j;
218 
219 	if (wpa_s->last_scan_res) {
220 		unsigned int i;
221 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
222 			if (wpa_s->last_scan_res[i] == bss) {
223 				os_memmove(&wpa_s->last_scan_res[i],
224 					   &wpa_s->last_scan_res[i + 1],
225 					   (wpa_s->last_scan_res_used - i - 1)
226 					   * sizeof(struct wpa_bss *));
227 				wpa_s->last_scan_res_used--;
228 				break;
229 			}
230 		}
231 	}
232 	cwork = wpa_bss_check_pending_connect(wpa_s, bss);
233 	if (cwork)
234 		wpa_bss_update_pending_connect(cwork, NULL);
235 	dl_list_del(&bss->list);
236 	dl_list_del(&bss->list_id);
237 	wpa_s->num_bss--;
238 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
239 		" SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
240 		wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
241 	wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
242 	wpa_bss_anqp_free(bss->anqp);
243 
244 	if (wpa_s->current_bss == bss) {
245 		wpa_printf(MSG_DEBUG,
246 			   "BSS: Clear current_bss due to bss removal");
247 		wpa_s->current_bss = NULL;
248 	}
249 
250 #ifdef CONFIG_INTERWORKING
251 	if (wpa_s->interworking_gas_bss == bss) {
252 		wpa_printf(MSG_DEBUG,
253 			   "BSS: Clear interworking_gas_bss due to bss removal");
254 		wpa_s->interworking_gas_bss = NULL;
255 	}
256 #endif /* CONFIG_INTERWORKING */
257 
258 #ifdef CONFIG_WNM
259 	if (wpa_s->wnm_target_bss == bss) {
260 		wpa_printf(MSG_DEBUG,
261 			   "BSS: Clear wnm_target_bss due to bss removal");
262 		wpa_s->wnm_target_bss = NULL;
263 	}
264 #endif /* CONFIG_WNM */
265 
266 	if (wpa_s->ml_connect_probe_bss == bss) {
267 		wpa_printf(MSG_DEBUG,
268 			   "BSS: Clear ml_connect_probe_bss due to bss removal");
269 		wpa_s->ml_connect_probe_bss = NULL;
270 	}
271 
272 	for (j = 0; j < MAX_NUM_MLD_LINKS; j++) {
273 		if (wpa_s->links[j].bss == bss) {
274 			wpa_printf(MSG_DEBUG,
275 				   "BSS: Clear links[%d].bss due to bss removal",
276 				   j);
277 			wpa_s->valid_links &= ~BIT(j);
278 			wpa_s->links[j].bss = NULL;
279 		}
280 	}
281 
282 	os_free(bss);
283 }
284 
285 
286 /**
287  * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
288  * @wpa_s: Pointer to wpa_supplicant data
289  * @bssid: BSSID, or %NULL to match any BSSID
290  * @ssid: SSID
291  * @ssid_len: Length of @ssid
292  * Returns: Pointer to the BSS entry or %NULL if not found
293  */
wpa_bss_get(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len)294 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
295 			     const u8 *ssid, size_t ssid_len)
296 {
297 	struct wpa_bss *bss;
298 
299 	if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid))
300 		return NULL;
301 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
302 		if ((!bssid || ether_addr_equal(bss->bssid, bssid)) &&
303 		    bss->ssid_len == ssid_len &&
304 		    os_memcmp(bss->ssid, ssid, ssid_len) == 0)
305 			return bss;
306 	}
307 	return NULL;
308 }
309 
310 /**
311  * wpa_bss_get_connection - Fetch a BSS table entry based on BSSID and SSID.
312  * @wpa_s: Pointer to wpa_supplicant data
313  * @bssid: BSSID, or %NULL to match any BSSID
314  * @ssid: SSID
315  * @ssid_len: Length of @ssid
316  * Returns: Pointer to the BSS entry or %NULL if not found
317  *
318  * This function is similar to wpa_bss_get() but it will also return OWE
319  * transition mode encrypted networks for which transition-element matches
320  * @ssid.
321  */
wpa_bss_get_connection(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len)322 struct wpa_bss * wpa_bss_get_connection(struct wpa_supplicant *wpa_s,
323 					const u8 *bssid,
324 					const u8 *ssid, size_t ssid_len)
325 {
326 	struct wpa_bss *bss;
327 #ifdef CONFIG_OWE
328 	const u8 *owe, *owe_bssid, *owe_ssid;
329 	size_t owe_ssid_len;
330 #endif /* CONFIG_OWE */
331 
332 	if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid))
333 		return NULL;
334 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
335 		if (bssid && !ether_addr_equal(bss->bssid, bssid))
336 			continue;
337 
338 		if (bss->ssid_len == ssid_len &&
339 		    os_memcmp(bss->ssid, ssid, ssid_len) == 0)
340 			return bss;
341 
342 #ifdef CONFIG_OWE
343 		/* Check if OWE transition mode element is present and matches
344 		 * the SSID */
345 		owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
346 		if (!owe)
347 			continue;
348 
349 		if (wpas_get_owe_trans_network(owe, &owe_bssid, &owe_ssid,
350 					       &owe_ssid_len))
351 			continue;
352 
353 		if (bss->ssid_len &&
354 		    owe_ssid_len == ssid_len &&
355 		    os_memcmp(owe_ssid, ssid, ssid_len) == 0)
356 			return bss;
357 #endif /* CONFIG_OWE */
358 	}
359 	return NULL;
360 }
361 
362 
calculate_update_time(const struct os_reltime * fetch_time,unsigned int age_ms,struct os_reltime * update_time)363 void calculate_update_time(const struct os_reltime *fetch_time,
364 			   unsigned int age_ms,
365 			   struct os_reltime *update_time)
366 {
367 	os_time_t usec;
368 
369 	update_time->sec = fetch_time->sec;
370 	update_time->usec = fetch_time->usec;
371 	update_time->sec -= age_ms / 1000;
372 	usec = (age_ms % 1000) * 1000;
373 	if (update_time->usec < usec) {
374 		update_time->sec--;
375 		update_time->usec += 1000000;
376 	}
377 	update_time->usec -= usec;
378 }
379 
380 
wpa_bss_copy_res(struct wpa_bss * dst,struct wpa_scan_res * src,struct os_reltime * fetch_time)381 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
382 			     struct os_reltime *fetch_time)
383 {
384 	dst->flags = src->flags;
385 	os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
386 	dst->freq = src->freq;
387 	dst->max_cw = src->max_cw;
388 	dst->beacon_int = src->beacon_int;
389 	dst->caps = src->caps;
390 	dst->qual = src->qual;
391 	dst->noise = src->noise;
392 	dst->level = src->level;
393 	dst->tsf = src->tsf;
394 	dst->beacon_newer = src->beacon_newer;
395 	dst->est_throughput = src->est_throughput;
396 	dst->snr = src->snr;
397 
398 	calculate_update_time(fetch_time, src->age, &dst->last_update);
399 }
400 
401 
wpa_bss_is_wps_candidate(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)402 static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
403 				    struct wpa_bss *bss)
404 {
405 #ifdef CONFIG_WPS
406 	struct wpa_ssid *ssid;
407 	struct wpabuf *wps_ie;
408 	int pbc = 0, ret;
409 
410 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
411 	if (!wps_ie)
412 		return 0;
413 
414 	if (wps_is_selected_pbc_registrar(wps_ie)) {
415 		pbc = 1;
416 	} else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
417 		wpabuf_free(wps_ie);
418 		return 0;
419 	}
420 
421 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
422 		if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
423 			continue;
424 		if (ssid->ssid_len &&
425 		    (ssid->ssid_len != bss->ssid_len ||
426 		     os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
427 			continue;
428 
429 		if (pbc)
430 			ret = eap_is_wps_pbc_enrollee(&ssid->eap);
431 		else
432 			ret = eap_is_wps_pin_enrollee(&ssid->eap);
433 		wpabuf_free(wps_ie);
434 		return ret;
435 	}
436 	wpabuf_free(wps_ie);
437 #endif /* CONFIG_WPS */
438 
439 	return 0;
440 }
441 
442 
is_p2p_pending_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)443 static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s,
444 			       struct wpa_bss *bss)
445 {
446 #ifdef CONFIG_P2P
447 	u8 addr[ETH_ALEN];
448 
449 	if (ether_addr_equal(bss->bssid, wpa_s->pending_join_iface_addr))
450 		return true;
451 	if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) &&
452 	    p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 &&
453 	    ether_addr_equal(addr, wpa_s->pending_join_dev_addr))
454 		return true;
455 #endif /* CONFIG_P2P */
456 	return false;
457 }
458 
459 
460 #ifdef CONFIG_OWE
wpa_bss_owe_trans_known(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const u8 * entry_ssid,size_t entry_ssid_len)461 static int wpa_bss_owe_trans_known(struct wpa_supplicant *wpa_s,
462 				   struct wpa_bss *bss,
463 				   const u8 *entry_ssid, size_t entry_ssid_len)
464 {
465 	const u8 *owe, *owe_bssid, *owe_ssid;
466 	size_t owe_ssid_len;
467 
468 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
469 	if (!owe)
470 		return 0;
471 
472 	if (wpas_get_owe_trans_network(owe, &owe_bssid, &owe_ssid,
473 				       &owe_ssid_len))
474 		return 0;
475 
476 	return entry_ssid_len == owe_ssid_len &&
477 		os_memcmp(owe_ssid, entry_ssid, owe_ssid_len) == 0;
478 }
479 #endif /* CONFIG_OWE */
480 
481 
wpa_bss_known(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)482 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
483 {
484 	struct wpa_ssid *ssid;
485 
486 	if (is_p2p_pending_bss(wpa_s, bss))
487 		return 1;
488 
489 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
490 		if (ssid->ssid == NULL || ssid->ssid_len == 0)
491 			continue;
492 		if (ssid->ssid_len == bss->ssid_len &&
493 		    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
494 			return 1;
495 #ifdef CONFIG_OWE
496 		if (wpa_bss_owe_trans_known(wpa_s, bss, ssid->ssid,
497 					    ssid->ssid_len))
498 			return 1;
499 #endif /* CONFIG_OWE */
500 	}
501 
502 	return 0;
503 }
504 
505 
wpa_bss_in_use(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)506 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
507 {
508 	int i;
509 
510 	if (bss == wpa_s->current_bss)
511 		return 1;
512 
513 	if (bss == wpa_s->ml_connect_probe_bss)
514 		return 1;
515 
516 #ifdef CONFIG_WNM
517 	if (bss == wpa_s->wnm_target_bss)
518 		return 1;
519 #endif /* CONFIG_WNM */
520 
521 	if (wpa_s->current_bss &&
522 	    (bss->ssid_len != wpa_s->current_bss->ssid_len ||
523 	     os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
524 		       bss->ssid_len) != 0))
525 		return 0; /* SSID has changed */
526 
527 	if (!is_zero_ether_addr(bss->bssid) &&
528 	    (ether_addr_equal(bss->bssid, wpa_s->bssid) ||
529 	     ether_addr_equal(bss->bssid, wpa_s->pending_bssid)))
530 		return 1;
531 
532 	if (!wpa_s->valid_links)
533 		return 0;
534 
535 	for_each_link(wpa_s->valid_links, i) {
536 		if (ether_addr_equal(bss->bssid, wpa_s->links[i].bssid))
537 			return 1;
538 	}
539 
540 	return 0;
541 }
542 
543 
wpa_bss_remove_oldest_unknown(struct wpa_supplicant * wpa_s)544 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
545 {
546 	struct wpa_bss *bss;
547 
548 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
549 		if (!wpa_bss_known(wpa_s, bss) &&
550 		    !wpa_bss_in_use(wpa_s, bss) &&
551 		    !wpa_bss_is_wps_candidate(wpa_s, bss)) {
552 			wpa_bss_remove(wpa_s, bss, __func__);
553 			return 0;
554 		}
555 	}
556 
557 	return -1;
558 }
559 
560 
wpa_bss_remove_oldest(struct wpa_supplicant * wpa_s)561 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
562 {
563 	struct wpa_bss *bss;
564 
565 	/*
566 	 * Remove the oldest entry that does not match with any configured
567 	 * network.
568 	 */
569 	if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
570 		return 0;
571 
572 	/*
573 	 * Remove the oldest entry that isn't currently in use.
574 	 */
575 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
576 		if (!wpa_bss_in_use(wpa_s, bss)) {
577 			wpa_bss_remove(wpa_s, bss, __func__);
578 			return 0;
579 		}
580 	}
581 
582 	return -1;
583 }
584 
585 
wpa_bss_add(struct wpa_supplicant * wpa_s,const u8 * ssid,size_t ssid_len,struct wpa_scan_res * res,struct os_reltime * fetch_time)586 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
587 				    const u8 *ssid, size_t ssid_len,
588 				    struct wpa_scan_res *res,
589 				    struct os_reltime *fetch_time)
590 {
591 	struct wpa_bss *bss;
592 	char extra[100];
593 	char *pos, *end;
594 	int ret = 0;
595 
596 	bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
597 	if (bss == NULL)
598 		return NULL;
599 	bss->id = wpa_s->bss_next_id++;
600 	bss->last_update_idx = wpa_s->bss_update_idx;
601 	wpa_bss_copy_res(bss, res, fetch_time);
602 	os_memcpy(bss->ssid, ssid, ssid_len);
603 	bss->ssid_len = ssid_len;
604 	bss->ie_len = res->ie_len;
605 	bss->beacon_ie_len = res->beacon_ie_len;
606 	os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
607 	wpa_bss_set_hessid(bss);
608 
609 	wpa_bss_parse_basic_ml_element(wpa_s, bss);
610 
611 	if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
612 	    wpa_bss_remove_oldest(wpa_s) != 0) {
613 		wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
614 			   "because all BSSes are in use. We should normally "
615 			   "not get here!", (int) wpa_s->num_bss + 1);
616 		wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
617 	}
618 
619 	dl_list_add_tail(&wpa_s->bss, &bss->list);
620 	dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
621 	wpa_s->num_bss++;
622 
623 	extra[0] = '\0';
624 	pos = extra;
625 	end = pos + sizeof(extra);
626 	if (!is_zero_ether_addr(bss->hessid))
627 		ret = os_snprintf(pos, end - pos, " HESSID " MACSTR,
628 				  MAC2STR(bss->hessid));
629 
630 	if (!is_zero_ether_addr(bss->mld_addr) &&
631 	    !os_snprintf_error(end - pos, ret)) {
632 		pos += ret;
633 		ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR,
634 				  MAC2STR(bss->mld_addr));
635 	}
636 
637 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
638 		" SSID '%s' freq %d%s",
639 		bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
640 		bss->freq, extra);
641 	wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
642 	return bss;
643 }
644 
645 
are_ies_equal(const struct wpa_bss * old,const struct wpa_scan_res * new_res,u32 ie)646 static int are_ies_equal(const struct wpa_bss *old,
647 			 const struct wpa_scan_res *new_res, u32 ie)
648 {
649 	const u8 *old_ie, *new_ie;
650 	struct wpabuf *old_ie_buff = NULL;
651 	struct wpabuf *new_ie_buff = NULL;
652 	int new_ie_len, old_ie_len, ret, is_multi;
653 
654 	switch (ie) {
655 	case WPA_IE_VENDOR_TYPE:
656 		old_ie = wpa_bss_get_vendor_ie(old, ie);
657 		new_ie = wpa_scan_get_vendor_ie(new_res, ie);
658 		is_multi = 0;
659 		break;
660 	case WPS_IE_VENDOR_TYPE:
661 		old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
662 		new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
663 		is_multi = 1;
664 		break;
665 	case WLAN_EID_RSN:
666 	case WLAN_EID_SUPP_RATES:
667 	case WLAN_EID_EXT_SUPP_RATES:
668 		old_ie = wpa_bss_get_ie(old, ie);
669 		new_ie = wpa_scan_get_ie(new_res, ie);
670 		is_multi = 0;
671 		break;
672 	default:
673 		wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
674 		return 0;
675 	}
676 
677 	if (is_multi) {
678 		/* in case of multiple IEs stored in buffer */
679 		old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
680 		new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
681 		old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
682 		new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
683 	} else {
684 		/* in case of single IE */
685 		old_ie_len = old_ie ? old_ie[1] + 2 : 0;
686 		new_ie_len = new_ie ? new_ie[1] + 2 : 0;
687 	}
688 
689 	if (!old_ie || !new_ie)
690 		ret = !old_ie && !new_ie;
691 	else
692 		ret = (old_ie_len == new_ie_len &&
693 		       os_memcmp(old_ie, new_ie, old_ie_len) == 0);
694 
695 	wpabuf_free(old_ie_buff);
696 	wpabuf_free(new_ie_buff);
697 
698 	return ret;
699 }
700 
701 
wpa_bss_compare_res(const struct wpa_bss * old,const struct wpa_scan_res * new_res)702 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
703 			       const struct wpa_scan_res *new_res)
704 {
705 	u32 changes = 0;
706 	int caps_diff = old->caps ^ new_res->caps;
707 
708 	if (old->freq != new_res->freq)
709 		changes |= WPA_BSS_FREQ_CHANGED_FLAG;
710 
711 	if (old->level != new_res->level)
712 		changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
713 
714 	if (caps_diff & IEEE80211_CAP_PRIVACY)
715 		changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
716 
717 	if (caps_diff & IEEE80211_CAP_IBSS)
718 		changes |= WPA_BSS_MODE_CHANGED_FLAG;
719 
720 	if (old->ie_len == new_res->ie_len &&
721 	    os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0)
722 		return changes;
723 	changes |= WPA_BSS_IES_CHANGED_FLAG;
724 
725 	if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
726 		changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
727 
728 	if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
729 		changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
730 
731 	if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
732 		changes |= WPA_BSS_WPS_CHANGED_FLAG;
733 
734 	if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
735 	    !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
736 		changes |= WPA_BSS_RATES_CHANGED_FLAG;
737 
738 	return changes;
739 }
740 
741 
notify_bss_changes(struct wpa_supplicant * wpa_s,u32 changes,const struct wpa_bss * bss)742 void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
743 			const struct wpa_bss *bss)
744 {
745 	if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
746 		wpas_notify_bss_freq_changed(wpa_s, bss->id);
747 
748 	if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
749 		wpas_notify_bss_signal_changed(wpa_s, bss->id);
750 
751 	if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
752 		wpas_notify_bss_privacy_changed(wpa_s, bss->id);
753 
754 	if (changes & WPA_BSS_MODE_CHANGED_FLAG)
755 		wpas_notify_bss_mode_changed(wpa_s, bss->id);
756 
757 	if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
758 		wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
759 
760 	if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
761 		wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
762 
763 	if (changes & WPA_BSS_WPS_CHANGED_FLAG)
764 		wpas_notify_bss_wps_changed(wpa_s, bss->id);
765 
766 	if (changes & WPA_BSS_IES_CHANGED_FLAG)
767 		wpas_notify_bss_ies_changed(wpa_s, bss->id);
768 
769 	if (changes & WPA_BSS_RATES_CHANGED_FLAG)
770 		wpas_notify_bss_rates_changed(wpa_s, bss->id);
771 
772 	wpas_notify_bss_seen(wpa_s, bss->id);
773 }
774 
775 
776 static struct wpa_bss *
wpa_bss_update(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_scan_res * res,struct os_reltime * fetch_time)777 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
778 	       struct wpa_scan_res *res, struct os_reltime *fetch_time)
779 {
780 	u32 changes;
781 
782 	if (bss->last_update_idx == wpa_s->bss_update_idx) {
783 		struct os_reltime update_time;
784 
785 		/*
786 		 * Some drivers (e.g., cfg80211) include multiple BSS entries
787 		 * for the same BSS if that BSS's channel changes. The BSS list
788 		 * implementation in wpa_supplicant does not do that and we need
789 		 * to filter out the obsolete results here to make sure only the
790 		 * most current BSS information remains in the table.
791 		 */
792 		wpa_printf(MSG_DEBUG, "BSS: " MACSTR
793 			   " has multiple entries in the scan results - select the most current one",
794 			   MAC2STR(bss->bssid));
795 		calculate_update_time(fetch_time, res->age, &update_time);
796 		wpa_printf(MSG_DEBUG,
797 			   "Previous last_update: %u.%06u (freq %d%s)",
798 			   (unsigned int) bss->last_update.sec,
799 			   (unsigned int) bss->last_update.usec,
800 			   bss->freq,
801 			   (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
802 		wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
803 			   (unsigned int) update_time.sec,
804 			   (unsigned int) update_time.usec,
805 			   res->freq,
806 			   (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
807 		if ((bss->flags & WPA_BSS_ASSOCIATED) ||
808 		    (!(res->flags & WPA_SCAN_ASSOCIATED) &&
809 		     !os_reltime_before(&bss->last_update, &update_time))) {
810 			wpa_printf(MSG_DEBUG,
811 				   "Ignore this BSS entry since the previous update looks more current");
812 			return bss;
813 		}
814 		wpa_printf(MSG_DEBUG,
815 			   "Accept this BSS entry since it looks more current than the previous update");
816 	}
817 
818 	changes = wpa_bss_compare_res(bss, res);
819 	if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
820 		wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
821 			   MAC2STR(bss->bssid), bss->freq, res->freq);
822 	bss->scan_miss_count = 0;
823 	bss->last_update_idx = wpa_s->bss_update_idx;
824 	wpa_bss_copy_res(bss, res, fetch_time);
825 	/* Move the entry to the end of the list */
826 	dl_list_del(&bss->list);
827 #ifdef CONFIG_P2P
828 	if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
829 	    !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE) &&
830 	    !(changes & WPA_BSS_FREQ_CHANGED_FLAG)) {
831 		/*
832 		 * This can happen when non-P2P station interface runs a scan
833 		 * without P2P IE in the Probe Request frame. P2P GO would reply
834 		 * to that with a Probe Response that does not include P2P IE.
835 		 * Do not update the IEs in this BSS entry to avoid such loss of
836 		 * information that may be needed for P2P operations to
837 		 * determine group information.
838 		 */
839 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
840 			MACSTR " since that would remove P2P IE information",
841 			MAC2STR(bss->bssid));
842 	} else
843 #endif /* CONFIG_P2P */
844 	if (bss->ie_len + bss->beacon_ie_len >=
845 	    res->ie_len + res->beacon_ie_len) {
846 		os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
847 		bss->ie_len = res->ie_len;
848 		bss->beacon_ie_len = res->beacon_ie_len;
849 	} else {
850 		struct wpa_bss *nbss;
851 		struct dl_list *prev = bss->list_id.prev;
852 		struct wpa_connect_work *cwork;
853 		unsigned int i, j;
854 		bool update_current_bss = wpa_s->current_bss == bss;
855 		bool update_ml_probe_bss = wpa_s->ml_connect_probe_bss == bss;
856 		int update_link_bss = -1;
857 #ifdef CONFIG_WNM
858 		bool update_wnm_target = wpa_s->wnm_target_bss == bss;
859 #endif /* CONFIG_WNM */
860 #ifdef CONFIG_INTERWORKING
861 		bool update_interworking = wpa_s->interworking_gas_bss == bss;
862 #endif /* CONFIG_INTERWORKING */
863 
864 		for (j = 0; j < MAX_NUM_MLD_LINKS; j++) {
865 			if (wpa_s->links[j].bss == bss) {
866 				update_link_bss = j;
867 				break;
868 			}
869 		}
870 
871 		cwork = wpa_bss_check_pending_connect(wpa_s, bss);
872 
873 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
874 			if (wpa_s->last_scan_res[i] == bss)
875 				break;
876 		}
877 
878 		dl_list_del(&bss->list_id);
879 		nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
880 				  res->beacon_ie_len);
881 		if (nbss) {
882 			if (i != wpa_s->last_scan_res_used)
883 				wpa_s->last_scan_res[i] = nbss;
884 
885 			if (update_current_bss)
886 				wpa_s->current_bss = nbss;
887 
888 			if (update_ml_probe_bss)
889 				wpa_s->ml_connect_probe_bss = nbss;
890 
891 			if (update_link_bss >= 0)
892 				wpa_s->links[update_link_bss].bss = nbss;
893 
894 #ifdef CONFIG_WNM
895 			if (update_wnm_target)
896 				wpa_s->wnm_target_bss = nbss;
897 #endif /* CONFIG_WNM */
898 #ifdef CONFIG_INTERWORKING
899 			if (update_interworking)
900 				wpa_s->interworking_gas_bss = nbss;
901 #endif /* CONFIG_INTERWORKING */
902 
903 			if (cwork)
904 				wpa_bss_update_pending_connect(cwork, nbss);
905 
906 			bss = nbss;
907 			os_memcpy(bss->ies, res + 1,
908 				  res->ie_len + res->beacon_ie_len);
909 			bss->ie_len = res->ie_len;
910 			bss->beacon_ie_len = res->beacon_ie_len;
911 		}
912 		dl_list_add(prev, &bss->list_id);
913 	}
914 	if (changes & WPA_BSS_IES_CHANGED_FLAG) {
915 		wpa_bss_set_hessid(bss);
916 
917 		wpa_bss_parse_basic_ml_element(wpa_s, bss);
918 	}
919 	dl_list_add_tail(&wpa_s->bss, &bss->list);
920 
921 	notify_bss_changes(wpa_s, changes, bss);
922 
923 	return bss;
924 }
925 
926 
927 /**
928  * wpa_bss_update_start - Start a BSS table update from scan results
929  * @wpa_s: Pointer to wpa_supplicant data
930  *
931  * This function is called at the start of each BSS table update round for new
932  * scan results. The actual scan result entries are indicated with calls to
933  * wpa_bss_update_scan_res() and the update round is finished with a call to
934  * wpa_bss_update_end().
935  */
wpa_bss_update_start(struct wpa_supplicant * wpa_s)936 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
937 {
938 	wpa_s->bss_update_idx++;
939 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
940 		wpa_s->bss_update_idx);
941 	wpa_s->last_scan_res_used = 0;
942 }
943 
944 
945 /**
946  * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
947  * @wpa_s: Pointer to wpa_supplicant data
948  * @res: Scan result
949  * @fetch_time: Time when the result was fetched from the driver
950  *
951  * This function updates a BSS table entry (or adds one) based on a scan result.
952  * This is called separately for each scan result between the calls to
953  * wpa_bss_update_start() and wpa_bss_update_end().
954  */
wpa_bss_update_scan_res(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res,struct os_reltime * fetch_time)955 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
956 			     struct wpa_scan_res *res,
957 			     struct os_reltime *fetch_time)
958 {
959 	const u8 *ssid, *p2p, *mesh;
960 	struct wpa_bss *bss;
961 
962 	if (wpa_s->conf->ignore_old_scan_res) {
963 		struct os_reltime update;
964 		calculate_update_time(fetch_time, res->age, &update);
965 		if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
966 			struct os_reltime age;
967 			os_reltime_sub(&wpa_s->scan_trigger_time, &update,
968 				       &age);
969 			wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
970 				"table entry that is %u.%06u seconds older "
971 				"than our scan trigger",
972 				(unsigned int) age.sec,
973 				(unsigned int) age.usec);
974 			return;
975 		}
976 	}
977 
978 	ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
979 	if (ssid == NULL) {
980 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
981 			MACSTR, MAC2STR(res->bssid));
982 		return;
983 	}
984 	if (ssid[1] > SSID_MAX_LEN) {
985 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
986 			MACSTR, MAC2STR(res->bssid));
987 		return;
988 	}
989 
990 	p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
991 #ifdef CONFIG_P2P
992 	if (p2p == NULL &&
993 	    wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
994 		/*
995 		 * If it's a P2P specific interface, then don't update
996 		 * the scan result without a P2P IE.
997 		 */
998 		wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
999 			   " update for P2P interface", MAC2STR(res->bssid));
1000 		return;
1001 	}
1002 #endif /* CONFIG_P2P */
1003 	if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
1004 	    os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
1005 		return; /* Skip P2P listen discovery results here */
1006 
1007 	/* TODO: add option for ignoring BSSes we are not interested in
1008 	 * (to save memory) */
1009 
1010 	mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
1011 	if (mesh && mesh[1] <= SSID_MAX_LEN)
1012 		ssid = mesh;
1013 
1014 	bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
1015 	if (bss == NULL)
1016 		bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
1017 	else {
1018 		bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
1019 		if (wpa_s->last_scan_res) {
1020 			unsigned int i;
1021 			for (i = 0; i < wpa_s->last_scan_res_used; i++) {
1022 				if (bss == wpa_s->last_scan_res[i]) {
1023 					/* Already in the list */
1024 					return;
1025 				}
1026 			}
1027 		}
1028 	}
1029 
1030 	if (bss == NULL)
1031 		return;
1032 	if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
1033 		struct wpa_bss **n;
1034 		unsigned int siz;
1035 		if (wpa_s->last_scan_res_size == 0)
1036 			siz = 32;
1037 		else
1038 			siz = wpa_s->last_scan_res_size * 2;
1039 		n = os_realloc_array(wpa_s->last_scan_res, siz,
1040 				     sizeof(struct wpa_bss *));
1041 		if (n == NULL)
1042 			return;
1043 		wpa_s->last_scan_res = n;
1044 		wpa_s->last_scan_res_size = siz;
1045 	}
1046 
1047 	if (wpa_s->last_scan_res)
1048 		wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
1049 }
1050 
1051 
wpa_bss_included_in_scan(const struct wpa_bss * bss,const struct scan_info * info)1052 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
1053 				    const struct scan_info *info)
1054 {
1055 	int found;
1056 	size_t i;
1057 
1058 	if (info == NULL)
1059 		return 1;
1060 
1061 	if (info->num_freqs) {
1062 		found = 0;
1063 		for (i = 0; i < info->num_freqs; i++) {
1064 			if (bss->freq == info->freqs[i]) {
1065 				found = 1;
1066 				break;
1067 			}
1068 		}
1069 		if (!found)
1070 			return 0;
1071 	}
1072 
1073 	if (info->num_ssids) {
1074 		found = 0;
1075 		for (i = 0; i < info->num_ssids; i++) {
1076 			const struct wpa_driver_scan_ssid *s = &info->ssids[i];
1077 			if ((s->ssid == NULL || s->ssid_len == 0) ||
1078 			    (s->ssid_len == bss->ssid_len &&
1079 			     os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
1080 			     0)) {
1081 				found = 1;
1082 				break;
1083 			}
1084 		}
1085 		if (!found)
1086 			return 0;
1087 	}
1088 
1089 	return 1;
1090 }
1091 
1092 
1093 /**
1094  * wpa_bss_update_end - End a BSS table update from scan results
1095  * @wpa_s: Pointer to wpa_supplicant data
1096  * @info: Information about scan parameters
1097  * @new_scan: Whether this update round was based on a new scan
1098  *
1099  * This function is called at the end of each BSS table update round for new
1100  * scan results. The start of the update was indicated with a call to
1101  * wpa_bss_update_start().
1102  */
wpa_bss_update_end(struct wpa_supplicant * wpa_s,struct scan_info * info,int new_scan)1103 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
1104 			int new_scan)
1105 {
1106 	struct wpa_bss *bss, *n;
1107 
1108 	os_get_reltime(&wpa_s->last_scan);
1109 	if ((info && info->aborted) || !new_scan)
1110 		return; /* do not expire entries without new scan */
1111 
1112 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1113 		if (wpa_bss_in_use(wpa_s, bss))
1114 			continue;
1115 		if (!wpa_bss_included_in_scan(bss, info))
1116 			continue; /* expire only BSSes that were scanned */
1117 		if (bss->last_update_idx < wpa_s->bss_update_idx)
1118 			bss->scan_miss_count++;
1119 		if (bss->scan_miss_count >=
1120 		    wpa_s->conf->bss_expiration_scan_count) {
1121 			wpa_bss_remove(wpa_s, bss, "no match in scan");
1122 		}
1123 	}
1124 
1125 	wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu",
1126 		   wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
1127 }
1128 
1129 
1130 /**
1131  * wpa_bss_flush_by_age - Flush old BSS entries
1132  * @wpa_s: Pointer to wpa_supplicant data
1133  * @age: Maximum entry age in seconds
1134  *
1135  * Remove BSS entries that have not been updated during the last @age seconds.
1136  */
wpa_bss_flush_by_age(struct wpa_supplicant * wpa_s,int age)1137 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
1138 {
1139 	struct wpa_bss *bss, *n;
1140 	struct os_reltime t;
1141 
1142 	if (dl_list_empty(&wpa_s->bss))
1143 		return;
1144 
1145 	os_get_reltime(&t);
1146 
1147 	if (t.sec < age)
1148 		return; /* avoid underflow; there can be no older entries */
1149 
1150 	t.sec -= age;
1151 
1152 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1153 		if (wpa_bss_in_use(wpa_s, bss))
1154 			continue;
1155 
1156 		if (wpa_s->reassoc_same_ess &&
1157 		    wpa_s->wpa_state != WPA_COMPLETED &&
1158 		    wpa_s->last_ssid &&
1159 		    wpa_s->last_ssid->ssid &&
1160 		    bss->ssid_len == wpa_s->last_ssid->ssid_len &&
1161 		    os_memcmp(bss->ssid, wpa_s->last_ssid->ssid,
1162 			      bss->ssid_len) == 0)
1163 			continue;
1164 
1165 		if (os_reltime_before(&bss->last_update, &t)) {
1166 			wpa_bss_remove(wpa_s, bss, __func__);
1167 		} else
1168 			break;
1169 	}
1170 }
1171 
1172 
1173 /**
1174  * wpa_bss_init - Initialize BSS table
1175  * @wpa_s: Pointer to wpa_supplicant data
1176  * Returns: 0 on success, -1 on failure
1177  *
1178  * This prepares BSS table lists and timer for periodic updates. The BSS table
1179  * is deinitialized with wpa_bss_deinit() once not needed anymore.
1180  */
wpa_bss_init(struct wpa_supplicant * wpa_s)1181 int wpa_bss_init(struct wpa_supplicant *wpa_s)
1182 {
1183 	dl_list_init(&wpa_s->bss);
1184 	dl_list_init(&wpa_s->bss_id);
1185 	return 0;
1186 }
1187 
1188 
1189 /**
1190  * wpa_bss_flush - Flush all unused BSS entries
1191  * @wpa_s: Pointer to wpa_supplicant data
1192  */
wpa_bss_flush(struct wpa_supplicant * wpa_s)1193 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
1194 {
1195 	struct wpa_bss *bss, *n;
1196 
1197 	wpa_s->clear_driver_scan_cache = 1;
1198 
1199 	if (wpa_s->bss.next == NULL)
1200 		return; /* BSS table not yet initialized */
1201 
1202 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1203 		if (wpa_bss_in_use(wpa_s, bss))
1204 			continue;
1205 		wpa_bss_remove(wpa_s, bss, __func__);
1206 	}
1207 }
1208 
1209 
1210 /**
1211  * wpa_bss_deinit - Deinitialize BSS table
1212  * @wpa_s: Pointer to wpa_supplicant data
1213  */
wpa_bss_deinit(struct wpa_supplicant * wpa_s)1214 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
1215 {
1216 	wpa_bss_flush(wpa_s);
1217 }
1218 
1219 
1220 /**
1221  * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
1222  * @wpa_s: Pointer to wpa_supplicant data
1223  * @bssid: BSSID
1224  * Returns: Pointer to the BSS entry or %NULL if not found
1225  */
wpa_bss_get_bssid(struct wpa_supplicant * wpa_s,const u8 * bssid)1226 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
1227 				   const u8 *bssid)
1228 {
1229 	struct wpa_bss *bss;
1230 	if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1231 		return NULL;
1232 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1233 		if (ether_addr_equal(bss->bssid, bssid))
1234 			return bss;
1235 	}
1236 	return NULL;
1237 }
1238 
1239 
1240 /**
1241  * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1242  * @wpa_s: Pointer to wpa_supplicant data
1243  * @bssid: BSSID
1244  * Returns: Pointer to the BSS entry or %NULL if not found
1245  *
1246  * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1247  * find the entry that has the most recent update. This can help in finding the
1248  * correct entry in cases where the SSID of the AP may have changed recently
1249  * (e.g., in WPS reconfiguration cases).
1250  */
wpa_bss_get_bssid_latest(struct wpa_supplicant * wpa_s,const u8 * bssid)1251 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1252 					  const u8 *bssid)
1253 {
1254 	struct wpa_bss *bss, *found = NULL;
1255 	if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1256 		return NULL;
1257 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1258 		if (!ether_addr_equal(bss->bssid, bssid))
1259 			continue;
1260 		if (found == NULL ||
1261 		    os_reltime_before(&found->last_update, &bss->last_update))
1262 			found = bss;
1263 	}
1264 	return found;
1265 }
1266 
1267 
1268 #ifdef CONFIG_P2P
1269 /**
1270  * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr
1271  * @wpa_s: Pointer to wpa_supplicant data
1272  * @dev_addr: P2P Device Address of the GO
1273  * Returns: Pointer to the BSS entry or %NULL if not found
1274  *
1275  * This function tries to find the entry that has the most recent update. This
1276  * can help in finding the correct entry in cases where the SSID of the P2P
1277  * Device may have changed recently.
1278  */
wpa_bss_get_p2p_dev_addr(struct wpa_supplicant * wpa_s,const u8 * dev_addr)1279 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1280 					  const u8 *dev_addr)
1281 {
1282 	struct wpa_bss *bss, *found = NULL;
1283 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1284 		u8 addr[ETH_ALEN];
1285 		if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
1286 				       addr) != 0 ||
1287 		    !ether_addr_equal(addr, dev_addr))
1288 			continue;
1289 		if (!found ||
1290 		    os_reltime_before(&found->last_update, &bss->last_update))
1291 			found = bss;
1292 	}
1293 	return found;
1294 }
1295 #endif /* CONFIG_P2P */
1296 
1297 
1298 /**
1299  * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1300  * @wpa_s: Pointer to wpa_supplicant data
1301  * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1302  * Returns: Pointer to the BSS entry or %NULL if not found
1303  */
wpa_bss_get_id(struct wpa_supplicant * wpa_s,unsigned int id)1304 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1305 {
1306 	struct wpa_bss *bss;
1307 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1308 		if (bss->id == id)
1309 			return bss;
1310 	}
1311 	return NULL;
1312 }
1313 
1314 
1315 /**
1316  * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1317  * @wpa_s: Pointer to wpa_supplicant data
1318  * @idf: Smallest allowed identifier assigned for the entry
1319  * @idf: Largest allowed identifier assigned for the entry
1320  * Returns: Pointer to the BSS entry or %NULL if not found
1321  *
1322  * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1323  * smallest id value to be fetched within the specified range without the
1324  * caller having to know the exact id.
1325  */
wpa_bss_get_id_range(struct wpa_supplicant * wpa_s,unsigned int idf,unsigned int idl)1326 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1327 				      unsigned int idf, unsigned int idl)
1328 {
1329 	struct wpa_bss *bss;
1330 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1331 		if (bss->id >= idf && bss->id <= idl)
1332 			return bss;
1333 	}
1334 	return NULL;
1335 }
1336 
1337 
1338 /**
1339  * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1340  * @bss: BSS table entry
1341  * @ie: Information element identitifier (WLAN_EID_*)
1342  * Returns: Pointer to the information element (id field) or %NULL if not found
1343  *
1344  * This function returns the first matching information element in the BSS
1345  * entry.
1346  */
wpa_bss_get_ie(const struct wpa_bss * bss,u8 ie)1347 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1348 {
1349 	return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie);
1350 }
1351 
1352 
1353 /**
1354  * wpa_bss_get_ie_beacon - Fetch a specified information element from a BSS entry
1355  * @bss: BSS table entry
1356  * @ie: Information element identitifier (WLAN_EID_*)
1357  * Returns: Pointer to the information element (id field) or %NULL if not found
1358  *
1359  * This function returns the first matching information element in the BSS
1360  * entry.
1361  *
1362  * This function is like wpa_bss_get_ie(), but uses IE buffer only from Beacon
1363  * frames instead of either Beacon or Probe Response frames.
1364  */
wpa_bss_get_ie_beacon(const struct wpa_bss * bss,u8 ie)1365 const u8 * wpa_bss_get_ie_beacon(const struct wpa_bss *bss, u8 ie)
1366 {
1367 	const u8 *ies;
1368 
1369 	if (bss->beacon_ie_len == 0)
1370 		return NULL;
1371 
1372 	ies = wpa_bss_ie_ptr(bss);
1373 	ies += bss->ie_len;
1374 	return get_ie(ies, bss->beacon_ie_len, ie);
1375 }
1376 
1377 
1378 /**
1379  * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry
1380  * @bss: BSS table entry
1381  * @ext: Information element extension identifier (WLAN_EID_EXT_*)
1382  * Returns: Pointer to the information element (id field) or %NULL if not found
1383  *
1384  * This function returns the first matching information element in the BSS
1385  * entry.
1386  */
wpa_bss_get_ie_ext(const struct wpa_bss * bss,u8 ext)1387 const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext)
1388 {
1389 	return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext);
1390 }
1391 
1392 
1393 /**
1394  * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1395  * @bss: BSS table entry
1396  * @vendor_type: Vendor type (four octets starting the IE payload)
1397  * Returns: Pointer to the information element (id field) or %NULL if not found
1398  *
1399  * This function returns the first matching information element in the BSS
1400  * entry.
1401  */
wpa_bss_get_vendor_ie(const struct wpa_bss * bss,u32 vendor_type)1402 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1403 {
1404 	const u8 *ies;
1405 	const struct element *elem;
1406 
1407 	ies = wpa_bss_ie_ptr(bss);
1408 
1409 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) {
1410 		if (elem->datalen >= 4 &&
1411 		    vendor_type == WPA_GET_BE32(elem->data))
1412 			return &elem->id;
1413 	}
1414 
1415 	return NULL;
1416 }
1417 
1418 
1419 /**
1420  * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1421  * @bss: BSS table entry
1422  * @vendor_type: Vendor type (four octets starting the IE payload)
1423  * Returns: Pointer to the information element (id field) or %NULL if not found
1424  *
1425  * This function returns the first matching information element in the BSS
1426  * entry.
1427  *
1428  * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1429  * from Beacon frames instead of either Beacon or Probe Response frames.
1430  */
wpa_bss_get_vendor_ie_beacon(const struct wpa_bss * bss,u32 vendor_type)1431 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1432 					u32 vendor_type)
1433 {
1434 	const u8 *ies;
1435 	const struct element *elem;
1436 
1437 	if (bss->beacon_ie_len == 0)
1438 		return NULL;
1439 
1440 	ies = wpa_bss_ie_ptr(bss);
1441 	ies += bss->ie_len;
1442 
1443 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1444 			    bss->beacon_ie_len) {
1445 		if (elem->datalen >= 4 &&
1446 		    vendor_type == WPA_GET_BE32(elem->data))
1447 			return &elem->id;
1448 	}
1449 
1450 	return NULL;
1451 }
1452 
1453 
1454 /**
1455  * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1456  * @bss: BSS table entry
1457  * @vendor_type: Vendor type (four octets starting the IE payload)
1458  * Returns: Pointer to the information element payload or %NULL if not found
1459  *
1460  * This function returns concatenated payload of possibly fragmented vendor
1461  * specific information elements in the BSS entry. The caller is responsible for
1462  * freeing the returned buffer.
1463  */
wpa_bss_get_vendor_ie_multi(const struct wpa_bss * bss,u32 vendor_type)1464 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1465 					    u32 vendor_type)
1466 {
1467 	struct wpabuf *buf;
1468 	const u8 *end, *pos;
1469 
1470 	buf = wpabuf_alloc(bss->ie_len);
1471 	if (buf == NULL)
1472 		return NULL;
1473 
1474 	pos = wpa_bss_ie_ptr(bss);
1475 	end = pos + bss->ie_len;
1476 
1477 	while (end - pos > 1) {
1478 		u8 ie, len;
1479 
1480 		ie = pos[0];
1481 		len = pos[1];
1482 		if (len > end - pos - 2)
1483 			break;
1484 		pos += 2;
1485 		if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1486 		    vendor_type == WPA_GET_BE32(pos))
1487 			wpabuf_put_data(buf, pos + 4, len - 4);
1488 		pos += len;
1489 	}
1490 
1491 	if (wpabuf_len(buf) == 0) {
1492 		wpabuf_free(buf);
1493 		buf = NULL;
1494 	}
1495 
1496 	return buf;
1497 }
1498 
1499 
1500 /**
1501  * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1502  * @bss: BSS table entry
1503  * @vendor_type: Vendor type (four octets starting the IE payload)
1504  * Returns: Pointer to the information element payload or %NULL if not found
1505  *
1506  * This function returns concatenated payload of possibly fragmented vendor
1507  * specific information elements in the BSS entry. The caller is responsible for
1508  * freeing the returned buffer.
1509  *
1510  * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1511  * from Beacon frames instead of either Beacon or Probe Response frames.
1512  */
wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss * bss,u32 vendor_type)1513 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1514 						   u32 vendor_type)
1515 {
1516 	struct wpabuf *buf;
1517 	const u8 *end, *pos;
1518 
1519 	buf = wpabuf_alloc(bss->beacon_ie_len);
1520 	if (buf == NULL)
1521 		return NULL;
1522 
1523 	pos = wpa_bss_ie_ptr(bss);
1524 	pos += bss->ie_len;
1525 	end = pos + bss->beacon_ie_len;
1526 
1527 	while (end - pos > 1) {
1528 		u8 id, len;
1529 
1530 		id = *pos++;
1531 		len = *pos++;
1532 		if (len > end - pos)
1533 			break;
1534 		if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1535 		    vendor_type == WPA_GET_BE32(pos))
1536 			wpabuf_put_data(buf, pos + 4, len - 4);
1537 		pos += len;
1538 	}
1539 
1540 	if (wpabuf_len(buf) == 0) {
1541 		wpabuf_free(buf);
1542 		buf = NULL;
1543 	}
1544 
1545 	return buf;
1546 }
1547 
1548 
1549 /**
1550  * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1551  * @bss: BSS table entry
1552  * Returns: Maximum legacy rate in units of 500 kbps
1553  */
wpa_bss_get_max_rate(const struct wpa_bss * bss)1554 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1555 {
1556 	int rate = 0;
1557 	const u8 *ie;
1558 	int i;
1559 
1560 	ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1561 	for (i = 0; ie && i < ie[1]; i++) {
1562 		if ((ie[i + 2] & 0x7f) > rate)
1563 			rate = ie[i + 2] & 0x7f;
1564 	}
1565 
1566 	ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1567 	for (i = 0; ie && i < ie[1]; i++) {
1568 		if ((ie[i + 2] & 0x7f) > rate)
1569 			rate = ie[i + 2] & 0x7f;
1570 	}
1571 
1572 	return rate;
1573 }
1574 
1575 
1576 /**
1577  * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1578  * @bss: BSS table entry
1579  * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1580  * Returns: number of legacy TX rates or -1 on failure
1581  *
1582  * The caller is responsible for freeing the returned buffer with os_free() in
1583  * case of success.
1584  */
wpa_bss_get_bit_rates(const struct wpa_bss * bss,u8 ** rates)1585 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1586 {
1587 	const u8 *ie, *ie2;
1588 	int i, j;
1589 	unsigned int len;
1590 	u8 *r;
1591 
1592 	ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1593 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1594 
1595 	len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1596 
1597 	r = os_malloc(len);
1598 	if (!r)
1599 		return -1;
1600 
1601 	for (i = 0; ie && i < ie[1]; i++)
1602 		r[i] = ie[i + 2] & 0x7f;
1603 
1604 	for (j = 0; ie2 && j < ie2[1]; j++)
1605 		r[i + j] = ie2[j + 2] & 0x7f;
1606 
1607 	*rates = r;
1608 	return len;
1609 }
1610 
1611 
1612 #ifdef CONFIG_FILS
wpa_bss_get_fils_cache_id(const struct wpa_bss * bss)1613 const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss)
1614 {
1615 	const u8 *ie;
1616 
1617 	if (bss) {
1618 		ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
1619 		if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7))
1620 			return ie + 4;
1621 	}
1622 
1623 	return NULL;
1624 }
1625 #endif /* CONFIG_FILS */
1626 
1627 
wpa_bss_ext_capab(const struct wpa_bss * bss,unsigned int capab)1628 int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab)
1629 {
1630 	if (!bss)
1631 		return 0;
1632 	return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB),
1633 				    capab);
1634 }
1635 
1636 
1637 static void
wpa_bss_parse_ml_rnr_ap_info(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,u8 ap_mld_id,const struct ieee80211_neighbor_ap_info * ap_info,size_t len,u16 * seen)1638 wpa_bss_parse_ml_rnr_ap_info(struct wpa_supplicant *wpa_s,
1639 			     struct wpa_bss *bss, u8 ap_mld_id,
1640 			     const struct ieee80211_neighbor_ap_info *ap_info,
1641 			     size_t len, u16 *seen)
1642 {
1643 	const u8 *pos, *end;
1644 	const u8 *mld_params;
1645 	u8 count, mld_params_offset;
1646 	u8 i, type, link_id;
1647 
1648 	count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
1649 	type = ap_info->tbtt_info_hdr & RNR_TBTT_INFO_HDR_TYPE_MSK;
1650 
1651 	/* MLD information is at offset 13 or at start */
1652 	if (type == 0 && ap_info->tbtt_info_len >= RNR_TBTT_INFO_MLD_LEN) {
1653 		/* MLD info is appended */
1654 		mld_params_offset = RNR_TBTT_INFO_LEN;
1655 	} else {
1656 		/* TODO: Support NSTR AP */
1657 		return;
1658 	}
1659 
1660 	pos = (const u8 *) ap_info;
1661 	end = pos + len;
1662 	pos += sizeof(*ap_info);
1663 
1664 	for (i = 0; i < count; i++, pos += ap_info->tbtt_info_len) {
1665 		if (end - pos < ap_info->tbtt_info_len)
1666 			break;
1667 
1668 		mld_params = pos + mld_params_offset;
1669 
1670 		link_id = *(mld_params + 1) & EHT_ML_LINK_ID_MSK;
1671 		if (link_id >= MAX_NUM_MLD_LINKS)
1672 			continue;
1673 
1674 		if (*mld_params != ap_mld_id) {
1675 			wpa_printf(MSG_DEBUG,
1676 				   "MLD: Reported link not part of MLD");
1677 		} else if (!(BIT(link_id) & *seen)) {
1678 			struct mld_link *l;
1679 
1680 			*seen |= BIT(link_id);
1681 			wpa_printf(MSG_DEBUG, "MLD: mld ID=%u, link ID=%u",
1682 				   *mld_params, link_id);
1683 
1684 			bss->valid_links |= BIT(link_id);
1685 			l = &bss->mld_links[link_id];
1686 			os_memcpy(l->bssid, pos + 1, ETH_ALEN);
1687 			l->disabled = mld_params[2] &
1688 				RNR_TBTT_INFO_MLD_PARAM2_LINK_DISABLED;
1689 			l->freq = ieee80211_chan_to_freq(NULL,
1690 							 ap_info->op_class,
1691 							 ap_info->channel);
1692 		}
1693 	}
1694 }
1695 
1696 
1697 /**
1698  * wpa_bss_validate_rsne_ml - Validate RSN IEs (RSNE/RSNOE/RSNO2E) of a BSS
1699  * @wpa_s: Pointer to wpa_supplicant data
1700  * @ssid: Network config
1701  * @bss: BSS table entry
1702  * @rsne_type_p: Type of RSNE to validate. If -1 is given, choose as per the
1703  *	presence of RSN elements (association link); otherwise, validate
1704  *	against the requested type (other affiliated links).
1705  * @ref_rsne: Buffer for RSNE data; filled in from the main link to compare
1706  *	against and used internally
1707  * Returns: true if the BSS configuration matches local profile and the elements
1708  * meet MLO requirements, false otherwise
1709  */
1710 static bool
wpa_bss_validate_rsne_ml(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_bss * bss,int * rsne_type_p,struct wpa_ie_data * ref_rsne)1711 wpa_bss_validate_rsne_ml(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
1712 			 struct wpa_bss *bss, int *rsne_type_p,
1713 			 struct wpa_ie_data *ref_rsne)
1714 {
1715 	struct ieee802_11_elems elems;
1716 	struct wpa_ie_data wpa_ie;
1717 	const u8 *rsne;
1718 	size_t rsne_len;
1719 	int rsne_type;
1720 	const u8 *ies_pos = wpa_bss_ie_ptr(bss);
1721 	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
1722 
1723 	if (ieee802_11_parse_elems(ies_pos, ies_len, &elems, 0) ==
1724 	    ParseFailed) {
1725 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: Failed to parse elements");
1726 		return false;
1727 	}
1728 
1729 	if (elems.rsne_override_2 && wpas_rsn_overriding(wpa_s, ssid)) {
1730 		rsne = elems.rsne_override_2;
1731 		rsne_len = elems.rsne_override_2_len;
1732 		rsne_type = 2;
1733 	} else if (elems.rsne_override && wpas_rsn_overriding(wpa_s, ssid)) {
1734 		rsne = elems.rsne_override;
1735 		rsne_len = elems.rsne_override_len;
1736 		rsne_type = 1;
1737 	} else {
1738 		rsne = elems.rsn_ie;
1739 		rsne_len = elems.rsn_ie_len;
1740 		rsne_type = 0;
1741 	}
1742 
1743 	if (!rsne ||
1744 	    wpa_parse_wpa_ie(rsne - 2, 2 + rsne_len, &wpa_ie)) {
1745 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No RSN element");
1746 		return false;
1747 	}
1748 
1749 	if (*rsne_type_p != -1 && *rsne_type_p != rsne_type) {
1750 		wpa_dbg(wpa_s, MSG_DEBUG,
1751 			"MLD: No matching RSN element (RSNO mismatch)");
1752 		return false;
1753 	}
1754 
1755 	if (!(wpa_ie.capabilities & WPA_CAPABILITY_MFPC) ||
1756 	    wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION) {
1757 		wpa_dbg(wpa_s, MSG_DEBUG,
1758 			"MLD: No management frame protection");
1759 		return false;
1760 	}
1761 
1762 	wpa_ie.key_mgmt &= ~(WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
1763 			     WPA_KEY_MGMT_PSK_SHA256);
1764 	if (!(wpa_ie.key_mgmt & ssid->key_mgmt)) {
1765 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No valid key management");
1766 		return false;
1767 	}
1768 	wpa_dbg(wpa_s, MSG_DEBUG, "MLD: key_mgmt=0x%x", wpa_ie.key_mgmt);
1769 
1770 	wpa_ie.pairwise_cipher &= ~(WPA_CIPHER_NONE | WPA_CIPHER_WEP40 |
1771 				    WPA_CIPHER_WEP104 | WPA_CIPHER_TKIP);
1772 	if (!(wpa_ie.pairwise_cipher & ssid->pairwise_cipher)) {
1773 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No valid pairwise cipher");
1774 		return false;
1775 	}
1776 
1777 	if (*rsne_type_p == -1) {
1778 		os_memcpy(ref_rsne, &wpa_ie, sizeof(wpa_ie));
1779 
1780 		*rsne_type_p = rsne_type;
1781 	} else {
1782 		/* Verify the neighbor given rsne_type_p and ref_rsne */
1783 		if (!(wpa_ie.key_mgmt & ref_rsne->key_mgmt)) {
1784 			wpa_dbg(wpa_s, MSG_DEBUG,
1785 				"MLD: Neighbor without common AKM");
1786 			return false;
1787 		}
1788 
1789 		if (!(wpa_ie.pairwise_cipher & ref_rsne->pairwise_cipher)) {
1790 			wpa_dbg(wpa_s, MSG_DEBUG,
1791 				"MLD: Neighbor without common pairwise cipher");
1792 			return false;
1793 		}
1794 	}
1795 
1796 	return true;
1797 }
1798 
1799 
1800 /*
1801  * SME-based association with an AP MLD requires the driver to have a
1802  * sufficiently recent scan entry for every requested link. With nl80211-based
1803  * drivers, cfg80211 rejects the association if the BSS entry for any link is
1804  * older than 30 seconds (IEEE80211_SCAN_RESULT_EXPIRE). Since the BSS table in
1805  * wpa_supplicant keeps entries considerably longer than that
1806  * (bss_expiration_age, 180 seconds by default) and does not refresh entries for
1807  * the links of the current AP MLD while associated, consider an affiliated link
1808  * with an older scan entry to be missing so that an ML probe request gets used
1809  * to refresh the information before association. Leave some margin below the
1810  * kernel limit to cover the time needed for authentication.
1811  */
1812 #define WPA_BSS_ML_LINK_FRESH_AGE 25 /* seconds */
1813 
1814 /**
1815  * wpa_bss_get_usable_links - Retrieve the usable links of the AP MLD
1816  * @wpa_s: Pointer to wpa_supplicant data
1817  * @bss: BSS table entry
1818  * @ssid: Target SSID (or %NULL)
1819  * @missing_links: Result bitmask of links that were not discovered (or %NULL)
1820  * Returns: Bitmap of links that are usable, or 0 for non-MLD or failure
1821  *
1822  * Validate each link of the MLD to verify that it is compatible and connection
1823  * to each of the links is allowed.
1824  */
wpa_bss_get_usable_links(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,struct wpa_ssid * ssid,u16 * missing_links)1825 u16 wpa_bss_get_usable_links(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
1826 			     struct wpa_ssid *ssid, u16 *missing_links)
1827 {
1828 	struct wpa_ie_data rsne;
1829 	struct os_reltime now;
1830 	int rsne_type;
1831 	u16 usable_links = 0;
1832 	u8 link_id;
1833 
1834 	if (!bss->valid_links)
1835 		return 0;
1836 
1837 	os_get_reltime(&now);
1838 
1839 	rsne_type = -1;
1840 	os_memset(&rsne, 0, sizeof(rsne));
1841 	if (ssid &&
1842 	    !wpa_bss_validate_rsne_ml(wpa_s, ssid, bss, &rsne_type, &rsne)) {
1843 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No valid key management");
1844 		return 0;
1845 	}
1846 
1847 	usable_links = BIT(bss->mld_link_id);
1848 
1849 	for_each_link(bss->valid_links, link_id) {
1850 		struct wpa_bss *neigh_bss;
1851 		u16 ext_mld_capa_mask;
1852 
1853 		if (link_id == bss->mld_link_id)
1854 			continue;
1855 
1856 		if (ssid && ssid->ssid_len)
1857 			neigh_bss = wpa_bss_get(wpa_s,
1858 						bss->mld_links[link_id].bssid,
1859 						ssid->ssid,
1860 						ssid->ssid_len);
1861 		else
1862 			neigh_bss = wpa_bss_get_bssid(wpa_s,
1863 						      bss->mld_links[link_id].bssid);
1864 
1865 		if (!neigh_bss) {
1866 			if (missing_links)
1867 				*missing_links |= BIT(link_id);
1868 			continue;
1869 		}
1870 
1871 		if (os_reltime_expired(&now, &neigh_bss->last_update,
1872 				       WPA_BSS_ML_LINK_FRESH_AGE)) {
1873 			wpa_dbg(wpa_s, MSG_DEBUG,
1874 				"MLD: Scan entry for neighbor " MACSTR
1875 				" is too old for association; consider link %u missing",
1876 				MAC2STR(neigh_bss->bssid), link_id);
1877 			if (missing_links)
1878 				*missing_links |= BIT(link_id);
1879 			continue;
1880 		}
1881 
1882 		/* Check that the affiliated links are for the same AP MLD and
1883 		 * the information matches */
1884 		if (!neigh_bss->valid_links) {
1885 			wpa_dbg(wpa_s, MSG_DEBUG,
1886 				"MLD: Neighbor without Multi-Link support");
1887 			continue;
1888 		}
1889 
1890 		if (neigh_bss->mld_link_id != link_id) {
1891 			wpa_dbg(wpa_s, MSG_DEBUG,
1892 				"MLD: Neighbor has unexpected link ID (%d != %d)",
1893 				neigh_bss->mld_link_id, link_id);
1894 			continue;
1895 		}
1896 
1897 		if (!ether_addr_equal(bss->mld_addr, neigh_bss->mld_addr)) {
1898 			wpa_dbg(wpa_s, MSG_DEBUG,
1899 				"MLD: Neighbor has a different MLD MAC address ("
1900 				MACSTR " != " MACSTR ")",
1901 				MAC2STR(neigh_bss->mld_addr),
1902 				MAC2STR(bss->mld_addr));
1903 			continue;
1904 		}
1905 
1906 		if ((bss->mld_capa & ~EHT_ML_MLD_CAPA_RESERVED) !=
1907 		    (neigh_bss->mld_capa & ~EHT_ML_MLD_CAPA_RESERVED)) {
1908 			wpa_dbg(wpa_s, MSG_DEBUG,
1909 				"MLD: Neighbor's MLD Capabilities do not match (0x%04x != 0x%04x)",
1910 				neigh_bss->mld_capa, bss->mld_capa);
1911 			continue;
1912 		}
1913 
1914 		if ((bss->eml_capa & ~EHT_ML_EML_CAPA_RESERVED) !=
1915 		    (neigh_bss->eml_capa & ~EHT_ML_EML_CAPA_RESERVED)) {
1916 			wpa_dbg(wpa_s, MSG_DEBUG,
1917 				"MLD: Neighbor's EML Capabilities do not match (0x%04x != 0x%04x",
1918 				neigh_bss->eml_capa, bss->eml_capa);
1919 			continue;
1920 		}
1921 
1922 		/*
1923 		 * Check well-defined values in Extended MLD Capabilities.
1924 		 * In particular the Recommended Max Simultaneous Links
1925 		 * subfield may change over time and is reserved depending on
1926 		 * the frame that it is carried in.
1927 		 * See IEEE Std 802.11be-2024, Table 9-417o.
1928 		 */
1929 		ext_mld_capa_mask =
1930 			EHT_ML_EXT_MLD_CAPA_OP_PARAM_UPDATE |
1931 			EHT_ML_EXT_MLD_CAPA_NSTR_UPDATE |
1932 			EHT_ML_EXT_MLD_CAPA_EMLSR_ENA_ONE_LINK;
1933 		if ((bss->ext_mld_capa & ext_mld_capa_mask) !=
1934 		    (neigh_bss->ext_mld_capa & ext_mld_capa_mask)) {
1935 			wpa_dbg(wpa_s, MSG_DEBUG,
1936 				"MLD: Neighbors Extended MLD Capabilities do not match (0x%04x != 0x%04x)",
1937 				neigh_bss->ext_mld_capa, bss->ext_mld_capa);
1938 			continue;
1939 		}
1940 
1941 		if (ssid) {
1942 			/* As per IEEE Std 802.11be-2024, 12.6.2 (RSNA
1943 			 * selection), all APs affiliated with an AP MLD shall
1944 			 * advertise at least one common AKM suite selector in
1945 			 * the AKM Suite List field of the RSNE. Discard links
1946 			 * that do not have compatible configuration with the
1947 			 * association link.
1948 			 */
1949 			if (!wpa_bss_validate_rsne_ml(wpa_s, ssid, neigh_bss,
1950 						      &rsne_type, &rsne)) {
1951 				wpa_printf(MSG_DEBUG,
1952 					   "MLD: Discard link %u due to RSN parameter mismatch",
1953 					   link_id);
1954 				continue;
1955 			}
1956 		}
1957 
1958 		if ((!ssid ||
1959 		     wpa_scan_res_match(wpa_s, 0, neigh_bss, ssid, 1, 0,
1960 					true)) &&
1961 		    !wpa_bssid_ignore_is_listed(wpa_s, neigh_bss->bssid)) {
1962 			usable_links |= BIT(link_id);
1963 		}
1964 	}
1965 
1966 	return usable_links;
1967 }
1968 
1969 
1970 /**
1971  * wpa_bss_parse_basic_ml_element - Parse the Basic Multi-Link element
1972  * @wpa_s: Pointer to wpa_supplicant data
1973  * @bss: BSS table entry
1974  *
1975  * Parses the Basic Multi-Link element of the BSS into @link_info using the scan
1976  * information stored in the wpa_supplicant data to fill in information for
1977  * links where possible.
1978  */
wpa_bss_parse_basic_ml_element(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)1979 void wpa_bss_parse_basic_ml_element(struct wpa_supplicant *wpa_s,
1980 				    struct wpa_bss *bss)
1981 {
1982 	struct ieee802_11_elems elems;
1983 	struct wpabuf *mlbuf = NULL;
1984 	const struct element *elem;
1985 	size_t ml_ie_len;
1986 	const struct ieee80211_eht_ml *eht_ml;
1987 	const struct eht_ml_basic_common_info *ml_basic_common_info;
1988 	const u8 *mbssid_idx_elem;
1989 	u8 i, pos, link_id, ap_mld_id;
1990 	const u16 control_mask =
1991 		MULTI_LINK_CONTROL_TYPE_MASK |
1992 		BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1993 		BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1994 		BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
1995 	const u16 control =
1996 		MULTI_LINK_CONTROL_TYPE_BASIC |
1997 		BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
1998 		BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT |
1999 		BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA;
2000 	u16 seen;
2001 	const u8 *ies_pos = wpa_bss_ie_ptr(bss);
2002 	size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
2003 	struct mld_link *l;
2004 
2005 	if (ieee802_11_parse_elems(ies_pos, ies_len, &elems, 1) ==
2006 	    ParseFailed) {
2007 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: Failed to parse elements");
2008 		goto out;
2009 	}
2010 
2011 	mlbuf = ieee802_11_defrag(elems.basic_mle, elems.basic_mle_len, true);
2012 	if (!mlbuf) {
2013 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No Multi-Link element");
2014 		goto out;
2015 	}
2016 
2017 	ml_ie_len = wpabuf_len(mlbuf);
2018 
2019 	/*
2020 	 * for ext ID + 2 control + common info len
2021 	 */
2022 	if (ml_ie_len < sizeof(*eht_ml) + sizeof(*ml_basic_common_info))
2023 		goto out;
2024 
2025 	eht_ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
2026 	if ((le_to_host16(eht_ml->ml_control) & control_mask) != control) {
2027 		wpa_printf(MSG_DEBUG,
2028 			   "MLD: Unexpected Multi-Link element control=0x%x (mask 0x%x expected 0x%x)",
2029 			   le_to_host16(eht_ml->ml_control), control_mask,
2030 			   control);
2031 		goto out;
2032 	}
2033 
2034 	ml_basic_common_info =
2035 		(const struct eht_ml_basic_common_info *) eht_ml->variable;
2036 
2037 	if (ml_ie_len < sizeof(*eht_ml) + ml_basic_common_info->len)
2038 		goto out;
2039 
2040 	/* Minimum Common info length to be valid */
2041 	if (ml_basic_common_info->len <
2042 	    sizeof(*ml_basic_common_info) + 1 + 1 + 2)
2043 		goto out;
2044 
2045 	/* Link ID Info, BSS Parameters Change Count (see control/control_mask)
2046 	 */
2047 	pos = 1 + 1;
2048 
2049 	/* Medium Synchronization Delay Information */
2050 	if (le_to_host16(eht_ml->ml_control) &
2051 	    BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
2052 		if (ml_basic_common_info->len <
2053 		    sizeof(*ml_basic_common_info) + pos + 2)
2054 			goto out;
2055 		pos += 2;
2056 	}
2057 
2058 	/* EML Capabilities */
2059 	bss->eml_capa = 0;
2060 	if (le_to_host16(eht_ml->ml_control) &
2061 	    BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
2062 		if (ml_basic_common_info->len <
2063 		    sizeof(*ml_basic_common_info) + pos + 2)
2064 			goto out;
2065 		bss->eml_capa =
2066 			WPA_GET_LE16(&ml_basic_common_info->variable[pos]);
2067 		pos += 2;
2068 	}
2069 
2070 	/* MLD Capabilities And Operations (always present, see
2071 	 * control/control_mask) */
2072 	if (ml_basic_common_info->len < sizeof(*ml_basic_common_info) + pos + 2)
2073 		goto out;
2074 	bss->mld_capa = WPA_GET_LE16(&ml_basic_common_info->variable[pos]);
2075 	pos += 2;
2076 
2077 	/* AP MLD ID from MLE if present (see comment below) */
2078 	if (le_to_host16(eht_ml->ml_control) &
2079 	    BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
2080 		if (ml_basic_common_info->len <
2081 		    sizeof(*ml_basic_common_info) + pos + 1)
2082 			goto out;
2083 
2084 		ap_mld_id = ml_basic_common_info->variable[pos];
2085 
2086 		pos++;
2087 	} else {
2088 		ap_mld_id = 0;
2089 	}
2090 
2091 	/* Extended MLD Capabilities And Operations */
2092 	bss->ext_mld_capa = 0;
2093 	if (le_to_host16(eht_ml->ml_control) &
2094 	    BASIC_MULTI_LINK_CTRL_PRES_EXT_MLD_CAP) {
2095 		if (ml_basic_common_info->len <
2096 		    sizeof(*ml_basic_common_info) + pos + 2)
2097 			goto out;
2098 
2099 		bss->ext_mld_capa =
2100 			WPA_GET_LE16(&ml_basic_common_info->variable[pos]);
2101 
2102 		pos += 2;
2103 	}
2104 
2105 	if (ml_basic_common_info->len < sizeof(*ml_basic_common_info) + pos)
2106 		goto out;
2107 
2108 	link_id = ml_basic_common_info->variable[0] & EHT_ML_LINK_ID_MSK;
2109 	if (link_id >= MAX_NUM_MLD_LINKS) {
2110 		wpa_printf(MSG_DEBUG, "MLD: Invalid link ID %u in Basic MLE",
2111 			   link_id);
2112 		goto out;
2113 	}
2114 
2115 	os_memcpy(bss->mld_addr, ml_basic_common_info->mld_addr, ETH_ALEN);
2116 
2117 	bss->mld_link_id = link_id;
2118 	bss->valid_links = BIT(link_id);
2119 	seen = bss->valid_links;
2120 
2121 	l = &bss->mld_links[link_id];
2122 	os_memcpy(l->bssid, bss->bssid, ETH_ALEN);
2123 	l->freq = bss->freq;
2124 
2125 	bss->mld_bss_non_transmitted = false;
2126 
2127 	/*
2128 	 * We should be able to rely on the Multiple BSSID Index element
2129 	 * to be included if the BSS is nontransmitted. Both if it was
2130 	 * extracted from a beacon and if it came from an ML probe
2131 	 * response (i.e. not listed in IEEE Std 802.11be-2024, 35.3.3.4).
2132 	 *
2133 	 * Note that the AP MLD ID and the Multiple-BSSID Index will be
2134 	 * identical if the information was reported by the
2135 	 * corresponding transmitting AP (IEEE Std 802.11be-2024, 9.4.2.169.2).
2136 	 * As an AP MLD ID will not be explicitly provided we need to
2137 	 * rely on the Multiple-BSSID Index element. This is generally the case
2138 	 * when the BSS information was read from a Multiple-BSSID element.
2139 	 *
2140 	 * The alternative scenario is a BSS discovered using a
2141 	 * Multi-Link Probe Response. In that case, we can still
2142 	 * determine whether the BSS is nontransmitted or not using the
2143 	 * Multiple BSSID-Index element. However, the AP MLD ID may be
2144 	 * different inside the ML Probe Response and the driver also
2145 	 * needs to deal with this during inheritance.
2146 	 *
2147 	 * We assume the driver either
2148 	 *  - includes the appropriate AP MLD ID in the MLE it generates
2149 	 *    (see above), or
2150 	 *  - rewrites the RNR so that the AP MLD ID matches the
2151 	 *    Multiple-BSSID Index element.
2152 	 */
2153 	mbssid_idx_elem = wpa_bss_get_ie(bss, WLAN_EID_MULTIPLE_BSSID_INDEX);
2154 	if (mbssid_idx_elem && mbssid_idx_elem[1] >= 1) {
2155 		if (!(le_to_host16(eht_ml->ml_control) &
2156 		      BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID))
2157 			ap_mld_id = mbssid_idx_elem[2];
2158 		bss->mld_bss_non_transmitted = !!mbssid_idx_elem[2];
2159 	}
2160 
2161 	for_each_element_id(elem, WLAN_EID_REDUCED_NEIGHBOR_REPORT,
2162 			    wpa_bss_ie_ptr(bss),
2163 			    bss->ie_len ? bss->ie_len : bss->beacon_ie_len) {
2164 		const struct ieee80211_neighbor_ap_info *ap_info;
2165 		const u8 *ap_info_pos = elem->data;
2166 		size_t len = elem->datalen;
2167 
2168 		/* RNR IE may contain more than one Neighbor AP Info */
2169 		while (sizeof(*ap_info) <= len) {
2170 			size_t ap_info_len = sizeof(*ap_info);
2171 			u8 count;
2172 
2173 			ap_info = (const struct ieee80211_neighbor_ap_info *)
2174 				ap_info_pos;
2175 			count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1;
2176 			ap_info_len += count * ap_info->tbtt_info_len;
2177 
2178 			if (ap_info_len > len)
2179 				goto out;
2180 
2181 			wpa_bss_parse_ml_rnr_ap_info(wpa_s, bss, ap_mld_id,
2182 						     ap_info, len, &seen);
2183 
2184 			ap_info_pos += ap_info_len;
2185 			len -= ap_info_len;
2186 		}
2187 	}
2188 
2189 	wpa_printf(MSG_DEBUG, "MLD: valid_links=0x%04hx",
2190 		   bss->valid_links);
2191 
2192 	for_each_link(bss->valid_links, i) {
2193 		wpa_printf(MSG_DEBUG, "MLD: link=%u, bssid=" MACSTR,
2194 			   i, MAC2STR(bss->mld_links[i].bssid));
2195 	}
2196 
2197 	wpabuf_free(mlbuf);
2198 	return;
2199 
2200 out:
2201 	os_memset(bss->mld_addr, 0, ETH_ALEN);
2202 	bss->valid_links = 0;
2203 	wpabuf_free(mlbuf);
2204 }
2205 
2206 
2207 /*
2208  * wpa_bss_parse_reconf_ml_element - Parse the Reconfiguration ML element
2209  * @wpa_s: Pointer to wpa_supplicant data
2210  * @bss: BSS table entry
2211  * Returns: The bitmap of links that are going to be removed
2212  */
wpa_bss_parse_reconf_ml_element(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)2213 u16 wpa_bss_parse_reconf_ml_element(struct wpa_supplicant *wpa_s,
2214 				    struct wpa_bss *bss)
2215 {
2216 	struct ieee802_11_elems elems;
2217 	struct wpabuf *mlbuf;
2218 	const u8 *pos = wpa_bss_ie_ptr(bss);
2219 	size_t len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
2220 	const struct ieee80211_eht_ml *ml;
2221 	const struct eht_ml_reconf_common_info *common_info;
2222 	u16 removed_links = 0;
2223 	u8 expected_ml_common_len;
2224 
2225 	if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
2226 		return 0;
2227 
2228 	if (!elems.reconf_mle || !elems.reconf_mle_len)
2229 		return 0;
2230 
2231 	mlbuf = ieee802_11_defrag(elems.reconf_mle, elems.reconf_mle_len, true);
2232 	if (!mlbuf)
2233 		return 0;
2234 
2235 	ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
2236 	len = wpabuf_len(mlbuf);
2237 
2238 	/* There must be at least one octet for the Common Info Length subfield
2239 	 */
2240 	if (len < sizeof(*ml) + 1UL)
2241 		goto out;
2242 
2243 	expected_ml_common_len = 1;
2244 	if (le_to_host16(ml->ml_control) &
2245 	    RECONF_MULTI_LINK_CTRL_PRES_MLD_MAC_ADDR)
2246 		expected_ml_common_len += ETH_ALEN;
2247 
2248 	common_info = (const struct eht_ml_reconf_common_info *) ml->variable;
2249 	if (len < sizeof(*ml) + common_info->len) {
2250 		wpa_printf(MSG_DEBUG,
2251 			   "MLD: Unexpected Reconfiguration ML element length: (%zu < %zu)",
2252 			   len, sizeof(*ml) + common_info->len);
2253 		goto out;
2254 	}
2255 
2256 	if (common_info->len < expected_ml_common_len) {
2257 		wpa_printf(MSG_DEBUG,
2258 			   "MLD: Invalid common info len=%u; min expected=%u",
2259 			   common_info->len, expected_ml_common_len);
2260 		goto out;
2261 	}
2262 
2263 	pos = ml->variable + common_info->len;
2264 	len -= sizeof(*ml) + common_info->len;
2265 
2266 	while (len >= 2 + sizeof(struct ieee80211_eht_per_sta_profile)) {
2267 		size_t sub_elem_len;
2268 		int num_frag_subelems;
2269 
2270 		num_frag_subelems =
2271 			ieee802_11_defrag_mle_subelem(mlbuf, pos,
2272 						      &sub_elem_len);
2273 		if (num_frag_subelems < 0) {
2274 			wpa_printf(MSG_DEBUG,
2275 				   "MLD: Failed to parse MLE subelem");
2276 			break;
2277 		}
2278 
2279 		if ((size_t) num_frag_subelems * 2 > len)
2280 			goto out;
2281 		len -= num_frag_subelems * 2;
2282 
2283 		if (2 + sub_elem_len > len) {
2284 			wpa_printf(MSG_DEBUG,
2285 				   "MLD: Invalid link info len: %zu %zu",
2286 				   2 + sub_elem_len, len);
2287 			goto out;
2288 		}
2289 
2290 		if  (*pos == MULTI_LINK_SUB_ELEM_ID_PER_STA_PROFILE &&
2291 		     sub_elem_len >= 2) {
2292 			const struct ieee80211_eht_per_sta_profile *sta_prof =
2293 				(const struct ieee80211_eht_per_sta_profile *)
2294 				(pos + 2);
2295 			u16 control = le_to_host16(sta_prof->sta_control);
2296 			u8 link_id;
2297 
2298 			link_id = control & EHT_PER_STA_RECONF_CTRL_LINK_ID_MSK;
2299 			if (link_id < MAX_NUM_MLD_LINKS)
2300 				removed_links |= BIT(link_id);
2301 		}
2302 
2303 		pos += 2 + sub_elem_len;
2304 		len -= 2 + sub_elem_len;
2305 	}
2306 
2307 	wpa_printf(MSG_DEBUG, "MLD: Reconfiguration: removed_links=0x%x",
2308 		   removed_links);
2309 out:
2310 	wpabuf_free(mlbuf);
2311 	return removed_links;
2312 }
2313 
2314 
2315 #ifndef CONFIG_NO_WPA
2316 
wpa_bss_supported_cipher(struct wpa_supplicant * wpa_s,int pairwise_cipher)2317 static bool wpa_bss_supported_cipher(struct wpa_supplicant *wpa_s,
2318 				     int pairwise_cipher)
2319 {
2320 	if (!wpa_s->drv_enc)
2321 		return true;
2322 
2323 	if ((pairwise_cipher & WPA_CIPHER_CCMP) &&
2324 	    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP))
2325 		return true;
2326 
2327 	if ((pairwise_cipher & WPA_CIPHER_GCMP) &&
2328 	    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP))
2329 		return true;
2330 
2331 	if ((pairwise_cipher & WPA_CIPHER_CCMP_256) &&
2332 	    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_CCMP_256))
2333 		return true;
2334 
2335 	if ((pairwise_cipher & WPA_CIPHER_GCMP_256) &&
2336 	    (wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_GCMP_256))
2337 		return true;
2338 
2339 	return false;
2340 }
2341 
2342 
wpa_bss_supported_key_mgmt(struct wpa_supplicant * wpa_s,int key_mgmt)2343 static bool wpa_bss_supported_key_mgmt(struct wpa_supplicant *wpa_s,
2344 				       int key_mgmt)
2345 {
2346 	if (!wpa_s->drv_key_mgmt)
2347 		return true;
2348 
2349 	if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X) &&
2350 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2))
2351 		return true;
2352 	if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) &&
2353 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_802_1X_SHA256))
2354 		return true;
2355 	if ((key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) &&
2356 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT))
2357 		return true;
2358 	if ((key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
2359 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_802_1X_SHA384))
2360 		return true;
2361 	if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) &&
2362 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B))
2363 		return true;
2364 	if ((key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) &&
2365 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192))
2366 		return true;
2367 	if ((key_mgmt & WPA_KEY_MGMT_PSK) &&
2368 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK))
2369 		return true;
2370 	if ((key_mgmt & WPA_KEY_MGMT_FT_PSK) &&
2371 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK))
2372 		return true;
2373 	if ((key_mgmt & WPA_KEY_MGMT_PSK_SHA256) &&
2374 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_PSK_SHA256))
2375 		return true;
2376 	if ((key_mgmt & WPA_KEY_MGMT_SAE) &&
2377 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE))
2378 		return true;
2379 	if ((key_mgmt & WPA_KEY_MGMT_SAE_EXT_KEY) &&
2380 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SAE_EXT_KEY))
2381 		return true;
2382 	if ((key_mgmt & WPA_KEY_MGMT_FT_SAE) &&
2383 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE))
2384 		return true;
2385 	if ((key_mgmt & WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
2386 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_SAE_EXT_KEY))
2387 		return true;
2388 	if ((key_mgmt & WPA_KEY_MGMT_OWE) &&
2389 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE))
2390 		return true;
2391 	if ((key_mgmt & WPA_KEY_MGMT_DPP) &&
2392 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP))
2393 		return true;
2394 	if ((key_mgmt & WPA_KEY_MGMT_FILS_SHA256) &&
2395 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA256))
2396 		return true;
2397 	if ((key_mgmt & WPA_KEY_MGMT_FILS_SHA384) &&
2398 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FILS_SHA384))
2399 		return true;
2400 	if ((key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) &&
2401 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA256))
2402 		return true;
2403 	if ((key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) &&
2404 	    (wpa_s->drv_key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_FILS_SHA384))
2405 		return true;
2406 
2407 	return false;
2408 }
2409 
2410 
wpa_bss_supported_rsne(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const u8 * ie)2411 static bool wpa_bss_supported_rsne(struct wpa_supplicant *wpa_s,
2412 				   struct wpa_ssid *ssid, const u8 *ie)
2413 {
2414 	struct wpa_ie_data data;
2415 
2416 	if (wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) < 0)
2417 		return false;
2418 
2419 	/* Check that there is a supported AKM and pairwise cipher based on
2420 	 * overall capabilities */
2421 	if (!data.pairwise_cipher || !data.key_mgmt)
2422 		return false;
2423 
2424 	if (wpa_s->drv_capa_known) {
2425 		if (!wpa_bss_supported_cipher(wpa_s, data.pairwise_cipher) ||
2426 		    !wpa_bss_supported_key_mgmt(wpa_s, data.key_mgmt))
2427 			return false;
2428 	}
2429 
2430 	if (ssid) {
2431 		/* Check that there is a supported AKM and pairwise cipher
2432 		 * based on the specific network profile. */
2433 		if ((ssid->pairwise_cipher & data.pairwise_cipher) == 0)
2434 			return false;
2435 		if ((ssid->key_mgmt & data.key_mgmt) == 0)
2436 			return false;
2437 	}
2438 
2439 	return true;
2440 }
2441 
2442 #endif /* CONFIG_NO_WPA */
2443 
2444 
wpa_bss_get_rsne(struct wpa_supplicant * wpa_s,const struct wpa_bss * bss,struct wpa_ssid * ssid,bool mlo)2445 const u8 * wpa_bss_get_rsne(struct wpa_supplicant *wpa_s,
2446 			    const struct wpa_bss *bss, struct wpa_ssid *ssid,
2447 			    bool mlo)
2448 {
2449 #ifndef CONFIG_NO_WPA
2450 	const u8 *ie;
2451 
2452 	if (wpas_rsn_overriding(wpa_s, ssid)) {
2453 		if (!ssid)
2454 			ssid = wpa_s->current_ssid;
2455 
2456 		/* MLO cases for RSN overriding are required to use RSNE
2457 		 * Override 2 element and RSNXE Override element together. */
2458 		ie = wpa_bss_get_vendor_ie(bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
2459 		if (mlo && ie &&
2460 		    !wpa_bss_get_vendor_ie(bss,
2461 					   RSNXE_OVERRIDE_IE_VENDOR_TYPE)) {
2462 			wpa_printf(MSG_DEBUG, "BSS " MACSTR
2463 				   " advertises RSNE Override 2 element without RSNXE Override element - ignore RSNE Override 2 element for MLO",
2464 				   MAC2STR(bss->bssid));
2465 		} else if (ie && wpa_bss_supported_rsne(wpa_s, ssid, ie)) {
2466 			return ie;
2467 		}
2468 
2469 		if (!mlo) {
2470 			ie = wpa_bss_get_vendor_ie(
2471 				bss, RSNE_OVERRIDE_IE_VENDOR_TYPE);
2472 			if (ie && wpa_bss_supported_rsne(wpa_s, ssid, ie))
2473 				return ie;
2474 		}
2475 	}
2476 #endif /* CONFIG_NO_WPA */
2477 
2478 	return wpa_bss_get_ie(bss, WLAN_EID_RSN);
2479 }
2480 
2481 
wpa_bss_get_rsnxe(struct wpa_supplicant * wpa_s,const struct wpa_bss * bss,struct wpa_ssid * ssid,bool mlo)2482 const u8 * wpa_bss_get_rsnxe(struct wpa_supplicant *wpa_s,
2483 			     const struct wpa_bss *bss, struct wpa_ssid *ssid,
2484 			     bool mlo)
2485 {
2486 	const u8 *ie;
2487 
2488 	if (wpas_rsn_overriding(wpa_s, ssid)) {
2489 		ie = wpa_bss_get_vendor_ie(bss, RSNXE_OVERRIDE_IE_VENDOR_TYPE);
2490 		if (ie) {
2491 			const u8 *tmp;
2492 
2493 			tmp = wpa_bss_get_rsne(wpa_s, bss, ssid, mlo);
2494 			if (!tmp || tmp[0] == WLAN_EID_RSN) {
2495 				/* An acceptable RSNE override element was not
2496 				 * found, so need to ignore RSNXE overriding. */
2497 				goto out;
2498 			}
2499 
2500 			return ie;
2501 		}
2502 
2503 		/* MLO cases for RSN overriding are required to use RSNE
2504 		 * Override 2 element and RSNXE Override element together. */
2505 		if (mlo && wpa_bss_get_vendor_ie(
2506 			    bss, RSNE_OVERRIDE_2_IE_VENDOR_TYPE)) {
2507 			wpa_printf(MSG_DEBUG, "BSS " MACSTR
2508 				   " advertises RSNXE Override element without RSNE Override 2 element - ignore RSNXE Override element for MLO",
2509 				   MAC2STR(bss->bssid));
2510 			goto out;
2511 		}
2512 	}
2513 
2514 out:
2515 	return wpa_bss_get_ie(bss, WLAN_EID_RSNX);
2516 }
2517