• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "wpa_supplicant_i.h"
17 #include "config.h"
18 #include "notify.h"
19 #include "scan.h"
20 #include "bss.h"
21 
wpa_bss_set_hessid(struct wpa_bss * bss)22 static 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 
38 /**
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  */
wpa_bss_anqp_alloc(void)46 struct 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;
52 #ifdef CONFIG_INTERWORKING
53 	dl_list_init(&anqp->anqp_elems);
54 #endif /* CONFIG_INTERWORKING */
55 	anqp->users = 1;
56 	return anqp;
57 }
58 
59 
60 /**
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  */
wpa_bss_anqp_clone(struct wpa_bss_anqp * anqp)65 static 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
75 	dl_list_init(&n->anqp_elems);
76 	ANQP_DUP(capability_list);
77 	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);
84 	ANQP_DUP(fils_realm_info);
85 #endif /* CONFIG_INTERWORKING */
86 #ifdef CONFIG_HS20
87 	ANQP_DUP(hs20_capability_list);
88 	ANQP_DUP(hs20_operator_friendly_name);
89 	ANQP_DUP(hs20_wan_metrics);
90 	ANQP_DUP(hs20_connection_capability);
91 	ANQP_DUP(hs20_operating_class);
92 	ANQP_DUP(hs20_osu_providers_list);
93 	ANQP_DUP(hs20_operator_icon_metadata);
94 	ANQP_DUP(hs20_osu_providers_nai_list);
95 #endif /* CONFIG_HS20 */
96 #undef ANQP_DUP
97 
98 	return n;
99 }
100 
101 
102 /**
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  */
wpa_bss_anqp_unshare_alloc(struct wpa_bss * bss)110 int 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 
134 /**
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  */
wpa_bss_anqp_free(struct wpa_bss_anqp * anqp)138 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
139 {
140 #ifdef CONFIG_INTERWORKING
141 	struct wpa_bss_anqp_elem *elem;
142 #endif /* CONFIG_INTERWORKING */
143 
144 	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
154 	wpabuf_free(anqp->capability_list);
155 	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);
162 	wpabuf_free(anqp->fils_realm_info);
163 
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 	}
170 #endif /* CONFIG_INTERWORKING */
171 #ifdef CONFIG_HS20
172 	wpabuf_free(anqp->hs20_capability_list);
173 	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);
177 	wpabuf_free(anqp->hs20_osu_providers_list);
178 	wpabuf_free(anqp->hs20_operator_icon_metadata);
179 	wpabuf_free(anqp->hs20_osu_providers_nai_list);
180 #endif /* CONFIG_HS20 */
181 
182 	os_free(anqp);
183 }
184 
185 
wpa_bss_update_pending_connect(struct wpa_supplicant * wpa_s,struct wpa_bss * old_bss,struct wpa_bss * new_bss)186 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
187 					   struct wpa_bss *old_bss,
188 					   struct wpa_bss *new_bss)
189 {
190 	struct wpa_radio_work *work;
191 	struct wpa_connect_work *cwork;
192 
193 	work = radio_work_pending(wpa_s, "sme-connect");
194 	if (!work)
195 		work = radio_work_pending(wpa_s, "connect");
196 	if (!work)
197 		return;
198 
199 	cwork = work->ctx;
200 	if (cwork->bss != old_bss)
201 		return;
202 
203 	wpa_printf(MSG_DEBUG,
204 		   "Update BSS pointer for the pending connect radio work");
205 	cwork->bss = new_bss;
206 	if (!new_bss)
207 		cwork->bss_removed = 1;
208 }
209 
210 
wpa_bss_remove(struct wpa_supplicant * wpa_s,struct wpa_bss * bss,const char * reason)211 void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
212 		    const char *reason)
213 {
214 	if (wpa_s->last_scan_res) {
215 		unsigned int i;
216 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
217 			if (wpa_s->last_scan_res[i] == bss) {
218 				os_memmove(&wpa_s->last_scan_res[i],
219 					   &wpa_s->last_scan_res[i + 1],
220 					   (wpa_s->last_scan_res_used - i - 1)
221 					   * sizeof(struct wpa_bss *));
222 				wpa_s->last_scan_res_used--;
223 				break;
224 			}
225 		}
226 	}
227 	wpa_bss_update_pending_connect(wpa_s, bss, NULL);
228 	dl_list_del(&bss->list);
229 	dl_list_del(&bss->list_id);
230 	wpa_s->num_bss--;
231 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
232 		" SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
233 		wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
234 	wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
235 	wpa_bss_anqp_free(bss->anqp);
236 	os_free(bss);
237 }
238 
239 
240 /**
241  * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
242  * @wpa_s: Pointer to wpa_supplicant data
243  * @bssid: BSSID, or %NULL to match any BSSID
244  * @ssid: SSID
245  * @ssid_len: Length of @ssid
246  * Returns: Pointer to the BSS entry or %NULL if not found
247  */
wpa_bss_get(struct wpa_supplicant * wpa_s,const u8 * bssid,const u8 * ssid,size_t ssid_len)248 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
249 			     const u8 *ssid, size_t ssid_len)
250 {
251 	struct wpa_bss *bss;
252 
253 	if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid))
254 		return NULL;
255 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
256 		if ((!bssid || os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) &&
257 		    bss->ssid_len == ssid_len &&
258 		    os_memcmp(bss->ssid, ssid, ssid_len) == 0)
259 			return bss;
260 	}
261 	return NULL;
262 }
263 
264 
calculate_update_time(const struct os_reltime * fetch_time,unsigned int age_ms,struct os_reltime * update_time)265 void calculate_update_time(const struct os_reltime *fetch_time,
266 			   unsigned int age_ms,
267 			   struct os_reltime *update_time)
268 {
269 	os_time_t usec;
270 
271 	update_time->sec = fetch_time->sec;
272 	update_time->usec = fetch_time->usec;
273 	update_time->sec -= age_ms / 1000;
274 	usec = (age_ms % 1000) * 1000;
275 	if (update_time->usec < usec) {
276 		update_time->sec--;
277 		update_time->usec += 1000000;
278 	}
279 	update_time->usec -= usec;
280 }
281 
282 
wpa_bss_copy_res(struct wpa_bss * dst,struct wpa_scan_res * src,struct os_reltime * fetch_time)283 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
284 			     struct os_reltime *fetch_time)
285 {
286 	dst->flags = src->flags;
287 	os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
288 	dst->freq = src->freq;
289 	dst->beacon_int = src->beacon_int;
290 	dst->caps = src->caps;
291 	dst->qual = src->qual;
292 	dst->noise = src->noise;
293 	dst->level = src->level;
294 	dst->tsf = src->tsf;
295 	dst->beacon_newer = src->beacon_newer;
296 	dst->est_throughput = src->est_throughput;
297 	dst->snr = src->snr;
298 
299 	calculate_update_time(fetch_time, src->age, &dst->last_update);
300 }
301 
302 
wpa_bss_is_wps_candidate(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)303 static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
304 				    struct wpa_bss *bss)
305 {
306 #ifdef CONFIG_WPS
307 	struct wpa_ssid *ssid;
308 	struct wpabuf *wps_ie;
309 	int pbc = 0, ret;
310 
311 	wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
312 	if (!wps_ie)
313 		return 0;
314 
315 	if (wps_is_selected_pbc_registrar(wps_ie)) {
316 		pbc = 1;
317 	} else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
318 		wpabuf_free(wps_ie);
319 		return 0;
320 	}
321 
322 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
323 		if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
324 			continue;
325 		if (ssid->ssid_len &&
326 		    (ssid->ssid_len != bss->ssid_len ||
327 		     os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
328 			continue;
329 
330 		if (pbc)
331 			ret = eap_is_wps_pbc_enrollee(&ssid->eap);
332 		else
333 			ret = eap_is_wps_pin_enrollee(&ssid->eap);
334 		wpabuf_free(wps_ie);
335 		return ret;
336 	}
337 	wpabuf_free(wps_ie);
338 #endif /* CONFIG_WPS */
339 
340 	return 0;
341 }
342 
343 
is_p2p_pending_bss(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)344 static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s,
345 			       struct wpa_bss *bss)
346 {
347 #ifdef CONFIG_P2P
348 	u8 addr[ETH_ALEN];
349 
350 	if (os_memcmp(bss->bssid, wpa_s->pending_join_iface_addr,
351 		      ETH_ALEN) == 0)
352 		return true;
353 	if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) &&
354 	    p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 &&
355 	    os_memcmp(addr, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0)
356 		return true;
357 #endif /* CONFIG_P2P */
358 	return false;
359 }
360 
361 
wpa_bss_known(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)362 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
363 {
364 	struct wpa_ssid *ssid;
365 
366 	if (is_p2p_pending_bss(wpa_s, bss))
367 		return 1;
368 
369 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
370 		if (ssid->ssid == NULL || ssid->ssid_len == 0)
371 			continue;
372 		if (ssid->ssid_len == bss->ssid_len &&
373 		    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
374 			return 1;
375 	}
376 
377 	return 0;
378 }
379 
380 
wpa_bss_in_use(struct wpa_supplicant * wpa_s,struct wpa_bss * bss)381 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
382 {
383 	int i;
384 
385 	if (bss == wpa_s->current_bss)
386 		return 1;
387 
388 	if (wpa_s->current_bss &&
389 	    (bss->ssid_len != wpa_s->current_bss->ssid_len ||
390 	     os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
391 		       bss->ssid_len) != 0))
392 		return 0; /* SSID has changed */
393 
394 	if (!is_zero_ether_addr(bss->bssid) &&
395 	    (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
396 	     os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0))
397 		return 1;
398 
399 	if (!wpa_s->valid_links)
400 		return 0;
401 
402 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
403 		if (!(wpa_s->valid_links & BIT(i)))
404 			continue;
405 
406 		if (os_memcmp(bss->bssid, wpa_s->links[i].bssid, ETH_ALEN) == 0)
407 			return 1;
408 	}
409 
410 	return 0;
411 }
412 
413 
wpa_bss_remove_oldest_unknown(struct wpa_supplicant * wpa_s)414 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
415 {
416 	struct wpa_bss *bss;
417 
418 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
419 		if (!wpa_bss_known(wpa_s, bss) &&
420 		    !wpa_bss_is_wps_candidate(wpa_s, bss)) {
421 			wpa_bss_remove(wpa_s, bss, __func__);
422 			return 0;
423 		}
424 	}
425 
426 	return -1;
427 }
428 
429 
wpa_bss_remove_oldest(struct wpa_supplicant * wpa_s)430 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
431 {
432 	struct wpa_bss *bss;
433 
434 	/*
435 	 * Remove the oldest entry that does not match with any configured
436 	 * network.
437 	 */
438 	if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
439 		return 0;
440 
441 	/*
442 	 * Remove the oldest entry that isn't currently in use.
443 	 */
444 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
445 		if (!wpa_bss_in_use(wpa_s, bss)) {
446 			wpa_bss_remove(wpa_s, bss, __func__);
447 			return 0;
448 		}
449 	}
450 
451 	return -1;
452 }
453 
454 
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)455 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
456 				    const u8 *ssid, size_t ssid_len,
457 				    struct wpa_scan_res *res,
458 				    struct os_reltime *fetch_time)
459 {
460 	struct wpa_bss *bss;
461 	char extra[100];
462 	const u8 *ml_ie;
463 	char *pos, *end;
464 	int ret = 0;
465 	const u8 *mld_addr;
466 
467 	bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
468 	if (bss == NULL)
469 		return NULL;
470 	bss->id = wpa_s->bss_next_id++;
471 	bss->last_update_idx = wpa_s->bss_update_idx;
472 	wpa_bss_copy_res(bss, res, fetch_time);
473 	os_memcpy(bss->ssid, ssid, ssid_len);
474 	bss->ssid_len = ssid_len;
475 	bss->ie_len = res->ie_len;
476 	bss->beacon_ie_len = res->beacon_ie_len;
477 	os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
478 	wpa_bss_set_hessid(bss);
479 
480 	os_memset(bss->mld_addr, 0, ETH_ALEN);
481 	ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
482 	if (ml_ie) {
483 		mld_addr = get_basic_mle_mld_addr(&ml_ie[3], ml_ie[1] - 1);
484 		if (mld_addr)
485 			os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
486 	}
487 
488 	if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
489 	    wpa_bss_remove_oldest(wpa_s) != 0) {
490 		wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
491 			   "because all BSSes are in use. We should normally "
492 			   "not get here!", (int) wpa_s->num_bss + 1);
493 		wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
494 	}
495 
496 	dl_list_add_tail(&wpa_s->bss, &bss->list);
497 	dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
498 	wpa_s->num_bss++;
499 
500 	extra[0] = '\0';
501 	pos = extra;
502 	end = pos + sizeof(extra);
503 	if (!is_zero_ether_addr(bss->hessid))
504 		ret = os_snprintf(pos, end - pos, " HESSID " MACSTR,
505 				  MAC2STR(bss->hessid));
506 
507 	if (!is_zero_ether_addr(bss->mld_addr) &&
508 	    !os_snprintf_error(end - pos, ret)) {
509 		pos += ret;
510 		ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR,
511 				  MAC2STR(bss->mld_addr));
512 	}
513 
514 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
515 		" SSID '%s' freq %d%s",
516 		bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
517 		bss->freq, extra);
518 	wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
519 	return bss;
520 }
521 
522 
are_ies_equal(const struct wpa_bss * old,const struct wpa_scan_res * new_res,u32 ie)523 static int are_ies_equal(const struct wpa_bss *old,
524 			 const struct wpa_scan_res *new_res, u32 ie)
525 {
526 	const u8 *old_ie, *new_ie;
527 	struct wpabuf *old_ie_buff = NULL;
528 	struct wpabuf *new_ie_buff = NULL;
529 	int new_ie_len, old_ie_len, ret, is_multi;
530 
531 	switch (ie) {
532 	case WPA_IE_VENDOR_TYPE:
533 		old_ie = wpa_bss_get_vendor_ie(old, ie);
534 		new_ie = wpa_scan_get_vendor_ie(new_res, ie);
535 		is_multi = 0;
536 		break;
537 	case WPS_IE_VENDOR_TYPE:
538 		old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
539 		new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
540 		is_multi = 1;
541 		break;
542 	case WLAN_EID_RSN:
543 	case WLAN_EID_SUPP_RATES:
544 	case WLAN_EID_EXT_SUPP_RATES:
545 		old_ie = wpa_bss_get_ie(old, ie);
546 		new_ie = wpa_scan_get_ie(new_res, ie);
547 		is_multi = 0;
548 		break;
549 	default:
550 		wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
551 		return 0;
552 	}
553 
554 	if (is_multi) {
555 		/* in case of multiple IEs stored in buffer */
556 		old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
557 		new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
558 		old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
559 		new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
560 	} else {
561 		/* in case of single IE */
562 		old_ie_len = old_ie ? old_ie[1] + 2 : 0;
563 		new_ie_len = new_ie ? new_ie[1] + 2 : 0;
564 	}
565 
566 	if (!old_ie || !new_ie)
567 		ret = !old_ie && !new_ie;
568 	else
569 		ret = (old_ie_len == new_ie_len &&
570 		       os_memcmp(old_ie, new_ie, old_ie_len) == 0);
571 
572 	wpabuf_free(old_ie_buff);
573 	wpabuf_free(new_ie_buff);
574 
575 	return ret;
576 }
577 
578 
wpa_bss_compare_res(const struct wpa_bss * old,const struct wpa_scan_res * new_res)579 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
580 			       const struct wpa_scan_res *new_res)
581 {
582 	u32 changes = 0;
583 	int caps_diff = old->caps ^ new_res->caps;
584 
585 	if (old->freq != new_res->freq)
586 		changes |= WPA_BSS_FREQ_CHANGED_FLAG;
587 
588 	if (old->level != new_res->level)
589 		changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
590 
591 	if (caps_diff & IEEE80211_CAP_PRIVACY)
592 		changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
593 
594 	if (caps_diff & IEEE80211_CAP_IBSS)
595 		changes |= WPA_BSS_MODE_CHANGED_FLAG;
596 
597 	if (old->ie_len == new_res->ie_len &&
598 	    os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0)
599 		return changes;
600 	changes |= WPA_BSS_IES_CHANGED_FLAG;
601 
602 	if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
603 		changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
604 
605 	if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
606 		changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
607 
608 	if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
609 		changes |= WPA_BSS_WPS_CHANGED_FLAG;
610 
611 	if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
612 	    !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
613 		changes |= WPA_BSS_RATES_CHANGED_FLAG;
614 
615 	return changes;
616 }
617 
618 
notify_bss_changes(struct wpa_supplicant * wpa_s,u32 changes,const struct wpa_bss * bss)619 void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
620 			const struct wpa_bss *bss)
621 {
622 	if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
623 		wpas_notify_bss_freq_changed(wpa_s, bss->id);
624 
625 	if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
626 		wpas_notify_bss_signal_changed(wpa_s, bss->id);
627 
628 	if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
629 		wpas_notify_bss_privacy_changed(wpa_s, bss->id);
630 
631 	if (changes & WPA_BSS_MODE_CHANGED_FLAG)
632 		wpas_notify_bss_mode_changed(wpa_s, bss->id);
633 
634 	if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
635 		wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
636 
637 	if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
638 		wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
639 
640 	if (changes & WPA_BSS_WPS_CHANGED_FLAG)
641 		wpas_notify_bss_wps_changed(wpa_s, bss->id);
642 
643 	if (changes & WPA_BSS_IES_CHANGED_FLAG)
644 		wpas_notify_bss_ies_changed(wpa_s, bss->id);
645 
646 	if (changes & WPA_BSS_RATES_CHANGED_FLAG)
647 		wpas_notify_bss_rates_changed(wpa_s, bss->id);
648 
649 	wpas_notify_bss_seen(wpa_s, bss->id);
650 }
651 
652 
653 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)654 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
655 	       struct wpa_scan_res *res, struct os_reltime *fetch_time)
656 {
657 	u32 changes;
658 
659 	if (bss->last_update_idx == wpa_s->bss_update_idx) {
660 		struct os_reltime update_time;
661 
662 		/*
663 		 * Some drivers (e.g., cfg80211) include multiple BSS entries
664 		 * for the same BSS if that BSS's channel changes. The BSS list
665 		 * implementation in wpa_supplicant does not do that and we need
666 		 * to filter out the obsolete results here to make sure only the
667 		 * most current BSS information remains in the table.
668 		 */
669 		wpa_printf(MSG_DEBUG, "BSS: " MACSTR
670 			   " has multiple entries in the scan results - select the most current one",
671 			   MAC2STR(bss->bssid));
672 		calculate_update_time(fetch_time, res->age, &update_time);
673 		wpa_printf(MSG_DEBUG,
674 			   "Previous last_update: %u.%06u (freq %d%s)",
675 			   (unsigned int) bss->last_update.sec,
676 			   (unsigned int) bss->last_update.usec,
677 			   bss->freq,
678 			   (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : "");
679 		wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)",
680 			   (unsigned int) update_time.sec,
681 			   (unsigned int) update_time.usec,
682 			   res->freq,
683 			   (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : "");
684 		if ((bss->flags & WPA_BSS_ASSOCIATED) ||
685 		    (!(res->flags & WPA_SCAN_ASSOCIATED) &&
686 		     !os_reltime_before(&bss->last_update, &update_time))) {
687 			wpa_printf(MSG_DEBUG,
688 				   "Ignore this BSS entry since the previous update looks more current");
689 			return bss;
690 		}
691 		wpa_printf(MSG_DEBUG,
692 			   "Accept this BSS entry since it looks more current than the previous update");
693 	}
694 
695 	changes = wpa_bss_compare_res(bss, res);
696 	if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
697 		wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
698 			   MAC2STR(bss->bssid), bss->freq, res->freq);
699 	bss->scan_miss_count = 0;
700 	bss->last_update_idx = wpa_s->bss_update_idx;
701 	wpa_bss_copy_res(bss, res, fetch_time);
702 	/* Move the entry to the end of the list */
703 	dl_list_del(&bss->list);
704 #ifdef CONFIG_P2P
705 	if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
706 	    !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
707 		/*
708 		 * This can happen when non-P2P station interface runs a scan
709 		 * without P2P IE in the Probe Request frame. P2P GO would reply
710 		 * to that with a Probe Response that does not include P2P IE.
711 		 * Do not update the IEs in this BSS entry to avoid such loss of
712 		 * information that may be needed for P2P operations to
713 		 * determine group information.
714 		 */
715 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
716 			MACSTR " since that would remove P2P IE information",
717 			MAC2STR(bss->bssid));
718 	} else
719 #endif /* CONFIG_P2P */
720 	if (bss->ie_len + bss->beacon_ie_len >=
721 	    res->ie_len + res->beacon_ie_len) {
722 		os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len);
723 		bss->ie_len = res->ie_len;
724 		bss->beacon_ie_len = res->beacon_ie_len;
725 	} else {
726 		struct wpa_bss *nbss;
727 		struct dl_list *prev = bss->list_id.prev;
728 		dl_list_del(&bss->list_id);
729 		nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
730 				  res->beacon_ie_len);
731 		if (nbss) {
732 			unsigned int i;
733 			for (i = 0; i < wpa_s->last_scan_res_used; i++) {
734 				if (wpa_s->last_scan_res[i] == bss) {
735 					wpa_s->last_scan_res[i] = nbss;
736 					break;
737 				}
738 			}
739 			if (wpa_s->current_bss == bss)
740 				wpa_s->current_bss = nbss;
741 			wpa_bss_update_pending_connect(wpa_s, bss, nbss);
742 			bss = nbss;
743 			os_memcpy(bss->ies, res + 1,
744 				  res->ie_len + res->beacon_ie_len);
745 			bss->ie_len = res->ie_len;
746 			bss->beacon_ie_len = res->beacon_ie_len;
747 		}
748 		dl_list_add(prev, &bss->list_id);
749 	}
750 	if (changes & WPA_BSS_IES_CHANGED_FLAG) {
751 		const u8 *ml_ie, *mld_addr;
752 
753 		wpa_bss_set_hessid(bss);
754 		os_memset(bss->mld_addr, 0, ETH_ALEN);
755 		ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC);
756 		if (ml_ie) {
757 			mld_addr = get_basic_mle_mld_addr(&ml_ie[3],
758 							  ml_ie[1] - 1);
759 			if (mld_addr)
760 				os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN);
761 		}
762 	}
763 	dl_list_add_tail(&wpa_s->bss, &bss->list);
764 
765 	notify_bss_changes(wpa_s, changes, bss);
766 
767 	return bss;
768 }
769 
770 
771 /**
772  * wpa_bss_update_start - Start a BSS table update from scan results
773  * @wpa_s: Pointer to wpa_supplicant data
774  *
775  * This function is called at the start of each BSS table update round for new
776  * scan results. The actual scan result entries are indicated with calls to
777  * wpa_bss_update_scan_res() and the update round is finished with a call to
778  * wpa_bss_update_end().
779  */
wpa_bss_update_start(struct wpa_supplicant * wpa_s)780 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
781 {
782 	wpa_s->bss_update_idx++;
783 	wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
784 		wpa_s->bss_update_idx);
785 	wpa_s->last_scan_res_used = 0;
786 }
787 
788 
789 /**
790  * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
791  * @wpa_s: Pointer to wpa_supplicant data
792  * @res: Scan result
793  * @fetch_time: Time when the result was fetched from the driver
794  *
795  * This function updates a BSS table entry (or adds one) based on a scan result.
796  * This is called separately for each scan result between the calls to
797  * wpa_bss_update_start() and wpa_bss_update_end().
798  */
wpa_bss_update_scan_res(struct wpa_supplicant * wpa_s,struct wpa_scan_res * res,struct os_reltime * fetch_time)799 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
800 			     struct wpa_scan_res *res,
801 			     struct os_reltime *fetch_time)
802 {
803 	const u8 *ssid, *p2p, *mesh;
804 	struct wpa_bss *bss;
805 
806 	if (wpa_s->conf->ignore_old_scan_res) {
807 		struct os_reltime update;
808 		calculate_update_time(fetch_time, res->age, &update);
809 		if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
810 			struct os_reltime age;
811 			os_reltime_sub(&wpa_s->scan_trigger_time, &update,
812 				       &age);
813 			wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
814 				"table entry that is %u.%06u seconds older "
815 				"than our scan trigger",
816 				(unsigned int) age.sec,
817 				(unsigned int) age.usec);
818 			return;
819 		}
820 	}
821 
822 	ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
823 	if (ssid == NULL) {
824 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
825 			MACSTR, MAC2STR(res->bssid));
826 		return;
827 	}
828 	if (ssid[1] > SSID_MAX_LEN) {
829 		wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
830 			MACSTR, MAC2STR(res->bssid));
831 		return;
832 	}
833 
834 	p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
835 #ifdef CONFIG_P2P
836 	if (p2p == NULL &&
837 	    wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
838 		/*
839 		 * If it's a P2P specific interface, then don't update
840 		 * the scan result without a P2P IE.
841 		 */
842 		wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
843 			   " update for P2P interface", MAC2STR(res->bssid));
844 		return;
845 	}
846 #endif /* CONFIG_P2P */
847 	if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
848 	    os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
849 		return; /* Skip P2P listen discovery results here */
850 
851 	/* TODO: add option for ignoring BSSes we are not interested in
852 	 * (to save memory) */
853 
854 	mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
855 	if (mesh && mesh[1] <= SSID_MAX_LEN)
856 		ssid = mesh;
857 
858 	bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
859 	if (bss == NULL)
860 		bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
861 	else {
862 		bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
863 		if (wpa_s->last_scan_res) {
864 			unsigned int i;
865 			for (i = 0; i < wpa_s->last_scan_res_used; i++) {
866 				if (bss == wpa_s->last_scan_res[i]) {
867 					/* Already in the list */
868 					return;
869 				}
870 			}
871 		}
872 	}
873 
874 	if (bss == NULL)
875 		return;
876 	if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
877 		struct wpa_bss **n;
878 		unsigned int siz;
879 		if (wpa_s->last_scan_res_size == 0)
880 			siz = 32;
881 		else
882 			siz = wpa_s->last_scan_res_size * 2;
883 		n = os_realloc_array(wpa_s->last_scan_res, siz,
884 				     sizeof(struct wpa_bss *));
885 		if (n == NULL)
886 			return;
887 		wpa_s->last_scan_res = n;
888 		wpa_s->last_scan_res_size = siz;
889 	}
890 
891 	if (wpa_s->last_scan_res)
892 		wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
893 }
894 
895 
wpa_bss_included_in_scan(const struct wpa_bss * bss,const struct scan_info * info)896 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
897 				    const struct scan_info *info)
898 {
899 	int found;
900 	size_t i;
901 
902 	if (info == NULL)
903 		return 1;
904 
905 	if (info->num_freqs) {
906 		found = 0;
907 		for (i = 0; i < info->num_freqs; i++) {
908 			if (bss->freq == info->freqs[i]) {
909 				found = 1;
910 				break;
911 			}
912 		}
913 		if (!found)
914 			return 0;
915 	}
916 
917 	if (info->num_ssids) {
918 		found = 0;
919 		for (i = 0; i < info->num_ssids; i++) {
920 			const struct wpa_driver_scan_ssid *s = &info->ssids[i];
921 			if ((s->ssid == NULL || s->ssid_len == 0) ||
922 			    (s->ssid_len == bss->ssid_len &&
923 			     os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
924 			     0)) {
925 				found = 1;
926 				break;
927 			}
928 		}
929 		if (!found)
930 			return 0;
931 	}
932 
933 	return 1;
934 }
935 
936 
937 /**
938  * wpa_bss_update_end - End a BSS table update from scan results
939  * @wpa_s: Pointer to wpa_supplicant data
940  * @info: Information about scan parameters
941  * @new_scan: Whether this update round was based on a new scan
942  *
943  * This function is called at the end of each BSS table update round for new
944  * scan results. The start of the update was indicated with a call to
945  * wpa_bss_update_start().
946  */
wpa_bss_update_end(struct wpa_supplicant * wpa_s,struct scan_info * info,int new_scan)947 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
948 			int new_scan)
949 {
950 	struct wpa_bss *bss, *n;
951 
952 	os_get_reltime(&wpa_s->last_scan);
953 	if ((info && info->aborted) || !new_scan)
954 		return; /* do not expire entries without new scan */
955 
956 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
957 		if (wpa_bss_in_use(wpa_s, bss))
958 			continue;
959 		if (!wpa_bss_included_in_scan(bss, info))
960 			continue; /* expire only BSSes that were scanned */
961 		if (bss->last_update_idx < wpa_s->bss_update_idx)
962 			bss->scan_miss_count++;
963 		if (bss->scan_miss_count >=
964 		    wpa_s->conf->bss_expiration_scan_count) {
965 			wpa_bss_remove(wpa_s, bss, "no match in scan");
966 		}
967 	}
968 
969 	wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu",
970 		   wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
971 }
972 
973 
974 /**
975  * wpa_bss_flush_by_age - Flush old BSS entries
976  * @wpa_s: Pointer to wpa_supplicant data
977  * @age: Maximum entry age in seconds
978  *
979  * Remove BSS entries that have not been updated during the last @age seconds.
980  */
wpa_bss_flush_by_age(struct wpa_supplicant * wpa_s,int age)981 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
982 {
983 	struct wpa_bss *bss, *n;
984 	struct os_reltime t;
985 
986 	if (dl_list_empty(&wpa_s->bss))
987 		return;
988 
989 	os_get_reltime(&t);
990 
991 	if (t.sec < age)
992 		return; /* avoid underflow; there can be no older entries */
993 
994 	t.sec -= age;
995 
996 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
997 		if (wpa_bss_in_use(wpa_s, bss))
998 			continue;
999 
1000 		if (wpa_s->reassoc_same_ess &&
1001 		    wpa_s->wpa_state != WPA_COMPLETED &&
1002 		    wpa_s->last_ssid &&
1003 		    bss->ssid_len == wpa_s->last_ssid->ssid_len &&
1004 		    os_memcmp(bss->ssid, wpa_s->last_ssid->ssid,
1005 			      bss->ssid_len) == 0)
1006 			continue;
1007 
1008 		if (os_reltime_before(&bss->last_update, &t)) {
1009 			wpa_bss_remove(wpa_s, bss, __func__);
1010 		} else
1011 			break;
1012 	}
1013 }
1014 
1015 
1016 /**
1017  * wpa_bss_init - Initialize BSS table
1018  * @wpa_s: Pointer to wpa_supplicant data
1019  * Returns: 0 on success, -1 on failure
1020  *
1021  * This prepares BSS table lists and timer for periodic updates. The BSS table
1022  * is deinitialized with wpa_bss_deinit() once not needed anymore.
1023  */
wpa_bss_init(struct wpa_supplicant * wpa_s)1024 int wpa_bss_init(struct wpa_supplicant *wpa_s)
1025 {
1026 	dl_list_init(&wpa_s->bss);
1027 	dl_list_init(&wpa_s->bss_id);
1028 	return 0;
1029 }
1030 
1031 
1032 /**
1033  * wpa_bss_flush - Flush all unused BSS entries
1034  * @wpa_s: Pointer to wpa_supplicant data
1035  */
wpa_bss_flush(struct wpa_supplicant * wpa_s)1036 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
1037 {
1038 	struct wpa_bss *bss, *n;
1039 
1040 	wpa_s->clear_driver_scan_cache = 1;
1041 
1042 	if (wpa_s->bss.next == NULL)
1043 		return; /* BSS table not yet initialized */
1044 
1045 	dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
1046 		if (wpa_bss_in_use(wpa_s, bss))
1047 			continue;
1048 		wpa_bss_remove(wpa_s, bss, __func__);
1049 	}
1050 }
1051 
1052 
1053 /**
1054  * wpa_bss_deinit - Deinitialize BSS table
1055  * @wpa_s: Pointer to wpa_supplicant data
1056  */
wpa_bss_deinit(struct wpa_supplicant * wpa_s)1057 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
1058 {
1059 	wpa_bss_flush(wpa_s);
1060 }
1061 
1062 
1063 /**
1064  * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
1065  * @wpa_s: Pointer to wpa_supplicant data
1066  * @bssid: BSSID
1067  * Returns: Pointer to the BSS entry or %NULL if not found
1068  */
wpa_bss_get_bssid(struct wpa_supplicant * wpa_s,const u8 * bssid)1069 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
1070 				   const u8 *bssid)
1071 {
1072 	struct wpa_bss *bss;
1073 	if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1074 		return NULL;
1075 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1076 		if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
1077 			return bss;
1078 	}
1079 	return NULL;
1080 }
1081 
1082 
1083 /**
1084  * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
1085  * @wpa_s: Pointer to wpa_supplicant data
1086  * @bssid: BSSID
1087  * Returns: Pointer to the BSS entry or %NULL if not found
1088  *
1089  * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
1090  * find the entry that has the most recent update. This can help in finding the
1091  * correct entry in cases where the SSID of the AP may have changed recently
1092  * (e.g., in WPS reconfiguration cases).
1093  */
wpa_bss_get_bssid_latest(struct wpa_supplicant * wpa_s,const u8 * bssid)1094 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
1095 					  const u8 *bssid)
1096 {
1097 	struct wpa_bss *bss, *found = NULL;
1098 	if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
1099 		return NULL;
1100 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1101 		if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
1102 			continue;
1103 		if (found == NULL ||
1104 		    os_reltime_before(&found->last_update, &bss->last_update))
1105 			found = bss;
1106 	}
1107 	return found;
1108 }
1109 
1110 
1111 #ifdef CONFIG_P2P
1112 /**
1113  * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr
1114  * @wpa_s: Pointer to wpa_supplicant data
1115  * @dev_addr: P2P Device Address of the GO
1116  * Returns: Pointer to the BSS entry or %NULL if not found
1117  *
1118  * This function tries to find the entry that has the most recent update. This
1119  * can help in finding the correct entry in cases where the SSID of the P2P
1120  * Device may have changed recently.
1121  */
wpa_bss_get_p2p_dev_addr(struct wpa_supplicant * wpa_s,const u8 * dev_addr)1122 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
1123 					  const u8 *dev_addr)
1124 {
1125 	struct wpa_bss *bss, *found = NULL;
1126 	dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
1127 		u8 addr[ETH_ALEN];
1128 		if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len,
1129 				       addr) != 0 ||
1130 		    os_memcmp(addr, dev_addr, ETH_ALEN) != 0)
1131 			continue;
1132 		if (!found ||
1133 		    os_reltime_before(&found->last_update, &bss->last_update))
1134 			found = bss;
1135 	}
1136 	return found;
1137 }
1138 #endif /* CONFIG_P2P */
1139 
1140 
1141 /**
1142  * wpa_bss_get_id - Fetch a BSS table entry based on identifier
1143  * @wpa_s: Pointer to wpa_supplicant data
1144  * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
1145  * Returns: Pointer to the BSS entry or %NULL if not found
1146  */
wpa_bss_get_id(struct wpa_supplicant * wpa_s,unsigned int id)1147 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
1148 {
1149 	struct wpa_bss *bss;
1150 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
1151 		if (bss->id == id)
1152 			return bss;
1153 	}
1154 	return NULL;
1155 }
1156 
1157 
1158 /**
1159  * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
1160  * @wpa_s: Pointer to wpa_supplicant data
1161  * @idf: Smallest allowed identifier assigned for the entry
1162  * @idf: Largest allowed identifier assigned for the entry
1163  * Returns: Pointer to the BSS entry or %NULL if not found
1164  *
1165  * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
1166  * smallest id value to be fetched within the specified range without the
1167  * caller having to know the exact id.
1168  */
wpa_bss_get_id_range(struct wpa_supplicant * wpa_s,unsigned int idf,unsigned int idl)1169 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
1170 				      unsigned int idf, unsigned int idl)
1171 {
1172 	struct wpa_bss *bss;
1173 	dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
1174 		if (bss->id >= idf && bss->id <= idl)
1175 			return bss;
1176 	}
1177 	return NULL;
1178 }
1179 
1180 
1181 /**
1182  * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
1183  * @bss: BSS table entry
1184  * @ie: Information element identitifier (WLAN_EID_*)
1185  * Returns: Pointer to the information element (id field) or %NULL if not found
1186  *
1187  * This function returns the first matching information element in the BSS
1188  * entry.
1189  */
wpa_bss_get_ie(const struct wpa_bss * bss,u8 ie)1190 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1191 {
1192 	return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie);
1193 }
1194 
1195 
1196 /**
1197  * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry
1198  * @bss: BSS table entry
1199  * @ext: Information element extension identifier (WLAN_EID_EXT_*)
1200  * Returns: Pointer to the information element (id field) or %NULL if not found
1201  *
1202  * This function returns the first matching information element in the BSS
1203  * entry.
1204  */
wpa_bss_get_ie_ext(const struct wpa_bss * bss,u8 ext)1205 const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext)
1206 {
1207 	return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext);
1208 }
1209 
1210 
1211 /**
1212  * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1213  * @bss: BSS table entry
1214  * @vendor_type: Vendor type (four octets starting the IE payload)
1215  * Returns: Pointer to the information element (id field) or %NULL if not found
1216  *
1217  * This function returns the first matching information element in the BSS
1218  * entry.
1219  */
wpa_bss_get_vendor_ie(const struct wpa_bss * bss,u32 vendor_type)1220 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1221 {
1222 	const u8 *ies;
1223 	const struct element *elem;
1224 
1225 	ies = wpa_bss_ie_ptr(bss);
1226 
1227 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) {
1228 		if (elem->datalen >= 4 &&
1229 		    vendor_type == WPA_GET_BE32(elem->data))
1230 			return &elem->id;
1231 	}
1232 
1233 	return NULL;
1234 }
1235 
1236 
1237 /**
1238  * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1239  * @bss: BSS table entry
1240  * @vendor_type: Vendor type (four octets starting the IE payload)
1241  * Returns: Pointer to the information element (id field) or %NULL if not found
1242  *
1243  * This function returns the first matching information element in the BSS
1244  * entry.
1245  *
1246  * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1247  * from Beacon frames instead of either Beacon or Probe Response frames.
1248  */
wpa_bss_get_vendor_ie_beacon(const struct wpa_bss * bss,u32 vendor_type)1249 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1250 					u32 vendor_type)
1251 {
1252 	const u8 *ies;
1253 	const struct element *elem;
1254 
1255 	if (bss->beacon_ie_len == 0)
1256 		return NULL;
1257 
1258 	ies = wpa_bss_ie_ptr(bss);
1259 	ies += bss->ie_len;
1260 
1261 	for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies,
1262 			    bss->beacon_ie_len) {
1263 		if (elem->datalen >= 4 &&
1264 		    vendor_type == WPA_GET_BE32(elem->data))
1265 			return &elem->id;
1266 	}
1267 
1268 	return NULL;
1269 }
1270 
1271 
1272 /**
1273  * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1274  * @bss: BSS table entry
1275  * @vendor_type: Vendor type (four octets starting the IE payload)
1276  * Returns: Pointer to the information element payload or %NULL if not found
1277  *
1278  * This function returns concatenated payload of possibly fragmented vendor
1279  * specific information elements in the BSS entry. The caller is responsible for
1280  * freeing the returned buffer.
1281  */
wpa_bss_get_vendor_ie_multi(const struct wpa_bss * bss,u32 vendor_type)1282 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1283 					    u32 vendor_type)
1284 {
1285 	struct wpabuf *buf;
1286 	const u8 *end, *pos;
1287 
1288 	buf = wpabuf_alloc(bss->ie_len);
1289 	if (buf == NULL)
1290 		return NULL;
1291 
1292 	pos = wpa_bss_ie_ptr(bss);
1293 	end = pos + bss->ie_len;
1294 
1295 	while (end - pos > 1) {
1296 		u8 ie, len;
1297 
1298 		ie = pos[0];
1299 		len = pos[1];
1300 		if (len > end - pos - 2)
1301 			break;
1302 		pos += 2;
1303 		if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1304 		    vendor_type == WPA_GET_BE32(pos))
1305 			wpabuf_put_data(buf, pos + 4, len - 4);
1306 		pos += len;
1307 	}
1308 
1309 	if (wpabuf_len(buf) == 0) {
1310 		wpabuf_free(buf);
1311 		buf = NULL;
1312 	}
1313 
1314 	return buf;
1315 }
1316 
1317 
1318 /**
1319  * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1320  * @bss: BSS table entry
1321  * @vendor_type: Vendor type (four octets starting the IE payload)
1322  * Returns: Pointer to the information element payload or %NULL if not found
1323  *
1324  * This function returns concatenated payload of possibly fragmented vendor
1325  * specific information elements in the BSS entry. The caller is responsible for
1326  * freeing the returned buffer.
1327  *
1328  * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1329  * from Beacon frames instead of either Beacon or Probe Response frames.
1330  */
wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss * bss,u32 vendor_type)1331 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1332 						   u32 vendor_type)
1333 {
1334 	struct wpabuf *buf;
1335 	const u8 *end, *pos;
1336 
1337 	buf = wpabuf_alloc(bss->beacon_ie_len);
1338 	if (buf == NULL)
1339 		return NULL;
1340 
1341 	pos = wpa_bss_ie_ptr(bss);
1342 	pos += bss->ie_len;
1343 	end = pos + bss->beacon_ie_len;
1344 
1345 	while (end - pos > 1) {
1346 		u8 id, len;
1347 
1348 		id = *pos++;
1349 		len = *pos++;
1350 		if (len > end - pos)
1351 			break;
1352 		if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 &&
1353 		    vendor_type == WPA_GET_BE32(pos))
1354 			wpabuf_put_data(buf, pos + 4, len - 4);
1355 		pos += len;
1356 	}
1357 
1358 	if (wpabuf_len(buf) == 0) {
1359 		wpabuf_free(buf);
1360 		buf = NULL;
1361 	}
1362 
1363 	return buf;
1364 }
1365 
1366 
1367 /**
1368  * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1369  * @bss: BSS table entry
1370  * Returns: Maximum legacy rate in units of 500 kbps
1371  */
wpa_bss_get_max_rate(const struct wpa_bss * bss)1372 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1373 {
1374 	int rate = 0;
1375 	const u8 *ie;
1376 	int i;
1377 
1378 	ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1379 	for (i = 0; ie && i < ie[1]; i++) {
1380 		if ((ie[i + 2] & 0x7f) > rate)
1381 			rate = ie[i + 2] & 0x7f;
1382 	}
1383 
1384 	ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1385 	for (i = 0; ie && i < ie[1]; i++) {
1386 		if ((ie[i + 2] & 0x7f) > rate)
1387 			rate = ie[i + 2] & 0x7f;
1388 	}
1389 
1390 	return rate;
1391 }
1392 
1393 
1394 /**
1395  * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1396  * @bss: BSS table entry
1397  * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1398  * Returns: number of legacy TX rates or -1 on failure
1399  *
1400  * The caller is responsible for freeing the returned buffer with os_free() in
1401  * case of success.
1402  */
wpa_bss_get_bit_rates(const struct wpa_bss * bss,u8 ** rates)1403 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1404 {
1405 	const u8 *ie, *ie2;
1406 	int i, j;
1407 	unsigned int len;
1408 	u8 *r;
1409 
1410 	ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1411 	ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1412 
1413 	len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1414 
1415 	r = os_malloc(len);
1416 	if (!r)
1417 		return -1;
1418 
1419 	for (i = 0; ie && i < ie[1]; i++)
1420 		r[i] = ie[i + 2] & 0x7f;
1421 
1422 	for (j = 0; ie2 && j < ie2[1]; j++)
1423 		r[i + j] = ie2[j + 2] & 0x7f;
1424 
1425 	*rates = r;
1426 	return len;
1427 }
1428 
1429 
1430 #ifdef CONFIG_FILS
wpa_bss_get_fils_cache_id(const struct wpa_bss * bss)1431 const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss)
1432 {
1433 	const u8 *ie;
1434 
1435 	if (bss) {
1436 		ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
1437 		if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7))
1438 			return ie + 4;
1439 	}
1440 
1441 	return NULL;
1442 }
1443 #endif /* CONFIG_FILS */
1444 
1445 
wpa_bss_ext_capab(const struct wpa_bss * bss,unsigned int capab)1446 int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab)
1447 {
1448 	if (!bss)
1449 		return 0;
1450 	return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB),
1451 				    capab);
1452 }
1453 
1454 
1455 /**
1456  * wpa_bss_defrag_mle - Get a buffer holding a de-fragmented ML element
1457  * @bss: BSS table entry
1458  * @type: ML control type
1459  */
wpa_bss_defrag_mle(const struct wpa_bss * bss,u8 type)1460 struct wpabuf * wpa_bss_defrag_mle(const struct wpa_bss *bss, u8 type)
1461 {
1462 	struct ieee802_11_elems elems;
1463 	const u8 *pos = wpa_bss_ie_ptr(bss);
1464 	size_t len = bss->ie_len;
1465 
1466 	if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed)
1467 		return NULL;
1468 
1469 	return ieee802_11_defrag_mle(&elems, type);
1470 }
1471