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