P2P: Do not drop P2P IEs from BSS table on non-P2P scans
[mech_eap.git] / wpa_supplicant / bss.c
index c50de53..86f4bf1 100644 (file)
@@ -224,11 +224,27 @@ struct wpa_bss * wpa_bss_get(struct wpa_supplicant *wpa_s, const u8 *bssid,
 }
 
 
-static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
-                            struct os_time *fetch_time)
+static void calculate_update_time(const struct os_time *fetch_time,
+                                 unsigned int age_ms,
+                                 struct os_time *update_time)
 {
        os_time_t usec;
 
+       update_time->sec = fetch_time->sec;
+       update_time->usec = fetch_time->usec;
+       update_time->sec -= age_ms / 1000;
+       usec = (age_ms % 1000) * 1000;
+       if (update_time->usec < usec) {
+               update_time->sec--;
+               update_time->usec += 1000000;
+       }
+       update_time->usec -= usec;
+}
+
+
+static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
+                            struct os_time *fetch_time)
+{
        dst->flags = src->flags;
        os_memcpy(dst->bssid, src->bssid, ETH_ALEN);
        dst->freq = src->freq;
@@ -239,15 +255,7 @@ static void wpa_bss_copy_res(struct wpa_bss *dst, struct wpa_scan_res *src,
        dst->level = src->level;
        dst->tsf = src->tsf;
 
-       dst->last_update.sec = fetch_time->sec;
-       dst->last_update.usec = fetch_time->usec;
-       dst->last_update.sec -= src->age / 1000;
-       usec = (src->age % 1000) * 1000;
-       if (dst->last_update.usec < usec) {
-               dst->last_update.sec--;
-               dst->last_update.usec += 1000000;
-       }
-       dst->last_update.usec -= usec;
+       calculate_update_time(fetch_time, src->age, &dst->last_update);
 }
 
 
@@ -335,6 +343,14 @@ static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
        os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
        wpa_bss_set_hessid(bss);
 
+       if (wpa_s->num_bss + 1 > wpa_s->conf->bss_max_count &&
+           wpa_bss_remove_oldest(wpa_s) != 0) {
+               wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
+                          "because all BSSes are in use. We should normally "
+                          "not get here!", (int) wpa_s->num_bss + 1);
+               wpa_s->conf->bss_max_count = wpa_s->num_bss + 1;
+       }
+
        dl_list_add_tail(&wpa_s->bss, &bss->list);
        dl_list_add_tail(&wpa_s->bss_id, &bss->list_id);
        wpa_s->num_bss++;
@@ -342,13 +358,6 @@ static struct wpa_bss * wpa_bss_add(struct wpa_supplicant *wpa_s,
                " SSID '%s'",
                bss->id, MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
        wpas_notify_bss_added(wpa_s, bss->bssid, bss->id);
-       if (wpa_s->num_bss > wpa_s->conf->bss_max_count &&
-           wpa_bss_remove_oldest(wpa_s) != 0) {
-               wpa_printf(MSG_ERROR, "Increasing the MAX BSS count to %d "
-                          "because all BSSes are in use. We should normally "
-                          "not get here!", (int) wpa_s->num_bss);
-               wpa_s->conf->bss_max_count = wpa_s->num_bss;
-       }
        return bss;
 }
 
@@ -493,6 +502,22 @@ wpa_bss_update(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
        wpa_bss_copy_res(bss, res, fetch_time);
        /* Move the entry to the end of the list */
        dl_list_del(&bss->list);
+#ifdef CONFIG_P2P
+       if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
+           !wpa_scan_get_vendor_ie(res, P2P_IE_VENDOR_TYPE)) {
+               /*
+                * This can happen when non-P2P station interface runs a scan
+                * without P2P IE in the Probe Request frame. P2P GO would reply
+                * to that with a Probe Response that does not include P2P IE.
+                * Do not update the IEs in this BSS entry to avoid such loss of
+                * information that may be needed for P2P operations to
+                * determine group information.
+                */
+               wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Do not update scan IEs for "
+                       MACSTR " since that would remove P2P IE information",
+                       MAC2STR(bss->bssid));
+       } else
+#endif /* CONFIG_P2P */
        if (bss->ie_len + bss->beacon_ie_len >=
            res->ie_len + res->beacon_ie_len) {
                os_memcpy(bss + 1, res + 1, res->ie_len + res->beacon_ie_len);
@@ -567,6 +592,21 @@ void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
        const u8 *ssid, *p2p;
        struct wpa_bss *bss;
 
+       if (wpa_s->conf->ignore_old_scan_res) {
+               struct os_time update;
+               calculate_update_time(fetch_time, res->age, &update);
+               if (os_time_before(&update, &wpa_s->scan_trigger_time)) {
+                       struct os_time age;
+                       os_time_sub(&wpa_s->scan_trigger_time, &update, &age);
+                       wpa_dbg(wpa_s, MSG_DEBUG, "BSS: Ignore driver BSS "
+                               "table entry that is %u.%06u seconds older "
+                               "than our scan trigger",
+                               (unsigned int) age.sec,
+                               (unsigned int) age.usec);
+                       return;
+               }
+       }
+
        ssid = wpa_scan_get_ie(res, WLAN_EID_SSID);
        if (ssid == NULL) {
                wpa_dbg(wpa_s, MSG_DEBUG, "BSS: No SSID IE included for "
@@ -601,8 +641,18 @@ void wpa_bss_update_scan_res(struct wpa_supplicant *wpa_s,
        bss = wpa_bss_get(wpa_s, res->bssid, ssid + 2, ssid[1]);
        if (bss == NULL)
                bss = wpa_bss_add(wpa_s, ssid + 2, ssid[1], res, fetch_time);
-       else
+       else {
                bss = wpa_bss_update(wpa_s, bss, res, fetch_time);
+               if (wpa_s->last_scan_res) {
+                       unsigned int i;
+                       for (i = 0; i < wpa_s->last_scan_res_used; i++) {
+                               if (bss == wpa_s->last_scan_res[i]) {
+                                       /* Already in the list */
+                                       return;
+                               }
+                       }
+               }
+       }
 
        if (bss == NULL)
                return;
@@ -829,6 +879,34 @@ struct wpa_bss * wpa_bss_get_bssid(struct wpa_supplicant *wpa_s,
 }
 
 
+/**
+ * wpa_bss_get_bssid_latest - Fetch the latest BSS table entry based on BSSID
+ * @wpa_s: Pointer to wpa_supplicant data
+ * @bssid: BSSID
+ * Returns: Pointer to the BSS entry or %NULL if not found
+ *
+ * This function is like wpa_bss_get_bssid(), but full BSS table is iterated to
+ * find the entry that has the most recent update. This can help in finding the
+ * correct entry in cases where the SSID of the AP may have changed recently
+ * (e.g., in WPS reconfiguration cases).
+ */
+struct wpa_bss * wpa_bss_get_bssid_latest(struct wpa_supplicant *wpa_s,
+                                         const u8 *bssid)
+{
+       struct wpa_bss *bss, *found = NULL;
+       if (!wpa_supplicant_filter_bssid_match(wpa_s, bssid))
+               return NULL;
+       dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss, list) {
+               if (os_memcmp(bss->bssid, bssid, ETH_ALEN) != 0)
+                       continue;
+               if (found == NULL ||
+                   os_time_before(&found->last_update, &bss->last_update))
+                       found = bss;
+       }
+       return found;
+}
+
+
 #ifdef CONFIG_P2P
 /**
  * wpa_bss_get_p2p_dev_addr - Fetch a BSS table entry based on P2P Device Addr