blob: 7d9bac5d38f9143a7633a4811920de8ec62dd475 [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 Shmidt444d5672013-04-01 13:08:44 -0700227static void calculate_update_time(const struct os_time *fetch_time,
228 unsigned int age_ms,
229 struct os_time *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230{
231 os_time_t usec;
232
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700233 update_time->sec = fetch_time->sec;
234 update_time->usec = fetch_time->usec;
235 update_time->sec -= age_ms / 1000;
236 usec = (age_ms % 1000) * 1000;
237 if (update_time->usec < usec) {
238 update_time->sec--;
239 update_time->usec += 1000000;
240 }
241 update_time->usec -= usec;
242}
243
244
245static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
246 struct os_time *fetch_time)
247{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 dst->flags = src->flags;
249 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
250 dst->freq = src->freq;
251 dst->beacon_int = src->beacon_int;
252 dst->caps = src->caps;
253 dst->qual = src->qual;
254 dst->noise = src->noise;
255 dst->level = src->level;
256 dst->tsf = src->tsf;
257
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700258 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259}
260
261
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800262static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
263{
264 struct wpa_ssid *ssid;
265
266 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
267 if (ssid->ssid == NULL || ssid->ssid_len == 0)
268 continue;
269 if (ssid->ssid_len == bss->ssid_len &&
270 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
271 return 1;
272 }
273
274 return 0;
275}
276
277
Dmitry Shmidt04949592012-07-19 12:16:46 -0700278static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
279{
280 return bss == wpa_s->current_bss ||
281 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
282 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
283}
284
285
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800286static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
287{
288 struct wpa_bss *bss;
289
290 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
291 if (!wpa_bss_known(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700292 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800293 return 0;
294 }
295 }
296
297 return -1;
298}
299
300
Dmitry Shmidt04949592012-07-19 12:16:46 -0700301static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800302{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303 struct wpa_bss *bss;
304
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800305 /*
306 * Remove the oldest entry that does not match with any configured
307 * network.
308 */
309 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700310 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800311
312 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700313 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800314 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700315 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
316 if (!wpa_bss_in_use(wpa_s, bss)) {
317 wpa_bss_remove(wpa_s, bss, __func__);
318 return 0;
319 }
320 }
321
322 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800323}
324
325
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700326static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
327 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800328 struct wpa_scan_res *res,
329 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330{
331 struct wpa_bss *bss;
332
333 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
334 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700335 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336 bss->id = wpa_s->bss_next_id++;
337 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800338 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 os_memcpy(bss->ssid, ssid, ssid_len);
340 bss->ssid_len = ssid_len;
341 bss->ie_len = res->ie_len;
342 bss->beacon_ie_len = res->beacon_ie_len;
343 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700344 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700345
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300346 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
347 wpa_bss_remove_oldest(wpa_s) != 0) {
348 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
349 "because all BSSes are in use. We should normally "
350 "not get here!", (int) wpa_s->num_bss + 1);
351 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
352 }
353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 dl_list_add_tail(&wpa_s->bss, &bss->list);
355 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
356 wpa_s->num_bss++;
357 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
358 " SSID '%s'",
359 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
360 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700361 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362}
363
364
365static int are_ies_equal(const struct wpa_bss *old,
366 const struct wpa_scan_res *new, u32 ie)
367{
368 const u8 *old_ie, *new_ie;
369 struct wpabuf *old_ie_buff = NULL;
370 struct wpabuf *new_ie_buff = NULL;
371 int new_ie_len, old_ie_len, ret, is_multi;
372
373 switch (ie) {
374 case WPA_IE_VENDOR_TYPE:
375 old_ie = wpa_bss_get_vendor_ie(old, ie);
376 new_ie = wpa_scan_get_vendor_ie(new, ie);
377 is_multi = 0;
378 break;
379 case WPS_IE_VENDOR_TYPE:
380 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
381 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
382 is_multi = 1;
383 break;
384 case WLAN_EID_RSN:
385 case WLAN_EID_SUPP_RATES:
386 case WLAN_EID_EXT_SUPP_RATES:
387 old_ie = wpa_bss_get_ie(old, ie);
388 new_ie = wpa_scan_get_ie(new, ie);
389 is_multi = 0;
390 break;
391 default:
392 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
393 return 0;
394 }
395
396 if (is_multi) {
397 /* in case of multiple IEs stored in buffer */
398 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
399 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
400 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
401 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
402 } else {
403 /* in case of single IE */
404 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
405 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
406 }
407
408 if (!old_ie || !new_ie)
409 ret = !old_ie && !new_ie;
410 else
411 ret = (old_ie_len == new_ie_len &&
412 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
413
414 wpabuf_free(old_ie_buff);
415 wpabuf_free(new_ie_buff);
416
417 return ret;
418}
419
420
421static u32 wpa_bss_compare_res(const struct wpa_bss *old,
422 const struct wpa_scan_res *new)
423{
424 u32 changes = 0;
425 int caps_diff = old->caps ^ new->caps;
426
427 if (old->freq != new->freq)
428 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
429
430 if (old->level != new->level)
431 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
432
433 if (caps_diff & IEEE80211_CAP_PRIVACY)
434 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
435
436 if (caps_diff & IEEE80211_CAP_IBSS)
437 changes |= WPA_BSS_MODE_CHANGED_FLAG;
438
439 if (old->ie_len == new->ie_len &&
440 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
441 return changes;
442 changes |= WPA_BSS_IES_CHANGED_FLAG;
443
444 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
445 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
446
447 if (!are_ies_equal(old, new, WLAN_EID_RSN))
448 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
449
450 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
451 changes |= WPA_BSS_WPS_CHANGED_FLAG;
452
453 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
454 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
455 changes |= WPA_BSS_RATES_CHANGED_FLAG;
456
457 return changes;
458}
459
460
461static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
462 const struct wpa_bss *bss)
463{
464 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
465 wpas_notify_bss_freq_changed(wpa_s, bss->id);
466
467 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
468 wpas_notify_bss_signal_changed(wpa_s, bss->id);
469
470 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
471 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
472
473 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
474 wpas_notify_bss_mode_changed(wpa_s, bss->id);
475
476 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
477 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
478
479 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
480 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
481
482 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
483 wpas_notify_bss_wps_changed(wpa_s, bss->id);
484
485 if (changes & WPA_BSS_IES_CHANGED_FLAG)
486 wpas_notify_bss_ies_changed(wpa_s, bss->id);
487
488 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
489 wpas_notify_bss_rates_changed(wpa_s, bss->id);
490}
491
492
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700493static struct wpa_bss *
494wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800495 struct wpa_scan_res *res, struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496{
497 u32 changes;
498
499 changes = wpa_bss_compare_res(bss, res);
500 bss->scan_miss_count = 0;
501 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800502 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 /* Move the entry to the end of the list */
504 dl_list_del(&bss->list);
505 if (bss->ie_len + bss->beacon_ie_len >=
506 res->ie_len + res->beacon_ie_len) {
507 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
508 bss->ie_len = res->ie_len;
509 bss->beacon_ie_len = res->beacon_ie_len;
510 } else {
511 struct wpa_bss *nbss;
512 struct dl_list *prev = bss->list_id.prev;
513 dl_list_del(&bss->list_id);
514 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
515 res->beacon_ie_len);
516 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700517 unsigned int i;
518 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
519 if (wpa_s->last_scan_res[i] == bss) {
520 wpa_s->last_scan_res[i] = nbss;
521 break;
522 }
523 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700524 if (wpa_s->current_bss == bss)
525 wpa_s->current_bss = nbss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 bss = nbss;
527 os_memcpy(bss + 1, res + 1,
528 res->ie_len + res->beacon_ie_len);
529 bss->ie_len = res->ie_len;
530 bss->beacon_ie_len = res->beacon_ie_len;
531 }
532 dl_list_add(prev, &bss->list_id);
533 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700534 if (changes & WPA_BSS_IES_CHANGED_FLAG)
535 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536 dl_list_add_tail(&wpa_s->bss, &bss->list);
537
538 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700539
540 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541}
542
543
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800544/**
545 * wpa_bss_update_start - Start a BSS table update from scan results
546 * @wpa_s: Pointer to wpa_supplicant data
547 *
548 * This function is called at the start of each BSS table update round for new
549 * scan results. The actual scan result entries are indicated with calls to
550 * wpa_bss_update_scan_res() and the update round is finished with a call to
551 * wpa_bss_update_end().
552 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
554{
555 wpa_s->bss_update_idx++;
556 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
557 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700558 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559}
560
561
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800562/**
563 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
564 * @wpa_s: Pointer to wpa_supplicant data
565 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800566 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800567 *
568 * This function updates a BSS table entry (or adds one) based on a scan result.
569 * This is called separately for each scan result between the calls to
570 * wpa_bss_update_start() and wpa_bss_update_end().
571 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800573 struct wpa_scan_res *res,
574 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575{
576 const u8 *ssid, *p2p;
577 struct wpa_bss *bss;
578
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700579 if (wpa_s->conf->ignore_old_scan_res) {
580 struct os_time update;
581 calculate_update_time(fetch_time, res->age, &update);
582 if (os_time_before(&update, &wpa_s->scan_trigger_time)) {
583 struct os_time age;
584 os_time_sub(&wpa_s->scan_trigger_time, &update, &age);
585 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
586 "table entry that is %u.%06u seconds older "
587 "than our scan trigger",
588 (unsigned int) age.sec,
589 (unsigned int) age.usec);
590 return;
591 }
592 }
593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
595 if (ssid == NULL) {
596 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
597 MACSTR, MAC2STR(res->bssid));
598 return;
599 }
600 if (ssid[1] > 32) {
601 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
602 MACSTR, MAC2STR(res->bssid));
603 return;
604 }
605
606 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700607#ifdef CONFIG_P2P
608 if (p2p == NULL &&
609 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
610 /*
611 * If it's a P2P specific interface, then don't update
612 * the scan result without a P2P IE.
613 */
614 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
615 " update for P2P interface", MAC2STR(res->bssid));
616 return;
617 }
618#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
620 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
621 return; /* Skip P2P listen discovery results here */
622
623 /* TODO: add option for ignoring BSSes we are not interested in
624 * (to save memory) */
625 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
626 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800627 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700628 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800629 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700630 if (wpa_s->last_scan_res) {
631 unsigned int i;
632 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
633 if (bss == wpa_s->last_scan_res[i]) {
634 /* Already in the list */
635 return;
636 }
637 }
638 }
639 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700640
641 if (bss == NULL)
642 return;
643 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
644 struct wpa_bss **n;
645 unsigned int siz;
646 if (wpa_s->last_scan_res_size == 0)
647 siz = 32;
648 else
649 siz = wpa_s->last_scan_res_size * 2;
650 n = os_realloc_array(wpa_s->last_scan_res, siz,
651 sizeof(struct wpa_bss *));
652 if (n == NULL)
653 return;
654 wpa_s->last_scan_res = n;
655 wpa_s->last_scan_res_size = siz;
656 }
657
658 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659}
660
661
662static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
663 const struct scan_info *info)
664{
665 int found;
666 size_t i;
667
668 if (info == NULL)
669 return 1;
670
671 if (info->num_freqs) {
672 found = 0;
673 for (i = 0; i < info->num_freqs; i++) {
674 if (bss->freq == info->freqs[i]) {
675 found = 1;
676 break;
677 }
678 }
679 if (!found)
680 return 0;
681 }
682
683 if (info->num_ssids) {
684 found = 0;
685 for (i = 0; i < info->num_ssids; i++) {
686 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
687 if ((s->ssid == NULL || s->ssid_len == 0) ||
688 (s->ssid_len == bss->ssid_len &&
689 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
690 0)) {
691 found = 1;
692 break;
693 }
694 }
695 if (!found)
696 return 0;
697 }
698
699 return 1;
700}
701
702
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800703/**
704 * wpa_bss_update_end - End a BSS table update from scan results
705 * @wpa_s: Pointer to wpa_supplicant data
706 * @info: Information about scan parameters
707 * @new_scan: Whether this update round was based on a new scan
708 *
709 * This function is called at the end of each BSS table update round for new
710 * scan results. The start of the update was indicated with a call to
711 * wpa_bss_update_start().
712 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
714 int new_scan)
715{
716 struct wpa_bss *bss, *n;
717
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700718 wpa_s->last_scan_full = 0;
719 os_get_time(&wpa_s->last_scan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 if (!new_scan)
721 return; /* do not expire entries without new scan */
722
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700723 if (info && !info->aborted && !info->freqs) {
724 size_t i;
725 if (info->num_ssids == 0) {
726 wpa_s->last_scan_full = 1;
727 } else {
728 for (i = 0; i < info->num_ssids; i++) {
729 if (info->ssids[i].ssid == NULL ||
730 info->ssids[i].ssid_len == 0) {
731 wpa_s->last_scan_full = 1;
732 break;
733 }
734 }
735 }
736 }
737
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
739 if (wpa_bss_in_use(wpa_s, bss))
740 continue;
741 if (!wpa_bss_included_in_scan(bss, info))
742 continue; /* expire only BSSes that were scanned */
743 if (bss->last_update_idx < wpa_s->bss_update_idx)
744 bss->scan_miss_count++;
745 if (bss->scan_miss_count >=
746 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700747 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 }
749 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700750
751 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u "
752 "last_scan_full=%d",
753 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size,
754 wpa_s->last_scan_full);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755}
756
757
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800758/**
759 * wpa_bss_flush_by_age - Flush old BSS entries
760 * @wpa_s: Pointer to wpa_supplicant data
761 * @age: Maximum entry age in seconds
762 *
763 * Remove BSS entries that have not been updated during the last @age seconds.
764 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
766{
767 struct wpa_bss *bss, *n;
768 struct os_time t;
769
770 if (dl_list_empty(&wpa_s->bss))
771 return;
772
773 os_get_time(&t);
774 t.sec -= age;
775
776 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
777 if (wpa_bss_in_use(wpa_s, bss))
778 continue;
779
780 if (os_time_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700781 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 } else
783 break;
784 }
785}
786
787
788static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
789{
790 struct wpa_supplicant *wpa_s = eloop_ctx;
791
792 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
793 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
794 wpa_bss_timeout, wpa_s, NULL);
795}
796
797
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800798/**
799 * wpa_bss_init - Initialize BSS table
800 * @wpa_s: Pointer to wpa_supplicant data
801 * Returns: 0 on success, -1 on failure
802 *
803 * This prepares BSS table lists and timer for periodic updates. The BSS table
804 * is deinitialized with wpa_bss_deinit() once not needed anymore.
805 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806int wpa_bss_init(struct wpa_supplicant *wpa_s)
807{
808 dl_list_init(&wpa_s->bss);
809 dl_list_init(&wpa_s->bss_id);
810 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
811 wpa_bss_timeout, wpa_s, NULL);
812 return 0;
813}
814
815
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800816/**
817 * wpa_bss_flush - Flush all unused BSS entries
818 * @wpa_s: Pointer to wpa_supplicant data
819 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820void wpa_bss_flush(struct wpa_supplicant *wpa_s)
821{
822 struct wpa_bss *bss, *n;
823
824 if (wpa_s->bss.next == NULL)
825 return; /* BSS table not yet initialized */
826
827 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
828 if (wpa_bss_in_use(wpa_s, bss))
829 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700830 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 }
832}
833
834
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800835/**
836 * wpa_bss_deinit - Deinitialize BSS table
837 * @wpa_s: Pointer to wpa_supplicant data
838 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
840{
841 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
842 wpa_bss_flush(wpa_s);
843}
844
845
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800846/**
847 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
848 * @wpa_s: Pointer to wpa_supplicant data
849 * @bssid: BSSID
850 * Returns: Pointer to the BSS entry or %NULL if not found
851 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
853 const u8 *bssid)
854{
855 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
857 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
859 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
860 return bss;
861 }
862 return NULL;
863}
864
865
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700866/**
867 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
868 * @wpa_s: Pointer to wpa_supplicant data
869 * @bssid: BSSID
870 * Returns: Pointer to the BSS entry or %NULL if not found
871 *
872 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
873 * find the entry that has the most recent update. This can help in finding the
874 * correct entry in cases where the SSID of the AP may have changed recently
875 * (e.g., in WPS reconfiguration cases).
876 */
877struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
878 const u8 *bssid)
879{
880 struct wpa_bss *bss, *found = NULL;
881 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
882 return NULL;
883 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
884 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
885 continue;
886 if (found == NULL ||
887 os_time_before(&found->last_update, &bss->last_update))
888 found = bss;
889 }
890 return found;
891}
892
893
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800894#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800895/**
896 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
897 * @wpa_s: Pointer to wpa_supplicant data
898 * @dev_addr: P2P Device Address of the GO
899 * Returns: Pointer to the BSS entry or %NULL if not found
900 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800901struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
902 const u8 *dev_addr)
903{
904 struct wpa_bss *bss;
905 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
906 u8 addr[ETH_ALEN];
907 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
908 addr) == 0 &&
909 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
910 return bss;
911 }
912 return NULL;
913}
914#endif /* CONFIG_P2P */
915
916
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800917/**
918 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
919 * @wpa_s: Pointer to wpa_supplicant data
920 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
921 * Returns: Pointer to the BSS entry or %NULL if not found
922 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
924{
925 struct wpa_bss *bss;
926 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
927 if (bss->id == id)
928 return bss;
929 }
930 return NULL;
931}
932
933
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800934/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800935 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
936 * @wpa_s: Pointer to wpa_supplicant data
937 * @idf: Smallest allowed identifier assigned for the entry
938 * @idf: Largest allowed identifier assigned for the entry
939 * Returns: Pointer to the BSS entry or %NULL if not found
940 *
941 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
942 * smallest id value to be fetched within the specified range without the
943 * caller having to know the exact id.
944 */
945struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
946 unsigned int idf, unsigned int idl)
947{
948 struct wpa_bss *bss;
949 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
950 if (bss->id >= idf && bss->id <= idl)
951 return bss;
952 }
953 return NULL;
954}
955
956
957/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800958 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
959 * @bss: BSS table entry
960 * @ie: Information element identitifier (WLAN_EID_*)
961 * Returns: Pointer to the information element (id field) or %NULL if not found
962 *
963 * This function returns the first matching information element in the BSS
964 * entry.
965 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
967{
968 const u8 *end, *pos;
969
970 pos = (const u8 *) (bss + 1);
971 end = pos + bss->ie_len;
972
973 while (pos + 1 < end) {
974 if (pos + 2 + pos[1] > end)
975 break;
976 if (pos[0] == ie)
977 return pos;
978 pos += 2 + pos[1];
979 }
980
981 return NULL;
982}
983
984
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800985/**
986 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
987 * @bss: BSS table entry
988 * @vendor_type: Vendor type (four octets starting the IE payload)
989 * Returns: Pointer to the information element (id field) or %NULL if not found
990 *
991 * This function returns the first matching information element in the BSS
992 * entry.
993 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
995{
996 const u8 *end, *pos;
997
998 pos = (const u8 *) (bss + 1);
999 end = pos + bss->ie_len;
1000
1001 while (pos + 1 < end) {
1002 if (pos + 2 + pos[1] > end)
1003 break;
1004 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1005 vendor_type == WPA_GET_BE32(&pos[2]))
1006 return pos;
1007 pos += 2 + pos[1];
1008 }
1009
1010 return NULL;
1011}
1012
1013
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001014/**
1015 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1016 * @bss: BSS table entry
1017 * @vendor_type: Vendor type (four octets starting the IE payload)
1018 * Returns: Pointer to the information element payload or %NULL if not found
1019 *
1020 * This function returns concatenated payload of possibly fragmented vendor
1021 * specific information elements in the BSS entry. The caller is responsible for
1022 * freeing the returned buffer.
1023 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1025 u32 vendor_type)
1026{
1027 struct wpabuf *buf;
1028 const u8 *end, *pos;
1029
1030 buf = wpabuf_alloc(bss->ie_len);
1031 if (buf == NULL)
1032 return NULL;
1033
1034 pos = (const u8 *) (bss + 1);
1035 end = pos + bss->ie_len;
1036
1037 while (pos + 1 < end) {
1038 if (pos + 2 + pos[1] > end)
1039 break;
1040 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1041 vendor_type == WPA_GET_BE32(&pos[2]))
1042 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1043 pos += 2 + pos[1];
1044 }
1045
1046 if (wpabuf_len(buf) == 0) {
1047 wpabuf_free(buf);
1048 buf = NULL;
1049 }
1050
1051 return buf;
1052}
1053
1054
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001055/**
1056 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1057 * @bss: BSS table entry
1058 * @vendor_type: Vendor type (four octets starting the IE payload)
1059 * Returns: Pointer to the information element payload or %NULL if not found
1060 *
1061 * This function returns concatenated payload of possibly fragmented vendor
1062 * specific information elements in the BSS entry. The caller is responsible for
1063 * freeing the returned buffer.
1064 *
1065 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1066 * from Beacon frames instead of either Beacon or Probe Response frames.
1067 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001068struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1069 u32 vendor_type)
1070{
1071 struct wpabuf *buf;
1072 const u8 *end, *pos;
1073
1074 buf = wpabuf_alloc(bss->beacon_ie_len);
1075 if (buf == NULL)
1076 return NULL;
1077
1078 pos = (const u8 *) (bss + 1);
1079 pos += bss->ie_len;
1080 end = pos + bss->beacon_ie_len;
1081
1082 while (pos + 1 < end) {
1083 if (pos + 2 + pos[1] > end)
1084 break;
1085 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1086 vendor_type == WPA_GET_BE32(&pos[2]))
1087 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1088 pos += 2 + pos[1];
1089 }
1090
1091 if (wpabuf_len(buf) == 0) {
1092 wpabuf_free(buf);
1093 buf = NULL;
1094 }
1095
1096 return buf;
1097}
1098
1099
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001100/**
1101 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1102 * @bss: BSS table entry
1103 * Returns: Maximum legacy rate in units of 500 kbps
1104 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1106{
1107 int rate = 0;
1108 const u8 *ie;
1109 int i;
1110
1111 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1112 for (i = 0; ie && i < ie[1]; i++) {
1113 if ((ie[i + 2] & 0x7f) > rate)
1114 rate = ie[i + 2] & 0x7f;
1115 }
1116
1117 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1118 for (i = 0; ie && i < ie[1]; i++) {
1119 if ((ie[i + 2] & 0x7f) > rate)
1120 rate = ie[i + 2] & 0x7f;
1121 }
1122
1123 return rate;
1124}
1125
1126
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001127/**
1128 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1129 * @bss: BSS table entry
1130 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1131 * Returns: number of legacy TX rates or -1 on failure
1132 *
1133 * The caller is responsible for freeing the returned buffer with os_free() in
1134 * case of success.
1135 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1137{
1138 const u8 *ie, *ie2;
1139 int i, j;
1140 unsigned int len;
1141 u8 *r;
1142
1143 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1144 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1145
1146 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1147
1148 r = os_malloc(len);
1149 if (!r)
1150 return -1;
1151
1152 for (i = 0; ie && i < ie[1]; i++)
1153 r[i] = ie[i + 2] & 0x7f;
1154
1155 for (j = 0; ie2 && j < ie2[1]; j++)
1156 r[i + j] = ie2[j + 2] & 0x7f;
1157
1158 *rates = r;
1159 return len;
1160}