]>
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" | |
22 | #include "notify.h" | |
d1f9c410 | 23 | #include "scan.h" |
83922c2d JM |
24 | #include "bss.h" |
25 | ||
26 | ||
27 | #ifndef WPA_BSS_MAX_COUNT | |
28 | #define WPA_BSS_MAX_COUNT 200 | |
29 | #endif /* WPA_BSS_MAX_COUNT */ | |
30 | ||
31 | /** | |
32 | * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds | |
33 | */ | |
34 | #define WPA_BSS_EXPIRATION_PERIOD 10 | |
35 | ||
36 | /** | |
37 | * WPA_BSS_EXPIRATION_AGE - BSS entry age after which it can be expired | |
38 | * | |
39 | * This value control the time in seconds after which a BSS entry gets removed | |
40 | * if it has not been updated or is not in use. | |
41 | */ | |
42 | #define WPA_BSS_EXPIRATION_AGE 180 | |
43 | ||
44 | /** | |
45 | * WPA_BSS_EXPIRATION_SCAN_COUNT - Expire BSS after number of scans | |
46 | * | |
47 | * If the BSS entry has not been seen in this many scans, it will be removed. | |
48 | * Value 1 means that the entry is removed after the first scan without the | |
49 | * BSSID being seen. Larger values can be used to avoid BSS entries | |
50 | * disappearing if they are not visible in every scan (e.g., low signal quality | |
51 | * or interference). | |
52 | */ | |
53 | #define WPA_BSS_EXPIRATION_SCAN_COUNT 2 | |
54 | ||
55 | ||
56 | static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) | |
57 | { | |
58 | dl_list_del(&bss->list); | |
d4bf8f13 | 59 | dl_list_del(&bss->list_id); |
83922c2d JM |
60 | wpa_s->num_bss--; |
61 | wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'", | |
62 | bss->id, MAC2STR(bss->bssid), | |
63 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); | |
f0d126d3 | 64 | wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id); |
83922c2d JM |
65 | os_free(bss); |
66 | } | |
67 | ||
68 | ||
59f2caa9 JM |
69 | struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid, |
70 | const u8 *ssid, size_t ssid_len) | |
83922c2d JM |
71 | { |
72 | struct wpa_bss *bss; | |
73 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
74 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 && | |
75 | bss->ssid_len == ssid_len && | |
76 | os_memcmp(bss->ssid, ssid, ssid_len) == 0) | |
77 | return bss; | |
78 | } | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | ||
83 | static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src) | |
84 | { | |
85 | os_time_t usec; | |
86 | ||
87 | dst->flags = src->flags; | |
88 | os_memcpy(dst->bssid, src->bssid, ETH_ALEN); | |
89 | dst->freq = src->freq; | |
90 | dst->beacon_int = src->beacon_int; | |
91 | dst->caps = src->caps; | |
92 | dst->qual = src->qual; | |
93 | dst->noise = src->noise; | |
94 | dst->level = src->level; | |
95 | dst->tsf = src->tsf; | |
96 | ||
97 | os_get_time(&dst->last_update); | |
98 | dst->last_update.sec -= src->age / 1000; | |
99 | usec = (src->age % 1000) * 1000; | |
100 | if (dst->last_update.usec < usec) { | |
101 | dst->last_update.sec--; | |
102 | dst->last_update.usec += 1000000; | |
103 | } | |
104 | dst->last_update.usec -= usec; | |
105 | } | |
106 | ||
107 | ||
108 | static void wpa_bss_add(struct wpa_supplicant *wpa_s, | |
109 | const u8 *ssid, size_t ssid_len, | |
110 | struct wpa_scan_res *res) | |
111 | { | |
112 | struct wpa_bss *bss; | |
113 | ||
114 | bss = os_zalloc(sizeof(*bss) + res->ie_len); | |
115 | if (bss == NULL) | |
116 | return; | |
117 | bss->id = wpa_s->bss_next_id++; | |
118 | bss->last_update_idx = wpa_s->bss_update_idx; | |
119 | wpa_bss_copy_res(bss, res); | |
120 | os_memcpy(bss->ssid, ssid, ssid_len); | |
121 | bss->ssid_len = ssid_len; | |
122 | bss->ie_len = res->ie_len; | |
123 | os_memcpy(bss + 1, res + 1, res->ie_len); | |
124 | ||
125 | dl_list_add_tail(&wpa_s->bss, &bss->list); | |
d4bf8f13 | 126 | dl_list_add_tail(&wpa_s->bss_id, &bss->list_id); |
83922c2d JM |
127 | wpa_s->num_bss++; |
128 | wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'", | |
129 | bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len)); | |
f0d126d3 | 130 | wpas_notify_bss_added(wpa_s, bss->bssid, bss->id); |
83922c2d JM |
131 | if (wpa_s->num_bss > WPA_BSS_MAX_COUNT) { |
132 | /* Remove the oldest entry */ | |
133 | wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss, | |
134 | struct wpa_bss, list)); | |
135 | } | |
136 | } | |
137 | ||
138 | ||
139 | static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss, | |
140 | struct wpa_scan_res *res) | |
141 | { | |
142 | bss->scan_miss_count = 0; | |
143 | bss->last_update_idx = wpa_s->bss_update_idx; | |
144 | wpa_bss_copy_res(bss, res); | |
145 | /* Move the entry to the end of the list */ | |
146 | dl_list_del(&bss->list); | |
147 | if (bss->ie_len >= res->ie_len) { | |
148 | os_memcpy(bss + 1, res + 1, res->ie_len); | |
149 | bss->ie_len = res->ie_len; | |
150 | } else { | |
151 | struct wpa_bss *nbss; | |
1c83b67e JM |
152 | struct dl_list *prev = bss->list_id.prev; |
153 | dl_list_del(&bss->list_id); | |
83922c2d JM |
154 | nbss = os_realloc(bss, sizeof(*bss) + res->ie_len); |
155 | if (nbss) { | |
156 | bss = nbss; | |
157 | os_memcpy(bss + 1, res + 1, res->ie_len); | |
158 | bss->ie_len = res->ie_len; | |
159 | } | |
1c83b67e | 160 | dl_list_add(prev, &bss->list_id); |
83922c2d JM |
161 | } |
162 | dl_list_add_tail(&wpa_s->bss, &bss->list); | |
163 | } | |
164 | ||
165 | ||
7d7d57b2 JM |
166 | static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss) |
167 | { | |
168 | return bss == wpa_s->current_bss || | |
169 | os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 || | |
170 | os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0; | |
171 | } | |
172 | ||
173 | ||
83922c2d JM |
174 | void wpa_bss_update_start(struct wpa_supplicant *wpa_s) |
175 | { | |
176 | wpa_s->bss_update_idx++; | |
177 | wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u", | |
178 | wpa_s->bss_update_idx); | |
179 | } | |
180 | ||
181 | ||
182 | void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s, | |
183 | struct wpa_scan_res *res) | |
184 | { | |
185 | const u8 *ssid; | |
186 | struct wpa_bss *bss; | |
187 | ||
188 | ssid = wpa_scan_get_ie(res, WLAN_EID_SSID); | |
189 | if (ssid == NULL) { | |
190 | wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR, | |
191 | MAC2STR(res->bssid)); | |
192 | return; | |
193 | } | |
194 | if (ssid[1] > 32) { | |
195 | wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for " | |
196 | MACSTR, MAC2STR(res->bssid)); | |
197 | return; | |
198 | } | |
199 | ||
200 | /* TODO: add option for ignoring BSSes we are not interested in | |
201 | * (to save memory) */ | |
202 | bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]); | |
203 | if (bss == NULL) | |
204 | wpa_bss_add(wpa_s, ssid + 2, ssid[1], res); | |
205 | else | |
206 | wpa_bss_update(wpa_s, bss, res); | |
207 | } | |
208 | ||
209 | ||
8d923a4a JM |
210 | static int wpa_bss_included_in_scan(const struct wpa_bss *bss, |
211 | const struct scan_info *info) | |
212 | { | |
213 | int found; | |
214 | size_t i; | |
215 | ||
216 | if (info == NULL) | |
217 | return 1; | |
218 | ||
219 | if (info->num_freqs) { | |
220 | found = 0; | |
221 | for (i = 0; i < info->num_freqs; i++) { | |
222 | if (bss->freq == info->freqs[i]) { | |
223 | found = 1; | |
224 | break; | |
225 | } | |
226 | } | |
227 | if (!found) | |
228 | return 0; | |
229 | } | |
230 | ||
231 | if (info->num_ssids) { | |
232 | found = 0; | |
233 | for (i = 0; i < info->num_ssids; i++) { | |
234 | const struct wpa_driver_scan_ssid *s = &info->ssids[i]; | |
235 | if ((s->ssid == NULL || s->ssid_len == 0) || | |
236 | (s->ssid_len == bss->ssid_len && | |
237 | os_memcmp(s->ssid, bss->ssid, bss->ssid_len) == | |
238 | 0)) { | |
239 | found = 1; | |
240 | break; | |
241 | } | |
242 | } | |
243 | if (!found) | |
244 | return 0; | |
245 | } | |
246 | ||
247 | return 1; | |
248 | } | |
249 | ||
250 | ||
251 | void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info, | |
252 | int new_scan) | |
83922c2d JM |
253 | { |
254 | struct wpa_bss *bss, *n; | |
255 | ||
8d923a4a JM |
256 | if (!new_scan) |
257 | return; /* do not expire entries without new scan */ | |
83922c2d JM |
258 | |
259 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | |
7d7d57b2 JM |
260 | if (wpa_bss_in_use(wpa_s, bss)) |
261 | continue; | |
8d923a4a JM |
262 | if (!wpa_bss_included_in_scan(bss, info)) |
263 | continue; /* expire only BSSes that were scanned */ | |
83922c2d JM |
264 | if (bss->last_update_idx < wpa_s->bss_update_idx) |
265 | bss->scan_miss_count++; | |
266 | if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) { | |
267 | wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no " | |
268 | "match in scan", bss->id); | |
269 | wpa_bss_remove(wpa_s, bss); | |
270 | } | |
271 | } | |
272 | } | |
273 | ||
274 | ||
275 | static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx) | |
276 | { | |
277 | struct wpa_supplicant *wpa_s = eloop_ctx; | |
278 | struct wpa_bss *bss, *n; | |
279 | struct os_time t; | |
280 | ||
281 | if (dl_list_empty(&wpa_s->bss)) | |
282 | return; | |
283 | ||
284 | os_get_time(&t); | |
285 | t.sec -= WPA_BSS_EXPIRATION_AGE; | |
286 | ||
287 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) { | |
7d7d57b2 JM |
288 | if (wpa_bss_in_use(wpa_s, bss)) |
289 | continue; | |
83922c2d JM |
290 | |
291 | if (os_time_before(&bss->last_update, &t)) { | |
292 | wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age", | |
293 | bss->id); | |
294 | wpa_bss_remove(wpa_s, bss); | |
295 | } else | |
296 | break; | |
297 | } | |
298 | eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0, | |
299 | wpa_bss_timeout, wpa_s, NULL); | |
300 | } | |
301 | ||
302 | ||
303 | int wpa_bss_init(struct wpa_supplicant *wpa_s) | |
304 | { | |
305 | dl_list_init(&wpa_s->bss); | |
d4bf8f13 | 306 | dl_list_init(&wpa_s->bss_id); |
83922c2d JM |
307 | eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0, |
308 | wpa_bss_timeout, wpa_s, NULL); | |
309 | return 0; | |
310 | } | |
311 | ||
312 | ||
313 | void wpa_bss_deinit(struct wpa_supplicant *wpa_s) | |
314 | { | |
315 | struct wpa_bss *bss, *n; | |
316 | eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL); | |
e5fc8c8c JM |
317 | if (wpa_s->bss.next == NULL) |
318 | return; /* BSS table not yet initialized */ | |
83922c2d JM |
319 | dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) |
320 | wpa_bss_remove(wpa_s, bss); | |
321 | } | |
d4bf8f13 JM |
322 | |
323 | ||
324 | struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s, | |
325 | const u8 *bssid) | |
326 | { | |
327 | struct wpa_bss *bss; | |
328 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
329 | if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0) | |
330 | return bss; | |
331 | } | |
332 | return NULL; | |
333 | } | |
334 | ||
335 | ||
336 | struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id) | |
337 | { | |
338 | struct wpa_bss *bss; | |
339 | dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) { | |
340 | if (bss->id == id) | |
341 | return bss; | |
342 | } | |
343 | return NULL; | |
344 | } | |
345 | ||
346 | ||
347 | const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie) | |
348 | { | |
349 | const u8 *end, *pos; | |
350 | ||
351 | pos = (const u8 *) (bss + 1); | |
352 | end = pos + bss->ie_len; | |
353 | ||
354 | while (pos + 1 < end) { | |
355 | if (pos + 2 + pos[1] > end) | |
356 | break; | |
357 | if (pos[0] == ie) | |
358 | return pos; | |
359 | pos += 2 + pos[1]; | |
360 | } | |
361 | ||
362 | return NULL; | |
363 | } | |
364 | ||
365 | ||
366 | const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type) | |
367 | { | |
368 | const u8 *end, *pos; | |
369 | ||
370 | pos = (const u8 *) (bss + 1); | |
371 | end = pos + bss->ie_len; | |
372 | ||
373 | while (pos + 1 < end) { | |
374 | if (pos + 2 + pos[1] > end) | |
375 | break; | |
376 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
377 | vendor_type == WPA_GET_BE32(&pos[2])) | |
378 | return pos; | |
379 | pos += 2 + pos[1]; | |
380 | } | |
381 | ||
382 | return NULL; | |
383 | } | |
384 | ||
385 | ||
386 | struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss, | |
387 | u32 vendor_type) | |
388 | { | |
389 | struct wpabuf *buf; | |
390 | const u8 *end, *pos; | |
391 | ||
392 | buf = wpabuf_alloc(bss->ie_len); | |
393 | if (buf == NULL) | |
394 | return NULL; | |
395 | ||
396 | pos = (const u8 *) (bss + 1); | |
397 | end = pos + bss->ie_len; | |
398 | ||
399 | while (pos + 1 < end) { | |
400 | if (pos + 2 + pos[1] > end) | |
401 | break; | |
402 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && | |
403 | vendor_type == WPA_GET_BE32(&pos[2])) | |
404 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); | |
405 | pos += 2 + pos[1]; | |
406 | } | |
407 | ||
408 | if (wpabuf_len(buf) == 0) { | |
409 | wpabuf_free(buf); | |
410 | buf = NULL; | |
411 | } | |
412 | ||
413 | return buf; | |
414 | } | |
99a6a63f JM |
415 | |
416 | ||
417 | int wpa_bss_get_max_rate(const struct wpa_bss *bss) | |
418 | { | |
419 | int rate = 0; | |
420 | const u8 *ie; | |
421 | int i; | |
422 | ||
423 | ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES); | |
424 | for (i = 0; ie && i < ie[1]; i++) { | |
425 | if ((ie[i + 2] & 0x7f) > rate) | |
426 | rate = ie[i + 2] & 0x7f; | |
427 | } | |
428 | ||
429 | ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES); | |
430 | for (i = 0; ie && i < ie[1]; i++) { | |
431 | if ((ie[i + 2] & 0x7f) > rate) | |
432 | rate = ie[i + 2] & 0x7f; | |
433 | } | |
434 | ||
435 | return rate; | |
436 | } |