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