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