| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * BSS table | 
| Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3 | * Copyright (c) 2009-2019, Jouni Malinen <j@w1.fi> | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. | 
|  | 6 | * See README for more details. | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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" | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 15 | #include "eap_peer/eap.h" | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 16 | #include "wpa_supplicant_i.h" | 
|  | 17 | #include "config.h" | 
|  | 18 | #include "notify.h" | 
|  | 19 | #include "scan.h" | 
|  | 20 | #include "bss.h" | 
|  | 21 |  | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 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 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 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 | */ | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 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; | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 52 | #ifdef CONFIG_INTERWORKING | 
|  | 53 | dl_list_init(&anqp->anqp_elems); | 
|  | 54 | #endif /* CONFIG_INTERWORKING */ | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 55 | anqp->users = 1; | 
|  | 56 | return anqp; | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 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 | */ | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 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 | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 75 | dl_list_init(&n->anqp_elems); | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 76 | ANQP_DUP(capability_list); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 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); | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 84 | ANQP_DUP(fils_realm_info); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 85 | #endif /* CONFIG_INTERWORKING */ | 
|  | 86 | #ifdef CONFIG_HS20 | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 87 | ANQP_DUP(hs20_capability_list); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 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); | 
| Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 92 | ANQP_DUP(hs20_osu_providers_list); | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 93 | ANQP_DUP(hs20_operator_icon_metadata); | 
| Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 94 | ANQP_DUP(hs20_osu_providers_nai_list); | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 95 | #endif /* CONFIG_HS20 */ | 
|  | 96 | #undef ANQP_DUP | 
|  | 97 |  | 
|  | 98 | return n; | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 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 | */ | 
| Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 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 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 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 | */ | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 138 | static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp) | 
|  | 139 | { | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 140 | #ifdef CONFIG_INTERWORKING | 
|  | 141 | struct wpa_bss_anqp_elem *elem; | 
|  | 142 | #endif /* CONFIG_INTERWORKING */ | 
|  | 143 |  | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 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 | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 154 | wpabuf_free(anqp->capability_list); | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 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); | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 162 | wpabuf_free(anqp->fils_realm_info); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 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 | } | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 170 | #endif /* CONFIG_INTERWORKING */ | 
|  | 171 | #ifdef CONFIG_HS20 | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 172 | wpabuf_free(anqp->hs20_capability_list); | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 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); | 
| Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 177 | wpabuf_free(anqp->hs20_osu_providers_list); | 
| Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 178 | wpabuf_free(anqp->hs20_operator_icon_metadata); | 
| Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 179 | wpabuf_free(anqp->hs20_osu_providers_nai_list); | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 180 | #endif /* CONFIG_HS20 */ | 
|  | 181 |  | 
|  | 182 | os_free(anqp); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 |  | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 186 | static struct wpa_connect_work * | 
|  | 187 | wpa_bss_check_pending_connect(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | 
| Dmitry Shmidt | 2e425d6 | 2014-11-10 11:18:27 -0800 | [diff] [blame] | 188 | { | 
|  | 189 | struct wpa_radio_work *work; | 
|  | 190 | struct wpa_connect_work *cwork; | 
|  | 191 |  | 
|  | 192 | work = radio_work_pending(wpa_s, "sme-connect"); | 
|  | 193 | if (!work) | 
|  | 194 | work = radio_work_pending(wpa_s, "connect"); | 
|  | 195 | if (!work) | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 196 | return NULL; | 
| Dmitry Shmidt | 2e425d6 | 2014-11-10 11:18:27 -0800 | [diff] [blame] | 197 |  | 
|  | 198 | cwork = work->ctx; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 199 | if (cwork->bss != bss) | 
|  | 200 | return NULL; | 
| Dmitry Shmidt | 2e425d6 | 2014-11-10 11:18:27 -0800 | [diff] [blame] | 201 |  | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 202 | return cwork; | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 |  | 
|  | 206 | static void wpa_bss_update_pending_connect(struct wpa_connect_work *cwork, | 
|  | 207 | struct wpa_bss *new_bss) | 
|  | 208 | { | 
| Dmitry Shmidt | 2e425d6 | 2014-11-10 11:18:27 -0800 | [diff] [blame] | 209 | wpa_printf(MSG_DEBUG, | 
|  | 210 | "Update BSS pointer for the pending connect radio work"); | 
|  | 211 | cwork->bss = new_bss; | 
|  | 212 | if (!new_bss) | 
|  | 213 | cwork->bss_removed = 1; | 
|  | 214 | } | 
|  | 215 |  | 
|  | 216 |  | 
| Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 217 | void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, | 
|  | 218 | const char *reason) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 219 | { | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 220 | struct wpa_connect_work *cwork; | 
|  | 221 |  | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 222 | if (wpa_s->last_scan_res) { | 
|  | 223 | unsigned int i; | 
|  | 224 | for (i = 0; i < wpa_s->last_scan_res_used; i++) { | 
|  | 225 | if (wpa_s->last_scan_res[i] == bss) { | 
|  | 226 | os_memmove(&wpa_s->last_scan_res[i], | 
|  | 227 | &wpa_s->last_scan_res[i + 1], | 
|  | 228 | (wpa_s->last_scan_res_used - i - 1) | 
|  | 229 | * sizeof(struct wpa_bss *)); | 
|  | 230 | wpa_s->last_scan_res_used--; | 
|  | 231 | break; | 
|  | 232 | } | 
|  | 233 | } | 
|  | 234 | } | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 235 | cwork = wpa_bss_check_pending_connect(wpa_s, bss); | 
|  | 236 | if (cwork) | 
|  | 237 | wpa_bss_update_pending_connect(cwork, NULL); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 238 | dl_list_del(&bss->list); | 
|  | 239 | dl_list_del(&bss->list_id); | 
|  | 240 | wpa_s->num_bss--; | 
|  | 241 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 242 | " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid), | 
|  | 243 | wpa_ssid_txt(bss->ssid, bss->ssid_len), reason); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 244 | wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id); | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 245 | wpa_bss_anqp_free(bss->anqp); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 246 | os_free(bss); | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 250 | /** | 
|  | 251 | * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID | 
|  | 252 | * @wpa_s: Pointer to wpa_supplicant data | 
| Matthew Wang | dcf1945 | 2022-11-07 20:42:52 -0800 | [diff] [blame] | 253 | * @bssid: BSSID, or %NULL to match any BSSID | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 254 | * @ssid: SSID | 
|  | 255 | * @ssid_len: Length of @ssid | 
|  | 256 | * Returns: Pointer to the BSS entry or %NULL if not found | 
|  | 257 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 258 | struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid, | 
|  | 259 | const u8 *ssid, size_t ssid_len) | 
|  | 260 | { | 
|  | 261 | struct wpa_bss *bss; | 
| Matthew Wang | afc981e | 2023-03-17 21:30:12 +0000 | [diff] [blame] | 262 |  | 
|  | 263 | if (bssid && !wpa_supplicant_filter_bssid_match(wpa_s, bssid)) | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 264 | return NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 265 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 266 | if ((!bssid || os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) && | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 267 | bss->ssid_len == ssid_len && | 
|  | 268 | os_memcmp(bss->ssid, ssid, ssid_len) == 0) | 
|  | 269 | return bss; | 
|  | 270 | } | 
|  | 271 | return NULL; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 |  | 
| Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 275 | void calculate_update_time(const struct os_reltime *fetch_time, | 
|  | 276 | unsigned int age_ms, | 
|  | 277 | struct os_reltime *update_time) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 278 | { | 
|  | 279 | os_time_t usec; | 
|  | 280 |  | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 281 | update_time->sec = fetch_time->sec; | 
|  | 282 | update_time->usec = fetch_time->usec; | 
|  | 283 | update_time->sec -= age_ms / 1000; | 
|  | 284 | usec = (age_ms % 1000) * 1000; | 
|  | 285 | if (update_time->usec < usec) { | 
|  | 286 | update_time->sec--; | 
|  | 287 | update_time->usec += 1000000; | 
|  | 288 | } | 
|  | 289 | update_time->usec -= usec; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 |  | 
|  | 293 | static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src, | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 294 | struct os_reltime *fetch_time) | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 295 | { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 296 | dst->flags = src->flags; | 
|  | 297 | os_memcpy(dst->bssid, src->bssid, ETH_ALEN); | 
|  | 298 | dst->freq = src->freq; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 299 | dst->max_cw = src->max_cw; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 300 | dst->beacon_int = src->beacon_int; | 
|  | 301 | dst->caps = src->caps; | 
|  | 302 | dst->qual = src->qual; | 
|  | 303 | dst->noise = src->noise; | 
|  | 304 | dst->level = src->level; | 
|  | 305 | dst->tsf = src->tsf; | 
| Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 306 | dst->beacon_newer = src->beacon_newer; | 
| Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 307 | dst->est_throughput = src->est_throughput; | 
|  | 308 | dst->snr = src->snr; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 309 |  | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 310 | calculate_update_time(fetch_time, src->age, &dst->last_update); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
|  | 313 |  | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 314 | static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s, | 
|  | 315 | struct wpa_bss *bss) | 
|  | 316 | { | 
|  | 317 | #ifdef CONFIG_WPS | 
|  | 318 | struct wpa_ssid *ssid; | 
|  | 319 | struct wpabuf *wps_ie; | 
|  | 320 | int pbc = 0, ret; | 
|  | 321 |  | 
|  | 322 | wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE); | 
|  | 323 | if (!wps_ie) | 
|  | 324 | return 0; | 
|  | 325 |  | 
|  | 326 | if (wps_is_selected_pbc_registrar(wps_ie)) { | 
|  | 327 | pbc = 1; | 
|  | 328 | } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) { | 
|  | 329 | wpabuf_free(wps_ie); | 
|  | 330 | return 0; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { | 
|  | 334 | if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS)) | 
|  | 335 | continue; | 
|  | 336 | if (ssid->ssid_len && | 
|  | 337 | (ssid->ssid_len != bss->ssid_len || | 
|  | 338 | os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0)) | 
|  | 339 | continue; | 
|  | 340 |  | 
|  | 341 | if (pbc) | 
|  | 342 | ret = eap_is_wps_pbc_enrollee(&ssid->eap); | 
|  | 343 | else | 
|  | 344 | ret = eap_is_wps_pin_enrollee(&ssid->eap); | 
|  | 345 | wpabuf_free(wps_ie); | 
|  | 346 | return ret; | 
|  | 347 | } | 
|  | 348 | wpabuf_free(wps_ie); | 
|  | 349 | #endif /* CONFIG_WPS */ | 
|  | 350 |  | 
|  | 351 | return 0; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 355 | static bool is_p2p_pending_bss(struct wpa_supplicant *wpa_s, | 
|  | 356 | struct wpa_bss *bss) | 
|  | 357 | { | 
|  | 358 | #ifdef CONFIG_P2P | 
|  | 359 | u8 addr[ETH_ALEN]; | 
|  | 360 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 361 | if (os_memcmp(bss->bssid, wpa_s->pending_join_iface_addr, | 
|  | 362 | ETH_ALEN) == 0) | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 363 | return true; | 
|  | 364 | if (!is_zero_ether_addr(wpa_s->pending_join_dev_addr) && | 
|  | 365 | p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, addr) == 0 && | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 366 | os_memcmp(addr, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0) | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 367 | return true; | 
|  | 368 | #endif /* CONFIG_P2P */ | 
|  | 369 | return false; | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 373 | static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | 
|  | 374 | { | 
|  | 375 | struct wpa_ssid *ssid; | 
|  | 376 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 377 | if (is_p2p_pending_bss(wpa_s, bss)) | 
|  | 378 | return 1; | 
|  | 379 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 380 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { | 
|  | 381 | if (ssid->ssid == NULL || ssid->ssid_len == 0) | 
|  | 382 | continue; | 
|  | 383 | if (ssid->ssid_len == bss->ssid_len && | 
|  | 384 | os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0) | 
|  | 385 | return 1; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | return 0; | 
|  | 389 | } | 
|  | 390 |  | 
|  | 391 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 392 | static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | 
|  | 393 | { | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 394 | int i; | 
|  | 395 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 396 | if (bss == wpa_s->current_bss) | 
|  | 397 | return 1; | 
|  | 398 |  | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 399 | if (bss == wpa_s->ml_connect_probe_bss) | 
|  | 400 | return 1; | 
|  | 401 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 402 | if (wpa_s->current_bss && | 
|  | 403 | (bss->ssid_len != wpa_s->current_bss->ssid_len || | 
|  | 404 | os_memcmp(bss->ssid, wpa_s->current_bss->ssid, | 
|  | 405 | bss->ssid_len) != 0)) | 
|  | 406 | return 0; /* SSID has changed */ | 
|  | 407 |  | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 408 | if (!is_zero_ether_addr(bss->bssid) && | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 409 | (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 || | 
|  | 410 | os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)) | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 411 | return 1; | 
|  | 412 |  | 
|  | 413 | if (!wpa_s->valid_links) | 
|  | 414 | return 0; | 
|  | 415 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 416 | for (i = 0; i < MAX_NUM_MLD_LINKS; i++) { | 
|  | 417 | if (!(wpa_s->valid_links & BIT(i))) | 
|  | 418 | continue; | 
|  | 419 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 420 | if (os_memcmp(bss->bssid, wpa_s->links[i].bssid, ETH_ALEN) == 0) | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 421 | return 1; | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | return 0; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
|  | 427 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 428 | static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s) | 
|  | 429 | { | 
|  | 430 | struct wpa_bss *bss; | 
|  | 431 |  | 
|  | 432 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | 
| Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 433 | if (!wpa_bss_known(wpa_s, bss) && | 
|  | 434 | !wpa_bss_is_wps_candidate(wpa_s, bss)) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 435 | wpa_bss_remove(wpa_s, bss, __func__); | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 436 | return 0; | 
|  | 437 | } | 
|  | 438 | } | 
|  | 439 |  | 
|  | 440 | return -1; | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 |  | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 444 | static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s) | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 445 | { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 446 | struct wpa_bss *bss; | 
|  | 447 |  | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 448 | /* | 
|  | 449 | * Remove the oldest entry that does not match with any configured | 
|  | 450 | * network. | 
|  | 451 | */ | 
|  | 452 | if (wpa_bss_remove_oldest_unknown(wpa_s) == 0) | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 453 | return 0; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 454 |  | 
|  | 455 | /* | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 456 | * Remove the oldest entry that isn't currently in use. | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 457 | */ | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 458 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 459 | if (!wpa_bss_in_use(wpa_s, bss)) { | 
|  | 460 | wpa_bss_remove(wpa_s, bss, __func__); | 
|  | 461 | return 0; | 
|  | 462 | } | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | return -1; | 
| Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 466 | } | 
|  | 467 |  | 
|  | 468 |  | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 469 | static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s, | 
|  | 470 | const u8 *ssid, size_t ssid_len, | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 471 | struct wpa_scan_res *res, | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 472 | struct os_reltime *fetch_time) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 473 | { | 
|  | 474 | struct wpa_bss *bss; | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 475 | char extra[100]; | 
|  | 476 | const u8 *ml_ie; | 
|  | 477 | char *pos, *end; | 
|  | 478 | int ret = 0; | 
|  | 479 | const u8 *mld_addr; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 480 |  | 
|  | 481 | bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len); | 
|  | 482 | if (bss == NULL) | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 483 | return NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 484 | bss->id = wpa_s->bss_next_id++; | 
|  | 485 | bss->last_update_idx = wpa_s->bss_update_idx; | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 486 | wpa_bss_copy_res(bss, res, fetch_time); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 487 | os_memcpy(bss->ssid, ssid, ssid_len); | 
|  | 488 | bss->ssid_len = ssid_len; | 
|  | 489 | bss->ie_len = res->ie_len; | 
|  | 490 | bss->beacon_ie_len = res->beacon_ie_len; | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 491 | os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len); | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 492 | wpa_bss_set_hessid(bss); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 493 |  | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 494 | os_memset(bss->mld_addr, 0, ETH_ALEN); | 
|  | 495 | ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC); | 
|  | 496 | if (ml_ie) { | 
|  | 497 | mld_addr = get_basic_mle_mld_addr(&ml_ie[3], ml_ie[1] - 1); | 
|  | 498 | if (mld_addr) | 
|  | 499 | os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN); | 
|  | 500 | } | 
|  | 501 |  | 
| Jouni Malinen | 7a6c830 | 2013-09-27 15:47:09 +0300 | [diff] [blame] | 502 | if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count && | 
|  | 503 | wpa_bss_remove_oldest(wpa_s) != 0) { | 
|  | 504 | wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d " | 
|  | 505 | "because all BSSes are in use. We should normally " | 
|  | 506 | "not get here!", (int) wpa_s->num_bss + 1); | 
|  | 507 | wpa_s->conf->bss_max_count = wpa_s->num_bss + 1; | 
|  | 508 | } | 
|  | 509 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 510 | dl_list_add_tail(&wpa_s->bss, &bss->list); | 
|  | 511 | dl_list_add_tail(&wpa_s->bss_id, &bss->list_id); | 
|  | 512 | wpa_s->num_bss++; | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 513 |  | 
|  | 514 | extra[0] = '\0'; | 
|  | 515 | pos = extra; | 
|  | 516 | end = pos + sizeof(extra); | 
| Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 517 | if (!is_zero_ether_addr(bss->hessid)) | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 518 | ret = os_snprintf(pos, end - pos, " HESSID " MACSTR, | 
|  | 519 | MAC2STR(bss->hessid)); | 
|  | 520 |  | 
|  | 521 | if (!is_zero_ether_addr(bss->mld_addr) && | 
|  | 522 | !os_snprintf_error(end - pos, ret)) { | 
|  | 523 | pos += ret; | 
|  | 524 | ret = os_snprintf(pos, end - pos, " MLD ADDR " MACSTR, | 
|  | 525 | MAC2STR(bss->mld_addr)); | 
|  | 526 | } | 
|  | 527 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 528 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR | 
| Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 529 | " SSID '%s' freq %d%s", | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 530 | bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len), | 
| Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 531 | bss->freq, extra); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 532 | wpas_notify_bss_added(wpa_s, bss->bssid, bss->id); | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 533 | return bss; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 534 | } | 
|  | 535 |  | 
|  | 536 |  | 
|  | 537 | static int are_ies_equal(const struct wpa_bss *old, | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 538 | const struct wpa_scan_res *new_res, u32 ie) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 539 | { | 
|  | 540 | const u8 *old_ie, *new_ie; | 
|  | 541 | struct wpabuf *old_ie_buff = NULL; | 
|  | 542 | struct wpabuf *new_ie_buff = NULL; | 
|  | 543 | int new_ie_len, old_ie_len, ret, is_multi; | 
|  | 544 |  | 
|  | 545 | switch (ie) { | 
|  | 546 | case WPA_IE_VENDOR_TYPE: | 
|  | 547 | old_ie = wpa_bss_get_vendor_ie(old, ie); | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 548 | new_ie = wpa_scan_get_vendor_ie(new_res, ie); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 549 | is_multi = 0; | 
|  | 550 | break; | 
|  | 551 | case WPS_IE_VENDOR_TYPE: | 
|  | 552 | old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie); | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 553 | new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 554 | is_multi = 1; | 
|  | 555 | break; | 
|  | 556 | case WLAN_EID_RSN: | 
|  | 557 | case WLAN_EID_SUPP_RATES: | 
|  | 558 | case WLAN_EID_EXT_SUPP_RATES: | 
|  | 559 | old_ie = wpa_bss_get_ie(old, ie); | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 560 | new_ie = wpa_scan_get_ie(new_res, ie); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 561 | is_multi = 0; | 
|  | 562 | break; | 
|  | 563 | default: | 
|  | 564 | wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__); | 
|  | 565 | return 0; | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | if (is_multi) { | 
|  | 569 | /* in case of multiple IEs stored in buffer */ | 
|  | 570 | old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL; | 
|  | 571 | new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL; | 
|  | 572 | old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0; | 
|  | 573 | new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0; | 
|  | 574 | } else { | 
|  | 575 | /* in case of single IE */ | 
|  | 576 | old_ie_len = old_ie ? old_ie[1] + 2 : 0; | 
|  | 577 | new_ie_len = new_ie ? new_ie[1] + 2 : 0; | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | if (!old_ie || !new_ie) | 
|  | 581 | ret = !old_ie && !new_ie; | 
|  | 582 | else | 
|  | 583 | ret = (old_ie_len == new_ie_len && | 
|  | 584 | os_memcmp(old_ie, new_ie, old_ie_len) == 0); | 
|  | 585 |  | 
|  | 586 | wpabuf_free(old_ie_buff); | 
|  | 587 | wpabuf_free(new_ie_buff); | 
|  | 588 |  | 
|  | 589 | return ret; | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 |  | 
|  | 593 | static u32 wpa_bss_compare_res(const struct wpa_bss *old, | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 594 | const struct wpa_scan_res *new_res) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 595 | { | 
|  | 596 | u32 changes = 0; | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 597 | int caps_diff = old->caps ^ new_res->caps; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 598 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 599 | if (old->freq != new_res->freq) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 600 | changes |= WPA_BSS_FREQ_CHANGED_FLAG; | 
|  | 601 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 602 | if (old->level != new_res->level) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 603 | changes |= WPA_BSS_SIGNAL_CHANGED_FLAG; | 
|  | 604 |  | 
|  | 605 | if (caps_diff & IEEE80211_CAP_PRIVACY) | 
|  | 606 | changes |= WPA_BSS_PRIVACY_CHANGED_FLAG; | 
|  | 607 |  | 
|  | 608 | if (caps_diff & IEEE80211_CAP_IBSS) | 
|  | 609 | changes |= WPA_BSS_MODE_CHANGED_FLAG; | 
|  | 610 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 611 | if (old->ie_len == new_res->ie_len && | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 612 | os_memcmp(wpa_bss_ie_ptr(old), new_res + 1, old->ie_len) == 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 613 | return changes; | 
|  | 614 | changes |= WPA_BSS_IES_CHANGED_FLAG; | 
|  | 615 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 616 | if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 617 | changes |= WPA_BSS_WPAIE_CHANGED_FLAG; | 
|  | 618 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 619 | if (!are_ies_equal(old, new_res, WLAN_EID_RSN)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 620 | changes |= WPA_BSS_RSNIE_CHANGED_FLAG; | 
|  | 621 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 622 | if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 623 | changes |= WPA_BSS_WPS_CHANGED_FLAG; | 
|  | 624 |  | 
| Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 625 | if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) || | 
|  | 626 | !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES)) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 627 | changes |= WPA_BSS_RATES_CHANGED_FLAG; | 
|  | 628 |  | 
|  | 629 | return changes; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 633 | void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes, | 
|  | 634 | const struct wpa_bss *bss) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 635 | { | 
|  | 636 | if (changes & WPA_BSS_FREQ_CHANGED_FLAG) | 
|  | 637 | wpas_notify_bss_freq_changed(wpa_s, bss->id); | 
|  | 638 |  | 
|  | 639 | if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG) | 
|  | 640 | wpas_notify_bss_signal_changed(wpa_s, bss->id); | 
|  | 641 |  | 
|  | 642 | if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG) | 
|  | 643 | wpas_notify_bss_privacy_changed(wpa_s, bss->id); | 
|  | 644 |  | 
|  | 645 | if (changes & WPA_BSS_MODE_CHANGED_FLAG) | 
|  | 646 | wpas_notify_bss_mode_changed(wpa_s, bss->id); | 
|  | 647 |  | 
|  | 648 | if (changes & WPA_BSS_WPAIE_CHANGED_FLAG) | 
|  | 649 | wpas_notify_bss_wpaie_changed(wpa_s, bss->id); | 
|  | 650 |  | 
|  | 651 | if (changes & WPA_BSS_RSNIE_CHANGED_FLAG) | 
|  | 652 | wpas_notify_bss_rsnie_changed(wpa_s, bss->id); | 
|  | 653 |  | 
|  | 654 | if (changes & WPA_BSS_WPS_CHANGED_FLAG) | 
|  | 655 | wpas_notify_bss_wps_changed(wpa_s, bss->id); | 
|  | 656 |  | 
|  | 657 | if (changes & WPA_BSS_IES_CHANGED_FLAG) | 
|  | 658 | wpas_notify_bss_ies_changed(wpa_s, bss->id); | 
|  | 659 |  | 
|  | 660 | if (changes & WPA_BSS_RATES_CHANGED_FLAG) | 
|  | 661 | wpas_notify_bss_rates_changed(wpa_s, bss->id); | 
| Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 662 |  | 
|  | 663 | wpas_notify_bss_seen(wpa_s, bss->id); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 664 | } | 
|  | 665 |  | 
|  | 666 |  | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 667 | static struct wpa_bss * | 
|  | 668 | wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 669 | struct wpa_scan_res *res, struct os_reltime *fetch_time) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 670 | { | 
|  | 671 | u32 changes; | 
|  | 672 |  | 
| Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 673 | if (bss->last_update_idx == wpa_s->bss_update_idx) { | 
|  | 674 | struct os_reltime update_time; | 
|  | 675 |  | 
|  | 676 | /* | 
|  | 677 | * Some drivers (e.g., cfg80211) include multiple BSS entries | 
|  | 678 | * for the same BSS if that BSS's channel changes. The BSS list | 
|  | 679 | * implementation in wpa_supplicant does not do that and we need | 
|  | 680 | * to filter out the obsolete results here to make sure only the | 
|  | 681 | * most current BSS information remains in the table. | 
|  | 682 | */ | 
|  | 683 | wpa_printf(MSG_DEBUG, "BSS: " MACSTR | 
|  | 684 | " has multiple entries in the scan results - select the most current one", | 
|  | 685 | MAC2STR(bss->bssid)); | 
|  | 686 | calculate_update_time(fetch_time, res->age, &update_time); | 
|  | 687 | wpa_printf(MSG_DEBUG, | 
|  | 688 | "Previous last_update: %u.%06u (freq %d%s)", | 
|  | 689 | (unsigned int) bss->last_update.sec, | 
|  | 690 | (unsigned int) bss->last_update.usec, | 
|  | 691 | bss->freq, | 
|  | 692 | (bss->flags & WPA_BSS_ASSOCIATED) ? " assoc" : ""); | 
|  | 693 | wpa_printf(MSG_DEBUG, "New last_update: %u.%06u (freq %d%s)", | 
|  | 694 | (unsigned int) update_time.sec, | 
|  | 695 | (unsigned int) update_time.usec, | 
|  | 696 | res->freq, | 
|  | 697 | (res->flags & WPA_SCAN_ASSOCIATED) ? " assoc" : ""); | 
|  | 698 | if ((bss->flags & WPA_BSS_ASSOCIATED) || | 
|  | 699 | (!(res->flags & WPA_SCAN_ASSOCIATED) && | 
|  | 700 | !os_reltime_before(&bss->last_update, &update_time))) { | 
|  | 701 | wpa_printf(MSG_DEBUG, | 
|  | 702 | "Ignore this BSS entry since the previous update looks more current"); | 
|  | 703 | return bss; | 
|  | 704 | } | 
|  | 705 | wpa_printf(MSG_DEBUG, | 
|  | 706 | "Accept this BSS entry since it looks more current than the previous update"); | 
|  | 707 | } | 
|  | 708 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 709 | changes = wpa_bss_compare_res(bss, res); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 710 | if (changes & WPA_BSS_FREQ_CHANGED_FLAG) | 
|  | 711 | wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d", | 
|  | 712 | MAC2STR(bss->bssid), bss->freq, res->freq); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 713 | bss->scan_miss_count = 0; | 
|  | 714 | bss->last_update_idx = wpa_s->bss_update_idx; | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 715 | wpa_bss_copy_res(bss, res, fetch_time); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 716 | /* Move the entry to the end of the list */ | 
|  | 717 | dl_list_del(&bss->list); | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 718 | #ifdef CONFIG_P2P | 
|  | 719 | if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) && | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 720 | !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) { | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 721 | /* | 
|  | 722 | * This can happen when non-P2P station interface runs a scan | 
|  | 723 | * without P2P IE in the Probe Request frame. P2P GO would reply | 
|  | 724 | * to that with a Probe Response that does not include P2P IE. | 
|  | 725 | * Do not update the IEs in this BSS entry to avoid such loss of | 
|  | 726 | * information that may be needed for P2P operations to | 
|  | 727 | * determine group information. | 
|  | 728 | */ | 
|  | 729 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for " | 
|  | 730 | MACSTR " since that would remove P2P IE information", | 
|  | 731 | MAC2STR(bss->bssid)); | 
|  | 732 | } else | 
|  | 733 | #endif /* CONFIG_P2P */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 734 | if (bss->ie_len + bss->beacon_ie_len >= | 
|  | 735 | res->ie_len + res->beacon_ie_len) { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 736 | os_memcpy(bss->ies, res + 1, res->ie_len + res->beacon_ie_len); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 737 | bss->ie_len = res->ie_len; | 
|  | 738 | bss->beacon_ie_len = res->beacon_ie_len; | 
|  | 739 | } else { | 
|  | 740 | struct wpa_bss *nbss; | 
|  | 741 | struct dl_list *prev = bss->list_id.prev; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 742 | struct wpa_connect_work *cwork; | 
|  | 743 | unsigned int i; | 
|  | 744 | bool update_current_bss = wpa_s->current_bss == bss; | 
|  | 745 | bool update_ml_probe_bss = wpa_s->ml_connect_probe_bss == bss; | 
|  | 746 |  | 
|  | 747 | cwork = wpa_bss_check_pending_connect(wpa_s, bss); | 
|  | 748 |  | 
|  | 749 | for (i = 0; i < wpa_s->last_scan_res_used; i++) { | 
|  | 750 | if (wpa_s->last_scan_res[i] == bss) | 
|  | 751 | break; | 
|  | 752 | } | 
|  | 753 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 754 | dl_list_del(&bss->list_id); | 
|  | 755 | nbss = os_realloc(bss, sizeof(*bss) + res->ie_len + | 
|  | 756 | res->beacon_ie_len); | 
|  | 757 | if (nbss) { | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 758 | if (i != wpa_s->last_scan_res_used) | 
|  | 759 | wpa_s->last_scan_res[i] = nbss; | 
|  | 760 |  | 
|  | 761 | if (update_current_bss) | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 762 | wpa_s->current_bss = nbss; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 763 |  | 
|  | 764 | if (update_ml_probe_bss) | 
|  | 765 | wpa_s->ml_connect_probe_bss = nbss; | 
|  | 766 |  | 
|  | 767 | if (cwork) | 
|  | 768 | wpa_bss_update_pending_connect(cwork, nbss); | 
|  | 769 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 770 | bss = nbss; | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 771 | os_memcpy(bss->ies, res + 1, | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 772 | res->ie_len + res->beacon_ie_len); | 
|  | 773 | bss->ie_len = res->ie_len; | 
|  | 774 | bss->beacon_ie_len = res->beacon_ie_len; | 
|  | 775 | } | 
|  | 776 | dl_list_add(prev, &bss->list_id); | 
|  | 777 | } | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 778 | if (changes & WPA_BSS_IES_CHANGED_FLAG) { | 
|  | 779 | const u8 *ml_ie, *mld_addr; | 
|  | 780 |  | 
| Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 781 | wpa_bss_set_hessid(bss); | 
| Sunil Ravi | 89eba10 | 2022-09-13 21:04:37 -0700 | [diff] [blame] | 782 | os_memset(bss->mld_addr, 0, ETH_ALEN); | 
|  | 783 | ml_ie = wpa_scan_get_ml_ie(res, MULTI_LINK_CONTROL_TYPE_BASIC); | 
|  | 784 | if (ml_ie) { | 
|  | 785 | mld_addr = get_basic_mle_mld_addr(&ml_ie[3], | 
|  | 786 | ml_ie[1] - 1); | 
|  | 787 | if (mld_addr) | 
|  | 788 | os_memcpy(bss->mld_addr, mld_addr, ETH_ALEN); | 
|  | 789 | } | 
|  | 790 | } | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 791 | dl_list_add_tail(&wpa_s->bss, &bss->list); | 
|  | 792 |  | 
|  | 793 | notify_bss_changes(wpa_s, changes, bss); | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 794 |  | 
|  | 795 | return bss; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 796 | } | 
|  | 797 |  | 
|  | 798 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 799 | /** | 
|  | 800 | * wpa_bss_update_start - Start a BSS table update from scan results | 
|  | 801 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 802 | * | 
|  | 803 | * This function is called at the start of each BSS table update round for new | 
|  | 804 | * scan results. The actual scan result entries are indicated with calls to | 
|  | 805 | * wpa_bss_update_scan_res() and the update round is finished with a call to | 
|  | 806 | * wpa_bss_update_end(). | 
|  | 807 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 808 | void wpa_bss_update_start(struct wpa_supplicant *wpa_s) | 
|  | 809 | { | 
|  | 810 | wpa_s->bss_update_idx++; | 
|  | 811 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u", | 
|  | 812 | wpa_s->bss_update_idx); | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 813 | wpa_s->last_scan_res_used = 0; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 814 | } | 
|  | 815 |  | 
|  | 816 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 817 | /** | 
|  | 818 | * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result | 
|  | 819 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 820 | * @res: Scan result | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 821 | * @fetch_time: Time when the result was fetched from the driver | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 822 | * | 
|  | 823 | * This function updates a BSS table entry (or adds one) based on a scan result. | 
|  | 824 | * This is called separately for each scan result between the calls to | 
|  | 825 | * wpa_bss_update_start() and wpa_bss_update_end(). | 
|  | 826 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 827 | void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s, | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 828 | struct wpa_scan_res *res, | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 829 | struct os_reltime *fetch_time) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 830 | { | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 831 | const u8 *ssid, *p2p, *mesh; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 832 | struct wpa_bss *bss; | 
|  | 833 |  | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 834 | if (wpa_s->conf->ignore_old_scan_res) { | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 835 | struct os_reltime update; | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 836 | calculate_update_time(fetch_time, res->age, &update); | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 837 | if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) { | 
|  | 838 | struct os_reltime age; | 
|  | 839 | os_reltime_sub(&wpa_s->scan_trigger_time, &update, | 
|  | 840 | &age); | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 841 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS " | 
|  | 842 | "table entry that is %u.%06u seconds older " | 
|  | 843 | "than our scan trigger", | 
|  | 844 | (unsigned int) age.sec, | 
|  | 845 | (unsigned int) age.usec); | 
|  | 846 | return; | 
|  | 847 | } | 
|  | 848 | } | 
|  | 849 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 850 | ssid = wpa_scan_get_ie(res, WLAN_EID_SSID); | 
|  | 851 | if (ssid == NULL) { | 
|  | 852 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for " | 
|  | 853 | MACSTR, MAC2STR(res->bssid)); | 
|  | 854 | return; | 
|  | 855 | } | 
| Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 856 | if (ssid[1] > SSID_MAX_LEN) { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 857 | wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for " | 
|  | 858 | MACSTR, MAC2STR(res->bssid)); | 
|  | 859 | return; | 
|  | 860 | } | 
|  | 861 |  | 
|  | 862 | p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE); | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 863 | #ifdef CONFIG_P2P | 
|  | 864 | if (p2p == NULL && | 
|  | 865 | wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) { | 
|  | 866 | /* | 
|  | 867 | * If it's a P2P specific interface, then don't update | 
|  | 868 | * the scan result without a P2P IE. | 
|  | 869 | */ | 
|  | 870 | wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR | 
|  | 871 | " update for P2P interface", MAC2STR(res->bssid)); | 
|  | 872 | return; | 
|  | 873 | } | 
|  | 874 | #endif /* CONFIG_P2P */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 875 | if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN && | 
|  | 876 | os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0) | 
|  | 877 | return; /* Skip P2P listen discovery results here */ | 
|  | 878 |  | 
|  | 879 | /* TODO: add option for ignoring BSSes we are not interested in | 
|  | 880 | * (to save memory) */ | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 881 |  | 
|  | 882 | mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID); | 
| Dmitry Shmidt | 9d9e602 | 2015-04-23 10:34:55 -0700 | [diff] [blame] | 883 | if (mesh && mesh[1] <= SSID_MAX_LEN) | 
| Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 884 | ssid = mesh; | 
|  | 885 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 886 | bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]); | 
|  | 887 | if (bss == NULL) | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 888 | bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 889 | else { | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 890 | bss = wpa_bss_update(wpa_s, bss, res, fetch_time); | 
| Dmitry Shmidt | 5605286 | 2013-10-04 10:23:25 -0700 | [diff] [blame] | 891 | if (wpa_s->last_scan_res) { | 
|  | 892 | unsigned int i; | 
|  | 893 | for (i = 0; i < wpa_s->last_scan_res_used; i++) { | 
|  | 894 | if (bss == wpa_s->last_scan_res[i]) { | 
|  | 895 | /* Already in the list */ | 
|  | 896 | return; | 
|  | 897 | } | 
|  | 898 | } | 
|  | 899 | } | 
|  | 900 | } | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 901 |  | 
|  | 902 | if (bss == NULL) | 
|  | 903 | return; | 
|  | 904 | if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) { | 
|  | 905 | struct wpa_bss **n; | 
|  | 906 | unsigned int siz; | 
|  | 907 | if (wpa_s->last_scan_res_size == 0) | 
|  | 908 | siz = 32; | 
|  | 909 | else | 
|  | 910 | siz = wpa_s->last_scan_res_size * 2; | 
|  | 911 | n = os_realloc_array(wpa_s->last_scan_res, siz, | 
|  | 912 | sizeof(struct wpa_bss *)); | 
|  | 913 | if (n == NULL) | 
|  | 914 | return; | 
|  | 915 | wpa_s->last_scan_res = n; | 
|  | 916 | wpa_s->last_scan_res_size = siz; | 
|  | 917 | } | 
|  | 918 |  | 
| Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 919 | if (wpa_s->last_scan_res) | 
|  | 920 | wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 921 | } | 
|  | 922 |  | 
|  | 923 |  | 
|  | 924 | static int wpa_bss_included_in_scan(const struct wpa_bss *bss, | 
|  | 925 | const struct scan_info *info) | 
|  | 926 | { | 
|  | 927 | int found; | 
|  | 928 | size_t i; | 
|  | 929 |  | 
|  | 930 | if (info == NULL) | 
|  | 931 | return 1; | 
|  | 932 |  | 
|  | 933 | if (info->num_freqs) { | 
|  | 934 | found = 0; | 
|  | 935 | for (i = 0; i < info->num_freqs; i++) { | 
|  | 936 | if (bss->freq == info->freqs[i]) { | 
|  | 937 | found = 1; | 
|  | 938 | break; | 
|  | 939 | } | 
|  | 940 | } | 
|  | 941 | if (!found) | 
|  | 942 | return 0; | 
|  | 943 | } | 
|  | 944 |  | 
|  | 945 | if (info->num_ssids) { | 
|  | 946 | found = 0; | 
|  | 947 | for (i = 0; i < info->num_ssids; i++) { | 
|  | 948 | const struct wpa_driver_scan_ssid *s = &info->ssids[i]; | 
|  | 949 | if ((s->ssid == NULL || s->ssid_len == 0) || | 
|  | 950 | (s->ssid_len == bss->ssid_len && | 
|  | 951 | os_memcmp(s->ssid, bss->ssid, bss->ssid_len) == | 
|  | 952 | 0)) { | 
|  | 953 | found = 1; | 
|  | 954 | break; | 
|  | 955 | } | 
|  | 956 | } | 
|  | 957 | if (!found) | 
|  | 958 | return 0; | 
|  | 959 | } | 
|  | 960 |  | 
|  | 961 | return 1; | 
|  | 962 | } | 
|  | 963 |  | 
|  | 964 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 965 | /** | 
|  | 966 | * wpa_bss_update_end - End a BSS table update from scan results | 
|  | 967 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 968 | * @info: Information about scan parameters | 
|  | 969 | * @new_scan: Whether this update round was based on a new scan | 
|  | 970 | * | 
|  | 971 | * This function is called at the end of each BSS table update round for new | 
|  | 972 | * scan results. The start of the update was indicated with a call to | 
|  | 973 | * wpa_bss_update_start(). | 
|  | 974 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 975 | void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info, | 
|  | 976 | int new_scan) | 
|  | 977 | { | 
|  | 978 | struct wpa_bss *bss, *n; | 
|  | 979 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 980 | os_get_reltime(&wpa_s->last_scan); | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 981 | if ((info && info->aborted) || !new_scan) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 982 | return; /* do not expire entries without new scan */ | 
|  | 983 |  | 
|  | 984 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 985 | if (wpa_bss_in_use(wpa_s, bss)) | 
|  | 986 | continue; | 
|  | 987 | if (!wpa_bss_included_in_scan(bss, info)) | 
|  | 988 | continue; /* expire only BSSes that were scanned */ | 
|  | 989 | if (bss->last_update_idx < wpa_s->bss_update_idx) | 
|  | 990 | bss->scan_miss_count++; | 
|  | 991 | if (bss->scan_miss_count >= | 
|  | 992 | wpa_s->conf->bss_expiration_scan_count) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 993 | wpa_bss_remove(wpa_s, bss, "no match in scan"); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 994 | } | 
|  | 995 | } | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 996 |  | 
| Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 997 | wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%zu/%zu", | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 998 | wpa_s->last_scan_res_used, wpa_s->last_scan_res_size); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 999 | } | 
|  | 1000 |  | 
|  | 1001 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1002 | /** | 
|  | 1003 | * wpa_bss_flush_by_age - Flush old BSS entries | 
|  | 1004 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1005 | * @age: Maximum entry age in seconds | 
|  | 1006 | * | 
|  | 1007 | * Remove BSS entries that have not been updated during the last @age seconds. | 
|  | 1008 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1009 | void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age) | 
|  | 1010 | { | 
|  | 1011 | struct wpa_bss *bss, *n; | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1012 | struct os_reltime t; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1013 |  | 
|  | 1014 | if (dl_list_empty(&wpa_s->bss)) | 
|  | 1015 | return; | 
|  | 1016 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1017 | os_get_reltime(&t); | 
| Sunil Ravi | af8751c | 2023-03-29 11:35:17 -0700 | [diff] [blame] | 1018 |  | 
|  | 1019 | if (t.sec < age) | 
|  | 1020 | return; /* avoid underflow; there can be no older entries */ | 
|  | 1021 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1022 | t.sec -= age; | 
|  | 1023 |  | 
|  | 1024 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 1025 | if (wpa_bss_in_use(wpa_s, bss)) | 
|  | 1026 | continue; | 
|  | 1027 |  | 
| Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1028 | if (wpa_s->reassoc_same_ess && | 
|  | 1029 | wpa_s->wpa_state != WPA_COMPLETED && | 
|  | 1030 | wpa_s->last_ssid && | 
|  | 1031 | bss->ssid_len == wpa_s->last_ssid->ssid_len && | 
|  | 1032 | os_memcmp(bss->ssid, wpa_s->last_ssid->ssid, | 
|  | 1033 | bss->ssid_len) == 0) | 
|  | 1034 | continue; | 
|  | 1035 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1036 | if (os_reltime_before(&bss->last_update, &t)) { | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1037 | wpa_bss_remove(wpa_s, bss, __func__); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1038 | } else | 
|  | 1039 | break; | 
|  | 1040 | } | 
|  | 1041 | } | 
|  | 1042 |  | 
|  | 1043 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1044 | /** | 
|  | 1045 | * wpa_bss_init - Initialize BSS table | 
|  | 1046 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1047 | * Returns: 0 on success, -1 on failure | 
|  | 1048 | * | 
|  | 1049 | * This prepares BSS table lists and timer for periodic updates. The BSS table | 
|  | 1050 | * is deinitialized with wpa_bss_deinit() once not needed anymore. | 
|  | 1051 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1052 | int wpa_bss_init(struct wpa_supplicant *wpa_s) | 
|  | 1053 | { | 
|  | 1054 | dl_list_init(&wpa_s->bss); | 
|  | 1055 | dl_list_init(&wpa_s->bss_id); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1056 | return 0; | 
|  | 1057 | } | 
|  | 1058 |  | 
|  | 1059 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1060 | /** | 
|  | 1061 | * wpa_bss_flush - Flush all unused BSS entries | 
|  | 1062 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1063 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1064 | void wpa_bss_flush(struct wpa_supplicant *wpa_s) | 
|  | 1065 | { | 
|  | 1066 | struct wpa_bss *bss, *n; | 
|  | 1067 |  | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1068 | wpa_s->clear_driver_scan_cache = 1; | 
|  | 1069 |  | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1070 | if (wpa_s->bss.next == NULL) | 
|  | 1071 | return; /* BSS table not yet initialized */ | 
|  | 1072 |  | 
|  | 1073 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 1074 | if (wpa_bss_in_use(wpa_s, bss)) | 
|  | 1075 | continue; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1076 | wpa_bss_remove(wpa_s, bss, __func__); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1077 | } | 
|  | 1078 | } | 
|  | 1079 |  | 
|  | 1080 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1081 | /** | 
|  | 1082 | * wpa_bss_deinit - Deinitialize BSS table | 
|  | 1083 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1084 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1085 | void wpa_bss_deinit(struct wpa_supplicant *wpa_s) | 
|  | 1086 | { | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1087 | wpa_bss_flush(wpa_s); | 
|  | 1088 | } | 
|  | 1089 |  | 
|  | 1090 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1091 | /** | 
|  | 1092 | * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID | 
|  | 1093 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1094 | * @bssid: BSSID | 
|  | 1095 | * Returns: Pointer to the BSS entry or %NULL if not found | 
|  | 1096 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1097 | struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s, | 
|  | 1098 | const u8 *bssid) | 
|  | 1099 | { | 
|  | 1100 | struct wpa_bss *bss; | 
| Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1101 | if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid)) | 
|  | 1102 | return NULL; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1103 | dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) { | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1104 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1105 | return bss; | 
|  | 1106 | } | 
|  | 1107 | return NULL; | 
|  | 1108 | } | 
|  | 1109 |  | 
|  | 1110 |  | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1111 | /** | 
|  | 1112 | * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID | 
|  | 1113 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1114 | * @bssid: BSSID | 
|  | 1115 | * Returns: Pointer to the BSS entry or %NULL if not found | 
|  | 1116 | * | 
|  | 1117 | * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to | 
|  | 1118 | * find the entry that has the most recent update. This can help in finding the | 
|  | 1119 | * correct entry in cases where the SSID of the AP may have changed recently | 
|  | 1120 | * (e.g., in WPS reconfiguration cases). | 
|  | 1121 | */ | 
|  | 1122 | struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s, | 
|  | 1123 | const u8 *bssid) | 
|  | 1124 | { | 
|  | 1125 | struct wpa_bss *bss, *found = NULL; | 
|  | 1126 | if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid)) | 
|  | 1127 | return NULL; | 
|  | 1128 | dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) { | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1129 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0) | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1130 | continue; | 
|  | 1131 | if (found == NULL || | 
| Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1132 | os_reltime_before(&found->last_update, &bss->last_update)) | 
| Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1133 | found = bss; | 
|  | 1134 | } | 
|  | 1135 | return found; | 
|  | 1136 | } | 
|  | 1137 |  | 
|  | 1138 |  | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1139 | #ifdef CONFIG_P2P | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1140 | /** | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1141 | * wpa_bss_get_p2p_dev_addr - Fetch the latest BSS table entry based on P2P Device Addr | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1142 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1143 | * @dev_addr: P2P Device Address of the GO | 
|  | 1144 | * Returns: Pointer to the BSS entry or %NULL if not found | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1145 | * | 
|  | 1146 | * This function tries to find the entry that has the most recent update. This | 
|  | 1147 | * can help in finding the correct entry in cases where the SSID of the P2P | 
|  | 1148 | * Device may have changed recently. | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1149 | */ | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1150 | struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s, | 
|  | 1151 | const u8 *dev_addr) | 
|  | 1152 | { | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1153 | struct wpa_bss *bss, *found = NULL; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1154 | dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 1155 | u8 addr[ETH_ALEN]; | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1156 | if (p2p_parse_dev_addr(wpa_bss_ie_ptr(bss), bss->ie_len, | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1157 | addr) != 0 || | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1158 | os_memcmp(addr, dev_addr, ETH_ALEN) != 0) | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1159 | continue; | 
|  | 1160 | if (!found || | 
|  | 1161 | os_reltime_before(&found->last_update, &bss->last_update)) | 
|  | 1162 | found = bss; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1163 | } | 
| Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1164 | return found; | 
| Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1165 | } | 
|  | 1166 | #endif /* CONFIG_P2P */ | 
|  | 1167 |  | 
|  | 1168 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1169 | /** | 
|  | 1170 | * wpa_bss_get_id - Fetch a BSS table entry based on identifier | 
|  | 1171 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1172 | * @id: Unique identifier (struct wpa_bss::id) assigned for the entry | 
|  | 1173 | * Returns: Pointer to the BSS entry or %NULL if not found | 
|  | 1174 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1175 | struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id) | 
|  | 1176 | { | 
|  | 1177 | struct wpa_bss *bss; | 
|  | 1178 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | 
|  | 1179 | if (bss->id == id) | 
|  | 1180 | return bss; | 
|  | 1181 | } | 
|  | 1182 | return NULL; | 
|  | 1183 | } | 
|  | 1184 |  | 
|  | 1185 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1186 | /** | 
| Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 1187 | * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range | 
|  | 1188 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1189 | * @idf: Smallest allowed identifier assigned for the entry | 
|  | 1190 | * @idf: Largest allowed identifier assigned for the entry | 
|  | 1191 | * Returns: Pointer to the BSS entry or %NULL if not found | 
|  | 1192 | * | 
|  | 1193 | * This function is similar to wpa_bss_get_id() but allows a BSS entry with the | 
|  | 1194 | * smallest id value to be fetched within the specified range without the | 
|  | 1195 | * caller having to know the exact id. | 
|  | 1196 | */ | 
|  | 1197 | struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s, | 
|  | 1198 | unsigned int idf, unsigned int idl) | 
|  | 1199 | { | 
|  | 1200 | struct wpa_bss *bss; | 
|  | 1201 | dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) { | 
|  | 1202 | if (bss->id >= idf && bss->id <= idl) | 
|  | 1203 | return bss; | 
|  | 1204 | } | 
|  | 1205 | return NULL; | 
|  | 1206 | } | 
|  | 1207 |  | 
|  | 1208 |  | 
|  | 1209 | /** | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1210 | * wpa_bss_get_ie - Fetch a specified information element from a BSS entry | 
|  | 1211 | * @bss: BSS table entry | 
|  | 1212 | * @ie: Information element identitifier (WLAN_EID_*) | 
|  | 1213 | * Returns: Pointer to the information element (id field) or %NULL if not found | 
|  | 1214 | * | 
|  | 1215 | * This function returns the first matching information element in the BSS | 
|  | 1216 | * entry. | 
|  | 1217 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1218 | const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie) | 
|  | 1219 | { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1220 | return get_ie(wpa_bss_ie_ptr(bss), bss->ie_len, ie); | 
|  | 1221 | } | 
|  | 1222 |  | 
|  | 1223 |  | 
|  | 1224 | /** | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1225 | * wpa_bss_get_ie_nth - Fetch a specified information element from a BSS entry | 
|  | 1226 | * @bss: BSS table entry | 
|  | 1227 | * @ie: Information element identitifier (WLAN_EID_*) | 
|  | 1228 | * @nth: Return the nth element of the requested type (2 returns the second) | 
|  | 1229 | * Returns: Pointer to the information element (id field) or %NULL if not found | 
|  | 1230 | * | 
|  | 1231 | * This function returns the nth matching information element in the BSS | 
|  | 1232 | * entry. | 
|  | 1233 | */ | 
|  | 1234 | const u8 * wpa_bss_get_ie_nth(const struct wpa_bss *bss, u8 ie, int nth) | 
|  | 1235 | { | 
|  | 1236 | return get_ie_nth(wpa_bss_ie_ptr(bss), bss->ie_len, ie, nth); | 
|  | 1237 | } | 
|  | 1238 |  | 
|  | 1239 |  | 
|  | 1240 | /** | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1241 | * wpa_bss_get_ie_ext - Fetch a specified extended IE from a BSS entry | 
|  | 1242 | * @bss: BSS table entry | 
|  | 1243 | * @ext: Information element extension identifier (WLAN_EID_EXT_*) | 
|  | 1244 | * Returns: Pointer to the information element (id field) or %NULL if not found | 
|  | 1245 | * | 
|  | 1246 | * This function returns the first matching information element in the BSS | 
|  | 1247 | * entry. | 
|  | 1248 | */ | 
|  | 1249 | const u8 * wpa_bss_get_ie_ext(const struct wpa_bss *bss, u8 ext) | 
|  | 1250 | { | 
|  | 1251 | return get_ie_ext(wpa_bss_ie_ptr(bss), bss->ie_len, ext); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1252 | } | 
|  | 1253 |  | 
|  | 1254 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1255 | /** | 
|  | 1256 | * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry | 
|  | 1257 | * @bss: BSS table entry | 
|  | 1258 | * @vendor_type: Vendor type (four octets starting the IE payload) | 
|  | 1259 | * Returns: Pointer to the information element (id field) or %NULL if not found | 
|  | 1260 | * | 
|  | 1261 | * This function returns the first matching information element in the BSS | 
|  | 1262 | * entry. | 
|  | 1263 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1264 | const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type) | 
|  | 1265 | { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1266 | const u8 *ies; | 
|  | 1267 | const struct element *elem; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1268 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1269 | ies = wpa_bss_ie_ptr(bss); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1270 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1271 | for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, bss->ie_len) { | 
|  | 1272 | if (elem->datalen >= 4 && | 
|  | 1273 | vendor_type == WPA_GET_BE32(elem->data)) | 
|  | 1274 | return &elem->id; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1275 | } | 
|  | 1276 |  | 
|  | 1277 | return NULL; | 
|  | 1278 | } | 
|  | 1279 |  | 
|  | 1280 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1281 | /** | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1282 | * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry | 
|  | 1283 | * @bss: BSS table entry | 
|  | 1284 | * @vendor_type: Vendor type (four octets starting the IE payload) | 
|  | 1285 | * Returns: Pointer to the information element (id field) or %NULL if not found | 
|  | 1286 | * | 
|  | 1287 | * This function returns the first matching information element in the BSS | 
|  | 1288 | * entry. | 
|  | 1289 | * | 
|  | 1290 | * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only | 
|  | 1291 | * from Beacon frames instead of either Beacon or Probe Response frames. | 
|  | 1292 | */ | 
|  | 1293 | const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss, | 
|  | 1294 | u32 vendor_type) | 
|  | 1295 | { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1296 | const u8 *ies; | 
|  | 1297 | const struct element *elem; | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1298 |  | 
|  | 1299 | if (bss->beacon_ie_len == 0) | 
|  | 1300 | return NULL; | 
|  | 1301 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1302 | ies = wpa_bss_ie_ptr(bss); | 
|  | 1303 | ies += bss->ie_len; | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1304 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1305 | for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, ies, | 
|  | 1306 | bss->beacon_ie_len) { | 
|  | 1307 | if (elem->datalen >= 4 && | 
|  | 1308 | vendor_type == WPA_GET_BE32(elem->data)) | 
|  | 1309 | return &elem->id; | 
| Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1310 | } | 
|  | 1311 |  | 
|  | 1312 | return NULL; | 
|  | 1313 | } | 
|  | 1314 |  | 
|  | 1315 |  | 
|  | 1316 | /** | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1317 | * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry | 
|  | 1318 | * @bss: BSS table entry | 
|  | 1319 | * @vendor_type: Vendor type (four octets starting the IE payload) | 
|  | 1320 | * Returns: Pointer to the information element payload or %NULL if not found | 
|  | 1321 | * | 
|  | 1322 | * This function returns concatenated payload of possibly fragmented vendor | 
|  | 1323 | * specific information elements in the BSS entry. The caller is responsible for | 
|  | 1324 | * freeing the returned buffer. | 
|  | 1325 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1326 | struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss, | 
|  | 1327 | u32 vendor_type) | 
|  | 1328 | { | 
|  | 1329 | struct wpabuf *buf; | 
|  | 1330 | const u8 *end, *pos; | 
|  | 1331 |  | 
|  | 1332 | buf = wpabuf_alloc(bss->ie_len); | 
|  | 1333 | if (buf == NULL) | 
|  | 1334 | return NULL; | 
|  | 1335 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1336 | pos = wpa_bss_ie_ptr(bss); | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1337 | end = pos + bss->ie_len; | 
|  | 1338 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1339 | while (end - pos > 1) { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1340 | u8 ie, len; | 
|  | 1341 |  | 
|  | 1342 | ie = pos[0]; | 
|  | 1343 | len = pos[1]; | 
|  | 1344 | if (len > end - pos - 2) | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1345 | break; | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1346 | pos += 2; | 
|  | 1347 | if (ie == WLAN_EID_VENDOR_SPECIFIC && len >= 4 && | 
|  | 1348 | vendor_type == WPA_GET_BE32(pos)) | 
|  | 1349 | wpabuf_put_data(buf, pos + 4, len - 4); | 
|  | 1350 | pos += len; | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1351 | } | 
|  | 1352 |  | 
|  | 1353 | if (wpabuf_len(buf) == 0) { | 
|  | 1354 | wpabuf_free(buf); | 
|  | 1355 | buf = NULL; | 
|  | 1356 | } | 
|  | 1357 |  | 
|  | 1358 | return buf; | 
|  | 1359 | } | 
|  | 1360 |  | 
|  | 1361 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1362 | /** | 
|  | 1363 | * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry | 
|  | 1364 | * @bss: BSS table entry | 
|  | 1365 | * @vendor_type: Vendor type (four octets starting the IE payload) | 
|  | 1366 | * Returns: Pointer to the information element payload or %NULL if not found | 
|  | 1367 | * | 
|  | 1368 | * This function returns concatenated payload of possibly fragmented vendor | 
|  | 1369 | * specific information elements in the BSS entry. The caller is responsible for | 
|  | 1370 | * freeing the returned buffer. | 
|  | 1371 | * | 
|  | 1372 | * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only | 
|  | 1373 | * from Beacon frames instead of either Beacon or Probe Response frames. | 
|  | 1374 | */ | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 1375 | struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss, | 
|  | 1376 | u32 vendor_type) | 
|  | 1377 | { | 
|  | 1378 | struct wpabuf *buf; | 
|  | 1379 | const u8 *end, *pos; | 
|  | 1380 |  | 
|  | 1381 | buf = wpabuf_alloc(bss->beacon_ie_len); | 
|  | 1382 | if (buf == NULL) | 
|  | 1383 | return NULL; | 
|  | 1384 |  | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1385 | pos = wpa_bss_ie_ptr(bss); | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 1386 | pos += bss->ie_len; | 
|  | 1387 | end = pos + bss->beacon_ie_len; | 
|  | 1388 |  | 
| Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1389 | while (end - pos > 1) { | 
| Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1390 | u8 id, len; | 
|  | 1391 |  | 
|  | 1392 | id = *pos++; | 
|  | 1393 | len = *pos++; | 
|  | 1394 | if (len > end - pos) | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 1395 | break; | 
| Sunil | 8cd6f4d | 2022-06-28 18:40:46 +0000 | [diff] [blame] | 1396 | if (id == WLAN_EID_VENDOR_SPECIFIC && len >= 4 && | 
|  | 1397 | vendor_type == WPA_GET_BE32(pos)) | 
|  | 1398 | wpabuf_put_data(buf, pos + 4, len - 4); | 
|  | 1399 | pos += len; | 
| Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 1400 | } | 
|  | 1401 |  | 
|  | 1402 | if (wpabuf_len(buf) == 0) { | 
|  | 1403 | wpabuf_free(buf); | 
|  | 1404 | buf = NULL; | 
|  | 1405 | } | 
|  | 1406 |  | 
|  | 1407 | return buf; | 
|  | 1408 | } | 
|  | 1409 |  | 
|  | 1410 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1411 | /** | 
|  | 1412 | * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS | 
|  | 1413 | * @bss: BSS table entry | 
|  | 1414 | * Returns: Maximum legacy rate in units of 500 kbps | 
|  | 1415 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1416 | int wpa_bss_get_max_rate(const struct wpa_bss *bss) | 
|  | 1417 | { | 
|  | 1418 | int rate = 0; | 
|  | 1419 | const u8 *ie; | 
|  | 1420 | int i; | 
|  | 1421 |  | 
|  | 1422 | ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES); | 
|  | 1423 | for (i = 0; ie && i < ie[1]; i++) { | 
|  | 1424 | if ((ie[i + 2] & 0x7f) > rate) | 
|  | 1425 | rate = ie[i + 2] & 0x7f; | 
|  | 1426 | } | 
|  | 1427 |  | 
|  | 1428 | ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES); | 
|  | 1429 | for (i = 0; ie && i < ie[1]; i++) { | 
|  | 1430 | if ((ie[i + 2] & 0x7f) > rate) | 
|  | 1431 | rate = ie[i + 2] & 0x7f; | 
|  | 1432 | } | 
|  | 1433 |  | 
|  | 1434 | return rate; | 
|  | 1435 | } | 
|  | 1436 |  | 
|  | 1437 |  | 
| Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1438 | /** | 
|  | 1439 | * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS | 
|  | 1440 | * @bss: BSS table entry | 
|  | 1441 | * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps) | 
|  | 1442 | * Returns: number of legacy TX rates or -1 on failure | 
|  | 1443 | * | 
|  | 1444 | * The caller is responsible for freeing the returned buffer with os_free() in | 
|  | 1445 | * case of success. | 
|  | 1446 | */ | 
| Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1447 | int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates) | 
|  | 1448 | { | 
|  | 1449 | const u8 *ie, *ie2; | 
|  | 1450 | int i, j; | 
|  | 1451 | unsigned int len; | 
|  | 1452 | u8 *r; | 
|  | 1453 |  | 
|  | 1454 | ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES); | 
|  | 1455 | ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES); | 
|  | 1456 |  | 
|  | 1457 | len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0); | 
|  | 1458 |  | 
|  | 1459 | r = os_malloc(len); | 
|  | 1460 | if (!r) | 
|  | 1461 | return -1; | 
|  | 1462 |  | 
|  | 1463 | for (i = 0; ie && i < ie[1]; i++) | 
|  | 1464 | r[i] = ie[i + 2] & 0x7f; | 
|  | 1465 |  | 
|  | 1466 | for (j = 0; ie2 && j < ie2[1]; j++) | 
|  | 1467 | r[i + j] = ie2[j + 2] & 0x7f; | 
|  | 1468 |  | 
|  | 1469 | *rates = r; | 
|  | 1470 | return len; | 
|  | 1471 | } | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1472 |  | 
|  | 1473 |  | 
|  | 1474 | #ifdef CONFIG_FILS | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1475 | const u8 * wpa_bss_get_fils_cache_id(const struct wpa_bss *bss) | 
| Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1476 | { | 
|  | 1477 | const u8 *ie; | 
|  | 1478 |  | 
|  | 1479 | if (bss) { | 
|  | 1480 | ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION); | 
|  | 1481 | if (ie && ie[1] >= 4 && WPA_GET_LE16(ie + 2) & BIT(7)) | 
|  | 1482 | return ie + 4; | 
|  | 1483 | } | 
|  | 1484 |  | 
|  | 1485 | return NULL; | 
|  | 1486 | } | 
|  | 1487 | #endif /* CONFIG_FILS */ | 
| Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1488 |  | 
|  | 1489 |  | 
|  | 1490 | int wpa_bss_ext_capab(const struct wpa_bss *bss, unsigned int capab) | 
|  | 1491 | { | 
| Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1492 | if (!bss) | 
|  | 1493 | return 0; | 
| Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1494 | return ieee802_11_ext_capab(wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB), | 
|  | 1495 | capab); | 
|  | 1496 | } | 
| Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 1497 |  | 
|  | 1498 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1499 | /** | 
|  | 1500 | * wpa_bss_defrag_mle - Get a buffer holding a de-fragmented ML element | 
|  | 1501 | * @bss: BSS table entry | 
|  | 1502 | * @type: ML control type | 
|  | 1503 | */ | 
|  | 1504 | struct wpabuf * wpa_bss_defrag_mle(const struct wpa_bss *bss, u8 type) | 
|  | 1505 | { | 
|  | 1506 | struct ieee802_11_elems elems; | 
|  | 1507 | const u8 *pos = wpa_bss_ie_ptr(bss); | 
|  | 1508 | size_t len = bss->ie_len; | 
|  | 1509 |  | 
|  | 1510 | if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed) | 
|  | 1511 | return NULL; | 
|  | 1512 |  | 
|  | 1513 | return ieee802_11_defrag_mle(&elems, type); | 
|  | 1514 | } | 
|  | 1515 |  | 
|  | 1516 |  | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1517 | static void | 
|  | 1518 | wpa_bss_parse_ml_rnr_ap_info(struct wpa_supplicant *wpa_s, | 
|  | 1519 | struct wpa_bss *bss, u8 mbssid_idx, | 
|  | 1520 | const struct ieee80211_neighbor_ap_info *ap_info, | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1521 | size_t len, u16 *seen, u16 *missing) | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1522 | { | 
|  | 1523 | const u8 *pos, *end; | 
|  | 1524 | const u8 *mld_params; | 
|  | 1525 | u8 count, mld_params_offset; | 
|  | 1526 | u8 i, type, link_id; | 
|  | 1527 |  | 
|  | 1528 | count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1; | 
|  | 1529 | type = ap_info->tbtt_info_hdr & RNR_TBTT_INFO_HDR_TYPE_MSK; | 
|  | 1530 |  | 
|  | 1531 | /* MLD information is at offset 13 or at start */ | 
|  | 1532 | if (type == 0 && ap_info->tbtt_info_len >= RNR_TBTT_INFO_MLD_LEN) { | 
|  | 1533 | /* MLD info is appended */ | 
|  | 1534 | mld_params_offset = RNR_TBTT_INFO_LEN; | 
|  | 1535 | } else { | 
|  | 1536 | /* TODO: Support NSTR AP */ | 
|  | 1537 | return; | 
|  | 1538 | } | 
|  | 1539 |  | 
|  | 1540 | pos = (const u8 *) ap_info; | 
|  | 1541 | end = pos + len; | 
|  | 1542 | pos += sizeof(*ap_info); | 
|  | 1543 |  | 
|  | 1544 | for (i = 0; i < count; i++) { | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1545 | if (bss->n_mld_links >= MAX_NUM_MLD_LINKS) | 
|  | 1546 | return; | 
|  | 1547 |  | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1548 | if (end - pos < ap_info->tbtt_info_len) | 
|  | 1549 | break; | 
|  | 1550 |  | 
|  | 1551 | mld_params = pos + mld_params_offset; | 
|  | 1552 |  | 
|  | 1553 | link_id = *(mld_params + 1) & EHT_ML_LINK_ID_MSK; | 
|  | 1554 |  | 
|  | 1555 | if (*mld_params != mbssid_idx) { | 
|  | 1556 | wpa_printf(MSG_DEBUG, | 
|  | 1557 | "MLD: Reported link not part of MLD"); | 
|  | 1558 | } else if (!(BIT(link_id) & *seen)) { | 
|  | 1559 | struct wpa_bss *neigh_bss = | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1560 | wpa_bss_get_bssid(wpa_s, ap_info->data + 1); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1561 |  | 
|  | 1562 | *seen |= BIT(link_id); | 
|  | 1563 | wpa_printf(MSG_DEBUG, "MLD: mld ID=%u, link ID=%u", | 
|  | 1564 | *mld_params, link_id); | 
|  | 1565 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1566 | if (neigh_bss) { | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1567 | struct mld_link *l; | 
|  | 1568 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1569 | l = &bss->mld_links[bss->n_mld_links]; | 
|  | 1570 | l->link_id = link_id; | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1571 | os_memcpy(l->bssid, ap_info->data + 1, | 
|  | 1572 | ETH_ALEN); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1573 | l->freq = neigh_bss->freq; | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1574 | bss->n_mld_links++; | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1575 | } else { | 
|  | 1576 | *missing |= BIT(link_id); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1577 | } | 
|  | 1578 | } | 
|  | 1579 |  | 
|  | 1580 | pos += ap_info->tbtt_info_len; | 
|  | 1581 | } | 
|  | 1582 | } | 
|  | 1583 |  | 
|  | 1584 |  | 
|  | 1585 | /** | 
|  | 1586 | * wpa_bss_parse_basic_ml_element - Parse the Basic Multi-Link element | 
|  | 1587 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1588 | * @bss: BSS table entry | 
|  | 1589 | * @mld_addr: AP MLD address (or %NULL) | 
|  | 1590 | * @link_info: Array to store link information (or %NULL), | 
|  | 1591 | *   should be initialized and #MAX_NUM_MLD_LINKS elements long | 
|  | 1592 | * @missing_links: Result bitmask of links that were not discovered (or %NULL) | 
|  | 1593 | * Returns: 0 on success or -1 for non-MLD or parsing failures | 
|  | 1594 | * | 
|  | 1595 | * Parses the Basic Multi-Link element of the BSS into @link_info using the scan | 
|  | 1596 | * information stored in the wpa_supplicant data to fill in information for | 
|  | 1597 | * links where possible. The @missing_links out parameter will contain any links | 
|  | 1598 | * for which no corresponding BSS was found. | 
|  | 1599 | */ | 
|  | 1600 | int wpa_bss_parse_basic_ml_element(struct wpa_supplicant *wpa_s, | 
|  | 1601 | struct wpa_bss *bss, | 
|  | 1602 | u8 *ap_mld_addr, | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1603 | u16 *missing_links) | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1604 | { | 
|  | 1605 | struct ieee802_11_elems elems; | 
|  | 1606 | struct wpabuf *mlbuf; | 
|  | 1607 | const struct element *elem; | 
|  | 1608 | u8 mbssid_idx = 0; | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1609 | u8 ml_ie_len; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1610 | const struct ieee80211_eht_ml *eht_ml; | 
|  | 1611 | const struct eht_ml_basic_common_info *ml_basic_common_info; | 
|  | 1612 | u8 i, link_id; | 
|  | 1613 | const u16 control_mask = | 
|  | 1614 | MULTI_LINK_CONTROL_TYPE_MASK | | 
|  | 1615 | BASIC_MULTI_LINK_CTRL_PRES_LINK_ID | | 
|  | 1616 | BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT | | 
|  | 1617 | BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA; | 
|  | 1618 | const u16 control = | 
|  | 1619 | MULTI_LINK_CONTROL_TYPE_BASIC | | 
|  | 1620 | BASIC_MULTI_LINK_CTRL_PRES_LINK_ID | | 
|  | 1621 | BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT | | 
|  | 1622 | BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA; | 
|  | 1623 | u16 missing = 0; | 
|  | 1624 | u16 seen; | 
|  | 1625 | const u8 *ies_pos = wpa_bss_ie_ptr(bss); | 
|  | 1626 | size_t ies_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len; | 
|  | 1627 | int ret = -1; | 
|  | 1628 | struct mld_link *l; | 
|  | 1629 |  | 
|  | 1630 | if (ieee802_11_parse_elems(ies_pos, ies_len, &elems, 1) == | 
|  | 1631 | ParseFailed) { | 
|  | 1632 | wpa_dbg(wpa_s, MSG_DEBUG, "MLD: Failed to parse elements"); | 
|  | 1633 | return ret; | 
|  | 1634 | } | 
|  | 1635 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1636 | mlbuf = ieee802_11_defrag_mle(&elems, MULTI_LINK_CONTROL_TYPE_BASIC); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1637 | if (!mlbuf) { | 
|  | 1638 | wpa_dbg(wpa_s, MSG_DEBUG, "MLD: No Multi-Link element"); | 
|  | 1639 | return ret; | 
|  | 1640 | } | 
|  | 1641 |  | 
|  | 1642 | ml_ie_len = wpabuf_len(mlbuf); | 
|  | 1643 |  | 
|  | 1644 | /* | 
|  | 1645 | * for ext ID + 2 control + common info len + MLD address + | 
|  | 1646 | * link info | 
|  | 1647 | */ | 
|  | 1648 | if (ml_ie_len < 2UL + 1UL + ETH_ALEN + 1UL) | 
|  | 1649 | goto out; | 
|  | 1650 |  | 
|  | 1651 | eht_ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf); | 
|  | 1652 | if ((le_to_host16(eht_ml->ml_control) & control_mask) != control) { | 
|  | 1653 | wpa_printf(MSG_DEBUG, | 
|  | 1654 | "MLD: Unexpected Multi-Link element control=0x%x (mask 0x%x expected 0x%x)", | 
|  | 1655 | le_to_host16(eht_ml->ml_control), control_mask, | 
|  | 1656 | control); | 
|  | 1657 | goto out; | 
|  | 1658 | } | 
|  | 1659 |  | 
|  | 1660 | ml_basic_common_info = | 
|  | 1661 | (const struct eht_ml_basic_common_info *) eht_ml->variable; | 
|  | 1662 |  | 
|  | 1663 | /* Common info length should be valid */ | 
|  | 1664 | if (ml_basic_common_info->len < ETH_ALEN + 1UL) | 
|  | 1665 | goto out; | 
|  | 1666 |  | 
|  | 1667 | /* Get the MLD address and MLD link ID */ | 
|  | 1668 | if (ap_mld_addr) | 
|  | 1669 | os_memcpy(ap_mld_addr, ml_basic_common_info->mld_addr, | 
|  | 1670 | ETH_ALEN); | 
|  | 1671 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1672 | bss->n_mld_links = 0; | 
|  | 1673 | l = &bss->mld_links[bss->n_mld_links]; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1674 | link_id = ml_basic_common_info->variable[0] & EHT_ML_LINK_ID_MSK; | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1675 | l->link_id = link_id; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1676 | os_memcpy(l->bssid, bss->bssid, ETH_ALEN); | 
|  | 1677 | l->freq = bss->freq; | 
|  | 1678 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1679 | seen = BIT(link_id); | 
|  | 1680 | bss->n_mld_links++; | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1681 |  | 
|  | 1682 | /* | 
|  | 1683 | * The AP MLD ID in the RNR corresponds to the MBSSID index, see | 
|  | 1684 | * IEEE P802.11be/D4.0, 9.4.2.169.2 (Neighbor AP Information field). | 
|  | 1685 | * | 
|  | 1686 | * For the transmitting BSSID it is clear that both the MBSSID index | 
|  | 1687 | * and the AP MLD ID in the RNR are zero. | 
|  | 1688 | * | 
|  | 1689 | * For nontransmitted BSSIDs we will have a BSS generated from the | 
|  | 1690 | * MBSSID element(s) using inheritance rules. Included in the elements | 
|  | 1691 | * is the MBSSID Index Element. The RNR is copied from the Beacon/Probe | 
|  | 1692 | * Response frame that was send by the transmitting BSSID. As such, the | 
|  | 1693 | * reported AP MLD ID in the RNR will match the value in the MBSSID | 
|  | 1694 | * Index Element. | 
|  | 1695 | */ | 
|  | 1696 | elem = (const struct element *) | 
|  | 1697 | wpa_bss_get_ie(bss, WLAN_EID_MULTIPLE_BSSID_INDEX); | 
|  | 1698 | if (elem && elem->datalen >= 1) | 
|  | 1699 | mbssid_idx = elem->data[0]; | 
|  | 1700 |  | 
|  | 1701 | for_each_element_id(elem, WLAN_EID_REDUCED_NEIGHBOR_REPORT, | 
|  | 1702 | wpa_bss_ie_ptr(bss), | 
|  | 1703 | bss->ie_len ? bss->ie_len : bss->beacon_ie_len) { | 
|  | 1704 | const struct ieee80211_neighbor_ap_info *ap_info; | 
|  | 1705 | const u8 *pos = elem->data; | 
|  | 1706 | size_t len = elem->datalen; | 
|  | 1707 |  | 
|  | 1708 | /* RNR IE may contain more than one Neighbor AP Info */ | 
|  | 1709 | while (sizeof(*ap_info) <= len) { | 
|  | 1710 | size_t ap_info_len = sizeof(*ap_info); | 
|  | 1711 | u8 count; | 
|  | 1712 |  | 
|  | 1713 | ap_info = (const struct ieee80211_neighbor_ap_info *) | 
|  | 1714 | pos; | 
|  | 1715 | count = RNR_TBTT_INFO_COUNT_VAL(ap_info->tbtt_info_hdr) + 1; | 
|  | 1716 | ap_info_len += count * ap_info->tbtt_info_len; | 
|  | 1717 |  | 
|  | 1718 | if (ap_info_len > len) | 
|  | 1719 | goto out; | 
|  | 1720 |  | 
|  | 1721 | wpa_bss_parse_ml_rnr_ap_info(wpa_s, bss, mbssid_idx, | 
|  | 1722 | ap_info, len, &seen, | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1723 | &missing); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1724 |  | 
|  | 1725 | pos += ap_info_len; | 
|  | 1726 | len -= ap_info_len; | 
|  | 1727 | } | 
|  | 1728 | } | 
|  | 1729 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1730 | wpa_printf(MSG_DEBUG, "MLD: n_mld_links=%u (unresolved: 0x%04hx)", | 
|  | 1731 | bss->n_mld_links, missing); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1732 |  | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1733 | for (i = 0; i < bss->n_mld_links; i++) { | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1734 | wpa_printf(MSG_DEBUG, "MLD: link=%u, bssid=" MACSTR, | 
| Sunil Ravi | 8861141 | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1735 | bss->mld_links[i].link_id, | 
|  | 1736 | MAC2STR(bss->mld_links[i].bssid)); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1737 | } | 
|  | 1738 |  | 
|  | 1739 | if (missing_links) | 
|  | 1740 | *missing_links = missing; | 
|  | 1741 |  | 
|  | 1742 | ret = 0; | 
|  | 1743 | out: | 
|  | 1744 | wpabuf_free(mlbuf); | 
|  | 1745 | return ret; | 
|  | 1746 | } | 
|  | 1747 |  | 
|  | 1748 |  | 
|  | 1749 | /* | 
|  | 1750 | * wpa_bss_parse_reconf_ml_element - Parse the Reconfiguration ML element | 
|  | 1751 | * @wpa_s: Pointer to wpa_supplicant data | 
|  | 1752 | * @bss: BSS table entry | 
|  | 1753 | * Returns: The bitmap of links that are going to be removed | 
|  | 1754 | */ | 
|  | 1755 | u16 wpa_bss_parse_reconf_ml_element(struct wpa_supplicant *wpa_s, | 
|  | 1756 | struct wpa_bss *bss) | 
|  | 1757 | { | 
|  | 1758 | struct ieee802_11_elems elems; | 
|  | 1759 | struct wpabuf *mlbuf; | 
|  | 1760 | const u8 *pos = wpa_bss_ie_ptr(bss); | 
|  | 1761 | size_t len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len; | 
|  | 1762 | const struct ieee80211_eht_ml *ml; | 
|  | 1763 | u16 removed_links = 0; | 
|  | 1764 | u8 ml_common_len; | 
|  | 1765 |  | 
|  | 1766 | if (ieee802_11_parse_elems(pos, len, &elems, 1) == ParseFailed) | 
|  | 1767 | return 0; | 
|  | 1768 |  | 
|  | 1769 | if (!elems.reconf_mle || !elems.reconf_mle_len) | 
|  | 1770 | return 0; | 
|  | 1771 |  | 
| Sunil Ravi | eb83e2a | 2024-06-28 17:34:56 +0000 | [diff] [blame] | 1772 | mlbuf = ieee802_11_defrag_mle(&elems, MULTI_LINK_CONTROL_TYPE_RECONF); | 
| Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 1773 | if (!mlbuf) | 
|  | 1774 | return 0; | 
|  | 1775 |  | 
|  | 1776 | ml = (const struct ieee80211_eht_ml *) wpabuf_head(mlbuf); | 
|  | 1777 | len = wpabuf_len(mlbuf); | 
|  | 1778 |  | 
|  | 1779 | if (len < sizeof(*ml)) | 
|  | 1780 | goto out; | 
|  | 1781 |  | 
|  | 1782 | ml_common_len = 1; | 
|  | 1783 | if (ml->ml_control & RECONF_MULTI_LINK_CTRL_PRES_MLD_MAC_ADDR) | 
|  | 1784 | ml_common_len += ETH_ALEN; | 
|  | 1785 |  | 
|  | 1786 | if (len < sizeof(*ml) + ml_common_len) { | 
|  | 1787 | wpa_printf(MSG_DEBUG, | 
|  | 1788 | "MLD: Unexpected Reconfiguration ML element length: (%zu < %zu)", | 
|  | 1789 | len, sizeof(*ml) + ml_common_len); | 
|  | 1790 | goto out; | 
|  | 1791 | } | 
|  | 1792 |  | 
|  | 1793 | pos = ml->variable + ml_common_len; | 
|  | 1794 | len -= sizeof(*ml) + ml_common_len; | 
|  | 1795 |  | 
|  | 1796 | while (len >= 2 + sizeof(struct ieee80211_eht_per_sta_profile)) { | 
|  | 1797 | size_t sub_elem_len = *(pos + 1); | 
|  | 1798 |  | 
|  | 1799 | if (2 + sub_elem_len > len) { | 
|  | 1800 | wpa_printf(MSG_DEBUG, | 
|  | 1801 | "MLD: Invalid link info len: %zu %zu", | 
|  | 1802 | 2 + sub_elem_len, len); | 
|  | 1803 | goto out; | 
|  | 1804 | } | 
|  | 1805 |  | 
|  | 1806 | if  (*pos == EHT_ML_SUB_ELEM_PER_STA_PROFILE) { | 
|  | 1807 | const struct ieee80211_eht_per_sta_profile *sta_prof = | 
|  | 1808 | (const struct ieee80211_eht_per_sta_profile *) | 
|  | 1809 | (pos + 2); | 
|  | 1810 | u16 control = le_to_host16(sta_prof->sta_control); | 
|  | 1811 | u8 link_id; | 
|  | 1812 |  | 
|  | 1813 | link_id = control & EHT_PER_STA_RECONF_CTRL_LINK_ID_MSK; | 
|  | 1814 | removed_links |= BIT(link_id); | 
|  | 1815 | } | 
|  | 1816 |  | 
|  | 1817 | pos += 2 + sub_elem_len; | 
|  | 1818 | len -= 2 + sub_elem_len; | 
|  | 1819 | } | 
|  | 1820 |  | 
|  | 1821 | wpa_printf(MSG_DEBUG, "MLD: Reconfiguration: removed_links=0x%x", | 
|  | 1822 | removed_links); | 
|  | 1823 | out: | 
|  | 1824 | wpabuf_free(mlbuf); | 
|  | 1825 | return removed_links; | 
|  | 1826 | } |