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