blob: 482fc64f7ada053fb979c2aca41ec181593f1fd3 [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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800101 ANQP_DUP(hs20_osu_providers_list);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800102#endif /* CONFIG_HS20 */
103#undef ANQP_DUP
104
105 return n;
106}
107
108
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800109/**
110 * wpa_bss_anqp_unshare_alloc - Unshare ANQP data (if shared) in a BSS entry
111 * @bss: BSS entry
112 * Returns: 0 on success, -1 on failure
113 *
114 * This function ensures the specific BSS entry has an ANQP data structure that
115 * is not shared with any other BSS entry.
116 */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800117int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
118{
119 struct wpa_bss_anqp *anqp;
120
121 if (bss->anqp && bss->anqp->users > 1) {
122 /* allocated, but shared - clone an unshared copy */
123 anqp = wpa_bss_anqp_clone(bss->anqp);
124 if (anqp == NULL)
125 return -1;
126 anqp->users = 1;
127 bss->anqp->users--;
128 bss->anqp = anqp;
129 return 0;
130 }
131
132 if (bss->anqp)
133 return 0; /* already allocated and not shared */
134
135 /* not allocated - allocate a new storage area */
136 bss->anqp = wpa_bss_anqp_alloc();
137 return bss->anqp ? 0 : -1;
138}
139
140
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800141/**
142 * wpa_bss_anqp_free - Free an ANQP data structure
143 * @anqp: ANQP data structure from wpa_bss_anqp_alloc() or wpa_bss_anqp_clone()
144 */
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700145static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
146{
147 if (anqp == NULL)
148 return;
149
150 anqp->users--;
151 if (anqp->users > 0) {
152 /* Another BSS entry holds a pointer to this ANQP info */
153 return;
154 }
155
156#ifdef CONFIG_INTERWORKING
157 wpabuf_free(anqp->venue_name);
158 wpabuf_free(anqp->network_auth_type);
159 wpabuf_free(anqp->roaming_consortium);
160 wpabuf_free(anqp->ip_addr_type_availability);
161 wpabuf_free(anqp->nai_realm);
162 wpabuf_free(anqp->anqp_3gpp);
163 wpabuf_free(anqp->domain_name);
164#endif /* CONFIG_INTERWORKING */
165#ifdef CONFIG_HS20
166 wpabuf_free(anqp->hs20_operator_friendly_name);
167 wpabuf_free(anqp->hs20_wan_metrics);
168 wpabuf_free(anqp->hs20_connection_capability);
169 wpabuf_free(anqp->hs20_operating_class);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800170 wpabuf_free(anqp->hs20_osu_providers_list);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700171#endif /* CONFIG_HS20 */
172
173 os_free(anqp);
174}
175
176
Dmitry Shmidt04949592012-07-19 12:16:46 -0700177static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
178 const char *reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179{
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700180 if (wpa_s->last_scan_res) {
181 unsigned int i;
182 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
183 if (wpa_s->last_scan_res[i] == bss) {
184 os_memmove(&wpa_s->last_scan_res[i],
185 &wpa_s->last_scan_res[i + 1],
186 (wpa_s->last_scan_res_used - i - 1)
187 * sizeof(struct wpa_bss *));
188 wpa_s->last_scan_res_used--;
189 break;
190 }
191 }
192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193 dl_list_del(&bss->list);
194 dl_list_del(&bss->list_id);
195 wpa_s->num_bss--;
196 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR
Dmitry Shmidt04949592012-07-19 12:16:46 -0700197 " SSID '%s' due to %s", bss->id, MAC2STR(bss->bssid),
198 wpa_ssid_txt(bss->ssid, bss->ssid_len), reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700200 wpa_bss_anqp_free(bss->anqp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 os_free(bss);
202}
203
204
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800205/**
206 * wpa_bss_get - Fetch a BSS table entry based on BSSID and SSID
207 * @wpa_s: Pointer to wpa_supplicant data
208 * @bssid: BSSID
209 * @ssid: SSID
210 * @ssid_len: Length of @ssid
211 * Returns: Pointer to the BSS entry or %NULL if not found
212 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700213struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
214 const u8 *ssid, size_t ssid_len)
215{
216 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700217 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
218 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700219 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
220 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
221 bss->ssid_len == ssid_len &&
222 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
223 return bss;
224 }
225 return NULL;
226}
227
228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800229static void calculate_update_time(const struct os_reltime *fetch_time,
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700230 unsigned int age_ms,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800231 struct os_reltime *update_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232{
233 os_time_t usec;
234
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700235 update_time->sec = fetch_time->sec;
236 update_time->usec = fetch_time->usec;
237 update_time->sec -= age_ms / 1000;
238 usec = (age_ms % 1000) * 1000;
239 if (update_time->usec < usec) {
240 update_time->sec--;
241 update_time->usec += 1000000;
242 }
243 update_time->usec -= usec;
244}
245
246
247static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800248 struct os_reltime *fetch_time)
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700249{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 dst->flags = src->flags;
251 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
252 dst->freq = src->freq;
253 dst->beacon_int = src->beacon_int;
254 dst->caps = src->caps;
255 dst->qual = src->qual;
256 dst->noise = src->noise;
257 dst->level = src->level;
258 dst->tsf = src->tsf;
259
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700260 calculate_update_time(fetch_time, src->age, &dst->last_update);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261}
262
263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800264static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
265{
266 struct wpa_ssid *ssid;
267
268 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
269 if (ssid->ssid == NULL || ssid->ssid_len == 0)
270 continue;
271 if (ssid->ssid_len == bss->ssid_len &&
272 os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) == 0)
273 return 1;
274 }
275
276 return 0;
277}
278
279
Dmitry Shmidt04949592012-07-19 12:16:46 -0700280static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
281{
282 return bss == wpa_s->current_bss ||
283 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
284 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
285}
286
287
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800288static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
289{
290 struct wpa_bss *bss;
291
292 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
293 if (!wpa_bss_known(wpa_s, bss)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700294 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800295 return 0;
296 }
297 }
298
299 return -1;
300}
301
302
Dmitry Shmidt04949592012-07-19 12:16:46 -0700303static int wpa_bss_remove_oldest(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304{
Dmitry Shmidt04949592012-07-19 12:16:46 -0700305 struct wpa_bss *bss;
306
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800307 /*
308 * Remove the oldest entry that does not match with any configured
309 * network.
310 */
311 if (wpa_bss_remove_oldest_unknown(wpa_s) == 0)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700312 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313
314 /*
Dmitry Shmidt04949592012-07-19 12:16:46 -0700315 * Remove the oldest entry that isn't currently in use.
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800316 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
318 if (!wpa_bss_in_use(wpa_s, bss)) {
319 wpa_bss_remove(wpa_s, bss, __func__);
320 return 0;
321 }
322 }
323
324 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325}
326
327
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700328static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
329 const u8 *ssid, size_t ssid_len,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800330 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800331 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332{
333 struct wpa_bss *bss;
334
335 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
336 if (bss == NULL)
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700337 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700338 bss->id = wpa_s->bss_next_id++;
339 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800340 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 os_memcpy(bss->ssid, ssid, ssid_len);
342 bss->ssid_len = ssid_len;
343 bss->ie_len = res->ie_len;
344 bss->beacon_ie_len = res->beacon_ie_len;
345 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700346 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347
Jouni Malinen7a6c8302013-09-27 15:47:09 +0300348 if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
349 wpa_bss_remove_oldest(wpa_s) != 0) {
350 wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
351 "because all BSSes are in use. We should normally "
352 "not get here!", (int) wpa_s->num_bss + 1);
353 wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
354 }
355
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 dl_list_add_tail(&wpa_s->bss, &bss->list);
357 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
358 wpa_s->num_bss++;
359 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
360 " SSID '%s'",
361 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
362 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700363 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364}
365
366
367static int are_ies_equal(const struct wpa_bss *old,
368 const struct wpa_scan_res *new, u32 ie)
369{
370 const u8 *old_ie, *new_ie;
371 struct wpabuf *old_ie_buff = NULL;
372 struct wpabuf *new_ie_buff = NULL;
373 int new_ie_len, old_ie_len, ret, is_multi;
374
375 switch (ie) {
376 case WPA_IE_VENDOR_TYPE:
377 old_ie = wpa_bss_get_vendor_ie(old, ie);
378 new_ie = wpa_scan_get_vendor_ie(new, ie);
379 is_multi = 0;
380 break;
381 case WPS_IE_VENDOR_TYPE:
382 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
383 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
384 is_multi = 1;
385 break;
386 case WLAN_EID_RSN:
387 case WLAN_EID_SUPP_RATES:
388 case WLAN_EID_EXT_SUPP_RATES:
389 old_ie = wpa_bss_get_ie(old, ie);
390 new_ie = wpa_scan_get_ie(new, ie);
391 is_multi = 0;
392 break;
393 default:
394 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
395 return 0;
396 }
397
398 if (is_multi) {
399 /* in case of multiple IEs stored in buffer */
400 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
401 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
402 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
403 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
404 } else {
405 /* in case of single IE */
406 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
407 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
408 }
409
410 if (!old_ie || !new_ie)
411 ret = !old_ie && !new_ie;
412 else
413 ret = (old_ie_len == new_ie_len &&
414 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
415
416 wpabuf_free(old_ie_buff);
417 wpabuf_free(new_ie_buff);
418
419 return ret;
420}
421
422
423static u32 wpa_bss_compare_res(const struct wpa_bss *old,
424 const struct wpa_scan_res *new)
425{
426 u32 changes = 0;
427 int caps_diff = old->caps ^ new->caps;
428
429 if (old->freq != new->freq)
430 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
431
432 if (old->level != new->level)
433 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
434
435 if (caps_diff & IEEE80211_CAP_PRIVACY)
436 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
437
438 if (caps_diff & IEEE80211_CAP_IBSS)
439 changes |= WPA_BSS_MODE_CHANGED_FLAG;
440
441 if (old->ie_len == new->ie_len &&
442 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
443 return changes;
444 changes |= WPA_BSS_IES_CHANGED_FLAG;
445
446 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
447 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
448
449 if (!are_ies_equal(old, new, WLAN_EID_RSN))
450 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
451
452 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
453 changes |= WPA_BSS_WPS_CHANGED_FLAG;
454
455 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
456 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
457 changes |= WPA_BSS_RATES_CHANGED_FLAG;
458
459 return changes;
460}
461
462
463static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
464 const struct wpa_bss *bss)
465{
466 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
467 wpas_notify_bss_freq_changed(wpa_s, bss->id);
468
469 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
470 wpas_notify_bss_signal_changed(wpa_s, bss->id);
471
472 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
473 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
474
475 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
476 wpas_notify_bss_mode_changed(wpa_s, bss->id);
477
478 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
479 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
480
481 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
482 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
483
484 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
485 wpas_notify_bss_wps_changed(wpa_s, bss->id);
486
487 if (changes & WPA_BSS_IES_CHANGED_FLAG)
488 wpas_notify_bss_ies_changed(wpa_s, bss->id);
489
490 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
491 wpas_notify_bss_rates_changed(wpa_s, bss->id);
492}
493
494
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700495static struct wpa_bss *
496wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800497 struct wpa_scan_res *res, struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498{
499 u32 changes;
500
501 changes = wpa_bss_compare_res(bss, res);
502 bss->scan_miss_count = 0;
503 bss->last_update_idx = wpa_s->bss_update_idx;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800504 wpa_bss_copy_res(bss, res, fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 /* Move the entry to the end of the list */
506 dl_list_del(&bss->list);
Dmitry Shmidt96571392013-10-14 12:54:46 -0700507#ifdef CONFIG_P2P
508 if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
509 !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
510 /*
511 * This can happen when non-P2P station interface runs a scan
512 * without P2P IE in the Probe Request frame. P2P GO would reply
513 * to that with a Probe Response that does not include P2P IE.
514 * Do not update the IEs in this BSS entry to avoid such loss of
515 * information that may be needed for P2P operations to
516 * determine group information.
517 */
518 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
519 MACSTR " since that would remove P2P IE information",
520 MAC2STR(bss->bssid));
521 } else
522#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 if (bss->ie_len + bss->beacon_ie_len >=
524 res->ie_len + res->beacon_ie_len) {
525 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
526 bss->ie_len = res->ie_len;
527 bss->beacon_ie_len = res->beacon_ie_len;
528 } else {
529 struct wpa_bss *nbss;
530 struct dl_list *prev = bss->list_id.prev;
531 dl_list_del(&bss->list_id);
532 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
533 res->beacon_ie_len);
534 if (nbss) {
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700535 unsigned int i;
536 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
537 if (wpa_s->last_scan_res[i] == bss) {
538 wpa_s->last_scan_res[i] = nbss;
539 break;
540 }
541 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700542 if (wpa_s->current_bss == bss)
543 wpa_s->current_bss = nbss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 bss = nbss;
545 os_memcpy(bss + 1, res + 1,
546 res->ie_len + res->beacon_ie_len);
547 bss->ie_len = res->ie_len;
548 bss->beacon_ie_len = res->beacon_ie_len;
549 }
550 dl_list_add(prev, &bss->list_id);
551 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700552 if (changes & WPA_BSS_IES_CHANGED_FLAG)
553 wpa_bss_set_hessid(bss);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 dl_list_add_tail(&wpa_s->bss, &bss->list);
555
556 notify_bss_changes(wpa_s, changes, bss);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700557
558 return bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559}
560
561
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800562/**
563 * wpa_bss_update_start - Start a BSS table update from scan results
564 * @wpa_s: Pointer to wpa_supplicant data
565 *
566 * This function is called at the start of each BSS table update round for new
567 * scan results. The actual scan result entries are indicated with calls to
568 * wpa_bss_update_scan_res() and the update round is finished with a call to
569 * wpa_bss_update_end().
570 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700571void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
572{
573 wpa_s->bss_update_idx++;
574 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Start scan result update %u",
575 wpa_s->bss_update_idx);
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700576 wpa_s->last_scan_res_used = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577}
578
579
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800580/**
581 * wpa_bss_update_scan_res - Update a BSS table entry based on a scan result
582 * @wpa_s: Pointer to wpa_supplicant data
583 * @res: Scan result
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800584 * @fetch_time: Time when the result was fetched from the driver
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800585 *
586 * This function updates a BSS table entry (or adds one) based on a scan result.
587 * This is called separately for each scan result between the calls to
588 * wpa_bss_update_start() and wpa_bss_update_end().
589 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800591 struct wpa_scan_res *res,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800592 struct os_reltime *fetch_time)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593{
594 const u8 *ssid, *p2p;
595 struct wpa_bss *bss;
596
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700597 if (wpa_s->conf->ignore_old_scan_res) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800598 struct os_reltime update;
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700599 calculate_update_time(fetch_time, res->age, &update);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800600 if (os_reltime_before(&update, &wpa_s->scan_trigger_time)) {
601 struct os_reltime age;
602 os_reltime_sub(&wpa_s->scan_trigger_time, &update,
603 &age);
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700604 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
605 "table entry that is %u.%06u seconds older "
606 "than our scan trigger",
607 (unsigned int) age.sec,
608 (unsigned int) age.usec);
609 return;
610 }
611 }
612
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
614 if (ssid == NULL) {
615 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
616 MACSTR, MAC2STR(res->bssid));
617 return;
618 }
619 if (ssid[1] > 32) {
620 wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
621 MACSTR, MAC2STR(res->bssid));
622 return;
623 }
624
625 p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700626#ifdef CONFIG_P2P
627 if (p2p == NULL &&
628 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
629 /*
630 * If it's a P2P specific interface, then don't update
631 * the scan result without a P2P IE.
632 */
633 wpa_printf(MSG_DEBUG, "BSS: No P2P IE - skipping BSS " MACSTR
634 " update for P2P interface", MAC2STR(res->bssid));
635 return;
636 }
637#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN &&
639 os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0)
640 return; /* Skip P2P listen discovery results here */
641
642 /* TODO: add option for ignoring BSSes we are not interested in
643 * (to save memory) */
644 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
645 if (bss == NULL)
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800646 bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700647 else {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800648 bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
Dmitry Shmidt56052862013-10-04 10:23:25 -0700649 if (wpa_s->last_scan_res) {
650 unsigned int i;
651 for (i = 0; i < wpa_s->last_scan_res_used; i++) {
652 if (bss == wpa_s->last_scan_res[i]) {
653 /* Already in the list */
654 return;
655 }
656 }
657 }
658 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700659
660 if (bss == NULL)
661 return;
662 if (wpa_s->last_scan_res_used >= wpa_s->last_scan_res_size) {
663 struct wpa_bss **n;
664 unsigned int siz;
665 if (wpa_s->last_scan_res_size == 0)
666 siz = 32;
667 else
668 siz = wpa_s->last_scan_res_size * 2;
669 n = os_realloc_array(wpa_s->last_scan_res, siz,
670 sizeof(struct wpa_bss *));
671 if (n == NULL)
672 return;
673 wpa_s->last_scan_res = n;
674 wpa_s->last_scan_res_size = siz;
675 }
676
677 wpa_s->last_scan_res[wpa_s->last_scan_res_used++] = bss;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678}
679
680
681static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
682 const struct scan_info *info)
683{
684 int found;
685 size_t i;
686
687 if (info == NULL)
688 return 1;
689
690 if (info->num_freqs) {
691 found = 0;
692 for (i = 0; i < info->num_freqs; i++) {
693 if (bss->freq == info->freqs[i]) {
694 found = 1;
695 break;
696 }
697 }
698 if (!found)
699 return 0;
700 }
701
702 if (info->num_ssids) {
703 found = 0;
704 for (i = 0; i < info->num_ssids; i++) {
705 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
706 if ((s->ssid == NULL || s->ssid_len == 0) ||
707 (s->ssid_len == bss->ssid_len &&
708 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
709 0)) {
710 found = 1;
711 break;
712 }
713 }
714 if (!found)
715 return 0;
716 }
717
718 return 1;
719}
720
721
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800722/**
723 * wpa_bss_update_end - End a BSS table update from scan results
724 * @wpa_s: Pointer to wpa_supplicant data
725 * @info: Information about scan parameters
726 * @new_scan: Whether this update round was based on a new scan
727 *
728 * This function is called at the end of each BSS table update round for new
729 * scan results. The start of the update was indicated with a call to
730 * wpa_bss_update_start().
731 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700732void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
733 int new_scan)
734{
735 struct wpa_bss *bss, *n;
736
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800737 os_get_reltime(&wpa_s->last_scan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 if (!new_scan)
739 return; /* do not expire entries without new scan */
740
741 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
742 if (wpa_bss_in_use(wpa_s, bss))
743 continue;
744 if (!wpa_bss_included_in_scan(bss, info))
745 continue; /* expire only BSSes that were scanned */
746 if (bss->last_update_idx < wpa_s->bss_update_idx)
747 bss->scan_miss_count++;
748 if (bss->scan_miss_count >=
749 wpa_s->conf->bss_expiration_scan_count) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700750 wpa_bss_remove(wpa_s, bss, "no match in scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 }
752 }
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -0700753
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800754 wpa_printf(MSG_DEBUG, "BSS: last_scan_res_used=%u/%u",
755 wpa_s->last_scan_res_used, wpa_s->last_scan_res_size);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756}
757
758
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800759/**
760 * wpa_bss_flush_by_age - Flush old BSS entries
761 * @wpa_s: Pointer to wpa_supplicant data
762 * @age: Maximum entry age in seconds
763 *
764 * Remove BSS entries that have not been updated during the last @age seconds.
765 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
767{
768 struct wpa_bss *bss, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800769 struct os_reltime t;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770
771 if (dl_list_empty(&wpa_s->bss))
772 return;
773
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800774 os_get_reltime(&t);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 t.sec -= age;
776
777 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
778 if (wpa_bss_in_use(wpa_s, bss))
779 continue;
780
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800781 if (os_reltime_before(&bss->last_update, &t)) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700782 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 } else
784 break;
785 }
786}
787
788
789static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
790{
791 struct wpa_supplicant *wpa_s = eloop_ctx;
792
793 wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
794 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
795 wpa_bss_timeout, wpa_s, NULL);
796}
797
798
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800799/**
800 * wpa_bss_init - Initialize BSS table
801 * @wpa_s: Pointer to wpa_supplicant data
802 * Returns: 0 on success, -1 on failure
803 *
804 * This prepares BSS table lists and timer for periodic updates. The BSS table
805 * is deinitialized with wpa_bss_deinit() once not needed anymore.
806 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700807int wpa_bss_init(struct wpa_supplicant *wpa_s)
808{
809 dl_list_init(&wpa_s->bss);
810 dl_list_init(&wpa_s->bss_id);
811 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
812 wpa_bss_timeout, wpa_s, NULL);
813 return 0;
814}
815
816
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800817/**
818 * wpa_bss_flush - Flush all unused BSS entries
819 * @wpa_s: Pointer to wpa_supplicant data
820 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821void wpa_bss_flush(struct wpa_supplicant *wpa_s)
822{
823 struct wpa_bss *bss, *n;
824
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800825 wpa_s->clear_driver_scan_cache = 1;
826
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 if (wpa_s->bss.next == NULL)
828 return; /* BSS table not yet initialized */
829
830 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
831 if (wpa_bss_in_use(wpa_s, bss))
832 continue;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700833 wpa_bss_remove(wpa_s, bss, __func__);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 }
835}
836
837
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800838/**
839 * wpa_bss_deinit - Deinitialize BSS table
840 * @wpa_s: Pointer to wpa_supplicant data
841 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
843{
844 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
845 wpa_bss_flush(wpa_s);
846}
847
848
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800849/**
850 * wpa_bss_get_bssid - Fetch a BSS table entry based on BSSID
851 * @wpa_s: Pointer to wpa_supplicant data
852 * @bssid: BSSID
853 * Returns: Pointer to the BSS entry or %NULL if not found
854 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
856 const u8 *bssid)
857{
858 struct wpa_bss *bss;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
860 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
862 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
863 return bss;
864 }
865 return NULL;
866}
867
868
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700869/**
870 * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
871 * @wpa_s: Pointer to wpa_supplicant data
872 * @bssid: BSSID
873 * Returns: Pointer to the BSS entry or %NULL if not found
874 *
875 * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
876 * find the entry that has the most recent update. This can help in finding the
877 * correct entry in cases where the SSID of the AP may have changed recently
878 * (e.g., in WPS reconfiguration cases).
879 */
880struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
881 const u8 *bssid)
882{
883 struct wpa_bss *bss, *found = NULL;
884 if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
885 return NULL;
886 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
887 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
888 continue;
889 if (found == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800890 os_reltime_before(&found->last_update, &bss->last_update))
Dmitry Shmidt444d5672013-04-01 13:08:44 -0700891 found = bss;
892 }
893 return found;
894}
895
896
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800897#ifdef CONFIG_P2P
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800898/**
899 * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr
900 * @wpa_s: Pointer to wpa_supplicant data
901 * @dev_addr: P2P Device Address of the GO
902 * Returns: Pointer to the BSS entry or %NULL if not found
903 */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800904struct wpa_bss * wpa_bss_get_p2p_dev_addr(struct wpa_supplicant *wpa_s,
905 const u8 *dev_addr)
906{
907 struct wpa_bss *bss;
908 dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
909 u8 addr[ETH_ALEN];
910 if (p2p_parse_dev_addr((const u8 *) (bss + 1), bss->ie_len,
911 addr) == 0 &&
912 os_memcmp(addr, dev_addr, ETH_ALEN) == 0)
913 return bss;
914 }
915 return NULL;
916}
917#endif /* CONFIG_P2P */
918
919
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800920/**
921 * wpa_bss_get_id - Fetch a BSS table entry based on identifier
922 * @wpa_s: Pointer to wpa_supplicant data
923 * @id: Unique identifier (struct wpa_bss::id) assigned for the entry
924 * Returns: Pointer to the BSS entry or %NULL if not found
925 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
927{
928 struct wpa_bss *bss;
929 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
930 if (bss->id == id)
931 return bss;
932 }
933 return NULL;
934}
935
936
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800937/**
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800938 * wpa_bss_get_id_range - Fetch a BSS table entry based on identifier range
939 * @wpa_s: Pointer to wpa_supplicant data
940 * @idf: Smallest allowed identifier assigned for the entry
941 * @idf: Largest allowed identifier assigned for the entry
942 * Returns: Pointer to the BSS entry or %NULL if not found
943 *
944 * This function is similar to wpa_bss_get_id() but allows a BSS entry with the
945 * smallest id value to be fetched within the specified range without the
946 * caller having to know the exact id.
947 */
948struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
949 unsigned int idf, unsigned int idl)
950{
951 struct wpa_bss *bss;
952 dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
953 if (bss->id >= idf && bss->id <= idl)
954 return bss;
955 }
956 return NULL;
957}
958
959
960/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800961 * wpa_bss_get_ie - Fetch a specified information element from a BSS entry
962 * @bss: BSS table entry
963 * @ie: Information element identitifier (WLAN_EID_*)
964 * Returns: Pointer to the information element (id field) or %NULL if not found
965 *
966 * This function returns the first matching information element in the BSS
967 * entry.
968 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
970{
971 const u8 *end, *pos;
972
973 pos = (const u8 *) (bss + 1);
974 end = pos + bss->ie_len;
975
976 while (pos + 1 < end) {
977 if (pos + 2 + pos[1] > end)
978 break;
979 if (pos[0] == ie)
980 return pos;
981 pos += 2 + pos[1];
982 }
983
984 return NULL;
985}
986
987
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800988/**
989 * wpa_bss_get_vendor_ie - Fetch a vendor information element from a BSS entry
990 * @bss: BSS table entry
991 * @vendor_type: Vendor type (four octets starting the IE payload)
992 * Returns: Pointer to the information element (id field) or %NULL if not found
993 *
994 * This function returns the first matching information element in the BSS
995 * entry.
996 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
998{
999 const u8 *end, *pos;
1000
1001 pos = (const u8 *) (bss + 1);
1002 end = pos + bss->ie_len;
1003
1004 while (pos + 1 < end) {
1005 if (pos + 2 + pos[1] > end)
1006 break;
1007 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1008 vendor_type == WPA_GET_BE32(&pos[2]))
1009 return pos;
1010 pos += 2 + pos[1];
1011 }
1012
1013 return NULL;
1014}
1015
1016
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001017/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001018 * wpa_bss_get_vendor_ie_beacon - Fetch a vendor information from a BSS entry
1019 * @bss: BSS table entry
1020 * @vendor_type: Vendor type (four octets starting the IE payload)
1021 * Returns: Pointer to the information element (id field) or %NULL if not found
1022 *
1023 * This function returns the first matching information element in the BSS
1024 * entry.
1025 *
1026 * This function is like wpa_bss_get_vendor_ie(), but uses IE buffer only
1027 * from Beacon frames instead of either Beacon or Probe Response frames.
1028 */
1029const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
1030 u32 vendor_type)
1031{
1032 const u8 *end, *pos;
1033
1034 if (bss->beacon_ie_len == 0)
1035 return NULL;
1036
1037 pos = (const u8 *) (bss + 1);
1038 pos += bss->ie_len;
1039 end = pos + bss->beacon_ie_len;
1040
1041 while (pos + 1 < end) {
1042 if (pos + 2 + pos[1] > end)
1043 break;
1044 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1045 vendor_type == WPA_GET_BE32(&pos[2]))
1046 return pos;
1047 pos += 2 + pos[1];
1048 }
1049
1050 return NULL;
1051}
1052
1053
1054/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001055 * wpa_bss_get_vendor_ie_multi - Fetch vendor IE data from a BSS entry
1056 * @bss: BSS table entry
1057 * @vendor_type: Vendor type (four octets starting the IE payload)
1058 * Returns: Pointer to the information element payload or %NULL if not found
1059 *
1060 * This function returns concatenated payload of possibly fragmented vendor
1061 * specific information elements in the BSS entry. The caller is responsible for
1062 * freeing the returned buffer.
1063 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
1065 u32 vendor_type)
1066{
1067 struct wpabuf *buf;
1068 const u8 *end, *pos;
1069
1070 buf = wpabuf_alloc(bss->ie_len);
1071 if (buf == NULL)
1072 return NULL;
1073
1074 pos = (const u8 *) (bss + 1);
1075 end = pos + bss->ie_len;
1076
1077 while (pos + 1 < end) {
1078 if (pos + 2 + pos[1] > end)
1079 break;
1080 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1081 vendor_type == WPA_GET_BE32(&pos[2]))
1082 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1083 pos += 2 + pos[1];
1084 }
1085
1086 if (wpabuf_len(buf) == 0) {
1087 wpabuf_free(buf);
1088 buf = NULL;
1089 }
1090
1091 return buf;
1092}
1093
1094
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001095/**
1096 * wpa_bss_get_vendor_ie_multi_beacon - Fetch vendor IE data from a BSS entry
1097 * @bss: BSS table entry
1098 * @vendor_type: Vendor type (four octets starting the IE payload)
1099 * Returns: Pointer to the information element payload or %NULL if not found
1100 *
1101 * This function returns concatenated payload of possibly fragmented vendor
1102 * specific information elements in the BSS entry. The caller is responsible for
1103 * freeing the returned buffer.
1104 *
1105 * This function is like wpa_bss_get_vendor_ie_multi(), but uses IE buffer only
1106 * from Beacon frames instead of either Beacon or Probe Response frames.
1107 */
Dmitry Shmidt9bce59c2012-09-11 15:06:38 -07001108struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
1109 u32 vendor_type)
1110{
1111 struct wpabuf *buf;
1112 const u8 *end, *pos;
1113
1114 buf = wpabuf_alloc(bss->beacon_ie_len);
1115 if (buf == NULL)
1116 return NULL;
1117
1118 pos = (const u8 *) (bss + 1);
1119 pos += bss->ie_len;
1120 end = pos + bss->beacon_ie_len;
1121
1122 while (pos + 1 < end) {
1123 if (pos + 2 + pos[1] > end)
1124 break;
1125 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1126 vendor_type == WPA_GET_BE32(&pos[2]))
1127 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1128 pos += 2 + pos[1];
1129 }
1130
1131 if (wpabuf_len(buf) == 0) {
1132 wpabuf_free(buf);
1133 buf = NULL;
1134 }
1135
1136 return buf;
1137}
1138
1139
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001140/**
1141 * wpa_bss_get_max_rate - Get maximum legacy TX rate supported in a BSS
1142 * @bss: BSS table entry
1143 * Returns: Maximum legacy rate in units of 500 kbps
1144 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145int wpa_bss_get_max_rate(const struct wpa_bss *bss)
1146{
1147 int rate = 0;
1148 const u8 *ie;
1149 int i;
1150
1151 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1152 for (i = 0; ie && i < ie[1]; i++) {
1153 if ((ie[i + 2] & 0x7f) > rate)
1154 rate = ie[i + 2] & 0x7f;
1155 }
1156
1157 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1158 for (i = 0; ie && i < ie[1]; i++) {
1159 if ((ie[i + 2] & 0x7f) > rate)
1160 rate = ie[i + 2] & 0x7f;
1161 }
1162
1163 return rate;
1164}
1165
1166
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001167/**
1168 * wpa_bss_get_bit_rates - Get legacy TX rates supported in a BSS
1169 * @bss: BSS table entry
1170 * @rates: Buffer for returning a pointer to the rates list (units of 500 kbps)
1171 * Returns: number of legacy TX rates or -1 on failure
1172 *
1173 * The caller is responsible for freeing the returned buffer with os_free() in
1174 * case of success.
1175 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001176int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
1177{
1178 const u8 *ie, *ie2;
1179 int i, j;
1180 unsigned int len;
1181 u8 *r;
1182
1183 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
1184 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
1185
1186 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
1187
1188 r = os_malloc(len);
1189 if (!r)
1190 return -1;
1191
1192 for (i = 0; ie && i < ie[1]; i++)
1193 r[i] = ie[i + 2] & 0x7f;
1194
1195 for (j = 0; ie2 && j < ie2[1]; j++)
1196 r[i + j] = ie2[j + 2] & 0x7f;
1197
1198 *rates = r;
1199 return len;
1200}