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