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