]>
Commit | Line | Data |
---|---|---|
83922c2d JM |
1 | /* |
2 | * BSS table | |
8d923a4a | 3 | * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi> |
83922c2d JM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
15 | #include "utils/includes.h" | |
16 | ||
17 | #include "utils/common.h" | |
18 | #include "utils/eloop.h" | |
19 | #include "common/ieee802_11_defs.h" | |
20 | #include "drivers/driver.h" | |
21 | #include "wpa_supplicant_i.h" | |
c9c38b09 | 22 | #include "config.h" |
83922c2d | 23 | #include "notify.h" |
d1f9c410 | 24 | #include "scan.h" |
83922c2d JM |
25 | #include "bss.h" |
26 | ||
27 | ||
83922c2d JM |
28 | /** |
29 | * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds | |
30 | */ | |
31 | #define WPA_BSS_EXPIRATION_PERIOD 10 | |
32 | ||
33 | /** | |
34 | * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired | |
35 | * | |
36 | * This value control the time in seconds after which a BSS entry gets removed | |
37 | * if it has not been updated or is not in use. | |
38 | */ | |
39 | #define WPA_BSS_EXPIRATION_AGE 180 | |
40 | ||
41 | /** | |
42 | * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans | |
43 | * | |
44 | * If the BSS entry has not been seen in this many scans, it will be removed. | |
45 | * Value 1 means that the entry is removed after the first scan without the | |
46 | * BSSID being seen. Larger values can be used to avoid BSS entries | |
47 | * disappearing if they are not visible in every scan (e.g., low signal quality | |
48 | * or interference). | |
49 | */ | |
50 | #define WPA_BSS_EXPIRATION_SCAN_COUNT 2 | |
51 | ||
158c6c74 WS |
52 | #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0) |
53 | #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1) | |
54 | #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2) | |
55 | #define WPA_BSS_MODE_CHANGED_FLAG BIT(3) | |
56 | #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4) | |
57 | #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5) | |
58 | #define WPA_BSS_WPS_CHANGED_FLAG BIT(6) | |
59 | #define WPA_BSS_RATES_CHANGED_FLAG BIT(7) | |
7899e2f4 | 60 | #define WPA_BSS_IES_CHANGED_FLAG BIT(8) |
158c6c74 | 61 | |
83922c2d JM |
62 | |
63 | static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | |
64 | { | |
65 | dl_list_del(&bss->list); | |
d4bf8f13 | 66 | dl_list_del(&bss->list_id); |
83922c2d JM |
67 | wpa_s->num_bss--; |
68 | wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'", | |
69 | bss->id, MAC2STR(bss->bssid), | |
70 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); | |
f0d126d3 | 71 | wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id); |
83922c2d JM |
72 | os_free(bss); |
73 | } | |
74 | ||
75 | ||
59f2caa9 JM |
76 | struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid, |
77 | const u8 *ssid, size_t ssid_len) | |
83922c2d JM |
78 | { |
79 | struct wpa_bss *bss; | |
80 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
81 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 && | |
82 | bss->ssid_len == ssid_len && | |
83 | os_memcmp(bss->ssid, ssid, ssid_len) == 0) | |
84 | return bss; | |
85 | } | |
86 | return NULL; | |
87 | } | |
88 | ||
89 | ||
90 | static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src) | |
91 | { | |
92 | os_time_t usec; | |
93 | ||
94 | dst->flags = src->flags; | |
95 | os_memcpy(dst->bssid, src->bssid, ETH_ALEN); | |
96 | dst->freq = src->freq; | |
97 | dst->beacon_int = src->beacon_int; | |
98 | dst->caps = src->caps; | |
99 | dst->qual = src->qual; | |
100 | dst->noise = src->noise; | |
101 | dst->level = src->level; | |
102 | dst->tsf = src->tsf; | |
103 | ||
104 | os_get_time(&dst->last_update); | |
105 | dst->last_update.sec -= src->age / 1000; | |
106 | usec = (src->age % 1000) * 1000; | |
107 | if (dst->last_update.usec < usec) { | |
108 | dst->last_update.sec--; | |
109 | dst->last_update.usec += 1000000; | |
110 | } | |
111 | dst->last_update.usec -= usec; | |
112 | } | |
113 | ||
114 | ||
115 | static void wpa_bss_add(struct wpa_supplicant *wpa_s, | |
116 | const u8 *ssid, size_t ssid_len, | |
117 | struct wpa_scan_res *res) | |
118 | { | |
119 | struct wpa_bss *bss; | |
120 | ||
8c090654 | 121 | bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len); |
83922c2d JM |
122 | if (bss == NULL) |
123 | return; | |
124 | bss->id = wpa_s->bss_next_id++; | |
125 | bss->last_update_idx = wpa_s->bss_update_idx; | |
126 | wpa_bss_copy_res(bss, res); | |
127 | os_memcpy(bss->ssid, ssid, ssid_len); | |
128 | bss->ssid_len = ssid_len; | |
129 | bss->ie_len = res->ie_len; | |
8c090654 JM |
130 | bss->beacon_ie_len = res->beacon_ie_len; |
131 | os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len); | |
83922c2d JM |
132 | |
133 | dl_list_add_tail(&wpa_s->bss, &bss->list); | |
d4bf8f13 | 134 | dl_list_add_tail(&wpa_s->bss_id, &bss->list_id); |
83922c2d JM |
135 | wpa_s->num_bss++; |
136 | wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'", | |
137 | bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len)); | |
f0d126d3 | 138 | wpas_notify_bss_added(wpa_s, bss->bssid, bss->id); |
c9c38b09 | 139 | if (wpa_s->num_bss > wpa_s->conf->bss_max_count) { |
83922c2d JM |
140 | /* Remove the oldest entry */ |
141 | wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss, | |
142 | struct wpa_bss, list)); | |
143 | } | |
144 | } | |
145 | ||
146 | ||
158c6c74 WS |
147 | static int are_ies_equal(const struct wpa_bss *old, |
148 | const struct wpa_scan_res *new, u32 ie) | |
149 | { | |
150 | const u8 *old_ie, *new_ie; | |
151 | struct wpabuf *old_ie_buff = NULL; | |
152 | struct wpabuf *new_ie_buff = NULL; | |
153 | int new_ie_len, old_ie_len, ret, is_multi; | |
154 | ||
155 | switch (ie) { | |
156 | case WPA_IE_VENDOR_TYPE: | |
157 | old_ie = wpa_bss_get_vendor_ie(old, ie); | |
158 | new_ie = wpa_scan_get_vendor_ie(new, ie); | |
159 | is_multi = 0; | |
160 | break; | |
161 | case WPS_IE_VENDOR_TYPE: | |
162 | old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie); | |
163 | new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie); | |
164 | is_multi = 1; | |
165 | break; | |
166 | case WLAN_EID_RSN: | |
167 | case WLAN_EID_SUPP_RATES: | |
168 | case WLAN_EID_EXT_SUPP_RATES: | |
169 | old_ie = wpa_bss_get_ie(old, ie); | |
170 | new_ie = wpa_scan_get_ie(new, ie); | |
171 | is_multi = 0; | |
172 | break; | |
173 | default: | |
174 | wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__); | |
175 | return 0; | |
176 | } | |
177 | ||
178 | if (is_multi) { | |
179 | /* in case of multiple IEs stored in buffer */ | |
180 | old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL; | |
181 | new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL; | |
182 | old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0; | |
183 | new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0; | |
184 | } else { | |
185 | /* in case of single IE */ | |
186 | old_ie_len = old_ie ? old_ie[1] + 2 : 0; | |
187 | new_ie_len = new_ie ? new_ie[1] + 2 : 0; | |
188 | } | |
189 | ||
190 | ret = (old_ie_len == new_ie_len && | |
191 | os_memcmp(old_ie, new_ie, old_ie_len) == 0); | |
192 | ||
193 | wpabuf_free(old_ie_buff); | |
194 | wpabuf_free(new_ie_buff); | |
195 | ||
196 | return ret; | |
197 | } | |
198 | ||
199 | ||
200 | static u32 wpa_bss_compare_res(const struct wpa_bss *old, | |
201 | const struct wpa_scan_res *new) | |
202 | { | |
203 | u32 changes = 0; | |
204 | int caps_diff = old->caps ^ new->caps; | |
205 | ||
206 | if (old->freq != new->freq) | |
207 | changes |= WPA_BSS_FREQ_CHANGED_FLAG; | |
208 | ||
209 | if (old->level != new->level) | |
210 | changes |= WPA_BSS_SIGNAL_CHANGED_FLAG; | |
211 | ||
212 | if (caps_diff & IEEE80211_CAP_PRIVACY) | |
213 | changes |= WPA_BSS_PRIVACY_CHANGED_FLAG; | |
214 | ||
215 | if (caps_diff & IEEE80211_CAP_IBSS) | |
216 | changes |= WPA_BSS_MODE_CHANGED_FLAG; | |
217 | ||
7899e2f4 WS |
218 | if (old->ie_len == new->ie_len && |
219 | os_memcmp(old + 1, new + 1, old->ie_len) == 0) | |
220 | return changes; | |
221 | changes |= WPA_BSS_IES_CHANGED_FLAG; | |
222 | ||
158c6c74 WS |
223 | if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE)) |
224 | changes |= WPA_BSS_WPAIE_CHANGED_FLAG; | |
225 | ||
226 | if (!are_ies_equal(old, new, WLAN_EID_RSN)) | |
227 | changes |= WPA_BSS_RSNIE_CHANGED_FLAG; | |
228 | ||
229 | if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE)) | |
230 | changes |= WPA_BSS_WPS_CHANGED_FLAG; | |
231 | ||
232 | if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) || | |
233 | !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES)) | |
234 | changes |= WPA_BSS_RATES_CHANGED_FLAG; | |
235 | ||
236 | return changes; | |
237 | } | |
238 | ||
239 | ||
240 | static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes, | |
241 | const struct wpa_bss *bss) | |
242 | { | |
243 | if (changes & WPA_BSS_FREQ_CHANGED_FLAG) | |
244 | wpas_notify_bss_freq_changed(wpa_s, bss->id); | |
245 | ||
246 | if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG) | |
247 | wpas_notify_bss_signal_changed(wpa_s, bss->id); | |
248 | ||
249 | if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG) | |
250 | wpas_notify_bss_privacy_changed(wpa_s, bss->id); | |
251 | ||
252 | if (changes & WPA_BSS_MODE_CHANGED_FLAG) | |
253 | wpas_notify_bss_mode_changed(wpa_s, bss->id); | |
254 | ||
255 | if (changes & WPA_BSS_WPAIE_CHANGED_FLAG) | |
256 | wpas_notify_bss_wpaie_changed(wpa_s, bss->id); | |
257 | ||
258 | if (changes & WPA_BSS_RSNIE_CHANGED_FLAG) | |
259 | wpas_notify_bss_rsnie_changed(wpa_s, bss->id); | |
260 | ||
261 | if (changes & WPA_BSS_WPS_CHANGED_FLAG) | |
262 | wpas_notify_bss_wps_changed(wpa_s, bss->id); | |
263 | ||
7899e2f4 WS |
264 | if (changes & WPA_BSS_IES_CHANGED_FLAG) |
265 | wpas_notify_bss_ies_changed(wpa_s, bss->id); | |
266 | ||
158c6c74 WS |
267 | if (changes & WPA_BSS_RATES_CHANGED_FLAG) |
268 | wpas_notify_bss_rates_changed(wpa_s, bss->id); | |
269 | } | |
270 | ||
271 | ||
83922c2d JM |
272 | static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, |
273 | struct wpa_scan_res *res) | |
274 | { | |
158c6c74 WS |
275 | u32 changes; |
276 | ||
277 | changes = wpa_bss_compare_res(bss, res); | |
83922c2d JM |
278 | bss->scan_miss_count = 0; |
279 | bss->last_update_idx = wpa_s->bss_update_idx; | |
280 | wpa_bss_copy_res(bss, res); | |
281 | /* Move the entry to the end of the list */ | |
282 | dl_list_del(&bss->list); | |
8c090654 JM |
283 | if (bss->ie_len + bss->beacon_ie_len >= |
284 | res->ie_len + res->beacon_ie_len) { | |
285 | os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len); | |
83922c2d | 286 | bss->ie_len = res->ie_len; |
8c090654 | 287 | bss->beacon_ie_len = res->beacon_ie_len; |
83922c2d JM |
288 | } else { |
289 | struct wpa_bss *nbss; | |
1c83b67e JM |
290 | struct dl_list *prev = bss->list_id.prev; |
291 | dl_list_del(&bss->list_id); | |
8c090654 JM |
292 | nbss = os_realloc(bss, sizeof(*bss) + res->ie_len + |
293 | res->beacon_ie_len); | |
83922c2d JM |
294 | if (nbss) { |
295 | bss = nbss; | |
8c090654 JM |
296 | os_memcpy(bss + 1, res + 1, |
297 | res->ie_len + res->beacon_ie_len); | |
83922c2d | 298 | bss->ie_len = res->ie_len; |
8c090654 | 299 | bss->beacon_ie_len = res->beacon_ie_len; |
83922c2d | 300 | } |
1c83b67e | 301 | dl_list_add(prev, &bss->list_id); |
83922c2d JM |
302 | } |
303 | dl_list_add_tail(&wpa_s->bss, &bss->list); | |
158c6c74 WS |
304 | |
305 | notify_bss_changes(wpa_s, changes, bss); | |
83922c2d JM |
306 | } |
307 | ||
308 | ||
7d7d57b2 JM |
309 | static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
310 | { | |
311 | return bss == wpa_s->current_bss || | |
312 | os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 || | |
313 | os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0; | |
314 | } | |
315 | ||
316 | ||
83922c2d JM |
317 | void wpa_bss_update_start(struct wpa_supplicant *wpa_s) |
318 | { | |
319 | wpa_s->bss_update_idx++; | |
320 | wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u", | |
321 | wpa_s->bss_update_idx); | |
322 | } | |
323 | ||
324 | ||
325 | void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s, | |
326 | struct wpa_scan_res *res) | |
327 | { | |
0c6b310e | 328 | const u8 *ssid, *p2p; |
83922c2d JM |
329 | struct wpa_bss *bss; |
330 | ||
331 | ssid = wpa_scan_get_ie(res, WLAN_EID_SSID); | |
332 | if (ssid == NULL) { | |
333 | wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR, | |
334 | MAC2STR(res->bssid)); | |
335 | return; | |
336 | } | |
337 | if (ssid[1] > 32) { | |
338 | wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for " | |
339 | MACSTR, MAC2STR(res->bssid)); | |
340 | return; | |
341 | } | |
342 | ||
0c6b310e JM |
343 | p2p = wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE); |
344 | if (p2p && ssid[1] == P2P_WILDCARD_SSID_LEN && | |
345 | os_memcmp(ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) == 0) | |
346 | return; /* Skip P2P listen discovery results here */ | |
347 | ||
83922c2d JM |
348 | /* TODO: add option for ignoring BSSes we are not interested in |
349 | * (to save memory) */ | |
350 | bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]); | |
351 | if (bss == NULL) | |
352 | wpa_bss_add(wpa_s, ssid + 2, ssid[1], res); | |
353 | else | |
354 | wpa_bss_update(wpa_s, bss, res); | |
355 | } | |
356 | ||
357 | ||
8d923a4a JM |
358 | static int wpa_bss_included_in_scan(const struct wpa_bss *bss, |
359 | const struct scan_info *info) | |
360 | { | |
361 | int found; | |
362 | size_t i; | |
363 | ||
364 | if (info == NULL) | |
365 | return 1; | |
366 | ||
367 | if (info->num_freqs) { | |
368 | found = 0; | |
369 | for (i = 0; i < info->num_freqs; i++) { | |
370 | if (bss->freq == info->freqs[i]) { | |
371 | found = 1; | |
372 | break; | |
373 | } | |
374 | } | |
375 | if (!found) | |
376 | return 0; | |
377 | } | |
378 | ||
379 | if (info->num_ssids) { | |
380 | found = 0; | |
381 | for (i = 0; i < info->num_ssids; i++) { | |
382 | const struct wpa_driver_scan_ssid *s = &info->ssids[i]; | |
383 | if ((s->ssid == NULL || s->ssid_len == 0) || | |
384 | (s->ssid_len == bss->ssid_len && | |
385 | os_memcmp(s->ssid, bss->ssid, bss->ssid_len) == | |
386 | 0)) { | |
387 | found = 1; | |
388 | break; | |
389 | } | |
390 | } | |
391 | if (!found) | |
392 | return 0; | |
393 | } | |
394 | ||
395 | return 1; | |
396 | } | |
397 | ||
398 | ||
399 | void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info, | |
400 | int new_scan) | |
83922c2d JM |
401 | { |
402 | struct wpa_bss *bss, *n; | |
403 | ||
8d923a4a JM |
404 | if (!new_scan) |
405 | return; /* do not expire entries without new scan */ | |
83922c2d JM |
406 | |
407 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | |
7d7d57b2 JM |
408 | if (wpa_bss_in_use(wpa_s, bss)) |
409 | continue; | |
8d923a4a JM |
410 | if (!wpa_bss_included_in_scan(bss, info)) |
411 | continue; /* expire only BSSes that were scanned */ | |
83922c2d JM |
412 | if (bss->last_update_idx < wpa_s->bss_update_idx) |
413 | bss->scan_miss_count++; | |
414 | if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) { | |
415 | wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no " | |
416 | "match in scan", bss->id); | |
417 | wpa_bss_remove(wpa_s, bss); | |
418 | } | |
419 | } | |
420 | } | |
421 | ||
422 | ||
423 | static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx) | |
424 | { | |
425 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
426 | struct wpa_bss *bss, *n; | |
427 | struct os_time t; | |
428 | ||
429 | if (dl_list_empty(&wpa_s->bss)) | |
430 | return; | |
431 | ||
432 | os_get_time(&t); | |
433 | t.sec -= WPA_BSS_EXPIRATION_AGE; | |
434 | ||
435 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | |
7d7d57b2 JM |
436 | if (wpa_bss_in_use(wpa_s, bss)) |
437 | continue; | |
83922c2d JM |
438 | |
439 | if (os_time_before(&bss->last_update, &t)) { | |
440 | wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age", | |
441 | bss->id); | |
442 | wpa_bss_remove(wpa_s, bss); | |
443 | } else | |
444 | break; | |
445 | } | |
446 | eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0, | |
447 | wpa_bss_timeout, wpa_s, NULL); | |
448 | } | |
449 | ||
450 | ||
451 | int wpa_bss_init(struct wpa_supplicant *wpa_s) | |
452 | { | |
453 | dl_list_init(&wpa_s->bss); | |
d4bf8f13 | 454 | dl_list_init(&wpa_s->bss_id); |
83922c2d JM |
455 | eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0, |
456 | wpa_bss_timeout, wpa_s, NULL); | |
457 | return 0; | |
458 | } | |
459 | ||
460 | ||
461 | void wpa_bss_deinit(struct wpa_supplicant *wpa_s) | |
462 | { | |
463 | struct wpa_bss *bss, *n; | |
464 | eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL); | |
e5fc8c8c JM |
465 | if (wpa_s->bss.next == NULL) |
466 | return; /* BSS table not yet initialized */ | |
83922c2d JM |
467 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) |
468 | wpa_bss_remove(wpa_s, bss); | |
469 | } | |
d4bf8f13 JM |
470 | |
471 | ||
472 | struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s, | |
473 | const u8 *bssid) | |
474 | { | |
475 | struct wpa_bss *bss; | |
476 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
477 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) | |
478 | return bss; | |
479 | } | |
480 | return NULL; | |
481 | } | |
482 | ||
483 | ||
484 | struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id) | |
485 | { | |
486 | struct wpa_bss *bss; | |
487 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
488 | if (bss->id == id) | |
489 | return bss; | |
490 | } | |
491 | return NULL; | |
492 | } | |
493 | ||
494 | ||
495 | const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie) | |
496 | { | |
497 | const u8 *end, *pos; | |
498 | ||
499 | pos = (const u8 *) (bss + 1); | |
500 | end = pos + bss->ie_len; | |
501 | ||
502 | while (pos + 1 < end) { | |
503 | if (pos + 2 + pos[1] > end) | |
504 | break; | |
505 | if (pos[0] == ie) | |
506 | return pos; | |
507 | pos += 2 + pos[1]; | |
508 | } | |
509 | ||
510 | return NULL; | |
511 | } | |
512 | ||
513 | ||
514 | const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type) | |
515 | { | |
516 | const u8 *end, *pos; | |
517 | ||
518 | pos = (const u8 *) (bss + 1); | |
519 | end = pos + bss->ie_len; | |
520 | ||
521 | while (pos + 1 < end) { | |
522 | if (pos + 2 + pos[1] > end) | |
523 | break; | |
524 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
525 | vendor_type == WPA_GET_BE32(&pos[2])) | |
526 | return pos; | |
527 | pos += 2 + pos[1]; | |
528 | } | |
529 | ||
530 | return NULL; | |
531 | } | |
532 | ||
533 | ||
534 | struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss, | |
535 | u32 vendor_type) | |
536 | { | |
537 | struct wpabuf *buf; | |
538 | const u8 *end, *pos; | |
539 | ||
540 | buf = wpabuf_alloc(bss->ie_len); | |
541 | if (buf == NULL) | |
542 | return NULL; | |
543 | ||
544 | pos = (const u8 *) (bss + 1); | |
545 | end = pos + bss->ie_len; | |
546 | ||
547 | while (pos + 1 < end) { | |
548 | if (pos + 2 + pos[1] > end) | |
549 | break; | |
550 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
551 | vendor_type == WPA_GET_BE32(&pos[2])) | |
552 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); | |
553 | pos += 2 + pos[1]; | |
554 | } | |
555 | ||
556 | if (wpabuf_len(buf) == 0) { | |
557 | wpabuf_free(buf); | |
558 | buf = NULL; | |
559 | } | |
560 | ||
561 | return buf; | |
562 | } | |
99a6a63f JM |
563 | |
564 | ||
565 | int wpa_bss_get_max_rate(const struct wpa_bss *bss) | |
566 | { | |
567 | int rate = 0; | |
568 | const u8 *ie; | |
569 | int i; | |
570 | ||
571 | ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES); | |
572 | for (i = 0; ie && i < ie[1]; i++) { | |
573 | if ((ie[i + 2] & 0x7f) > rate) | |
574 | rate = ie[i + 2] & 0x7f; | |
575 | } | |
576 | ||
577 | ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES); | |
578 | for (i = 0; ie && i < ie[1]; i++) { | |
579 | if ((ie[i + 2] & 0x7f) > rate) | |
580 | rate = ie[i + 2] & 0x7f; | |
581 | } | |
582 | ||
583 | return rate; | |
584 | } | |
75d328af WS |
585 | |
586 | ||
587 | int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates) | |
588 | { | |
589 | const u8 *ie, *ie2; | |
20766f20 JM |
590 | int i, j; |
591 | unsigned int len; | |
592 | u8 *r; | |
75d328af WS |
593 | |
594 | ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES); | |
595 | ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES); | |
596 | ||
597 | len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0); | |
598 | ||
20766f20 JM |
599 | r = os_malloc(len); |
600 | if (!r) | |
75d328af WS |
601 | return -1; |
602 | ||
603 | for (i = 0; ie && i < ie[1]; i++) | |
20766f20 | 604 | r[i] = ie[i + 2] & 0x7f; |
75d328af WS |
605 | |
606 | for (j = 0; ie2 && j < ie2[1]; j++) | |
20766f20 | 607 | r[i + j] = ie2[j + 2] & 0x7f; |
75d328af | 608 | |
20766f20 | 609 | *rates = r; |
75d328af WS |
610 | return len; |
611 | } |