Add BSS list sorted by id and add some helper functions
[mech_eap.git] / wpa_supplicant / bss.c
1 /*
2  * BSS table
3  * Copyright (c) 2009, 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);
64         os_free(bss);
65 }
66
67
68 static struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s,
69                                     const u8 *bssid, const u8 *ssid,
70                                     size_t ssid_len)
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);
126         dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
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));
130         wpas_notify_bss_added(wpa_s, res->bssid);
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;
152                 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len);
153                 if (nbss) {
154                         bss = nbss;
155                         os_memcpy(bss + 1, res + 1, res->ie_len);
156                         bss->ie_len = res->ie_len;
157                 }
158         }
159         dl_list_add_tail(&wpa_s->bss, &bss->list);
160 }
161
162
163 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
164 {
165         wpa_s->bss_update_idx++;
166         wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
167                    wpa_s->bss_update_idx);
168 }
169
170
171 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
172                              struct wpa_scan_res *res)
173 {
174         const u8 *ssid;
175         struct wpa_bss *bss;
176
177         ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
178         if (ssid == NULL) {
179                 wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
180                            MAC2STR(res->bssid));
181                 return;
182         }
183         if (ssid[1] > 32) {
184                 wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
185                            MACSTR, MAC2STR(res->bssid));
186                 return;
187         }
188
189         /* TODO: add option for ignoring BSSes we are not interested in
190          * (to save memory) */
191         bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
192         if (bss == NULL)
193                 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
194         else
195                 wpa_bss_update(wpa_s, bss, res);
196 }
197
198
199 void wpa_bss_update_end(struct wpa_supplicant *wpa_s)
200 {
201         struct wpa_bss *bss, *n;
202
203         /* TODO: expire only entries that were on the scanned frequencies/SSIDs
204          * list; need to get info from driver about scanned frequencies and
205          * SSIDs to be able to figure out which entries should be expired based
206          * on this */
207
208         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
209                 if (bss->last_update_idx < wpa_s->bss_update_idx)
210                         bss->scan_miss_count++;
211                 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
212                         wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
213                                    "match in scan", bss->id);
214                         wpa_bss_remove(wpa_s, bss);
215                 }
216         }
217 }
218
219
220 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
221 {
222         struct wpa_supplicant *wpa_s = eloop_ctx;
223         struct wpa_bss *bss, *n;
224         struct os_time t;
225
226         if (dl_list_empty(&wpa_s->bss))
227                 return;
228
229         os_get_time(&t);
230         t.sec -= WPA_BSS_EXPIRATION_AGE;
231
232         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
233                 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
234                     os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0)
235                         continue; /* do not expire BSSes that are in use */
236
237                 if (os_time_before(&bss->last_update, &t)) {
238                         wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
239                                    bss->id);
240                         wpa_bss_remove(wpa_s, bss);
241                 } else
242                         break;
243         }
244         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
245                                wpa_bss_timeout, wpa_s, NULL);
246 }
247
248
249 int wpa_bss_init(struct wpa_supplicant *wpa_s)
250 {
251         dl_list_init(&wpa_s->bss);
252         dl_list_init(&wpa_s->bss_id);
253         eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
254                                wpa_bss_timeout, wpa_s, NULL);
255         return 0;
256 }
257
258
259 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
260 {
261         struct wpa_bss *bss, *n;
262         eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
263         dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
264                 wpa_bss_remove(wpa_s, bss);
265 }
266
267
268 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
269                                    const u8 *bssid)
270 {
271         struct wpa_bss *bss;
272         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
273                 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
274                         return bss;
275         }
276         return NULL;
277 }
278
279
280 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
281 {
282         struct wpa_bss *bss;
283         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
284                 if (bss->id == id)
285                         return bss;
286         }
287         return NULL;
288 }
289
290
291 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
292 {
293         const u8 *end, *pos;
294
295         pos = (const u8 *) (bss + 1);
296         end = pos + bss->ie_len;
297
298         while (pos + 1 < end) {
299                 if (pos + 2 + pos[1] > end)
300                         break;
301                 if (pos[0] == ie)
302                         return pos;
303                 pos += 2 + pos[1];
304         }
305
306         return NULL;
307 }
308
309
310 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
311 {
312         const u8 *end, *pos;
313
314         pos = (const u8 *) (bss + 1);
315         end = pos + bss->ie_len;
316
317         while (pos + 1 < end) {
318                 if (pos + 2 + pos[1] > end)
319                         break;
320                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
321                     vendor_type == WPA_GET_BE32(&pos[2]))
322                         return pos;
323                 pos += 2 + pos[1];
324         }
325
326         return NULL;
327 }
328
329
330 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
331                                             u32 vendor_type)
332 {
333         struct wpabuf *buf;
334         const u8 *end, *pos;
335
336         buf = wpabuf_alloc(bss->ie_len);
337         if (buf == NULL)
338                 return NULL;
339
340         pos = (const u8 *) (bss + 1);
341         end = pos + bss->ie_len;
342
343         while (pos + 1 < end) {
344                 if (pos + 2 + pos[1] > end)
345                         break;
346                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
347                     vendor_type == WPA_GET_BE32(&pos[2]))
348                         wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
349                 pos += 2 + pos[1];
350         }
351
352         if (wpabuf_len(buf) == 0) {
353                 wpabuf_free(buf);
354                 buf = NULL;
355         }
356
357         return buf;
358 }