21d63221b3f833726bd358d7f158695c72cd46e9
[mech_eap.git] / wpa_supplicant / bss.c
1 /*
2  * BSS table
3  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "drivers/driver.h"
21 #include "wpa_supplicant_i.h"
22 #include "config.h"
23 #include "notify.h"
24 #include "scan.h"
25 #include "bss.h"
26
27
28 /**
29  * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
30  */
31 #define WPA_BSS_EXPIRATION_PERIOD 10
32
33 #define WPA_BSS_FREQ_CHANGED_FLAG       BIT(0)
34 #define WPA_BSS_SIGNAL_CHANGED_FLAG     BIT(1)
35 #define WPA_BSS_PRIVACY_CHANGED_FLAG    BIT(2)
36 #define WPA_BSS_MODE_CHANGED_FLAG       BIT(3)
37 #define WPA_BSS_WPAIE_CHANGED_FLAG      BIT(4)
38 #define WPA_BSS_RSNIE_CHANGED_FLAG      BIT(5)
39 #define WPA_BSS_WPS_CHANGED_FLAG        BIT(6)
40 #define WPA_BSS_RATES_CHANGED_FLAG      BIT(7)
41 #define WPA_BSS_IES_CHANGED_FLAG        BIT(8)
42
43
44 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
45 {
46         dl_list_del(&bss->list);
47         dl_list_del(&bss->list_id);
48         wpa_s->num_bss--;
49         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
50                 " SSID '%s'", bss->id, MAC2STR(bss->bssid),
51                 wpa_ssid_txt(bss->ssid, bss->ssid_len));
52         wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
53         os_free(bss);
54 }
55
56
57 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
58                              const u8 *ssid, size_t ssid_len)
59 {
60         struct wpa_bss *bss;
61         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
62                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
63                     bss->ssid_len == ssid_len &&
64                     os_memcmp(bss->ssid, ssid, ssid_len) == 0)
65                         return bss;
66         }
67         return NULL;
68 }
69
70
71 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
72 {
73         os_time_t usec;
74
75         dst->flags = src->flags;
76         os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
77         dst->freq = src->freq;
78         dst->beacon_int = src->beacon_int;
79         dst->caps = src->caps;
80         dst->qual = src->qual;
81         dst->noise = src->noise;
82         dst->level = src->level;
83         dst->tsf = src->tsf;
84
85         os_get_time(&dst->last_update);
86         dst->last_update.sec -= src->age / 1000;
87         usec = (src->age % 1000) * 1000;
88         if (dst->last_update.usec < usec) {
89                 dst->last_update.sec--;
90                 dst->last_update.usec += 1000000;
91         }
92         dst->last_update.usec -= usec;
93 }
94
95
96 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
97 {
98         struct wpa_ssid *ssid;
99
100         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
101                 if (ssid->ssid == NULL || ssid->ssid_len == 0)
102                         continue;
103                 if (ssid->ssid_len == bss->ssid_len &&
104                     os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
105                         return 1;
106         }
107
108         return 0;
109 }
110
111
112 static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
113 {
114         struct wpa_bss *bss;
115
116         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
117                 if (!wpa_bss_known(wpa_s, bss)) {
118                         wpa_bss_remove(wpa_s, bss);
119                         return 0;
120                 }
121         }
122
123         return -1;
124 }
125
126
127 static void wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
128 {
129         /*
130          * Remove the oldest entry that does not match with any configured
131          * network.
132          */
133         if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
134                 return;
135
136         /*
137          * Remove the oldest entry since no better candidate for removal was
138          * found.
139          */
140         wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
141                                             struct wpa_bss, list));
142 }
143
144
145 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
146                         const u8 *ssid, size_t ssid_len,
147                         struct wpa_scan_res *res)
148 {
149         struct wpa_bss *bss;
150
151         bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
152         if (bss == NULL)
153                 return;
154         bss->id = wpa_s->bss_next_id++;
155         bss->last_update_idx = wpa_s->bss_update_idx;
156         wpa_bss_copy_res(bss, res);
157         os_memcpy(bss->ssid, ssid, ssid_len);
158         bss->ssid_len = ssid_len;
159         bss->ie_len = res->ie_len;
160         bss->beacon_ie_len = res->beacon_ie_len;
161         os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
162
163         dl_list_add_tail(&wpa_s->bss, &bss->list);
164         dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
165         wpa_s->num_bss++;
166         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
167                 " SSID '%s'",
168                 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
169         wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
170         if (wpa_s->num_bss > wpa_s->conf->bss_max_count)
171                 wpa_bss_remove_oldest(wpa_s);
172 }
173
174
175 static int are_ies_equal(const struct wpa_bss *old,
176                          const struct wpa_scan_res *new, u32 ie)
177 {
178         const u8 *old_ie, *new_ie;
179         struct wpabuf *old_ie_buff = NULL;
180         struct wpabuf *new_ie_buff = NULL;
181         int new_ie_len, old_ie_len, ret, is_multi;
182
183         switch (ie) {
184         case WPA_IE_VENDOR_TYPE:
185                 old_ie = wpa_bss_get_vendor_ie(old, ie);
186                 new_ie = wpa_scan_get_vendor_ie(new, ie);
187                 is_multi = 0;
188                 break;
189         case WPS_IE_VENDOR_TYPE:
190                 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
191                 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
192                 is_multi = 1;
193                 break;
194         case WLAN_EID_RSN:
195         case WLAN_EID_SUPP_RATES:
196         case WLAN_EID_EXT_SUPP_RATES:
197                 old_ie = wpa_bss_get_ie(old, ie);
198                 new_ie = wpa_scan_get_ie(new, ie);
199                 is_multi = 0;
200                 break;
201         default:
202                 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
203                 return 0;
204         }
205
206         if (is_multi) {
207                 /* in case of multiple IEs stored in buffer */
208                 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
209                 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
210                 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
211                 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
212         } else {
213                 /* in case of single IE */
214                 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
215                 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
216         }
217
218         if (!old_ie || !new_ie)
219                 ret = !old_ie && !new_ie;
220         else
221                 ret = (old_ie_len == new_ie_len &&
222                        os_memcmp(old_ie, new_ie, old_ie_len) == 0);
223
224         wpabuf_free(old_ie_buff);
225         wpabuf_free(new_ie_buff);
226
227         return ret;
228 }
229
230
231 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
232                                const struct wpa_scan_res *new)
233 {
234         u32 changes = 0;
235         int caps_diff = old->caps ^ new->caps;
236
237         if (old->freq != new->freq)
238                 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
239
240         if (old->level != new->level)
241                 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
242
243         if (caps_diff & IEEE80211_CAP_PRIVACY)
244                 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
245
246         if (caps_diff & IEEE80211_CAP_IBSS)
247                 changes |= WPA_BSS_MODE_CHANGED_FLAG;
248
249         if (old->ie_len == new->ie_len &&
250             os_memcmp(old + 1, new + 1, old->ie_len) == 0)
251                 return changes;
252         changes |= WPA_BSS_IES_CHANGED_FLAG;
253
254         if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
255                 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
256
257         if (!are_ies_equal(old, new, WLAN_EID_RSN))
258                 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
259
260         if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
261                 changes |= WPA_BSS_WPS_CHANGED_FLAG;
262
263         if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
264             !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
265                 changes |= WPA_BSS_RATES_CHANGED_FLAG;
266
267         return changes;
268 }
269
270
271 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
272                                const struct wpa_bss *bss)
273 {
274         if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
275                 wpas_notify_bss_freq_changed(wpa_s, bss->id);
276
277         if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
278                 wpas_notify_bss_signal_changed(wpa_s, bss->id);
279
280         if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
281                 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
282
283         if (changes & WPA_BSS_MODE_CHANGED_FLAG)
284                 wpas_notify_bss_mode_changed(wpa_s, bss->id);
285
286         if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
287                 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
288
289         if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
290                 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
291
292         if (changes & WPA_BSS_WPS_CHANGED_FLAG)
293                 wpas_notify_bss_wps_changed(wpa_s, bss->id);
294
295         if (changes & WPA_BSS_IES_CHANGED_FLAG)
296                 wpas_notify_bss_ies_changed(wpa_s, bss->id);
297
298         if (changes & WPA_BSS_RATES_CHANGED_FLAG)
299                 wpas_notify_bss_rates_changed(wpa_s, bss->id);
300 }
301
302
303 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
304                            struct wpa_scan_res *res)
305 {
306         u32 changes;
307
308         changes = wpa_bss_compare_res(bss, res);
309         bss->scan_miss_count = 0;
310         bss->last_update_idx = wpa_s->bss_update_idx;
311         wpa_bss_copy_res(bss, res);
312         /* Move the entry to the end of the list */
313         dl_list_del(&bss->list);
314         if (bss->ie_len + bss->beacon_ie_len >=
315             res->ie_len + res->beacon_ie_len) {
316                 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
317                 bss->ie_len = res->ie_len;
318                 bss->beacon_ie_len = res->beacon_ie_len;
319         } else {
320                 struct wpa_bss *nbss;
321                 struct dl_list *prev = bss->list_id.prev;
322                 dl_list_del(&bss->list_id);
323                 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
324                                   res->beacon_ie_len);
325                 if (nbss) {
326                         bss = nbss;
327                         os_memcpy(bss + 1, res + 1,
328                                   res->ie_len + res->beacon_ie_len);
329                         bss->ie_len = res->ie_len;
330                         bss->beacon_ie_len = res->beacon_ie_len;
331                 }
332                 dl_list_add(prev, &bss->list_id);
333         }
334         dl_list_add_tail(&wpa_s->bss, &bss->list);
335
336         notify_bss_changes(wpa_s, changes, bss);
337 }
338
339
340 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
341 {
342         return bss == wpa_s->current_bss ||
343                 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
344                 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
345 }
346
347
348 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
349 {
350         wpa_s->bss_update_idx++;
351         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
352                 wpa_s->bss_update_idx);
353 }
354
355
356 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
357                              struct wpa_scan_res *res)
358 {
359         const u8 *ssid, *p2p;
360         struct wpa_bss *bss;
361
362         ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
363         if (ssid == NULL) {
364                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
365                         MACSTR, MAC2STR(res->bssid));
366                 return;
367         }
368         if (ssid[1] > 32) {
369                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
370                         MACSTR, MAC2STR(res->bssid));
371                 return;
372         }
373
374         p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
375         if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
376             os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
377                 return; /* Skip P2P listen discovery results here */
378
379         /* TODO: add option for ignoring BSSes we are not interested in
380          * (to save memory) */
381         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
382         if (bss == NULL)
383                 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
384         else
385                 wpa_bss_update(wpa_s, bss, res);
386 }
387
388
389 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
390                                     const struct scan_info *info)
391 {
392         int found;
393         size_t i;
394
395         if (info == NULL)
396                 return 1;
397
398         if (info->num_freqs) {
399                 found = 0;
400                 for (i = 0; i < info->num_freqs; i++) {
401                         if (bss->freq == info->freqs[i]) {
402                                 found = 1;
403                                 break;
404                         }
405                 }
406                 if (!found)
407                         return 0;
408         }
409
410         if (info->num_ssids) {
411                 found = 0;
412                 for (i = 0; i < info->num_ssids; i++) {
413                         const struct wpa_driver_scan_ssid *s = &info->ssids[i];
414                         if ((s->ssid == NULL || s->ssid_len == 0) ||
415                             (s->ssid_len == bss->ssid_len &&
416                              os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
417                              0)) {
418                                 found = 1;
419                                 break;
420                         }
421                 }
422                 if (!found)
423                         return 0;
424         }
425
426         return 1;
427 }
428
429
430 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
431                         int new_scan)
432 {
433         struct wpa_bss *bss, *n;
434
435         if (!new_scan)
436                 return; /* do not expire entries without new scan */
437
438         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
439                 if (wpa_bss_in_use(wpa_s, bss))
440                         continue;
441                 if (!wpa_bss_included_in_scan(bss, info))
442                         continue; /* expire only BSSes that were scanned */
443                 if (bss->last_update_idx < wpa_s->bss_update_idx)
444                         bss->scan_miss_count++;
445                 if (bss->scan_miss_count >=
446                     wpa_s->conf->bss_expiration_scan_count) {
447                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
448                                 "no match in scan", bss->id);
449                         wpa_bss_remove(wpa_s, bss);
450                 }
451         }
452 }
453
454
455 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
456 {
457         struct wpa_bss *bss, *n;
458         struct os_time t;
459
460         if (dl_list_empty(&wpa_s->bss))
461                 return;
462
463         os_get_time(&t);
464         t.sec -= age;
465
466         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
467                 if (wpa_bss_in_use(wpa_s, bss))
468                         continue;
469
470                 if (os_time_before(&bss->last_update, &t)) {
471                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
472                                 "age", bss->id);
473                         wpa_bss_remove(wpa_s, bss);
474                 } else
475                         break;
476         }
477 }
478
479
480 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
481 {
482         struct wpa_supplicant *wpa_s = eloop_ctx;
483
484         wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
485         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
486                                wpa_bss_timeout, wpa_s, NULL);
487 }
488
489
490 int wpa_bss_init(struct wpa_supplicant *wpa_s)
491 {
492         dl_list_init(&wpa_s->bss);
493         dl_list_init(&wpa_s->bss_id);
494         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
495                                wpa_bss_timeout, wpa_s, NULL);
496         return 0;
497 }
498
499
500 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
501 {
502         struct wpa_bss *bss, *n;
503
504         if (wpa_s->bss.next == NULL)
505                 return; /* BSS table not yet initialized */
506
507         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
508                 if (wpa_bss_in_use(wpa_s, bss))
509                         continue;
510                 wpa_bss_remove(wpa_s, bss);
511         }
512 }
513
514
515 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
516 {
517         eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
518         wpa_bss_flush(wpa_s);
519 }
520
521
522 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
523                                    const u8 *bssid)
524 {
525         struct wpa_bss *bss;
526         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
527                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
528                         return bss;
529         }
530         return NULL;
531 }
532
533
534 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
535 {
536         struct wpa_bss *bss;
537         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
538                 if (bss->id == id)
539                         return bss;
540         }
541         return NULL;
542 }
543
544
545 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
546 {
547         const u8 *end, *pos;
548
549         pos = (const u8 *) (bss + 1);
550         end = pos + bss->ie_len;
551
552         while (pos + 1 < end) {
553                 if (pos + 2 + pos[1] > end)
554                         break;
555                 if (pos[0] == ie)
556                         return pos;
557                 pos += 2 + pos[1];
558         }
559
560         return NULL;
561 }
562
563
564 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
565 {
566         const u8 *end, *pos;
567
568         pos = (const u8 *) (bss + 1);
569         end = pos + bss->ie_len;
570
571         while (pos + 1 < end) {
572                 if (pos + 2 + pos[1] > end)
573                         break;
574                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
575                     vendor_type == WPA_GET_BE32(&pos[2]))
576                         return pos;
577                 pos += 2 + pos[1];
578         }
579
580         return NULL;
581 }
582
583
584 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
585                                             u32 vendor_type)
586 {
587         struct wpabuf *buf;
588         const u8 *end, *pos;
589
590         buf = wpabuf_alloc(bss->ie_len);
591         if (buf == NULL)
592                 return NULL;
593
594         pos = (const u8 *) (bss + 1);
595         end = pos + bss->ie_len;
596
597         while (pos + 1 < end) {
598                 if (pos + 2 + pos[1] > end)
599                         break;
600                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
601                     vendor_type == WPA_GET_BE32(&pos[2]))
602                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
603                 pos += 2 + pos[1];
604         }
605
606         if (wpabuf_len(buf) == 0) {
607                 wpabuf_free(buf);
608                 buf = NULL;
609         }
610
611         return buf;
612 }
613
614
615 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
616 {
617         int rate = 0;
618         const u8 *ie;
619         int i;
620
621         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
622         for (i = 0; ie && i < ie[1]; i++) {
623                 if ((ie[i + 2] & 0x7f) > rate)
624                         rate = ie[i + 2] & 0x7f;
625         }
626
627         ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
628         for (i = 0; ie && i < ie[1]; i++) {
629                 if ((ie[i + 2] & 0x7f) > rate)
630                         rate = ie[i + 2] & 0x7f;
631         }
632
633         return rate;
634 }
635
636
637 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
638 {
639         const u8 *ie, *ie2;
640         int i, j;
641         unsigned int len;
642         u8 *r;
643
644         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
645         ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
646
647         len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
648
649         r = os_malloc(len);
650         if (!r)
651                 return -1;
652
653         for (i = 0; ie && i < ie[1]; i++)
654                 r[i] = ie[i + 2] & 0x7f;
655
656         for (j = 0; ie2 && j < ie2[1]; j++)
657                 r[i + j] = ie2[j + 2] & 0x7f;
658
659         *rates = r;
660         return len;
661 }