mesh: Write close reason from Mesh Peering Close to debug log
[mech_eap.git] / wpa_supplicant / bss.c
index b4c47e2..3687a2e 100644 (file)
@@ -12,6 +12,7 @@
 #include "utils/eloop.h"
 #include "common/ieee802_11_defs.h"
 #include "drivers/driver.h"
+#include "eap_peer/eap.h"
 #include "wpa_supplicant_i.h"
 #include "config.h"
 #include "notify.h"
 #include "bss.h"
 
 
-/**
- * WPA_BSS_EXPIRATION_PERIOD - Period of expiration run in seconds
- */
-#define WPA_BSS_EXPIRATION_PERIOD 10
-
 #define WPA_BSS_FREQ_CHANGED_FLAG      BIT(0)
 #define WPA_BSS_SIGNAL_CHANGED_FLAG    BIT(1)
 #define WPA_BSS_PRIVACY_CHANGED_FLAG   BIT(2)
@@ -65,6 +61,9 @@ struct wpa_bss_anqp * wpa_bss_anqp_alloc(void)
        anqp = os_zalloc(sizeof(*anqp));
        if (anqp == NULL)
                return NULL;
+#ifdef CONFIG_INTERWORKING
+       dl_list_init(&anqp->anqp_elems);
+#endif /* CONFIG_INTERWORKING */
        anqp->users = 1;
        return anqp;
 }
@@ -85,6 +84,7 @@ static struct wpa_bss_anqp * wpa_bss_anqp_clone(struct wpa_bss_anqp *anqp)
 
 #define ANQP_DUP(f) if (anqp->f) n->f = wpabuf_dup(anqp->f)
 #ifdef CONFIG_INTERWORKING
+       dl_list_init(&n->anqp_elems);
        ANQP_DUP(capability_list);
        ANQP_DUP(venue_name);
        ANQP_DUP(network_auth_type);
@@ -146,6 +146,10 @@ int wpa_bss_anqp_unshare_alloc(struct wpa_bss *bss)
  */
 static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
 {
+#ifdef CONFIG_INTERWORKING
+       struct wpa_bss_anqp_elem *elem;
+#endif /* CONFIG_INTERWORKING */
+
        if (anqp == NULL)
                return;
 
@@ -164,6 +168,13 @@ static void wpa_bss_anqp_free(struct wpa_bss_anqp *anqp)
        wpabuf_free(anqp->nai_realm);
        wpabuf_free(anqp->anqp_3gpp);
        wpabuf_free(anqp->domain_name);
+
+       while ((elem = dl_list_first(&anqp->anqp_elems,
+                                    struct wpa_bss_anqp_elem, list))) {
+               dl_list_del(&elem->list);
+               wpabuf_free(elem->payload);
+               os_free(elem);
+       }
 #endif /* CONFIG_INTERWORKING */
 #ifdef CONFIG_HS20
        wpabuf_free(anqp->hs20_capability_list);
@@ -293,6 +304,47 @@ static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
 }
 
 
+static int wpa_bss_is_wps_candidate(struct wpa_supplicant *wpa_s,
+                                   struct wpa_bss *bss)
+{
+#ifdef CONFIG_WPS
+       struct wpa_ssid *ssid;
+       struct wpabuf *wps_ie;
+       int pbc = 0, ret;
+
+       wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
+       if (!wps_ie)
+               return 0;
+
+       if (wps_is_selected_pbc_registrar(wps_ie)) {
+               pbc = 1;
+       } else if (!wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1)) {
+               wpabuf_free(wps_ie);
+               return 0;
+       }
+
+       for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
+               if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
+                       continue;
+               if (ssid->ssid_len &&
+                   (ssid->ssid_len != bss->ssid_len ||
+                    os_memcmp(ssid->ssid, bss->ssid, ssid->ssid_len) != 0))
+                       continue;
+
+               if (pbc)
+                       ret = eap_is_wps_pbc_enrollee(&ssid->eap);
+               else
+                       ret = eap_is_wps_pin_enrollee(&ssid->eap);
+               wpabuf_free(wps_ie);
+               return ret;
+       }
+       wpabuf_free(wps_ie);
+#endif /* CONFIG_WPS */
+
+       return 0;
+}
+
+
 static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
        struct wpa_ssid *ssid;
