blob: 0e1576b0fde1a71f2478665594631bac4fed28bf [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
346 dl_list_add_tail(&wpa_s->bss, &bss->list);
347 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
348 wpa_s->num_bss++;
349 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
350 " SSID '%s'",
351 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
352 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700353 if (wpa_s->num_bss > wpa_s->conf->bss_max_count &&
354 wpa_bss_remove_oldest(wpa_s) != 0) {
355 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
356 "because all BSSes are in use. We should normally "
357 "not get here!", (int) wpa_s->num_bss);
358 wpa_s->conf->bss_max_count = wpa_s->num_bss;
359 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700360 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361}
362
363
364static int are_ies_equal(const struct wpa_bss *old,
365 const struct wpa_scan_res *new, u32 ie)
366{
367 const u8 *old_ie, *new_ie;
368 struct wpabuf *old_ie_buff = NULL;
369 struct wpabuf *new_ie_buff = NULL;
370 int new_ie_len, old_ie_len, ret, is_multi;
371
372 switch (ie) {
373 case WPA_IE_VENDOR_TYPE:
374 old_ie = wpa_bss_get_vendor_ie(old, ie);
375 new_ie = wpa_scan_get_vendor_ie(new, ie);
376 is_multi = 0;
377 break;
378 case WPS_IE_VENDOR_TYPE:
379 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
380 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
381 is_multi = 1;
382 break;
383 case WLAN_EID_RSN:
384 case WLAN_EID_SUPP_RATES:
385 case WLAN_EID_EXT_SUPP_RATES:
386 old_ie = wpa_bss_get_ie(old, ie);
387 new_ie = wpa_scan_get_ie(new, ie);
388 is_multi = 0;
389 break;
390 default:
391 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
392 return 0;
393 }
394
395 if (is_multi) {
396 /* in case of multiple IEs stored in buffer */
397 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
398 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
399 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
400 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
401 } else {
402 /* in case of single IE */
403 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
404 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
405 }
406
407 if (!old_ie || !new_ie)
408 ret = !old_ie && !new_ie;
409 else
410 ret = (old_ie_len == new_ie_len &&
411 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
412
413 wpabuf_free(old_ie_buff);
414 wpabuf_free(new_ie_buff);
415
416 return ret;
417}
418
419
420static u32 wpa_bss_compare_res(const struct wpa_bss *old,
421 const struct wpa_scan_res *new)
422{
423 u32 changes = 0;
424 int caps_diff = old->caps ^ new->caps;
425
426 if (old->freq != new->freq)
427 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
428
429 if (old->level != new->level)
430 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
431
432 if (caps_diff & IEEE80211_CAP_PRIVACY)
433 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
434
435 if (caps_diff & IEEE80211_CAP_IBSS)
436 changes |= WPA_BSS_MODE_CHANGED_FLAG;
437
438 if (old->ie_len == new->ie_len &&
439 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
440 return changes;
441 changes |= WPA_BSS_IES_CHANGED_FLAG;
442
443 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
444 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
445
446 if (!are_ies_equal(old, new, WLAN_EID_RSN))
447 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
448
449 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
450 changes |= WPA_BSS_WPS_CHANGED_FLAG;
451
452 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
453 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
454 changes |= WPA_BSS_RATES_CHANGED_FLAG;
455
456 return changes;
457}
458
459
460static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
461 const struct wpa_bss *bss)
462{
463 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
464 wpas_notify_bss_freq_changed(wpa_s, bss->id);
465
466 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
467 wpas_notify_bss_signal_changed(wpa_s, bss->id);
468
469 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
470 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
471
472 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
473 wpas_notify_bss_mode_changed(wpa_s, bss->id);
474
475 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
476 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
477
478 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
479 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
480
481 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
482 wpas_notify_bss_wps_changed(wpa_s, bss->id);
483
484 if (changes & WPA_BSS_IES_CHANGED_FLAG)
485 wpas_notify_bss_ies_changed(wpa_s, bss->id);
486
487 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
488 wpas_notify_bss_rates_changed(wpa_s, bss->id);
489}
490
491
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700492static struct wpa_bss *
493wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800494 struct wpa_scan_res *res, struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495{
496 u32 changes;
497
498 changes = wpa_bss_compare_res(bss, res);
499 bss->scan_miss_count = 0;
500 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800501 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 /* Move the entry to the end of the list */
503 dl_list_del(&bss->list);
504 if (bss->ie_len + bss->beacon_ie_len >=
505 res->ie_len + res->beacon_ie_len) {
506 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
507 bss->ie_len = res->ie_len;
508 bss->beacon_ie_len = res->beacon_ie_len;
509 } else {
510 struct wpa_bss *nbss;
511 struct dl_list *prev = bss->list_id.prev;
512 dl_list_del(&bss->list_id);
513 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
514 res->beacon_ie_len);
515 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700516 unsigned int i;
517 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
518 if (wpa_s->last_scan_res[i] == bss) {
519 wpa_s->last_scan_res[i] = nbss;
520 break;
521 }
522 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700523 if (wpa_s->current_bss == bss)
524 wpa_s->current_bss = nbss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 bss = nbss;
526 os_memcpy(bss + 1, res + 1,
527 res->ie_len + res->beacon_ie_len);
528 bss->ie_len = res->ie_len;
529 bss->beacon_ie_len = res->beacon_ie_len;
530 }
531 dl_list_add(prev, &bss->list_id);
532 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700533 if (changes & WPA_BSS_IES_CHANGED_FLAG)
534 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 dl_list_add_tail(&wpa_s->bss, &bss->list);
536
537 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700538
539 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540}
541
542
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800543/**
544 * wpa_bss_update_start - Start a BSS table update from scan results
545 * @wpa_s: Pointer to wpa_supplicant data
546 *
547 * This function is called at the start of each BSS table update round for new
548 * scan results. The actual scan result entries are indicated with calls to
549 * wpa_bss_update_scan_res() and the update round is finished with a call to
550 * wpa_bss_update_end().
551 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
553{
554 wpa_s->bss_update_idx++;
555 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
556 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700557 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558}
559
560
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800561/**
562 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
563 * @wpa_s: Pointer to wpa_supplicant data
564 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800565 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800566 *
567 * This function updates a BSS table entry (or adds one) based on a scan result.
568 * This is called separately for each scan result between the calls to
569 * wpa_bss_update_start() and wpa_bss_update_end().
570 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800572 struct wpa_scan_res *res,
573 struct os_time *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574{
575 const u8 *ssid, *p2p;
576 struct wpa_bss *bss;
577
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700578 if (wpa_s->conf->ignore_old_scan_res) {
579 struct os_time update;
580 calculate_update_time(fetch_time, res->age, &update);
581 if (os_time_before(&update, &wpa_s->scan_trigger_time)) {
582 struct os_time age;
583 os_time_sub(&wpa_s->scan_trigger_time, &update, &age);
584 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
585 "table entry that is %u.%06u seconds older "
586 "than our scan trigger",
587 (unsigned int) age.sec,
588 (unsigned int) age.usec);
589 return;
590 }
591 }
592
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
594 if (ssid == NULL) {
595 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
596 MACSTR, MAC2STR(res->bssid));
597 return;
598 }
599 if (ssid[1] > 32) {
600 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
601 MACSTR, MAC2STR(res->bssid));
602 return;
603 }
604
605 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700606#ifdef CONFIG_P2P
607 if (p2p == NULL &&
608 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
609 /*
610 * If it's a P2P specific interface, then don't update
611 * the scan result without a P2P IE.
612 */
613 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
614 " update for P2P interface", MAC2STR(res->bssid));
615 return;
616 }
617#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
619 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
620 return; /* Skip P2P listen discovery results here */
621
622 /* TODO: add option for ignoring BSSes we are not interested in
623 * (to save memory) */
624 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
625 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800626 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 else
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800628 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700629
630 if (bss == NULL)
631 return;
632 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
633 struct wpa_bss **n;
634 unsigned int siz;
635 if (wpa_s->last_scan_res_size == 0)
636 siz = 32;
637 else
638 siz = wpa_s->last_scan_res_size * 2;
639 n = os_realloc_array(wpa_s->last_scan_res, siz,
640 sizeof(struct wpa_bss *));
641 if (n == NULL)
642 return;
643 wpa_s->last_scan_res = n;
644 wpa_s->last_scan_res_size = siz;
645 }
646
647 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648}
649
650
651static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
652 const struct scan_info *info)
653{
654 int found;
655 size_t i;
656
657 if (info == NULL)
658 return 1;
659
660 if (info->num_freqs) {
661 found = 0;
662 for (i = 0; i < info->num_freqs; i++) {
663 if (bss->freq == info->freqs[i]) {
664 found = 1;
665 break;
666 }
667 }
668 if (!found)
669 return 0;
670 }
671
672 if (info->num_ssids) {
673 found = 0;
674 for (i = 0; i < info->num_ssids; i++) {
675 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
676 if ((s->ssid == NULL || s->ssid_len == 0) ||
677 (s->ssid_len == bss->ssid_len &&
678 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
679 0)) {
680 found = 1;
681 break;
682 }
683 }
684 if (!found)
685 return 0;
686 }
687
688 return 1;
689}
690
691
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800692/**
693 * wpa_bss_update_end - End a BSS table update from scan results
694 * @wpa_s: Pointer to wpa_supplicant data
695 * @info: Information about scan parameters
696 * @new_scan: Whether this update round was based on a new scan
697 *
698 * This function is called at the end of each BSS table update round for new
699 * scan results. The start of the update was indicated with a call to
700 * wpa_bss_update_start().
701 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
703 int new_scan)
704{
705 struct wpa_bss *bss, *n;
706
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700707 wpa_s->last_scan_full = 0;
708 os_get_time(&wpa_s->last_scan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 if (!new_scan)
710 return; /* do not expire entries without new scan */
711
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700712 if (info && !info->aborted && !info->freqs) {
713 size_t i;
714 if (info->num_ssids == 0) {
715 wpa_s->last_scan_full = 1;
716 } else {
717 for (i = 0; i < info->num_ssids; i++) {
718 if (info->ssids[i].ssid == NULL ||
719 info->ssids[i].ssid_len == 0) {
720 wpa_s->last_scan_full = 1;
721 break;
722 }
723 }
724 }
725 }
726
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
728 if (wpa_bss_in_use(wpa_s, bss))
729 continue;
730 if (!wpa_bss_included_in_scan(bss, info))
731 continue; /* expire only BSSes that were scanned */
732 if (bss->last_update_idx < wpa_s->bss_update_idx)
733 bss->scan_miss_count++;
734 if (bss->scan_miss_count >=
735 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700736 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 }
738 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700739
740 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u "
741 "last_scan_full=%d",
742 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size,
743 wpa_s->last_scan_full);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744}
745
746
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800747/**
748 * wpa_bss_flush_by_age - Flush old BSS entries
749 * @wpa_s: Pointer to wpa_supplicant data
750 * @age: Maximum entry age in seconds
751 *
752 * Remove BSS entries that have not been updated during the last @age seconds.
753 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
755{
756 struct wpa_bss *bss, *n;
757 struct os_time t;
758
759 if (dl_list_empty(&wpa_s->bss))
760 return;
761
762 os_get_time(&t);
763 t.sec -= age;
764
765 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
766 if (wpa_bss_in_use(wpa_s, bss))
767 continue;
768
769 if (os_time_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700770 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 } else
772 break;
773 }
774}
775
776
777static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
778{
779 struct wpa_supplicant *wpa_s = eloop_ctx;
780
781 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
782 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
783 wpa_bss_timeout, wpa_s, NULL);
784}
785
786
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800787/**
788 * wpa_bss_init - Initialize BSS table
789 * @wpa_s: Pointer to wpa_supplicant data
790 * Returns: 0 on success, -1 on failure
791 *
792 * This prepares BSS table lists and timer for periodic updates. The BSS table
793 * is deinitialized with wpa_bss_deinit() once not needed anymore.
794 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795int wpa_bss_init(struct wpa_supplicant *wpa_s)
796{
797 dl_list_init(&wpa_s->bss);
798 dl_list_init(&wpa_s->bss_id);
799 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
800 wpa_bss_timeout, wpa_s, NULL);
801 return 0;
802}
803
804
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800805/**
806 * wpa_bss_flush - Flush all unused BSS entries
807 * @wpa_s: Pointer to wpa_supplicant data
808 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809void wpa_bss_flush(struct wpa_supplicant *wpa_s)
810{
811 struct wpa_bss *bss, *n;
812
813 if (wpa_s->bss.next == NULL)
814 return; /* BSS table not yet initialized */
815
816 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
817 if (wpa_bss_in_use(wpa_s, bss))
818 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700819 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700820 }
821}
822
823
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800824/**
825 * wpa_bss_deinit - Deinitialize BSS table
826 * @wpa_s: Pointer to wpa_supplicant data
827 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
829{
830 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
831 wpa_bss_flush(wpa_s);
832}
833
834
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800835/**
836 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
837 * @wpa_s: Pointer to wpa_supplicant data
838 * @bssid: BSSID
839 * Returns: Pointer to the BSS entry or %NULL if not found
840 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
842 const u8 *bssid)
843{
844 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700845 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
846 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
848 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
849 return bss;
850 }
851 return NULL;
852}
853
854
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700855/**
856 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
857 * @wpa_s: Pointer to wpa_supplicant data
858 * @bssid: BSSID
859 * Returns: Pointer to the BSS entry or %NULL if not found
860 *
861 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
862 * find the entry that has the most recent update. This can help in finding the
863 * correct entry in cases where the SSID of the AP may have changed recently
864 * (e.g., in WPS reconfiguration cases).
865 */
866struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
867 const u8 *bssid)
868{
869 struct wpa_bss *bss, *found = NULL;
870 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
871 return NULL;
872 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
873 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
874 continue;
875 if (found == NULL ||
876 os_time_before(&found->last_update, &bss->last_update))
877 found = bss;
878 }
879 return found;
880}
881
882
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800883#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800884/**
885 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
886 * @wpa_s: Pointer to wpa_supplicant data
887 * @dev_addr: P2P Device Address of the GO
888 * Returns: Pointer to the BSS entry or %NULL if not found
889 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800890struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
891 const u8 *dev_addr)
892{
893 struct wpa_bss *bss;
894 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
895 u8 addr[ETH_ALEN];
896 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
897 addr) == 0 &&
898 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
899 return bss;
900 }
901 return NULL;
902}
903#endif /* CONFIG_P2P */
904
905
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800906/**
907 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
908 * @wpa_s: Pointer to wpa_supplicant data
909 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
910 * Returns: Pointer to the BSS entry or %NULL if not found
911 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
913{
914 struct wpa_bss *bss;
915 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
916 if (bss->id == id)
917 return bss;
918 }
919 return NULL;
920}
921
922
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800923/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800924 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
925 * @wpa_s: Pointer to wpa_supplicant data
926 * @idf: Smallest allowed identifier assigned for the entry
927 * @idf: Largest allowed identifier assigned for the entry
928 * Returns: Pointer to the BSS entry or %NULL if not found
929 *
930 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
931 * smallest id value to be fetched within the specified range without the
932 * caller having to know the exact id.
933 */
934struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
935 unsigned int idf, unsigned int idl)
936{
937 struct wpa_bss *bss;
938 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
939 if (bss->id >= idf && bss->id <= idl)
940 return bss;
941 }
942 return NULL;
943}
944
945
946/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800947 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
948 * @bss: BSS table entry
949 * @ie: Information element identitifier (WLAN_EID_*)
950 * Returns: Pointer to the information element (id field) or %NULL if not found
951 *
952 * This function returns the first matching information element in the BSS
953 * entry.
954 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700955const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
956{
957 const u8 *end, *pos;
958
959 pos = (const u8 *) (bss + 1);
960 end = pos + bss->ie_len;
961
962 while (pos + 1 < end) {
963 if (pos + 2 + pos[1] > end)
964 break;
965 if (pos[0] == ie)
966 return pos;
967 pos += 2 + pos[1];
968 }
969
970 return NULL;
971}
972
973
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800974/**
975 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
976 * @bss: BSS table entry
977 * @vendor_type: Vendor type (four octets starting the IE payload)
978 * Returns: Pointer to the information element (id field) or %NULL if not found
979 *
980 * This function returns the first matching information element in the BSS
981 * entry.
982 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
984{
985 const u8 *end, *pos;
986
987 pos = (const u8 *) (bss + 1);
988 end = pos + bss->ie_len;
989
990 while (pos + 1 < end) {
991 if (pos + 2 + pos[1] > end)
992 break;
993 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
994 vendor_type == WPA_GET_BE32(&pos[2]))
995 return pos;
996 pos += 2 + pos[1];
997 }
998
999 return NULL;
1000}
1001
1002
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001003/**
1004 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1005 * @bss: BSS table entry
1006 * @vendor_type: Vendor type (four octets starting the IE payload)
1007 * Returns: Pointer to the information element payload or %NULL if not found
1008 *
1009 * This function returns concatenated payload of possibly fragmented vendor
1010 * specific information elements in the BSS entry. The caller is responsible for
1011 * freeing the returned buffer.
1012 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1014 u32 vendor_type)
1015{
1016 struct wpabuf *buf;
1017 const u8 *end, *pos;
1018
1019 buf = wpabuf_alloc(bss->ie_len);
1020 if (buf == NULL)
1021 return NULL;
1022
1023 pos = (const u8 *) (bss + 1);
1024 end = pos + bss->ie_len;
1025
1026 while (pos + 1 < end) {
1027 if (pos + 2 + pos[1] > end)
1028 break;
1029 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1030 vendor_type == WPA_GET_BE32(&pos[2]))
1031 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1032 pos += 2 + pos[1];
1033 }
1034
1035 if (wpabuf_len(buf) == 0) {
1036 wpabuf_free(buf);
1037 buf = NULL;
1038 }
1039
1040 return buf;
1041}
1042
1043
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001044/**
1045 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1046 * @bss: BSS table entry
1047 * @vendor_type: Vendor type (four octets starting the IE payload)
1048 * Returns: Pointer to the information element payload or %NULL if not found
1049 *
1050 * This function returns concatenated payload of possibly fragmented vendor
1051 * specific information elements in the BSS entry. The caller is responsible for
1052 * freeing the returned buffer.
1053 *
1054 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1055 * from Beacon frames instead of either Beacon or Probe Response frames.
1056 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001057struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1058 u32 vendor_type)
1059{
1060 struct wpabuf *buf;
1061 const u8 *end, *pos;
1062
1063 buf = wpabuf_alloc(bss->beacon_ie_len);
1064 if (buf == NULL)
1065 return NULL;
1066
1067 pos = (const u8 *) (bss + 1);
1068 pos += bss->ie_len;
1069 end = pos + bss->beacon_ie_len;
1070
1071 while (pos + 1 < end) {
1072 if (pos + 2 + pos[1] > end)
1073 break;
1074 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1075 vendor_type == WPA_GET_BE32(&pos[2]))
1076 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1077 pos += 2 + pos[1];
1078 }
1079
1080 if (wpabuf_len(buf) == 0) {
1081 wpabuf_free(buf);
1082 buf = NULL;
1083 }
1084
1085 return buf;
1086}
1087
1088
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001089/**
1090 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1091 * @bss: BSS table entry
1092 * Returns: Maximum legacy rate in units of 500 kbps
1093 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1095{
1096 int rate = 0;
1097 const u8 *ie;
1098 int i;
1099
1100 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1101 for (i = 0; ie && i < ie[1]; i++) {
1102 if ((ie[i + 2] & 0x7f) > rate)
1103 rate = ie[i + 2] & 0x7f;
1104 }
1105
1106 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1107 for (i = 0; ie && i < ie[1]; i++) {
1108 if ((ie[i + 2] & 0x7f) > rate)
1109 rate = ie[i + 2] & 0x7f;
1110 }
1111
1112 return rate;
1113}
1114
1115
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001116/**
1117 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1118 * @bss: BSS table entry
1119 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1120 * Returns: number of legacy TX rates or -1 on failure
1121 *
1122 * The caller is responsible for freeing the returned buffer with os_free() in
1123 * case of success.
1124 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001125int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1126{
1127 const u8 *ie, *ie2;
1128 int i, j;
1129 unsigned int len;
1130 u8 *r;
1131
1132 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1133 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1134
1135 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1136
1137 r = os_malloc(len);
1138 if (!r)
1139 return -1;
1140
1141 for (i = 0; ie && i < ie[1]; i++)
1142 r[i] = ie[i + 2] & 0x7f;
1143
1144 for (j = 0; ie2 && j < ie2[1]; j++)
1145 r[i + j] = ie2[j + 2] & 0x7f;
1146
1147 *rates = r;
1148 return len;
1149}