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