00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "utils/includes.h"
00016
00017 #include "utils/common.h"
00018 #include "utils/eloop.h"
00019 #include "common/ieee802_11_defs.h"
00020 #include "drivers/driver.h"
00021 #include "wpa_supplicant_i.h"
00022 #include "config.h"
00023 #include "notify.h"
00024 #include "scan.h"
00025 #include "bss.h"
00026
00027
00031 #define WPA_BSS_EXPIRATION_PERIOD 10
00032
00039 #define WPA_BSS_EXPIRATION_AGE 180
00040
00050 #define WPA_BSS_EXPIRATION_SCAN_COUNT 2
00051
00052 #define WPA_BSS_FREQ_CHANGED_FLAG BIT(0)
00053 #define WPA_BSS_SIGNAL_CHANGED_FLAG BIT(1)
00054 #define WPA_BSS_PRIVACY_CHANGED_FLAG BIT(2)
00055 #define WPA_BSS_MODE_CHANGED_FLAG BIT(3)
00056 #define WPA_BSS_WPAIE_CHANGED_FLAG BIT(4)
00057 #define WPA_BSS_RSNIE_CHANGED_FLAG BIT(5)
00058 #define WPA_BSS_WPS_CHANGED_FLAG BIT(6)
00059 #define WPA_BSS_RATES_CHANGED_FLAG BIT(7)
00060 #define WPA_BSS_IES_CHANGED_FLAG BIT(8)
00061
00062
00063 static void wpa_bss_remove(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
00064 {
00065 dl_list_del(&bss->list);
00066 dl_list_del(&bss->list_id);
00067 wpa_s->num_bss--;
00068 wpa_printf(MSG_DEBUG, "BSS: Remove id %u BSSID " MACSTR " SSID '%s'",
00069 bss->id, MAC2STR(bss->bssid),
00070 wpa_ssid_txt(bss->ssid, bss->ssid_len));
00071 wpas_notify_bss_removed(wpa_s, bss->bssid, bss->id);
00072 os_free(bss);
00073 }
00074
00075
00076 struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
00077 const u8 *ssid, size_t ssid_len)
00078 {
00079 struct wpa_bss *bss;
00080 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
00081 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
00082 bss->ssid_len == ssid_len &&
00083 os_memcmp(bss->ssid, ssid, ssid_len) == 0)
00084 return bss;
00085 }
00086 return NULL;
00087 }
00088
00089
00090 static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src)
00091 {
00092 os_time_t usec;
00093
00094 dst->flags = src->flags;
00095 os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
00096 dst->freq = src->freq;
00097 dst->beacon_int = src->beacon_int;
00098 dst->caps = src->caps;
00099 dst->qual = src->qual;
00100 dst->noise = src->noise;
00101 dst->level = src->level;
00102 dst->tsf = src->tsf;
00103
00104 os_get_time(&dst->last_update);
00105 dst->last_update.sec -= src->age / 1000;
00106 usec = (src->age % 1000) * 1000;
00107 if (dst->last_update.usec < usec) {
00108 dst->last_update.sec--;
00109 dst->last_update.usec += 1000000;
00110 }
00111 dst->last_update.usec -= usec;
00112 }
00113
00114
00115 static void wpa_bss_add(struct wpa_supplicant *wpa_s,
00116 const u8 *ssid, size_t ssid_len,
00117 struct wpa_scan_res *res)
00118 {
00119 struct wpa_bss *bss;
00120
00121 bss = os_zalloc(sizeof(*bss) + res->ie_len + res->beacon_ie_len);
00122 if (bss == NULL)
00123 return;
00124 bss->id = wpa_s->bss_next_id++;
00125 bss->last_update_idx = wpa_s->bss_update_idx;
00126 wpa_bss_copy_res(bss, res);
00127 os_memcpy(bss->ssid, ssid, ssid_len);
00128 bss->ssid_len = ssid_len;
00129 bss->ie_len = res->ie_len;
00130 bss->beacon_ie_len = res->beacon_ie_len;
00131 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
00132
00133 dl_list_add_tail(&wpa_s->bss, &bss->list);
00134 dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
00135 wpa_s->num_bss++;
00136 wpa_printf(MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR " SSID '%s'",
00137 bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
00138 wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
00139 if (wpa_s->num_bss > wpa_s->conf->bss_max_count) {
00140
00141 wpa_bss_remove(wpa_s, dl_list_first(&wpa_s->bss,
00142 struct wpa_bss, list));
00143 }
00144 }
00145
00146
00147 static int are_ies_equal(const struct wpa_bss *old,
00148 const struct wpa_scan_res *new, u32 ie)
00149 {
00150 const u8 *old_ie, *new_ie;
00151 struct wpabuf *old_ie_buff = NULL;
00152 struct wpabuf *new_ie_buff = NULL;
00153 int new_ie_len, old_ie_len, ret, is_multi;
00154
00155 switch (ie) {
00156 case WPA_IE_VENDOR_TYPE:
00157 old_ie = wpa_bss_get_vendor_ie(old, ie);
00158 new_ie = wpa_scan_get_vendor_ie(new, ie);
00159 is_multi = 0;
00160 break;
00161 case WPS_IE_VENDOR_TYPE:
00162 old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
00163 new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
00164 is_multi = 1;
00165 break;
00166 case WLAN_EID_RSN:
00167 case WLAN_EID_SUPP_RATES:
00168 case WLAN_EID_EXT_SUPP_RATES:
00169 old_ie = wpa_bss_get_ie(old, ie);
00170 new_ie = wpa_scan_get_ie(new, ie);
00171 is_multi = 0;
00172 break;
00173 default:
00174 wpa_printf(MSG_DEBUG, "bss: %s: cannot compare IEs", __func__);
00175 return 0;
00176 }
00177
00178 if (is_multi) {
00179
00180 old_ie = old_ie_buff ? wpabuf_head_u8(old_ie_buff) : NULL;
00181 new_ie = new_ie_buff ? wpabuf_head_u8(new_ie_buff) : NULL;
00182 old_ie_len = old_ie_buff ? wpabuf_len(old_ie_buff) : 0;
00183 new_ie_len = new_ie_buff ? wpabuf_len(new_ie_buff) : 0;
00184 } else {
00185
00186 old_ie_len = old_ie ? old_ie[1] + 2 : 0;
00187 new_ie_len = new_ie ? new_ie[1] + 2 : 0;
00188 }
00189
00190 ret = (old_ie_len == new_ie_len &&
00191 os_memcmp(old_ie, new_ie, old_ie_len) == 0);
00192
00193 wpabuf_free(old_ie_buff);
00194 wpabuf_free(new_ie_buff);
00195
00196 return ret;
00197 }
00198
00199
00200 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
00201 const struct wpa_scan_res *new)
00202 {
00203 u32 changes = 0;
00204 int caps_diff = old->caps ^ new->caps;
00205
00206 if (old->freq != new->freq)
00207 changes |= WPA_BSS_FREQ_CHANGED_FLAG;
00208
00209 if (old->level != new->level)
00210 changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
00211
00212 if (caps_diff & IEEE80211_CAP_PRIVACY)
00213 changes |= WPA_BSS_PRIVACY_CHANGED_FLAG;
00214
00215 if (caps_diff & IEEE80211_CAP_IBSS)
00216 changes |= WPA_BSS_MODE_CHANGED_FLAG;
00217
00218 if (old->ie_len == new->ie_len &&
00219 os_memcmp(old + 1, new + 1, old->ie_len) == 0)
00220 return changes;
00221 changes |= WPA_BSS_IES_CHANGED_FLAG;
00222
00223 if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
00224 changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
00225
00226 if (!are_ies_equal(old, new, WLAN_EID_RSN))
00227 changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
00228
00229 if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
00230 changes |= WPA_BSS_WPS_CHANGED_FLAG;
00231
00232 if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
00233 !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
00234 changes |= WPA_BSS_RATES_CHANGED_FLAG;
00235
00236 return changes;
00237 }
00238
00239
00240 static void notify_bss_changes(struct wpa_supplicant *wpa_s, u32 changes,
00241 const struct wpa_bss *bss)
00242 {
00243 if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
00244 wpas_notify_bss_freq_changed(wpa_s, bss->id);
00245
00246 if (changes & WPA_BSS_SIGNAL_CHANGED_FLAG)
00247 wpas_notify_bss_signal_changed(wpa_s, bss->id);
00248
00249 if (changes & WPA_BSS_PRIVACY_CHANGED_FLAG)
00250 wpas_notify_bss_privacy_changed(wpa_s, bss->id);
00251
00252 if (changes & WPA_BSS_MODE_CHANGED_FLAG)
00253 wpas_notify_bss_mode_changed(wpa_s, bss->id);
00254
00255 if (changes & WPA_BSS_WPAIE_CHANGED_FLAG)
00256 wpas_notify_bss_wpaie_changed(wpa_s, bss->id);
00257
00258 if (changes & WPA_BSS_RSNIE_CHANGED_FLAG)
00259 wpas_notify_bss_rsnie_changed(wpa_s, bss->id);
00260
00261 if (changes & WPA_BSS_WPS_CHANGED_FLAG)
00262 wpas_notify_bss_wps_changed(wpa_s, bss->id);
00263
00264 if (changes & WPA_BSS_IES_CHANGED_FLAG)
00265 wpas_notify_bss_ies_changed(wpa_s, bss->id);
00266
00267 if (changes & WPA_BSS_RATES_CHANGED_FLAG)
00268 wpas_notify_bss_rates_changed(wpa_s, bss->id);
00269 }
00270
00271
00272 static void wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
00273 struct wpa_scan_res *res)
00274 {
00275 u32 changes;
00276
00277 changes = wpa_bss_compare_res(bss, res);
00278 bss->scan_miss_count = 0;
00279 bss->last_update_idx = wpa_s->bss_update_idx;
00280 wpa_bss_copy_res(bss, res);
00281
00282 dl_list_del(&bss->list);
00283 if (bss->ie_len + bss->beacon_ie_len >=
00284 res->ie_len + res->beacon_ie_len) {
00285 os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
00286 bss->ie_len = res->ie_len;
00287 bss->beacon_ie_len = res->beacon_ie_len;
00288 } else {
00289 struct wpa_bss *nbss;
00290 struct dl_list *prev = bss->list_id.prev;
00291 dl_list_del(&bss->list_id);
00292 nbss = os_realloc(bss, sizeof(*bss) + res->ie_len +
00293 res->beacon_ie_len);
00294 if (nbss) {
00295 bss = nbss;
00296 os_memcpy(bss + 1, res + 1,
00297 res->ie_len + res->beacon_ie_len);
00298 bss->ie_len = res->ie_len;
00299 bss->beacon_ie_len = res->beacon_ie_len;
00300 }
00301 dl_list_add(prev, &bss->list_id);
00302 }
00303 dl_list_add_tail(&wpa_s->bss, &bss->list);
00304
00305 notify_bss_changes(wpa_s, changes, bss);
00306 }
00307
00308
00309 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
00310 {
00311 return bss == wpa_s->current_bss ||
00312 os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
00313 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0;
00314 }
00315
00316
00317 void wpa_bss_update_start(struct wpa_supplicant *wpa_s)
00318 {
00319 wpa_s->bss_update_idx++;
00320 wpa_printf(MSG_DEBUG, "BSS: Start scan result update %u",
00321 wpa_s->bss_update_idx);
00322 }
00323
00324
00325 void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
00326 struct wpa_scan_res *res)
00327 {
00328 const u8 *ssid;
00329 struct wpa_bss *bss;
00330
00331 ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
00332 if (ssid == NULL) {
00333 wpa_printf(MSG_DEBUG, "BSS: No SSID IE included for " MACSTR,
00334 MAC2STR(res->bssid));
00335 return;
00336 }
00337 if (ssid[1] > 32) {
00338 wpa_printf(MSG_DEBUG, "BSS: Too long SSID IE included for "
00339 MACSTR, MAC2STR(res->bssid));
00340 return;
00341 }
00342
00343
00344
00345 bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
00346 if (bss == NULL)
00347 wpa_bss_add(wpa_s, ssid + 2, ssid[1], res);
00348 else
00349 wpa_bss_update(wpa_s, bss, res);
00350 }
00351
00352
00353 static int wpa_bss_included_in_scan(const struct wpa_bss *bss,
00354 const struct scan_info *info)
00355 {
00356 int found;
00357 size_t i;
00358
00359 if (info == NULL)
00360 return 1;
00361
00362 if (info->num_freqs) {
00363 found = 0;
00364 for (i = 0; i < info->num_freqs; i++) {
00365 if (bss->freq == info->freqs[i]) {
00366 found = 1;
00367 break;
00368 }
00369 }
00370 if (!found)
00371 return 0;
00372 }
00373
00374 if (info->num_ssids) {
00375 found = 0;
00376 for (i = 0; i < info->num_ssids; i++) {
00377 const struct wpa_driver_scan_ssid *s = &info->ssids[i];
00378 if ((s->ssid == NULL || s->ssid_len == 0) ||
00379 (s->ssid_len == bss->ssid_len &&
00380 os_memcmp(s->ssid, bss->ssid, bss->ssid_len) ==
00381 0)) {
00382 found = 1;
00383 break;
00384 }
00385 }
00386 if (!found)
00387 return 0;
00388 }
00389
00390 return 1;
00391 }
00392
00393
00394 void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
00395 int new_scan)
00396 {
00397 struct wpa_bss *bss, *n;
00398
00399 if (!new_scan)
00400 return;
00401
00402 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
00403 if (wpa_bss_in_use(wpa_s, bss))
00404 continue;
00405 if (!wpa_bss_included_in_scan(bss, info))
00406 continue;
00407 if (bss->last_update_idx < wpa_s->bss_update_idx)
00408 bss->scan_miss_count++;
00409 if (bss->scan_miss_count >= WPA_BSS_EXPIRATION_SCAN_COUNT) {
00410 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to no "
00411 "match in scan", bss->id);
00412 wpa_bss_remove(wpa_s, bss);
00413 }
00414 }
00415 }
00416
00417
00418 static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
00419 {
00420 struct wpa_supplicant *wpa_s = eloop_ctx;
00421 struct wpa_bss *bss, *n;
00422 struct os_time t;
00423
00424 if (dl_list_empty(&wpa_s->bss))
00425 return;
00426
00427 os_get_time(&t);
00428 t.sec -= WPA_BSS_EXPIRATION_AGE;
00429
00430 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
00431 if (wpa_bss_in_use(wpa_s, bss))
00432 continue;
00433
00434 if (os_time_before(&bss->last_update, &t)) {
00435 wpa_printf(MSG_DEBUG, "BSS: Expire BSS %u due to age",
00436 bss->id);
00437 wpa_bss_remove(wpa_s, bss);
00438 } else
00439 break;
00440 }
00441 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
00442 wpa_bss_timeout, wpa_s, NULL);
00443 }
00444
00445
00446 int wpa_bss_init(struct wpa_supplicant *wpa_s)
00447 {
00448 dl_list_init(&wpa_s->bss);
00449 dl_list_init(&wpa_s->bss_id);
00450 eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
00451 wpa_bss_timeout, wpa_s, NULL);
00452 return 0;
00453 }
00454
00455
00456 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
00457 {
00458 struct wpa_bss *bss, *n;
00459 eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
00460 if (wpa_s->bss.next == NULL)
00461 return;
00462 dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list)
00463 wpa_bss_remove(wpa_s, bss);
00464 }
00465
00466
00467 struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
00468 const u8 *bssid)
00469 {
00470 struct wpa_bss *bss;
00471 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
00472 if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0)
00473 return bss;
00474 }
00475 return NULL;
00476 }
00477
00478
00479 struct wpa_bss * wpa_bss_get_id(struct wpa_supplicant *wpa_s, unsigned int id)
00480 {
00481 struct wpa_bss *bss;
00482 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
00483 if (bss->id == id)
00484 return bss;
00485 }
00486 return NULL;
00487 }
00488
00489
00490 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
00491 {
00492 const u8 *end, *pos;
00493
00494 pos = (const u8 *) (bss + 1);
00495 end = pos + bss->ie_len;
00496
00497 while (pos + 1 < end) {
00498 if (pos + 2 + pos[1] > end)
00499 break;
00500 if (pos[0] == ie)
00501 return pos;
00502 pos += 2 + pos[1];
00503 }
00504
00505 return NULL;
00506 }
00507
00508
00509 const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
00510 {
00511 const u8 *end, *pos;
00512
00513 pos = (const u8 *) (bss + 1);
00514 end = pos + bss->ie_len;
00515
00516 while (pos + 1 < end) {
00517 if (pos + 2 + pos[1] > end)
00518 break;
00519 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
00520 vendor_type == WPA_GET_BE32(&pos[2]))
00521 return pos;
00522 pos += 2 + pos[1];
00523 }
00524
00525 return NULL;
00526 }
00527
00528
00529 struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
00530 u32 vendor_type)
00531 {
00532 struct wpabuf *buf;
00533 const u8 *end, *pos;
00534
00535 buf = wpabuf_alloc(bss->ie_len);
00536 if (buf == NULL)
00537 return NULL;
00538
00539 pos = (const u8 *) (bss + 1);
00540 end = pos + bss->ie_len;
00541
00542 while (pos + 1 < end) {
00543 if (pos + 2 + pos[1] > end)
00544 break;
00545 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
00546 vendor_type == WPA_GET_BE32(&pos[2]))
00547 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
00548 pos += 2 + pos[1];
00549 }
00550
00551 if (wpabuf_len(buf) == 0) {
00552 wpabuf_free(buf);
00553 buf = NULL;
00554 }
00555
00556 return buf;
00557 }
00558
00559
00560 int wpa_bss_get_max_rate(const struct wpa_bss *bss)
00561 {
00562 int rate = 0;
00563 const u8 *ie;
00564 int i;
00565
00566 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
00567 for (i = 0; ie && i < ie[1]; i++) {
00568 if ((ie[i + 2] & 0x7f) > rate)
00569 rate = ie[i + 2] & 0x7f;
00570 }
00571
00572 ie = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
00573 for (i = 0; ie && i < ie[1]; i++) {
00574 if ((ie[i + 2] & 0x7f) > rate)
00575 rate = ie[i + 2] & 0x7f;
00576 }
00577
00578 return rate;
00579 }
00580
00581
00582 int wpa_bss_get_bit_rates(const struct wpa_bss *bss, u8 **rates)
00583 {
00584 const u8 *ie, *ie2;
00585 int i, j;
00586 unsigned int len;
00587 u8 *r;
00588
00589 ie = wpa_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
00590 ie2 = wpa_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
00591
00592 len = (ie ? ie[1] : 0) + (ie2 ? ie2[1] : 0);
00593
00594 r = os_malloc(len);
00595 if (!r)
00596 return -1;
00597
00598 for (i = 0; ie && i < ie[1]; i++)
00599 r[i] = ie[i + 2] & 0x7f;
00600
00601 for (j = 0; ie2 && j < ie2[1]; j++)
00602 r[i + j] = ie2[j + 2] & 0x7f;
00603
00604 *rates = r;
00605 return len;
00606 }