b79510e4067ff59a9af47a8975d76669f005e105
[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                         if (wpa_s->current_bss == bss)
332                                 wpa_s->current_bss = nbss;
333                         bss = nbss;
334                         os_memcpy(bss + 1, res + 1,
335                                   res->ie_len + res->beacon_ie_len);
336                         bss->ie_len = res->ie_len;
337                         bss->beacon_ie_len = res->beacon_ie_len;
338                 }
339                 dl_list_add(prev, &bss->list_id);
340         }
341         dl_list_add_tail(&wpa_s->bss, &bss->list);
342
343         notify_bss_changes(wpa_s, changes, bss);
344 }
345
346
347 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
348 {
349         return bss == wpa_s->current_bss ||
350                 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
351                 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
352 }
353
354
355 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
356 {
357         wpa_s->bss_update_idx++;
358         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
359                 wpa_s->bss_update_idx);
360 }
361
362
363 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
364                              struct wpa_scan_res *res)
365 {
366         const u8 *ssid, *p2p;
367         struct wpa_bss *bss;
368
369         ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
370         if (ssid == NULL) {
371                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
372                         MACSTR, MAC2STR(res->bssid));
373                 return;
374         }
375         if (ssid[1] > 32) {
376                 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
377                         MACSTR, MAC2STR(res->bssid));
378                 return;
379         }
380
381         p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
382         if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
383             os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
384                 return; /* Skip P2P listen discovery results here */
385
386         /* TODO: add option for ignoring BSSes we are not interested in
387          * (to save memory) */
388         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
389         if (bss == NULL)
390                 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
391         else
392                 wpa_bss_update(wpa_s, bss, res);
393 }
394
395
396 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
397                                     const struct scan_info *info)
398 {
399         int found;
400         size_t i;
401
402         if (info == NULL)
403                 return 1;
404
405         if (info->num_freqs) {
406                 found = 0;
407                 for (i = 0; i < info->num_freqs; i++) {
408                         if (bss->freq == info->freqs[i]) {
409                                 found = 1;
410                                 break;
411                         }
412                 }
413                 if (!found)
414                         return 0;
415         }
416
417         if (info->num_ssids) {
418                 found = 0;
419                 for (i = 0; i < info->num_ssids; i++) {
420                         const struct wpa_driver_scan_ssid *s = &info->ssids[i];
421                         if ((s->ssid == NULL || s->ssid_len == 0) ||
422                             (s->ssid_len == bss->ssid_len &&
423                              os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
424                              0)) {
425                                 found = 1;
426                                 break;
427                         }
428                 }
429                 if (!found)
430                         return 0;
431         }
432
433         return 1;
434 }
435
436
437 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
438                         int new_scan)
439 {
440         struct wpa_bss *bss, *n;
441
442         if (!new_scan)
443                 return; /* do not expire entries without new scan */
444
445         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
446                 if (wpa_bss_in_use(wpa_s, bss))
447                         continue;
448                 if (!wpa_bss_included_in_scan(bss, info))
449                         continue; /* expire only BSSes that were scanned */
450                 if (bss->last_update_idx < wpa_s->bss_update_idx)
451                         bss->scan_miss_count++;
452                 if (bss->scan_miss_count >=
453                     wpa_s->conf->bss_expiration_scan_count) {
454                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
455                                 "no match in scan", bss->id);
456                         wpa_bss_remove(wpa_s, bss);
457                 }
458         }
459 }
460
461
462 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
463 {
464         struct wpa_bss *bss, *n;
465         struct os_time t;
466
467         if (dl_list_empty(&wpa_s->bss))
468                 return;
469
470         os_get_time(&t);
471         t.sec -= age;
472
473         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
474                 if (wpa_bss_in_use(wpa_s, bss))
475                         continue;
476
477                 if (os_time_before(&bss->last_update, &t)) {
478                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
479                                 "age", bss->id);
480                         wpa_bss_remove(wpa_s, bss);
481                 } else
482                         break;
483         }
484 }
485
486
487 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
488 {
489         struct wpa_supplicant *wpa_s = eloop_ctx;
490
491         wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
492         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
493                                wpa_bss_timeout, wpa_s, NULL);
494 }
495
496
497 int wpa_bss_init(struct wpa_supplicant *wpa_s)
498 {
499         dl_list_init(&wpa_s->bss);
500         dl_list_init(&wpa_s->bss_id);
501         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
502                                wpa_bss_timeout, wpa_s, NULL);
503         return 0;
504 }
505
506
507 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
508 {
509         struct wpa_bss *bss, *n;
510
511         if (wpa_s->bss.next == NULL)
512                 return; /* BSS table not yet initialized */
513
514         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
515                 if (wpa_bss_in_use(wpa_s, bss))
516                         continue;
517                 wpa_bss_remove(wpa_s, bss);
518         }
519 }
520
521
522 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
523 {
524         eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
525         wpa_bss_flush(wpa_s);
526 }
527
528
529 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
530                                    const u8 *bssid)
531 {
532         struct wpa_bss *bss;
533         if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
534                 return NULL;
535         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
536                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
537                         return bss;
538         }
539         return NULL;
540 }
541
542
543 #ifdef CONFIG_P2P
544 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
545                                           const u8 *dev_addr)
546 {
547         struct wpa_bss *bss;
548         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
549                 u8 addr[ETH_ALEN];
550                 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
551                                        addr) == 0 &&
552                     os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
553                         return bss;
554         }
555         return NULL;
556 }
557 #endif /* CONFIG_P2P */
558
559
560 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
561 {
562         struct wpa_bss *bss;
563         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
564                 if (bss->id == id)
565                         return bss;
566         }
567         return NULL;
568 }
569
570
571 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
572 {
573         const u8 *end, *pos;
574
575         pos = (const u8 *) (bss + 1);
576         end = pos + bss->ie_len;
577
578         while (pos + 1 < end) {
579                 if (pos + 2 + pos[1] > end)
580                         break;
581                 if (pos[0] == ie)
582                         return pos;
583                 pos += 2 + pos[1];
584         }
585
586         return NULL;
587 }
588
589
590 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
591 {
592         const u8 *end, *pos;
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                         return pos;
603                 pos += 2 + pos[1];
604         }
605
606         return NULL;
607 }
608
609
610 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
611                                             u32 vendor_type)
612 {
613         struct wpabuf *buf;
614         const u8 *end, *pos;
615
616         buf = wpabuf_alloc(bss->ie_len);
617         if (buf == NULL)
618                 return NULL;
619
620         pos = (const u8 *) (bss + 1);
621         end = pos + bss->ie_len;
622
623         while (pos + 1 < end) {
624                 if (pos + 2 + pos[1] > end)
625                         break;
626                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
627                     vendor_type == WPA_GET_BE32(&pos[2]))
628                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
629                 pos += 2 + pos[1];
630         }
631
632         if (wpabuf_len(buf) == 0) {
633                 wpabuf_free(buf);
634                 buf = NULL;
635         }
636
637         return buf;
638 }
639
640
641 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
642 {
643         int rate = 0;
644         const u8 *ie;
645         int i;
646
647         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
648         for (i = 0; ie && i < ie[1]; i++) {
649                 if ((ie[i + 2] & 0x7f) > rate)
650                         rate = ie[i + 2] & 0x7f;
651         }
652
653         ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
654         for (i = 0; ie && i < ie[1]; i++) {
655                 if ((ie[i + 2] & 0x7f) > rate)
656                         rate = ie[i + 2] & 0x7f;
657         }
658
659         return rate;
660 }
661
662
663 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
664 {
665         const u8 *ie, *ie2;
666         int i, j;
667         unsigned int len;
668         u8 *r;
669
670         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
671         ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
672
673         len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
674
675         r = os_malloc(len);
676         if (!r)
677                 return -1;
678
679         for (i = 0; ie && i < ie[1]; i++)
680                 r[i] = ie[i + 2] & 0x7f;
681
682         for (j = 0; ie2 && j < ie2[1]; j++)
683                 r[i + j] = ie2[j + 2] & 0x7f;
684
685         *rates = r;
686         return len;
687 }