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