blob: c50de539e9e9f6e925bdc05a0e260c89f024000d [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * BSS table
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003 * Copyright (c) 2009-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
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
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070038static void wpa_bss_set_hessid(struct wpa_bss *bss)
39{
40#ifdef CONFIG_INTERWORKING
41 const u8 *ie = wpa_bss_get_ie(bss, WLAN_EID_INTERWORKING);
42 if (ie == NULL || (ie[1] != 7 && ie[1] != 9)) {
43 os_memset(bss->hessid, 0, ETH_ALEN);
44 return;
45 }
46 if (ie[1] == 7)
47 os_memcpy(bss->hessid, ie + 3, ETH_ALEN);
48 else
49 os_memcpy(bss->hessid, ie + 5, ETH_ALEN);
50#endif /* CONFIG_INTERWORKING */
51}
52
53
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080054/**
55 * wpa_bss_anqp_alloc - Allocate ANQP data structure for a BSS entry
56 * Returns: Allocated ANQP data structure or %NULL on failure
57 *
58 * The allocated ANQP data structure has its users count set to 1. It may be
59 * shared by multiple BSS entries and each shared entry is freed with
60 * wpa_bss_anqp_free().
61 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -070062struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
63{
64 struct wpa_bss_anqp *anqp;
65 anqp = os_zalloc(sizeof(*anqp));
66 if (anqp == NULL)
67 return NULL;
68 anqp->users = 1;
69 return anqp;
70}
71
72
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080073/**
74 * wpa_bss_anqp_clone - Clone an ANQP data structure
75 * @anqp: ANQP data structure from wpa_bss_anqp_alloc()
76 * Returns: Cloned ANQP data structure or %NULL on failure
77 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080078static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
79{
80 struct wpa_bss_anqp *n;
81
82 n = os_zalloc(sizeof(*n));
83 if (n == NULL)
84 return NULL;
85
86#define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
87#ifdef CONFIG_INTERWORKING
88 ANQP_DUP(venue_name);
89 ANQP_DUP(network_auth_type);
90 ANQP_DUP(roaming_consortium);
91 ANQP_DUP(ip_addr_type_availability);
92 ANQP_DUP(nai_realm);
93 ANQP_DUP(anqp_3gpp);
94 ANQP_DUP(domain_name);
95#endif /* CONFIG_INTERWORKING */
96#ifdef CONFIG_HS20
97 ANQP_DUP(hs20_operator_friendly_name);
98 ANQP_DUP(hs20_wan_metrics);
99 ANQP_DUP(hs20_connection_capability);
100 ANQP_DUP(hs20_operating_class);
101#endif /* CONFIG_HS20 */
102#undef ANQP_DUP
103
104 return n;
105}
106
107
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800108/**
109 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
110 * @bss: BSS entry
111 * Returns: 0 on success, -1 on failure
112 *
113 * This function ensures the specific BSS entry has an ANQP data structure that
114 * is not shared with any other BSS entry.
115 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800116int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
117{
118 struct wpa_bss_anqp *anqp;
119
120 if (bss->anqp && bss->anqp->users > 1) {
121 /* allocated, but shared - clone an unshared copy */
122 anqp = wpa_bss_anqp_clone(bss->anqp);
123 if (anqp == NULL)
124 return -1;
125 anqp->users = 1;
126 bss->anqp->users--;
127 bss->anqp = anqp;
128 return 0;
129 }
130
131 if (bss->anqp)
132 return 0; /* already allocated and not shared */
133
134 /* not allocated - allocate a new storage area */
135 bss->anqp = wpa_bss_anqp_alloc();
136 return bss->anqp ? 0 : -1;
137}
138
139
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800140/**
141 * wpa_bss_anqp_free - Free an ANQP data structure
142 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
143 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700144static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
145{
146 if (anqp == NULL)
147 return;
148
149 anqp->users--;
150 if (anqp->users > 0) {
151 /* Another BSS entry holds a pointer to this ANQP info */
152 return;
153 }
154
155#ifdef CONFIG_INTERWORKING
156 wpabuf_free(anqp->venue_name);
157 wpabuf_free(anqp->network_auth_type);
158 wpabuf_free(anqp->roaming_consortium);
159 wpabuf_free(anqp->ip_addr_type_availability);
160 wpabuf_free(anqp->nai_realm);
161 wpabuf_free(anqp->anqp_3gpp);
162 wpabuf_free(anqp->domain_name);
163#endif /* CONFIG_INTERWORKING */
164#ifdef CONFIG_HS20
165 wpabuf_free(anqp->hs20_operator_friendly_name);
166 wpabuf_free(anqp->hs20_wan_metrics);
167 wpabuf_free(anqp->hs20_connection_capability);
168 wpabuf_free(anqp->hs20_operating_class);
169#endif /* CONFIG_HS20 */
170
171 os_free(anqp);
172}
173
174
Dmitry Shmidt04949592012-07-19 12:16:46 -0700175static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
176 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700178 if (wpa_s->last_scan_res) {
179 unsigned int i;
180 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
181 if (wpa_s->last_scan_res[i] == bss) {
182 os_memmove(&wpa_s->last_scan_res[i],
183 &wpa_s->last_scan_res[i + 1],
184 (wpa_s->last_scan_res_used - i - 1)
185 * sizeof(struct wpa_bss *));
186 wpa_s->last_scan_res_used--;
187 break;
188 }
189 }
190 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191 dl_list_del(&bss->list);
192 dl_list_del(&bss->list_id);
193 wpa_s->num_bss--;
194 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700195 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
196 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700197 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700198 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 os_free(bss);
200}
201
202
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800203/**
204 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
205 * @wpa_s: Pointer to wpa_supplicant data
206 * @bssid: BSSID
207 * @ssid: SSID
208 * @ssid_len: Length of @ssid
209 * Returns: Pointer to the BSS entry or %NULL if not found
210 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
212 const u8 *ssid, size_t ssid_len)
213{
214 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700215 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
216 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
218 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
219 bss->ssid_len == ssid_len &&
220 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
221 return bss;
222 }
223 return NULL;
224}
225
226
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800227static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
228 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229{
230 os_time_t usec;
231
232 dst->flags = src->flags;
233 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
234 dst->freq = src->freq;
235 dst->beacon_int = src->beacon_int;
236 dst->caps = src->caps;
237 dst->qual = src->qual;
238 dst->noise = src->noise;
239 dst->level = src->level;
240 dst->tsf = src->tsf;
241
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800242 dst->last_update.sec = fetch_time->sec;
243 dst->last_update.usec = fetch_time->usec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 dst->last_update.sec -= src->age / 1000;
245 usec = (src->age % 1000) * 1000;
246 if (dst->last_update.usec < usec) {
247 dst->last_update.sec--;
248 dst->last_update.usec += 1000000;
249 }
250 dst->last_update.usec -= usec;
251}
252
253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800254static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
255{
256 struct wpa_ssid *ssid;
257
258 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
259 if (ssid->ssid == NULL || ssid->ssid_len == 0)
260 continue;
261 if (ssid->ssid_len == bss->ssid_len &&
262 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
263 return 1;
264 }
265
266 return 0;
267}
268
269
Dmitry Shmidt04949592012-07-19 12:16:46 -0700270static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
271{
272 return bss == wpa_s->current_bss ||
273 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
274 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
275}
276
277
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800278static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
279{
280 struct wpa_bss *bss;
281
282 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
283 if (!wpa_bss_known(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700284 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800285 return 0;
286 }
287 }
288
289 return -1;
290}
291
292
Dmitry Shmidt04949592012-07-19 12:16:46 -0700293static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700295 struct wpa_bss *bss;
296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800297 /*
298 * Remove the oldest entry that does not match with any configured
299 * network.
300 */
301 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700302 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800303
304 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700305 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700307 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
308 if (!wpa_bss_in_use(wpa_s, bss)) {
309 wpa_bss_remove(wpa_s, bss, __func__);
310 return 0;
311 }
312 }
313
314 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800315}
316
317
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700318static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
319 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800320 struct wpa_scan_res *res,
321 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322{
323 struct wpa_bss *bss;
324
325 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
326 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700327 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 bss->id = wpa_s->bss_next_id++;
329 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800330 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 os_memcpy(bss->ssid, ssid, ssid_len);
332 bss->ssid_len = ssid_len;
333 bss->ie_len = res->ie_len;
334 bss->beacon_ie_len = res->beacon_ie_len;
335 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700336 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337
338 dl_list_add_tail(&wpa_s->bss, &bss->list);
339 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
340 wpa_s->num_bss++;
341 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
342 " SSID '%s'",
343 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
344 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700345 if (wpa_s->num_bss > wpa_s->conf->bss_max_count &&
346 wpa_bss_remove_oldest(wpa_s) != 0) {
347 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
348 "because all BSSes are in use. We should normally "
349 "not get here!", (int) wpa_s->num_bss);
350 wpa_s->conf->bss_max_count = wpa_s->num_bss;
351 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700352 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700353}
354
355
356static int are_ies_equal(const struct wpa_bss *old,
357 const struct wpa_scan_res *new, u32 ie)
358{
359 const u8 *old_ie, *new_ie;
360 struct wpabuf *old_ie_buff = NULL;
361 struct wpabuf *new_ie_buff = NULL;
362 int new_ie_len, old_ie_len, ret, is_multi;
363
364 switch (ie) {
365 case WPA_IE_VENDOR_TYPE:
366 old_ie = wpa_bss_get_vendor_ie(old, ie);
367 new_ie = wpa_scan_get_vendor_ie(new, ie);
368 is_multi = 0;
369 break;
370 case WPS_IE_VENDOR_TYPE:
371 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
372 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
373 is_multi = 1;
374 break;
375 case WLAN_EID_RSN:
376 case WLAN_EID_SUPP_RATES:
377 case WLAN_EID_EXT_SUPP_RATES:
378 old_ie = wpa_bss_get_ie(old, ie);
379 new_ie = wpa_scan_get_ie(new, ie);
380 is_multi = 0;
381 break;
382 default:
383 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
384 return 0;
385 }
386
387 if (is_multi) {
388 /* in case of multiple IEs stored in buffer */
389 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
390 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
391 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
392 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
393 } else {
394 /* in case of single IE */
395 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
396 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
397 }
398
399 if (!old_ie || !new_ie)
400 ret = !old_ie && !new_ie;
401 else
402 ret = (old_ie_len == new_ie_len &&
403 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
404
405 wpabuf_free(old_ie_buff);
406 wpabuf_free(new_ie_buff);
407
408 return ret;
409}
410
411
412static u32 wpa_bss_compare_res(const struct wpa_bss *old,
413 const struct wpa_scan_res *new)
414{
415 u32 changes = 0;
416 int caps_diff = old->caps ^ new->caps;
417
418 if (old->freq != new->freq)
419 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
420
421 if (old->level != new->level)
422 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
423
424 if (caps_diff & IEEE80211_CAP_PRIVACY)
425 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
426
427 if (caps_diff & IEEE80211_CAP_IBSS)
428 changes |= WPA_BSS_MODE_CHANGED_FLAG;
429
430 if (old->ie_len == new->ie_len &&
431 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
432 return changes;
433 changes |= WPA_BSS_IES_CHANGED_FLAG;
434
435 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
436 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
437
438 if (!are_ies_equal(old, new, WLAN_EID_RSN))
439 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
440
441 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
442 changes |= WPA_BSS_WPS_CHANGED_FLAG;
443
444 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
445 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
446 changes |= WPA_BSS_RATES_CHANGED_FLAG;
447
448 return changes;
449}
450
451
452static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
453 const struct wpa_bss *bss)
454{
455 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
456 wpas_notify_bss_freq_changed(wpa_s, bss->id);
457
458 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
459 wpas_notify_bss_signal_changed(wpa_s, bss->id);
460
461 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
462 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
463
464 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
465 wpas_notify_bss_mode_changed(wpa_s, bss->id);
466
467 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
468 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
469
470 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
471 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
472
473 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
474 wpas_notify_bss_wps_changed(wpa_s, bss->id);
475
476 if (changes & WPA_BSS_IES_CHANGED_FLAG)
477 wpas_notify_bss_ies_changed(wpa_s, bss->id);
478
479 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
480 wpas_notify_bss_rates_changed(wpa_s, bss->id);
481}
482
483
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700484static struct wpa_bss *
485wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800486 struct wpa_scan_res *res, struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487{
488 u32 changes;
489
490 changes = wpa_bss_compare_res(bss, res);
491 bss->scan_miss_count = 0;
492 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800493 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 /* Move the entry to the end of the list */
495 dl_list_del(&bss->list);
496 if (bss->ie_len + bss->beacon_ie_len >=
497 res->ie_len + res->beacon_ie_len) {
498 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
499 bss->ie_len = res->ie_len;
500 bss->beacon_ie_len = res->beacon_ie_len;
501 } else {
502 struct wpa_bss *nbss;
503 struct dl_list *prev = bss->list_id.prev;
504 dl_list_del(&bss->list_id);
505 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
506 res->beacon_ie_len);
507 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700508 unsigned int i;
509 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
510 if (wpa_s->last_scan_res[i] == bss) {
511 wpa_s->last_scan_res[i] = nbss;
512 break;
513 }
514 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700515 if (wpa_s->current_bss == bss)
516 wpa_s->current_bss = nbss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 bss = nbss;
518 os_memcpy(bss + 1, res + 1,
519 res->ie_len + res->beacon_ie_len);
520 bss->ie_len = res->ie_len;
521 bss->beacon_ie_len = res->beacon_ie_len;
522 }
523 dl_list_add(prev, &bss->list_id);
524 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700525 if (changes & WPA_BSS_IES_CHANGED_FLAG)
526 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527 dl_list_add_tail(&wpa_s->bss, &bss->list);
528
529 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700530
531 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532}
533
534
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800535/**
536 * wpa_bss_update_start - Start a BSS table update from scan results
537 * @wpa_s: Pointer to wpa_supplicant data
538 *
539 * This function is called at the start of each BSS table update round for new
540 * scan results. The actual scan result entries are indicated with calls to
541 * wpa_bss_update_scan_res() and the update round is finished with a call to
542 * wpa_bss_update_end().
543 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
545{
546 wpa_s->bss_update_idx++;
547 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
548 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700549 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550}
551
552
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800553/**
554 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
555 * @wpa_s: Pointer to wpa_supplicant data
556 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800557 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800558 *
559 * This function updates a BSS table entry (or adds one) based on a scan result.
560 * This is called separately for each scan result between the calls to
561 * wpa_bss_update_start() and wpa_bss_update_end().
562 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800564 struct wpa_scan_res *res,
565 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566{
567 const u8 *ssid, *p2p;
568 struct wpa_bss *bss;
569
570 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
571 if (ssid == NULL) {
572 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
573 MACSTR, MAC2STR(res->bssid));
574 return;
575 }
576 if (ssid[1] > 32) {
577 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
578 MACSTR, MAC2STR(res->bssid));
579 return;
580 }
581
582 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700583#ifdef CONFIG_P2P
584 if (p2p == NULL &&
585 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
586 /*
587 * If it's a P2P specific interface, then don't update
588 * the scan result without a P2P IE.
589 */
590 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
591 " update for P2P interface", MAC2STR(res->bssid));
592 return;
593 }
594#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
596 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
597 return; /* Skip P2P listen discovery results here */
598
599 /* TODO: add option for ignoring BSSes we are not interested in
600 * (to save memory) */
601 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
602 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800603 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 else
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800605 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700606
607 if (bss == NULL)
608 return;
609 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
610 struct wpa_bss **n;
611 unsigned int siz;
612 if (wpa_s->last_scan_res_size == 0)
613 siz = 32;
614 else
615 siz = wpa_s->last_scan_res_size * 2;
616 n = os_realloc_array(wpa_s->last_scan_res, siz,
617 sizeof(struct wpa_bss *));
618 if (n == NULL)
619 return;
620 wpa_s->last_scan_res = n;
621 wpa_s->last_scan_res_size = siz;
622 }
623
624 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625}
626
627
628static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
629 const struct scan_info *info)
630{
631 int found;
632 size_t i;
633
634 if (info == NULL)
635 return 1;
636
637 if (info->num_freqs) {
638 found = 0;
639 for (i = 0; i < info->num_freqs; i++) {
640 if (bss->freq == info->freqs[i]) {
641 found = 1;
642 break;
643 }
644 }
645 if (!found)
646 return 0;
647 }
648
649 if (info->num_ssids) {
650 found = 0;
651 for (i = 0; i < info->num_ssids; i++) {
652 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
653 if ((s->ssid == NULL || s->ssid_len == 0) ||
654 (s->ssid_len == bss->ssid_len &&
655 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
656 0)) {
657 found = 1;
658 break;
659 }
660 }
661 if (!found)
662 return 0;
663 }
664
665 return 1;
666}
667
668
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800669/**
670 * wpa_bss_update_end - End a BSS table update from scan results
671 * @wpa_s: Pointer to wpa_supplicant data
672 * @info: Information about scan parameters
673 * @new_scan: Whether this update round was based on a new scan
674 *
675 * This function is called at the end of each BSS table update round for new
676 * scan results. The start of the update was indicated with a call to
677 * wpa_bss_update_start().
678 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
680 int new_scan)
681{
682 struct wpa_bss *bss, *n;
683
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700684 wpa_s->last_scan_full = 0;
685 os_get_time(&wpa_s->last_scan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 if (!new_scan)
687 return; /* do not expire entries without new scan */
688
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700689 if (info && !info->aborted && !info->freqs) {
690 size_t i;
691 if (info->num_ssids == 0) {
692 wpa_s->last_scan_full = 1;
693 } else {
694 for (i = 0; i < info->num_ssids; i++) {
695 if (info->ssids[i].ssid == NULL ||
696 info->ssids[i].ssid_len == 0) {
697 wpa_s->last_scan_full = 1;
698 break;
699 }
700 }
701 }
702 }
703
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700704 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
705 if (wpa_bss_in_use(wpa_s, bss))
706 continue;
707 if (!wpa_bss_included_in_scan(bss, info))
708 continue; /* expire only BSSes that were scanned */
709 if (bss->last_update_idx < wpa_s->bss_update_idx)
710 bss->scan_miss_count++;
711 if (bss->scan_miss_count >=
712 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700713 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700714 }
715 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700716
717 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u "
718 "last_scan_full=%d",
719 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size,
720 wpa_s->last_scan_full);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721}
722
723
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800724/**
725 * wpa_bss_flush_by_age - Flush old BSS entries
726 * @wpa_s: Pointer to wpa_supplicant data
727 * @age: Maximum entry age in seconds
728 *
729 * Remove BSS entries that have not been updated during the last @age seconds.
730 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
732{
733 struct wpa_bss *bss, *n;
734 struct os_time t;
735
736 if (dl_list_empty(&wpa_s->bss))
737 return;
738
739 os_get_time(&t);
740 t.sec -= age;
741
742 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
743 if (wpa_bss_in_use(wpa_s, bss))
744 continue;
745
746 if (os_time_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 } else
749 break;
750 }
751}
752
753
754static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
755{
756 struct wpa_supplicant *wpa_s = eloop_ctx;
757
758 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
759 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
760 wpa_bss_timeout, wpa_s, NULL);
761}
762
763
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800764/**
765 * wpa_bss_init - Initialize BSS table
766 * @wpa_s: Pointer to wpa_supplicant data
767 * Returns: 0 on success, -1 on failure
768 *
769 * This prepares BSS table lists and timer for periodic updates. The BSS table
770 * is deinitialized with wpa_bss_deinit() once not needed anymore.
771 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772int wpa_bss_init(struct wpa_supplicant *wpa_s)
773{
774 dl_list_init(&wpa_s->bss);
775 dl_list_init(&wpa_s->bss_id);
776 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
777 wpa_bss_timeout, wpa_s, NULL);
778 return 0;
779}
780
781
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800782/**
783 * wpa_bss_flush - Flush all unused BSS entries
784 * @wpa_s: Pointer to wpa_supplicant data
785 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786void wpa_bss_flush(struct wpa_supplicant *wpa_s)
787{
788 struct wpa_bss *bss, *n;
789
790 if (wpa_s->bss.next == NULL)
791 return; /* BSS table not yet initialized */
792
793 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
794 if (wpa_bss_in_use(wpa_s, bss))
795 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700796 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 }
798}
799
800
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800801/**
802 * wpa_bss_deinit - Deinitialize BSS table
803 * @wpa_s: Pointer to wpa_supplicant data
804 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
806{
807 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
808 wpa_bss_flush(wpa_s);
809}
810
811
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800812/**
813 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
814 * @wpa_s: Pointer to wpa_supplicant data
815 * @bssid: BSSID
816 * Returns: Pointer to the BSS entry or %NULL if not found
817 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
819 const u8 *bssid)
820{
821 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
823 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
825 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
826 return bss;
827 }
828 return NULL;
829}
830
831
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800832#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800833/**
834 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
835 * @wpa_s: Pointer to wpa_supplicant data
836 * @dev_addr: P2P Device Address of the GO
837 * Returns: Pointer to the BSS entry or %NULL if not found
838 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800839struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
840 const u8 *dev_addr)
841{
842 struct wpa_bss *bss;
843 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
844 u8 addr[ETH_ALEN];
845 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
846 addr) == 0 &&
847 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
848 return bss;
849 }
850 return NULL;
851}
852#endif /* CONFIG_P2P */
853
854
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800855/**
856 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
857 * @wpa_s: Pointer to wpa_supplicant data
858 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
859 * Returns: Pointer to the BSS entry or %NULL if not found
860 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
862{
863 struct wpa_bss *bss;
864 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
865 if (bss->id == id)
866 return bss;
867 }
868 return NULL;
869}
870
871
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800872/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800873 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
874 * @wpa_s: Pointer to wpa_supplicant data
875 * @idf: Smallest allowed identifier assigned for the entry
876 * @idf: Largest allowed identifier assigned for the entry
877 * Returns: Pointer to the BSS entry or %NULL if not found
878 *
879 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
880 * smallest id value to be fetched within the specified range without the
881 * caller having to know the exact id.
882 */
883struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
884 unsigned int idf, unsigned int idl)
885{
886 struct wpa_bss *bss;
887 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
888 if (bss->id >= idf && bss->id <= idl)
889 return bss;
890 }
891 return NULL;
892}
893
894
895/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800896 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
897 * @bss: BSS table entry
898 * @ie: Information element identitifier (WLAN_EID_*)
899 * Returns: Pointer to the information element (id field) or %NULL if not found
900 *
901 * This function returns the first matching information element in the BSS
902 * entry.
903 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
905{
906 const u8 *end, *pos;
907
908 pos = (const u8 *) (bss + 1);
909 end = pos + bss->ie_len;
910
911 while (pos + 1 < end) {
912 if (pos + 2 + pos[1] > end)
913 break;
914 if (pos[0] == ie)
915 return pos;
916 pos += 2 + pos[1];
917 }
918
919 return NULL;
920}
921
922
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923/**
924 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
925 * @bss: BSS table entry
926 * @vendor_type: Vendor type (four octets starting the IE payload)
927 * Returns: Pointer to the information element (id field) or %NULL if not found
928 *
929 * This function returns the first matching information element in the BSS
930 * entry.
931 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
933{
934 const u8 *end, *pos;
935
936 pos = (const u8 *) (bss + 1);
937 end = pos + bss->ie_len;
938
939 while (pos + 1 < end) {
940 if (pos + 2 + pos[1] > end)
941 break;
942 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
943 vendor_type == WPA_GET_BE32(&pos[2]))
944 return pos;
945 pos += 2 + pos[1];
946 }
947
948 return NULL;
949}
950
951
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800952/**
953 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
954 * @bss: BSS table entry
955 * @vendor_type: Vendor type (four octets starting the IE payload)
956 * Returns: Pointer to the information element payload or %NULL if not found
957 *
958 * This function returns concatenated payload of possibly fragmented vendor
959 * specific information elements in the BSS entry. The caller is responsible for
960 * freeing the returned buffer.
961 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
963 u32 vendor_type)
964{
965 struct wpabuf *buf;
966 const u8 *end, *pos;
967
968 buf = wpabuf_alloc(bss->ie_len);
969 if (buf == NULL)
970 return NULL;
971
972 pos = (const u8 *) (bss + 1);
973 end = pos + bss->ie_len;
974
975 while (pos + 1 < end) {
976 if (pos + 2 + pos[1] > end)
977 break;
978 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
979 vendor_type == WPA_GET_BE32(&pos[2]))
980 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
981 pos += 2 + pos[1];
982 }
983
984 if (wpabuf_len(buf) == 0) {
985 wpabuf_free(buf);
986 buf = NULL;
987 }
988
989 return buf;
990}
991
992
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800993/**
994 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
995 * @bss: BSS table entry
996 * @vendor_type: Vendor type (four octets starting the IE payload)
997 * Returns: Pointer to the information element payload or %NULL if not found
998 *
999 * This function returns concatenated payload of possibly fragmented vendor
1000 * specific information elements in the BSS entry. The caller is responsible for
1001 * freeing the returned buffer.
1002 *
1003 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1004 * from Beacon frames instead of either Beacon or Probe Response frames.
1005 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001006struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1007 u32 vendor_type)
1008{
1009 struct wpabuf *buf;
1010 const u8 *end, *pos;
1011
1012 buf = wpabuf_alloc(bss->beacon_ie_len);
1013 if (buf == NULL)
1014 return NULL;
1015
1016 pos = (const u8 *) (bss + 1);
1017 pos += bss->ie_len;
1018 end = pos + bss->beacon_ie_len;
1019
1020 while (pos + 1 < end) {
1021 if (pos + 2 + pos[1] > end)
1022 break;
1023 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1024 vendor_type == WPA_GET_BE32(&pos[2]))
1025 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1026 pos += 2 + pos[1];
1027 }
1028
1029 if (wpabuf_len(buf) == 0) {
1030 wpabuf_free(buf);
1031 buf = NULL;
1032 }
1033
1034 return buf;
1035}
1036
1037
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001038/**
1039 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1040 * @bss: BSS table entry
1041 * Returns: Maximum legacy rate in units of 500 kbps
1042 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1044{
1045 int rate = 0;
1046 const u8 *ie;
1047 int i;
1048
1049 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1050 for (i = 0; ie && i < ie[1]; i++) {
1051 if ((ie[i + 2] & 0x7f) > rate)
1052 rate = ie[i + 2] & 0x7f;
1053 }
1054
1055 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1056 for (i = 0; ie && i < ie[1]; i++) {
1057 if ((ie[i + 2] & 0x7f) > rate)
1058 rate = ie[i + 2] & 0x7f;
1059 }
1060
1061 return rate;
1062}
1063
1064
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001065/**
1066 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1067 * @bss: BSS table entry
1068 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1069 * Returns: number of legacy TX rates or -1 on failure
1070 *
1071 * The caller is responsible for freeing the returned buffer with os_free() in
1072 * case of success.
1073 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1075{
1076 const u8 *ie, *ie2;
1077 int i, j;
1078 unsigned int len;
1079 u8 *r;
1080
1081 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1082 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1083
1084 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1085
1086 r = os_malloc(len);
1087 if (!r)
1088 return -1;
1089
1090 for (i = 0; ie && i < ie[1]; i++)
1091 r[i] = ie[i + 2] & 0x7f;
1092
1093 for (j = 0; ie2 && j < ie2[1]; j++)
1094 r[i + j] = ie2[j + 2] & 0x7f;
1095
1096 *rates = r;
1097 return len;
1098}