@@ -311,10 +363,18 @@ static int wpa_bss_known(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 
 static int wpa_bss_in_use(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
-       return bss == wpa_s->current_bss ||
-               (!is_zero_ether_addr(bss->bssid) &&
-                (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
-                 os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0));
+       if (bss == wpa_s->current_bss)
+               return 1;
+
+       if (wpa_s->current_bss &&
+           (bss->ssid_len != wpa_s->current_bss->ssid_len ||
+            os_memcmp(bss->ssid, wpa_s->current_bss->ssid,
+                      bss->ssid_len) != 0))
+               return 0; /* SSID has changed */
+
+       return !is_zero_ether_addr(bss->bssid) &&
+               (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) == 0 ||
+                os_memcmp(bss->bssid, wpa_s->pending_bssid, ETH_ALEN) == 0);
 }
 
 
@@ -323,7 +383,8 @@ static int wpa_bss_remove_oldest_unknown(struct wpa_supplicant *wpa_s)
        struct wpa_bss *bss;
 
        dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
-               if (!wpa_bss_known(wpa_s, bss)) {
+               if (!wpa_bss_known(wpa_s, bss) &&
+                   !wpa_bss_is_wps_candidate(wpa_s, bss)) {
                        wpa_bss_remove(wpa_s, bss, __func__);
                        return 0;
                }
@@ -390,15 +451,16 @@ static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
        dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
        wpa_s->num_bss++;
        wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Add new id %u BSSID " MACSTR
-               " SSID '%s'",
-               bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
+               " SSID '%s' freq %d",
+               bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len),
+               bss->freq);
        wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
        return bss;
 }
 
 
 static int are_ies_equal(const struct wpa_bss *old,
-                        const struct wpa_scan_res *new, u32 ie)
+                        const struct wpa_scan_res *new_res, u32 ie)
 {
        const u8 *old_ie, *new_ie;
        struct wpabuf *old_ie_buff = NULL;
@@ -408,19 +470,19 @@ static int are_ies_equal(const struct wpa_bss *old,
        switch (ie) {
        case WPA_IE_VENDOR_TYPE:
                old_ie = wpa_bss_get_vendor_ie(old, ie);
-               new_ie = wpa_scan_get_vendor_ie(new, ie);
+               new_ie = wpa_scan_get_vendor_ie(new_res, ie);
                is_multi = 0;
                break;
        case WPS_IE_VENDOR_TYPE:
                old_ie_buff = wpa_bss_get_vendor_ie_multi(old, ie);
-               new_ie_buff = wpa_scan_get_vendor_ie_multi(new, ie);
+               new_ie_buff = wpa_scan_get_vendor_ie_multi(new_res, ie);
                is_multi = 1;
                break;
        case WLAN_EID_RSN:
        case WLAN_EID_SUPP_RATES:
        case WLAN_EID_EXT_SUPP_RATES:
                old_ie = wpa_bss_get_ie(old, ie);
-               new_ie = wpa_scan_get_ie(new, ie);
+               new_ie = wpa_scan_get_ie(new_res, ie);
                is_multi = 0;
                break;
        default:
@@ -454,15 +516,15 @@ static int are_ies_equal(const struct wpa_bss *old,
 
 
 static u32 wpa_bss_compare_res(const struct wpa_bss *old,
-                              const struct wpa_scan_res *new)
+                              const struct wpa_scan_res *new_res)
 {
        u32 changes = 0;
-       int caps_diff = old->caps ^ new->caps;
+       int caps_diff = old->caps ^ new_res->caps;
 
-       if (old->freq != new->freq)
+       if (old->freq != new_res->freq)
                changes |= WPA_BSS_FREQ_CHANGED_FLAG;
 
-       if (old->level != new->level)
+       if (old->level != new_res->level)
                changes |= WPA_BSS_SIGNAL_CHANGED_FLAG;
 
        if (caps_diff & IEEE80211_CAP_PRIVACY)
@@ -471,22 +533,22 @@ static u32 wpa_bss_compare_res(const struct wpa_bss *old,
        if (caps_diff & IEEE80211_CAP_IBSS)
                changes |= WPA_BSS_MODE_CHANGED_FLAG;
 
-       if (old->ie_len == new->ie_len &&
-           os_memcmp(old + 1, new + 1, old->ie_len) == 0)
+       if (old->ie_len == new_res->ie_len &&
+           os_memcmp(old + 1, new_res + 1, old->ie_len) == 0)
                return changes;
        changes |= WPA_BSS_IES_CHANGED_FLAG;
 
-       if (!are_ies_equal(old, new, WPA_IE_VENDOR_TYPE))
+       if (!are_ies_equal(old, new_res, WPA_IE_VENDOR_TYPE))
                changes |= WPA_BSS_WPAIE_CHANGED_FLAG;
 
-       if (!are_ies_equal(old, new, WLAN_EID_RSN))
+       if (!are_ies_equal(old, new_res, WLAN_EID_RSN))
                changes |= WPA_BSS_RSNIE_CHANGED_FLAG;
 
-       if (!are_ies_equal(old, new, WPS_IE_VENDOR_TYPE))
+       if (!are_ies_equal(old, new_res, WPS_IE_VENDOR_TYPE))
                changes |= WPA_BSS_WPS_CHANGED_FLAG;
 
-       if (!are_ies_equal(old, new, WLAN_EID_SUPP_RATES) ||
-           !are_ies_equal(old, new, WLAN_EID_EXT_SUPP_RATES))
+       if (!are_ies_equal(old, new_res, WLAN_EID_SUPP_RATES) ||
+           !are_ies_equal(old, new_res, WLAN_EID_EXT_SUPP_RATES))
                changes |= WPA_BSS_RATES_CHANGED_FLAG;
 
        return changes;
@@ -534,6 +596,9 @@ wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
        u32 changes;
 
        changes = wpa_bss_compare_res(bss, res);
+       if (changes & WPA_BSS_FREQ_CHANGED_FLAG)
+               wpa_printf(MSG_DEBUG, "BSS: " MACSTR " changed freq %d --> %d",
+                          MAC2STR(bss->bssid), bss->freq, res->freq);
        bss->scan_miss_count = 0;
        bss->last_update_idx = wpa_s->bss_update_idx;
        wpa_bss_copy_res(bss, res, fetch_time);
@@ -652,7 +717,7 @@ void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
                        MACSTR, MAC2STR(res->bssid));
                return;
        }
-       if (ssid[1] > 32) {
+       if (ssid[1] > SSID_MAX_LEN) {
                wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Too long SSID IE included for "
                        MACSTR, MAC2STR(res->bssid));
                return;
@@ -679,7 +744,7 @@ void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
         * (to save memory) */
 
        mesh = wpa_scan_get_ie(res, WLAN_EID_MESH_ID);
-       if (mesh && mesh[1] <= 32)
+       if (mesh && mesh[1] <= SSID_MAX_LEN)
                ssid = mesh;
 
        bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
@@ -777,7 +842,7 @@ void wpa_bss_update_end(struct wpa_supplicant *wpa_s, struct scan_info *info,
        struct wpa_bss *bss, *n;
 
        os_get_reltime(&wpa_s->last_scan);
-       if (!new_scan)
+       if ((info && info->aborted) || !new_scan)
                return; /* do not expire entries without new scan */
 
        dl_list_for_each_safe(bss, n, &wpa_s->bss, struct wpa_bss, list) {
@@ -828,16 +893,6 @@ void wpa_bss_flush_by_age(struct wpa_supplicant *wpa_s, int age)
 }
 
 
-static void wpa_bss_timeout(void *eloop_ctx, void *timeout_ctx)
-{
-       struct wpa_supplicant *wpa_s = eloop_ctx;
-
-       wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
-       eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
-                              wpa_bss_timeout, wpa_s, NULL);
-}
-
-
 /**
  * wpa_bss_init - Initialize BSS table
  * @wpa_s: Pointer to wpa_supplicant data
@@ -850,8 +905,6 @@ int wpa_bss_init(struct wpa_supplicant *wpa_s)
 {
        dl_list_init(&wpa_s->bss);
        dl_list_init(&wpa_s->bss_id);
-       eloop_register_timeout(WPA_BSS_EXPIRATION_PERIOD, 0,
-                              wpa_bss_timeout, wpa_s, NULL);
        return 0;
 }
 
@@ -883,7 +936,6 @@ void wpa_bss_flush(struct wpa_supplicant *wpa_s)
  */
 void wpa_bss_deinit(struct wpa_supplicant *wpa_s)
 {
-       eloop_cancel_timeout(wpa_bss_timeout, wpa_s, NULL);
        wpa_bss_flush(wpa_s);
 }
 
@@ -1010,20 +1062,7 @@ struct wpa_bss * wpa_bss_get_id_range(struct wpa_supplicant *wpa_s,
  */
 const u8 * wpa_bss_get_ie(const struct wpa_bss *bss, u8 ie)
 {
-       const u8 *end, *pos;
-
-       pos = (const u8 *) (bss + 1);
-       end = pos + bss->ie_len;
-
-       while (pos + 1 < end) {
-               if (pos + 2 + pos[1] > end)
-                       break;
-               if (pos[0] == ie)
-                       return pos;
-               pos += 2 + pos[1];
-       }
-
-       return NULL;
+       return get_ie((const u8 *) (bss + 1), bss->ie_len, ie);
 }
 
 
@@ -1043,8 +1082,8 @@ const u8 * wpa_bss_get_vendor_ie(const struct wpa_bss *bss, u32 vendor_type)
        pos = (const u8 *) (bss + 1);
        end = pos + bss->ie_len;
 
-       while (pos + 1 < end) {
-               if (pos + 2 + pos[1] > end)
+       while (end - pos > 1) {
+               if (2 + pos[1] > end - pos)
                        break;
                if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
                    vendor_type == WPA_GET_BE32(&pos[2]))
@@ -1080,8 +1119,8 @@ const u8 * wpa_bss_get_vendor_ie_beacon(const struct wpa_bss *bss,
        pos += bss->ie_len;
        end = pos + bss->beacon_ie_len;
 
-       while (pos + 1 < end) {
-               if (pos + 2 + pos[1] > end)
+       while (end - pos > 1) {
+               if (2 + pos[1] > end - pos)
                        break;
                if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
                    vendor_type == WPA_GET_BE32(&pos[2]))
@@ -1116,8 +1155,8 @@ struct wpabuf * wpa_bss_get_vendor_ie_multi(const struct wpa_bss *bss,
        pos = (const u8 *) (bss + 1);
        end = pos + bss->ie_len;
 
-       while (pos + 1 < end) {
-               if (pos + 2 + pos[1] > end)
+       while (end - pos > 1) {
+               if (2 + pos[1] > end - pos)
                        break;
                if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
                    vendor_type == WPA_GET_BE32(&pos[2]))
@@ -1161,8 +1200,8 @@ struct wpabuf * wpa_bss_get_vendor_ie_multi_beacon(const struct wpa_bss *bss,
        pos += bss->ie_len;
        end = pos + bss->beacon_ie_len;
 
-       while (pos + 1 < end) {
-               if (pos + 2 + pos[1] > end)
+       while (end - pos > 1) {
+               if (2 + pos[1] > end - pos)
                        break;
                if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
                    vendor_type == WPA_GET_BE32(&pos[2]))