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