P2P: Update BSS entries in P2P group interface only if P2P IE is present
[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 #ifdef CONFIG_P2P
383         if (p2p == NULL &&
384             wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
385                 /*
386                  * If it's a P2P specific interface, then don't update
387                  * the scan result without a P2P IE.
388                  */
389                 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
390                            " update for P2P interface", MAC2STR(res->bssid));
391                 return;
392         }
393 #endif /* CONFIG_P2P */
394         if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
395             os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
396                 return; /* Skip P2P listen discovery results here */
397
398         /* TODO: add option for ignoring BSSes we are not interested in
399          * (to save memory) */
400         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
401         if (bss == NULL)
402                 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
403         else
404                 wpa_bss_update(wpa_s, bss, res);
405 }
406
407
408 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
409                                     const struct scan_info *info)
410 {
411         int found;
412         size_t i;
413
414         if (info == NULL)
415                 return 1;
416
417         if (info->num_freqs) {
418                 found = 0;
419                 for (i = 0; i < info->num_freqs; i++) {
420                         if (bss->freq == info->freqs[i]) {
421                                 found = 1;
422                                 break;
423                         }
424                 }
425                 if (!found)
426                         return 0;
427         }
428
429         if (info->num_ssids) {
430                 found = 0;
431                 for (i = 0; i < info->num_ssids; i++) {
432                         const struct wpa_driver_scan_ssid *s = &info->ssids[i];
433                         if ((s->ssid == NULL || s->ssid_len == 0) ||
434                             (s->ssid_len == bss->ssid_len &&
435                              os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
436                              0)) {
437                                 found = 1;
438                                 break;
439                         }
440                 }
441                 if (!found)
442                         return 0;
443         }
444
445         return 1;
446 }
447
448
449 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
450                         int new_scan)
451 {
452         struct wpa_bss *bss, *n;
453
454         if (!new_scan)
455                 return; /* do not expire entries without new scan */
456
457         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
458                 if (wpa_bss_in_use(wpa_s, bss))
459                         continue;
460                 if (!wpa_bss_included_in_scan(bss, info))
461                         continue; /* expire only BSSes that were scanned */
462                 if (bss->last_update_idx < wpa_s->bss_update_idx)
463                         bss->scan_miss_count++;
464                 if (bss->scan_miss_count >=
465                     wpa_s->conf->bss_expiration_scan_count) {
466                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
467                                 "no match in scan", bss->id);
468                         wpa_bss_remove(wpa_s, bss);
469                 }
470         }
471 }
472
473
474 void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
475 {
476         struct wpa_bss *bss, *n;
477         struct os_time t;
478
479         if (dl_list_empty(&wpa_s->bss))
480                 return;
481
482         os_get_time(&t);
483         t.sec -= age;
484
485         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
486                 if (wpa_bss_in_use(wpa_s, bss))
487                         continue;
488
489                 if (os_time_before(&bss->last_update, &t)) {
490                         wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Expire BSS %u due to "
491                                 "age", bss->id);
492                         wpa_bss_remove(wpa_s, bss);
493                 } else
494                         break;
495         }
496 }
497
498
499 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
500 {
501         struct wpa_supplicant *wpa_s = eloop_ctx;
502
503         wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
504         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
505                                wpa_bss_timeout, wpa_s, NULL);
506 }
507
508
509 int wpa_bss_init(struct wpa_supplicant *wpa_s)
510 {
511         dl_list_init(&wpa_s->bss);
512         dl_list_init(&wpa_s->bss_id);
513         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
514                                wpa_bss_timeout, wpa_s, NULL);
515         return 0;
516 }
517
518
519 void wpa_bss_flush(struct wpa_supplicant *wpa_s)
520 {
521         struct wpa_bss *bss, *n;
522
523         if (wpa_s->bss.next == NULL)
524                 return; /* BSS table not yet initialized */
525
526         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
527                 if (wpa_bss_in_use(wpa_s, bss))
528                         continue;
529                 wpa_bss_remove(wpa_s, bss);
530         }
531 }
532
533
534 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
535 {
536         eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
537         wpa_bss_flush(wpa_s);
538 }
539
540
541 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
542                                    const u8 *bssid)
543 {
544         struct wpa_bss *bss;
545         if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
546                 return NULL;
547         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
548                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
549                         return bss;
550         }
551         return NULL;
552 }
553
554
555 #ifdef CONFIG_P2P
556 struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
557                                           const u8 *dev_addr)
558 {
559         struct wpa_bss *bss;
560         dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
561                 u8 addr[ETH_ALEN];
562                 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
563                                        addr) == 0 &&
564                     os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
565                         return bss;
566         }
567         return NULL;
568 }
569 #endif /* CONFIG_P2P */
570
571
572 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
573 {
574         struct wpa_bss *bss;
575         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
576                 if (bss->id == id)
577                         return bss;
578         }
579         return NULL;
580 }
581
582
583 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
584 {
585         const u8 *end, *pos;
586
587         pos = (const u8 *) (bss + 1);
588         end = pos + bss->ie_len;
589
590         while (pos + 1 < end) {
591                 if (pos + 2 + pos[1] > end)
592                         break;
593                 if (pos[0] == ie)
594                         return pos;
595                 pos += 2 + pos[1];
596         }
597
598         return NULL;
599 }
600
601
602 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
603 {
604         const u8 *end, *pos;
605
606         pos = (const u8 *) (bss + 1);
607         end = pos + bss->ie_len;
608
609         while (pos + 1 < end) {
610                 if (pos + 2 + pos[1] > end)
611                         break;
612                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
613                     vendor_type == WPA_GET_BE32(&pos[2]))
614                         return pos;
615                 pos += 2 + pos[1];
616         }
617
618         return NULL;
619 }
620
621
622 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
623                                             u32 vendor_type)
624 {
625         struct wpabuf *buf;
626         const u8 *end, *pos;
627
628         buf = wpabuf_alloc(bss->ie_len);
629         if (buf == NULL)
630                 return NULL;
631
632         pos = (const u8 *) (bss + 1);
633         end = pos + bss->ie_len;
634
635         while (pos + 1 < end) {
636                 if (pos + 2 + pos[1] > end)
637                         break;
638                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
639                     vendor_type == WPA_GET_BE32(&pos[2]))
640                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
641                 pos += 2 + pos[1];
642         }
643
644         if (wpabuf_len(buf) == 0) {
645                 wpabuf_free(buf);
646                 buf = NULL;
647         }
648
649         return buf;
650 }
651
652
653 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
654 {
655         int rate = 0;
656         const u8 *ie;
657         int i;
658
659         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
660         for (i = 0; ie && i < ie[1]; i++) {
661                 if ((ie[i + 2] & 0x7f) > rate)
662                         rate = ie[i + 2] & 0x7f;
663         }
664
665         ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_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         return rate;
672 }
673
674
675 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
676 {
677         const u8 *ie, *ie2;
678         int i, j;
679         unsigned int len;
680         u8 *r;
681
682         ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
683         ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
684
685         len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
686
687         r = os_malloc(len);
688         if (!r)
689                 return -1;
690
691         for (i = 0; ie && i < ie[1]; i++)
692                 r[i] = ie[i + 2] & 0x7f;
693
694         for (j = 0; ie2 && j < ie2[1]; j++)
695                 r[i + j] = ie2[j + 2] & 0x7f;
696
697         *rates = r;
698         return len;
699 }