Do not mark BSS entry in use if SSID has changed
[mech_eap.git] / wpa_supplicant / bss.c
1 /*
2  * BSS table
3  * Copyright (c) 2009-2015, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "drivers/driver.h"
15 #include "wpa_supplicant_i.h"
16 #include "config.h"
17 #include "notify.h"
18 #include "scan.h"
19 #include "bss.h"
20
21
22 #define WPA_BSS_FREQ_CHANGED_FLAG       BIT(0)
23 #define WPA_BSS_SIGNAL_CHANGED_FLAG     BIT(1)
24 #define WPA_BSS_PRIVACY_CHANGED_FLAG    BIT(2)
25 #define WPA_BSS_MODE_CHANGED_FLAG       BIT(3)
26 #define WPA_BSS_WPAIE_CHANGED_FLAG      BIT(4)
27 #define WPA_BSS_RSNIE_CHANGED_FLAG      BIT(5)
28 #define WPA_BSS_WPS_CHANGED_FLAG        BIT(6)
29 #define WPA_BSS_RATES_CHANGED_FLAG      BIT(7)
30 #define WPA_BSS_IES_CHANGED_FLAG        BIT(8)
31
32
33 static void wpa_bss_set_hessid(struct wpa_bss *bss)
34 {
35 #ifdef CONFIG_INTERWORKING
36         const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
37         if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
38                 os_memset(bss->hessid, 0, ETH_ALEN);
39                 return;
40         }
41         if (ie[1] == 7)
42                 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
43         else
44                 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
45 #endif /* CONFIG_INTERWORKING */
46 }
47
48
49 /**
50  * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
51  * Returns: Allocated ANQP data structure or %NULL on failure
52  *
53  * The allocated ANQP data structure has its users count set to 1. It may be
54  * shared by multiple BSS entries and each shared entry is freed with
55  * wpa_bss_anqp_free().
56  */
57 struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
58 {
59         struct wpa_bss_anqp *anqp;
60         anqp = os_zalloc(sizeof(*anqp));
61         if (anqp == NULL)
62                 return NULL;
63         anqp->users = 1;
64         return anqp;
65 }
66
67
68 /**
69  * wpa_bss_anqp_clone - Clone an ANQP data structure
70  * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
71  * Returns: Cloned ANQP data structure or %NULL on failure
72  */
73 static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
74 {
75         struct wpa_bss_anqp *n;
76
77         n = os_zalloc(sizeof(*n));
78         if (n == NULL)
79                 return NULL;
80
81 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
82 #ifdef CONFIG_INTERWORKING
83         ANQP_DUP(capability_list);
84         ANQP_DUP(venue_name);
85         ANQP_DUP(network_auth_type);
86         ANQP_DUP(roaming_consortium);
87         ANQP_DUP(ip_addr_type_availability);
88         ANQP_DUP(nai_realm);
89         ANQP_DUP(anqp_3gpp);
90         ANQP_DUP(domain_name);
91 #endif /* CONFIG_INTERWORKING */
92 #ifdef CONFIG_HS20
93         ANQP_DUP(hs20_capability_list);
94         ANQP_DUP(hs20_operator_friendly_name);
95         ANQP_DUP(hs20_wan_metrics);
96         ANQP_DUP(hs20_connection_capability);
97         ANQP_DUP(hs20_operating_class);
98         ANQP_DUP(hs20_osu_providers_list);
99 #endif /* CONFIG_HS20 */
100 #undef ANQP_DUP
101
102         return n;
103 }
104
105
106 /**
107  * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
108  * @bss: BSS entry
109  * Returns: 0 on success, -1 on failure
110  *
111  * This function ensures the specific BSS entry has an ANQP data structure that
112  * is not shared with any other BSS entry.
113  */
114 int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
115 {
116         struct wpa_bss_anqp *anqp;
117
118         if (bss->anqp && bss->anqp->users > 1) {
119                 /* allocated, but shared - clone an unshared copy */
120                 anqp = wpa_bss_anqp_clone(bss->anqp);
121                 if (anqp == NULL)
122                         return -1;
123                 anqp->users = 1;
124                 bss->anqp->users--;
125                 bss->anqp = anqp;
126                 return 0;
127         }
128
129         if (bss->anqp)
130                 return 0; /* already allocated and not shared */
131
132         /* not allocated - allocate a new storage area */
133         bss->anqp = wpa_bss_anqp_alloc();
134         return bss->anqp ? 0 : -1;
135 }
136
137
138 /**
139  * wpa_bss_anqp_free - Free an ANQP data structure
140  * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
141  */
142 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
143 {
144         if (anqp == NULL)
145                 return;
146
147         anqp->users--;
148         if (anqp->users > 0) {
149                 /* Another BSS entry holds a pointer to this ANQP info */
150                 return;
151         }
152
153 #ifdef CONFIG_INTERWORKING
154         wpabuf_free(anqp->capability_list);
155         wpabuf_free(anqp->venue_name);
156         wpabuf_free(anqp->network_auth_type);
157         wpabuf_free(anqp->roaming_consortium);
158         wpabuf_free(anqp->ip_addr_type_availability);
159         wpabuf_free(anqp->nai_realm);
160         wpabuf_free(anqp->anqp_3gpp);
161         wpabuf_free(anqp->domain_name);
162 #endif /* CONFIG_INTERWORKING */
163 #ifdef CONFIG_HS20
164         wpabuf_free(anqp->hs20_capability_list);
165         wpabuf_free(anqp->hs20_operator_friendly_name);
166         wpabuf_free(anqp->hs20_wan_metrics);
167         wpabuf_free(anqp->hs20_connection_capability);
168         wpabuf_free(anqp->hs20_operating_class);
169         wpabuf_free(anqp->hs20_osu_providers_list);
170 #endif /* CONFIG_HS20 */
171
172         os_free(anqp);
173 }
174
175
176 static void wpa_bss_update_pending_connect(struct wpa_supplicant *wpa_s,
177                                            struct wpa_bss *old_bss,
178                                            struct wpa_bss *new_bss)
179 {
180         struct wpa_radio_work *work;
181         struct wpa_connect_work *cwork;
182
183         work = radio_work_pending(wpa_s, "sme-connect");
184         if (!work)
185                 work = radio_work_pending(wpa_s, "connect");
186         if (!work)
187                 return;
188
189         cwork = work->ctx;
190         if (cwork->bss != old_bss)
191                 return;
192
193         wpa_printf(MSG_DEBUG,
194                    "Update BSS pointer for the pending connect radio work");
195         cwork->bss = new_bss;
196         if (!new_bss)
197                 cwork->bss_removed = 1;
198 }
199
200
201 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
202                            const char *reason)
203 {
204         if (wpa_s->last_scan_res) {
205                 unsigned int i;
206                 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
207                         if (wpa_s->last_scan_res[i] == bss) {
208                                 os_memmove(&wpa_s->last_scan_res[i],
209                                            &wpa_s->last_scan_res[i + 1],
210                                            (wpa_s->last_scan_res_used - i - 1)
211                                            * sizeof(struct wpa_bss *));
212                                 wpa_s->last_scan_res_used--;
213                                 break;
214                         }
215                 }
216         }
217         wpa_bss_update_pending_connect(wpa_s, bss, NULL);
218         dl_list_del(&bss->list);
219         dl_list_del(&bss->list_id);
220         wpa_s->num_bss--;
221         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
222                 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
223                 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
224         wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
225         wpa_bss_anqp_free(bss->anqp);
226         os_free(bss);
227 }
228
229
230 /**
231  * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
232  * @wpa_s: Pointer to wpa_supplicant data
233  * @bssid: BSSID
234  * @ssid: SSID
235  * @ssid_len: Length of @ssid
236  * Returns: Pointer to the BSS entry or %NULL if not found
237  */
238 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
239                              const u8 *ssid, size_t ssid_len)
240 {
241         struct wpa_bss *bss;
242         if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
243                 return NULL;
244         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
245                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
246                     bss->ssid_len == ssid_len &&
247                     os_memcmp(bss->ssid, ssid, ssid_len) == 0)
248                         return bss;
249         }
250         return NULL;
251 }
252
253
254 static void calculate_update_time(const struct os_reltime *fetch_time,
255                                   unsigned int age_ms,
256                                   struct os_reltime *update_time)
257 {
258         os_time_t usec;
259
260         update_time->sec = fetch_time->sec;
261         update_time->usec = fetch_time->usec;
262         update_time->sec -= age_ms / 1000;
263         usec = (age_ms % 1000) * 1000;
264         if (update_time->usec < usec) {
265                 update_time->sec--;
266                 update_time->usec += 1000000;
267         }
268         update_time->usec -= usec;
269 }
270
271
272 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
273                              struct os_reltime *fetch_time)
274 {
275         dst->flags = src->flags;
276         os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
277         dst->freq = src->freq;
278         dst->beacon_int = src->beacon_int;
279         dst->caps = src->caps;
280         dst->qual = src->qual;
281         dst->noise = src->noise;
282         dst->level = src->level;
283         dst->tsf = src->tsf;
284         dst->est_throughput = src->est_throughput;
285         dst->snr = src->snr;
286
287         calculate_update_time(fetch_time, src->age, &dst->last_update);
288 }
289
290
291 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
292 {
293         struct wpa_ssid *ssid;
294
295         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
296                 if (ssid->ssid == NULL || ssid->ssid_len == 0)
297                         continue;
298                 if (ssid->ssid_len == bss->ssid_len &&
299                     os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
300                         return 1;
301         }
302
303         return 0;
304 }
305
306
307 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
308 {
309         if (bss == wpa_s->current_bss)
310                 return 1;
311
312         if (wpa_s->current_bss &&
313             (bss->ssid_len != wpa_s->current_bss->ssid_len ||
314              os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
315                        bss->ssid_len) != 0))
316                 return 0; /* SSID has changed */
317
318         return !is_zero_ether_addr(bss->bssid) &&
319                 (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
320                  os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
321 }
322
323
324 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
325 {
326         struct wpa_bss *bss;
327
328         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
329                 if (!wpa_bss_known(wpa_s, bss)) {
330                         wpa_bss_remove(wpa_s, bss, __func__);
331                         return 0;
332                 }
333         }
334
335         return -1;
336 }
337
338
339 static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
340 {
341         struct wpa_bss *bss;
342
343         /*
344          * Remove the oldest entry that does not match with any configured
345          * network.
346          */
347         if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
348                 return 0;
349
350         /*
351          * Remove the oldest entry that isn't currently in use.
352          */
353         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
354                 if (!wpa_bss_in_use(wpa_s, bss)) {
355                         wpa_bss_remove(wpa_s, bss, __func__);
356                         return 0;
357                 }
358         }
359
360         return -1;
361 }
362
363
364 static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
365                                     const u8 *ssid, size_t ssid_len,
366                                     struct wpa_scan_res *res,
367                                     struct os_reltime *fetch_time)
368 {
369         struct wpa_bss *bss;
370
371         bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
372         if (bss == NULL)
373                 return NULL;
374         bss->id = wpa_s->bss_next_id++;
375         bss->last_update_idx = wpa_s->bss_update_idx;
376         wpa_bss_copy_res(bss, res, fetch_time);
377         os_memcpy(bss->ssid, ssid, ssid_len);
378         bss->ssid_len = ssid_len;
379         bss->ie_len = res->ie_len;
380         bss->beacon_ie_len = res->beacon_ie_len;
381         os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
382         wpa_bss_set_hessid(bss);
383
384         if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
385             wpa_bss_remove_oldest(wpa_s) != 0) {
386                 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
387                            "because all BSSes are in use. We should normally "
388                            "not get here!", (int) wpa_s->num_bss + 1);
389                 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
390         }
391
392         dl_list_add_tail(&wpa_s->bss, &bss->list);
393         dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
394         wpa_s->num_bss++;
395         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
396                 " SSID '%s'",
397                 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
398         wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
399         return bss;
400 }
401
402
403 static int are_ies_equal(const struct wpa_bss *old,
404                          const struct wpa_scan_res *new_res, u32 ie)
405 {
406         const u8 *old_ie, *new_ie;
407         struct wpabuf *old_ie_buff = NULL;
408         struct wpabuf *new_ie_buff = NULL;
409         int new_ie_len, old_ie_len, ret, is_multi;
410
411         switch (ie) {
412         case WPA_IE_VENDOR_TYPE:
413                 old_ie = wpa_bss_get_vendor_ie(old, ie);
414                 new_ie = wpa_scan_get_vendor_ie(new_res, ie);
415                 is_multi = 0;
416                 break;
417         case WPS_IE_VENDOR_TYPE:
418                 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
419                 new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
420                 is_multi = 1;
421                 break;
422         case WLAN_EID_RSN:
423         case WLAN_EID_SUPP_RATES:
424         case WLAN_EID_EXT_SUPP_RATES:
425                 old_ie = wpa_bss_get_ie(old, ie);
426                 new_ie = wpa_scan_get_ie(new_res, ie);
427                 is_multi = 0;
428                 break;
429         default:
430                 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
431                 return 0;
432         }
433
434         if (is_multi) {
435                 /* in case of multiple IEs stored in buffer */
436                 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
437                 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
438                 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
439                 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
440         } else {
441                 /* in case of single IE */
442                 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
443                 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
444         }
445
446         if (!old_ie || !new_ie)
447                 ret = !old_ie && !new_ie;
448         else
449                 ret = (old_ie_len == new_ie_len &&
450                        os_memcmp(old_ie, new_ie, old_ie_len) == 0);
451
452         wpabuf_free(old_ie_buff);
453         wpabuf_free(new_ie_buff);
454
455         return ret;
456 }
457
458
459 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
460                                const struct wpa_scan_res *new_res)
461 {
462         u32 changes = 0;
463         int caps_diff = old->caps ^ new_res->caps;
464
465         if (old->freq != new_res->freq)
466                 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
467
468         if (old->level != new_res->level)
469                 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
470
471         if (caps_diff & IEEE80211_CAP_PRIVACY)
472                 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
473
474         if (caps_diff & IEEE80211_CAP_IBSS)
475                 changes |= WPA_BSS_MODE_CHANGED_FLAG;
476
477         if (old->ie_len == new_res->ie_len &&
478             os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
479                 return changes;
480         changes |= WPA_BSS_IES_CHANGED_FLAG;
481
482         if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
483                 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
484
485         if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
486                 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
487
488         if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
489                 changes |= WPA_BSS_WPS_CHANGED_FLAG;
490
491         if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
492             !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
493                 changes |= WPA_BSS_RATES_CHANGED_FLAG;
494
495         return changes;
496 }
497
498
499 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
500                                const struct wpa_bss *bss)
501 {
502         if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
503                 wpas_notify_bss_freq_changed(wpa_s, bss->id);
504
505         if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
506                 wpas_notify_bss_signal_changed(wpa_s, bss->id);
507
508         if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
509                 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
510
511         if (changes & WPA_BSS_MODE_CHANGED_FLAG)
512                 wpas_notify_bss_mode_changed(wpa_s, bss->id);
513
514         if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
515                 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
516
517         if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
518                 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
519
520         if (changes & WPA_BSS_WPS_CHANGED_FLAG)
521                 wpas_notify_bss_wps_changed(wpa_s, bss->id);
522
523         if (changes & WPA_BSS_IES_CHANGED_FLAG)
524                 wpas_notify_bss_ies_changed(wpa_s, bss->id);
525
526         if (changes & WPA_BSS_RATES_CHANGED_FLAG)
527                 wpas_notify_bss_rates_changed(wpa_s, bss->id);
528
529         wpas_notify_bss_seen(wpa_s, bss->id);
530 }
531
532
533 static struct wpa_bss *
534 wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
535                struct wpa_scan_res *res, struct os_reltime *fetch_time)
536 {
537         u32 changes;
538
539         changes = wpa_bss_compare_res(bss, res);
540         bss->scan_miss_count = 0;
541         bss->last_update_idx = wpa_s->bss_update_idx;
542         wpa_bss_copy_res(bss, res, fetch_time);
543         /* Move the entry to the end of the list */
544         dl_list_del(&bss->list);
545 #ifdef CONFIG_P2P
546         if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
547             !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
548                 /*
549                  * This can happen when non-P2P station interface runs a scan
550                  * without P2P IE in the Probe Request frame. P2P GO would reply
551                  * to that with a Probe Response that does not include P2P IE.
552                  * Do not update the IEs in this BSS entry to avoid such loss of
553                  * information that may be needed for P2P operations to
554                  * determine group information.
555                  */
556                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
557                         MACSTR " since that would remove P2P IE information",
558                         MAC2STR(bss->bssid));
559         } else
560 #endif /* CONFIG_P2P */
561         if (bss->ie_len + bss->beacon_ie_len >=
562             res->ie_len + res->beacon_ie_len) {
563                 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
564                 bss->ie_len = res->ie_len;
565                 bss->beacon_ie_len = res->beacon_ie_len;
566         } else {
567                 struct wpa_bss *nbss;
568                 struct dl_list *prev = bss->list_id.prev;
569                 dl_list_del(&bss->list_id);
570                 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
571                                   res->beacon_ie_len);
572                 if (nbss) {
573                         unsigned int i;
574                         for (i = 0; i < wpa_s->last_scan_res_used; i++) {
575                                 if (wpa_s->last_scan_res[i] == bss) {
576                                         wpa_s->last_scan_res[i] = nbss;
577                                         break;
578                                 }
579                         }
580                         if (wpa_s->current_bss == bss)
581                                 wpa_s->current_bss = nbss;
582                         wpa_bss_update_pending_connect(wpa_s, bss, nbss);
583                         bss = nbss;
584                         os_memcpy(bss + 1, res + 1,
585                                   res->ie_len + res->beacon_ie_len);
586                         bss->ie_len = res->ie_len;
587                         bss->beacon_ie_len = res->beacon_ie_len;
588                 }
589                 dl_list_add(prev, &bss->list_id);
590         }
591         if (changes & WPA_BSS_IES_CHANGED_FLAG)
592                 wpa_bss_set_hessid(bss);
593         dl_list_add_tail(&wpa_s->bss, &bss->list);
594
595         notify_bss_changes(wpa_s, changes, bss);
596
597         return bss;
598 }
599
600
601 /**
602  * wpa_bss_update_start - Start a BSS table update from scan results
603  * @wpa_s: Pointer to wpa_supplicant data
604  *
605  * This function is called at the start of each BSS table update round for new
606  * scan results. The actual scan result entries are indicated with calls to
607  * wpa_bss_update_scan_res() and the update round is finished with a call to
608  * wpa_bss_update_end().
609  */
610 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
611 {
612         wpa_s->bss_update_idx++;
613         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
614                 wpa_s->bss_update_idx);
615         wpa_s->last_scan_res_used = 0;
616 }
617
618
619 /**
620  * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
621  * @wpa_s: Pointer to wpa_supplicant data
622  * @res: Scan result
623  * @fetch_time: Time when the result was fetched from the driver
624  *
625  * This function updates a BSS table entry (or adds one) based on a scan result.
626  * This is called separately for each scan result between the calls to
627  * wpa_bss_update_start() and wpa_bss_update_end().
628  */
629 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
630                              struct wpa_scan_res *res,
631                              struct os_reltime *fetch_time)
632 {
633         const u8 *ssid, *p2p, *mesh;
634         struct wpa_bss *bss;
635
636         if (wpa_s->conf->ignore_old_scan_res) {
637                 struct os_reltime update;
638                 calculate_update_time(fetch_time, res->age, &update);
639                 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
640                         struct os_reltime age;
641                         os_reltime_sub(&wpa_s->scan_trigger_time, &update,
642                                        &age);
643                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
644                                 "table entry that is %u.%06u seconds older "
645                                 "than our scan trigger",
646                                 (unsigned int) age.sec,
647                                 (unsigned int) age.usec);
648                         return;
649                 }
650         }
651
652         ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
653         if (ssid == NULL) {
654                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
655                         MACSTR, MAC2STR(res->bssid));
656                 return;
657         }
658         if (ssid[1] > SSID_MAX_LEN) {
659                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
660                         MACSTR, MAC2STR(res->bssid));
661                 return;
662         }
663
664         p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
665 #ifdef CONFIG_P2P
666         if (p2p == NULL &&
667             wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
668                 /*
669                  * If it's a P2P specific interface, then don't update
670                  * the scan result without a P2P IE.
671                  */
672                 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
673                            " update for P2P interface", MAC2STR(res->bssid));
674                 return;
675         }
676 #endif /* CONFIG_P2P */
677         if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
678             os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
679                 return; /* Skip P2P listen discovery results here */
680
681         /* TODO: add option for ignoring BSSes we are not interested in
682          * (to save memory) */
683
684         mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
685         if (mesh && mesh[1] <= SSID_MAX_LEN)
686                 ssid = mesh;
687
688         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
689         if (bss == NULL)
690                 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
691         else {
692                 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
693                 if (wpa_s->last_scan_res) {
694                         unsigned int i;
695                         for (i = 0; i < wpa_s->last_scan_res_used; i++) {
696                                 if (bss == wpa_s->last_scan_res[i]) {
697                                         /* Already in the list */
698                                         return;
699                                 }
700                         }
701                 }
702         }
703
704         if (bss == NULL)
705                 return;
706         if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
707                 struct wpa_bss **n;
708                 unsigned int siz;
709                 if (wpa_s->last_scan_res_size == 0)
710                         siz = 32;
711                 else
712                         siz = wpa_s->last_scan_res_size * 2;
713                 n = os_realloc_array(wpa_s->last_scan_res, siz,
714                                      sizeof(struct wpa_bss *));
715                 if (n == NULL)
716                         return;
717                 wpa_s->last_scan_res = n;
718                 wpa_s->last_scan_res_size = siz;
719         }
720
721         if (wpa_s->last_scan_res)
722                 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
723 }
724
725
726 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
727                                     const struct scan_info *info)
728 {
729         int found;
730         size_t i;
731
732         if (info == NULL)
733                 return 1;
734
735         if (info->num_freqs) {
736                 found = 0;
737                 for (i = 0; i < info->num_freqs; i++) {
738                         if (bss->freq == info->freqs[i]) {
739                                 found = 1;
740                                 break;
741                         }
742                 }
743                 if (!found)
744                         return 0;
745         }
746
747         if (info->num_ssids) {
748                 found = 0;
749                 for (i = 0; i < info->num_ssids; i++) {
750                         const struct wpa_driver_scan_ssid *s = &info->ssids[i];
751                         if ((s->ssid == NULL || s->ssid_len == 0) ||
752                             (s->ssid_len == bss->ssid_len &&
753                              os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
754                              0)) {
755                                 found = 1;
756                                 break;
757                         }
758                 }
759                 if (!found)
760                         return 0;
761         }
762
763         return 1;
764 }
765
766
767 /**
768  * wpa_bss_update_end - End a BSS table update from scan results
769  * @wpa_s: Pointer to wpa_supplicant data
770  * @info: Information about scan parameters
771  * @new_scan: Whether this update round was based on a new scan
772  *
773  * This function is called at the end of each BSS table update round for new
774  * scan results. The start of the update was indicated with a call to
775  * wpa_bss_update_start().
776  */
777 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
778                         int new_scan)
779 {
780         struct wpa_bss *bss, *n;
781
782         os_get_reltime(&wpa_s->last_scan);
783         if (!new_scan)
784                 return; /* do not expire entries without new scan */
785
786         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
787                 if (wpa_bss_in_use(wpa_s, bss))
788                         continue;
789                 if (!wpa_bss_included_in_scan(bss, info))
790                         continue; /* expire only BSSes that were scanned */
791                 if (bss->last_update_idx < wpa_s->bss_update_idx)
792                         bss->scan_miss_count++;
793                 if (bss->scan_miss_count >=
794                     wpa_s->conf->bss_expiration_scan_count) {
795                         wpa_bss_remove(wpa_s, bss, "no match in scan");
796                 }
797         }
798
799         wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
800                    wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
801 }
802
803
804 /**
805  * wpa_bss_flush_by_age - Flush old BSS entries
806  * @wpa_s: Pointer to wpa_supplicant data
807  * @age: Maximum entry age in seconds
808  *
809  * Remove BSS entries that have not been updated during the last @age seconds.
810  */
811 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
812 {
813         struct wpa_bss *bss, *n;
814         struct os_reltime t;
815
816         if (dl_list_empty(&wpa_s->bss))
817                 return;
818
819         os_get_reltime(&t);
820         t.sec -= age;
821
822         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
823                 if (wpa_bss_in_use(wpa_s, bss))
824                         continue;
825
826                 if (os_reltime_before(&bss->last_update, &t)) {
827                         wpa_bss_remove(wpa_s, bss, __func__);
828                 } else
829                         break;
830         }
831 }
832
833
834 /**
835  * wpa_bss_init - Initialize BSS table
836  * @wpa_s: Pointer to wpa_supplicant data
837  * Returns: 0 on success, -1 on failure
838  *
839  * This prepares BSS table lists and timer for periodic updates. The BSS table
840  * is deinitialized with wpa_bss_deinit() once not needed anymore.
841  */
842 int wpa_bss_init(struct wpa_supplicant *wpa_s)
843 {
844         dl_list_init(&wpa_s->bss);
845         dl_list_init(&wpa_s->bss_id);
846         return 0;
847 }
848
849
850 /**
851  * wpa_bss_flush - Flush all unused BSS entries
852  * @wpa_s: Pointer to wpa_supplicant data
853  */
854 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
855 {
856         struct wpa_bss *bss, *n;
857
858         wpa_s->clear_driver_scan_cache = 1;
859
860         if (wpa_s->bss.next == NULL)
861                 return; /* BSS table not yet initialized */
862
863         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
864                 if (wpa_bss_in_use(wpa_s, bss))
865                         continue;
866                 wpa_bss_remove(wpa_s, bss, __func__);
867         }
868 }
869
870
871 /**
872  * wpa_bss_deinit - Deinitialize BSS table
873  * @wpa_s: Pointer to wpa_supplicant data
874  */
875 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
876 {
877         wpa_bss_flush(wpa_s);
878 }
879
880
881 /**
882  * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
883  * @wpa_s: Pointer to wpa_supplicant data
884  * @bssid: BSSID
885  * Returns: Pointer to the BSS entry or %NULL if not found
886  */
887 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
888                                    const u8 *bssid)
889 {
890         struct wpa_bss *bss;
891         if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
892                 return NULL;
893         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
894                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
895                         return bss;
896         }
897         return NULL;
898 }
899
900
901 /**
902  * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
903  * @wpa_s: Pointer to wpa_supplicant data
904  * @bssid: BSSID
905  * Returns: Pointer to the BSS entry or %NULL if not found
906  *
907  * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
908  * find the entry that has the most recent update. This can help in finding the
909  * correct entry in cases where the SSID of the AP may have changed recently
910  * (e.g., in WPS reconfiguration cases).
911  */
912 struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
913                                           const u8 *bssid)
914 {
915         struct wpa_bss *bss, *found = NULL;
916         if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
917                 return NULL;
918         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
919                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
920                         continue;
921                 if (found == NULL ||
922                     os_reltime_before(&found->last_update, &bss->last_update))
923                         found = bss;
924         }
925         return found;
926 }
927
928
929 #ifdef CONFIG_P2P
930 /**
931  * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
932  * @wpa_s: Pointer to wpa_supplicant data
933  * @dev_addr: P2P Device Address of the GO
934  * Returns: Pointer to the BSS entry or %NULL if not found
935  */
936 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
937                                           const u8 *dev_addr)
938 {
939         struct wpa_bss *bss;
940         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
941                 u8 addr[ETH_ALEN];
942                 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
943                                        addr) == 0 &&
944                     os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
945                         return bss;
946         }
947         return NULL;
948 }
949 #endif /* CONFIG_P2P */
950
951
952 /**
953  * wpa_bss_get_id - Fetch a BSS table entry based on identifier
954  * @wpa_s: Pointer to wpa_supplicant data
955  * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
956  * Returns: Pointer to the BSS entry or %NULL if not found
957  */
958 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
959 {
960         struct wpa_bss *bss;
961         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
962                 if (bss->id == id)
963                         return bss;
964         }
965         return NULL;
966 }
967
968
969 /**
970  * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
971  * @wpa_s: Pointer to wpa_supplicant data
972  * @idf: Smallest allowed identifier assigned for the entry
973  * @idf: Largest allowed identifier assigned for the entry
974  * Returns: Pointer to the BSS entry or %NULL if not found
975  *
976  * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
977  * smallest id value to be fetched within the specified range without the
978  * caller having to know the exact id.
979  */
980 struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
981                                       unsigned int idf, unsigned int idl)
982 {
983         struct wpa_bss *bss;
984         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
985                 if (bss->id >= idf && bss->id <= idl)
986                         return bss;
987         }
988         return NULL;
989 }
990
991
992 /**
993  * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
994  * @bss: BSS table entry
995  * @ie: Information element identitifier (WLAN_EID_*)
996  * Returns: Pointer to the information element (id field) or %NULL if not found
997  *
998  * This function returns the first matching information element in the BSS
999  * entry.
1000  */
1001 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
1002 {
1003         const u8 *end, *pos;
1004
1005         pos = (const u8 *) (bss + 1);
1006         end = pos + bss->ie_len;
1007
1008         while (pos + 1 < end) {
1009                 if (pos + 2 + pos[1] > end)
1010                         break;
1011                 if (pos[0] == ie)
1012                         return pos;
1013                 pos += 2 + pos[1];
1014         }
1015
1016         return NULL;
1017 }
1018
1019
1020 /**
1021  * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
1022  * @bss: BSS table entry
1023  * @vendor_type: Vendor type (four octets starting the IE payload)
1024  * Returns: Pointer to the information element (id field) or %NULL if not found
1025  *
1026  * This function returns the first matching information element in the BSS
1027  * entry.
1028  */
1029 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
1030 {
1031         const u8 *end, *pos;
1032
1033         pos = (const u8 *) (bss + 1);
1034         end = pos + bss->ie_len;
1035
1036         while (pos + 1 < end) {
1037                 if (pos + 2 + pos[1] > end)
1038                         break;
1039                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1040                     vendor_type == WPA_GET_BE32(&pos[2]))
1041                         return pos;
1042                 pos += 2 + pos[1];
1043         }
1044
1045         return NULL;
1046 }
1047
1048
1049 /**
1050  * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1051  * @bss: BSS table entry
1052  * @vendor_type: Vendor type (four octets starting the IE payload)
1053  * Returns: Pointer to the information element (id field) or %NULL if not found
1054  *
1055  * This function returns the first matching information element in the BSS
1056  * entry.
1057  *
1058  * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1059  * from Beacon frames instead of either Beacon or Probe Response frames.
1060  */
1061 const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1062                                         u32 vendor_type)
1063 {
1064         const u8 *end, *pos;
1065
1066         if (bss->beacon_ie_len == 0)
1067                 return NULL;
1068
1069         pos = (const u8 *) (bss + 1);
1070         pos += bss->ie_len;
1071         end = pos + bss->beacon_ie_len;
1072
1073         while (pos + 1 < end) {
1074                 if (pos + 2 + pos[1] > end)
1075                         break;
1076                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1077                     vendor_type == WPA_GET_BE32(&pos[2]))
1078                         return pos;
1079                 pos += 2 + pos[1];
1080         }
1081
1082         return NULL;
1083 }
1084
1085
1086 /**
1087  * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1088  * @bss: BSS table entry
1089  * @vendor_type: Vendor type (four octets starting the IE payload)
1090  * Returns: Pointer to the information element payload or %NULL if not found
1091  *
1092  * This function returns concatenated payload of possibly fragmented vendor
1093  * specific information elements in the BSS entry. The caller is responsible for
1094  * freeing the returned buffer.
1095  */
1096 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1097                                             u32 vendor_type)
1098 {
1099         struct wpabuf *buf;
1100         const u8 *end, *pos;
1101
1102         buf = wpabuf_alloc(bss->ie_len);
1103         if (buf == NULL)
1104                 return NULL;
1105
1106         pos = (const u8 *) (bss + 1);
1107         end = pos + bss->ie_len;
1108
1109         while (pos + 1 < end) {
1110                 if (pos + 2 + pos[1] > end)
1111                         break;
1112                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1113                     vendor_type == WPA_GET_BE32(&pos[2]))
1114                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1115                 pos += 2 + pos[1];
1116         }
1117
1118         if (wpabuf_len(buf) == 0) {
1119                 wpabuf_free(buf);
1120                 buf = NULL;
1121         }
1122
1123         return buf;
1124 }
1125
1126
1127 /**
1128  * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1129  * @bss: BSS table entry
1130  * @vendor_type: Vendor type (four octets starting the IE payload)
1131  * Returns: Pointer to the information element payload or %NULL if not found
1132  *
1133  * This function returns concatenated payload of possibly fragmented vendor
1134  * specific information elements in the BSS entry. The caller is responsible for
1135  * freeing the returned buffer.
1136  *
1137  * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1138  * from Beacon frames instead of either Beacon or Probe Response frames.
1139  */
1140 struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1141                                                    u32 vendor_type)
1142 {
1143         struct wpabuf *buf;
1144         const u8 *end, *pos;
1145
1146         buf = wpabuf_alloc(bss->beacon_ie_len);
1147         if (buf == NULL)
1148                 return NULL;
1149
1150         pos = (const u8 *) (bss + 1);
1151         pos += bss->ie_len;
1152         end = pos + bss->beacon_ie_len;
1153
1154         while (pos + 1 < end) {
1155                 if (pos + 2 + pos[1] > end)
1156                         break;
1157                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1158                     vendor_type == WPA_GET_BE32(&pos[2]))
1159                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1160                 pos += 2 + pos[1];
1161         }
1162
1163         if (wpabuf_len(buf) == 0) {
1164                 wpabuf_free(buf);
1165                 buf = NULL;
1166         }
1167
1168         return buf;
1169 }
1170
1171
1172 /**
1173  * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1174  * @bss: BSS table entry
1175  * Returns: Maximum legacy rate in units of 500 kbps
1176  */
1177 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1178 {
1179         int rate = 0;
1180         const u8 *ie;
1181         int i;
1182
1183         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1184         for (i = 0; ie && i < ie[1]; i++) {
1185                 if ((ie[i + 2] & 0x7f) > rate)
1186                         rate = ie[i + 2] & 0x7f;
1187         }
1188
1189         ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1190         for (i = 0; ie && i < ie[1]; i++) {
1191                 if ((ie[i + 2] & 0x7f) > rate)
1192                         rate = ie[i + 2] & 0x7f;
1193         }
1194
1195         return rate;
1196 }
1197
1198
1199 /**
1200  * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1201  * @bss: BSS table entry
1202  * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1203  * Returns: number of legacy TX rates or -1 on failure
1204  *
1205  * The caller is responsible for freeing the returned buffer with os_free() in
1206  * case of success.
1207  */
1208 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1209 {
1210         const u8 *ie, *ie2;
1211         int i, j;
1212         unsigned int len;
1213         u8 *r;
1214
1215         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1216         ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1217
1218         len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1219
1220         r = os_malloc(len);
1221         if (!r)
1222                 return -1;
1223
1224         for (i = 0; ie && i < ie[1]; i++)
1225                 r[i] = ie[i + 2] & 0x7f;
1226
1227         for (j = 0; ie2 && j < ie2[1]; j++)
1228                 r[i + j] = ie2[j + 2] & 0x7f;
1229
1230         *rates = r;
1231         return len;
1232 }